1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#include "cmd_spi.h"
/*
SPI command shows some info about SPI protocol
HELP - shows all params
MISO,MOSI,SCK,HOLD,CS shows meaning of them
MODES - shows SPI modes avaliable
SPI - show diagram of spi
MASTER - show master connection
SLAVE - show slave connection
*/
//TODO multiline output =[
char _spi_str_spi[]="""\
--------\n\
| MISO |\n\
| MOSI |\n\
| SCK |\n\
| CS |\n\
--------\n\
""";
char _spi_str_slave[]="""\
slave\n\
--------\n\
| MISO |>-\n\
| MOSI |<-\n\
| SCK |<-\n\
| CS |<-\n\
--------\n\
""";
char _spi_str_master[]="""\
master\n\
--------\n\
| MISO |<-\n\
| MOSI |>-\n\
| SCK |>-\n\
| CS |>-\n\
--------\n\
""";
char *_spi_help_table[] =
{
"help" ,"MODES,SPI,SLAVE,MASTER,MISO,MOSI,SCK,HOLD,CS",
"MODES" ,"QUAD(4-wires),DOUBLE(3-wires),SINGLE(2-wires)",
"SPI" ,_spi_str_spi,
"SLAVE" ,_spi_str_slave,
"MASTER",_spi_str_master,
"MISO" ,"(M)aster(I)nput(S)lave(O)utput",
"MOSI" ,"(M)aster(O)utput(S)lave(I)nput",
"SCK" ,"Serial Clockrate",
"HOLD" ,"HOLD operation for some time",
"CS" ,"(C)hip(S)elect",
NULL,NULL
};
char *__cmd_spi_iter(char **table, char *cmp)
{
int iter = 0;
char *ret = NULL;
//yea thats hack mate
iter = 0;
while (table[iter]!=NULL)
{
printf("%s\n",table[iter]);
if (strncmp(table[iter],cmp,strlen(table[iter]))==0)
{
ret = table[iter+1];
}
iter += 2;
}
return ret;
}
void *cmd_spi(void *data)
{
char *ret = NULL;
char *match = NULL;
int count=-1;
sds params;
sds *tokens=NULL;
printf("SPI\n");
if (data == NULL)
{
ret = alloc_new_str("SPI protocol info!");
} else
{
params = sdsnew(data);
tokens = sdssplitargs(params, &count);
//maybe useless thing but who knows
if (count < 1)
{
ret = alloc_new_str("None args");
}
match = __cmd_spi_iter(_spi_help_table, tokens[0]);
if (match)
{
ret = alloc_new_str(match);
} else
{
ret = alloc_new_str("None such commund");
}
sdsfree(params);
sdsfreesplitres(tokens, count);
}
return ret;
}
|