summaryrefslogtreecommitdiff
path: root/cmd/cmd_spi.c
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/cmd_spi.c')
-rw-r--r--cmd/cmd_spi.c118
1 files changed, 118 insertions, 0 deletions
diff --git a/cmd/cmd_spi.c b/cmd/cmd_spi.c
new file mode 100644
index 0000000..bfda27c
--- /dev/null
+++ b/cmd/cmd_spi.c
@@ -0,0 +1,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;
+}
+