/fw/turretController/console.c

https://github.com/alvarop/laserturret · C · 254 lines · 194 code · 44 blank · 16 comment · 66 complexity · 6016794c23f89b673aed90391573fda3 MD5 · raw file

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "console.h"
  5. #include "fifo.h"
  6. #include "qik.h"
  7. #include "motor.h"
  8. #include "stm32f4xx_conf.h"
  9. #include "stm32f4xx.h"
  10. typedef struct {
  11. char *commandStr;
  12. void (*fn)(uint8_t argc, char *argv[]);
  13. char *helpStr;
  14. } command_t;
  15. fifo_t usbRxFifo;
  16. static char cmdBuff[64];
  17. static uint8_t argc;
  18. static char* argv[8];
  19. static void helpFn(uint8_t argc, char *argv[]);
  20. static void laserCmd(uint8_t argc, char *argv[]);
  21. static void qikCmd(uint8_t argc, char *argv[]);
  22. static void motorCmd(uint8_t argc, char *argv[]);
  23. static command_t commands[] = {
  24. {"laser", laserCmd, "Usage: laser <0, 1>"},
  25. {"qik" , qikCmd, "Usage: qik"},
  26. {"m", motorCmd, "Usage: m <motor(0-1)> <stop | position>"},
  27. // Add new commands here!
  28. {"help", helpFn, "Print this!"},
  29. {NULL, NULL, NULL}
  30. };
  31. //
  32. // Print the help menu
  33. //
  34. static void helpFn(uint8_t argc, char *argv[]) {
  35. command_t *command = commands;
  36. if(argc < 2) {
  37. while(command->commandStr != NULL) {
  38. printf("%s - %s\n", command->commandStr, command->helpStr);
  39. command++;
  40. }
  41. } else {
  42. while(command->commandStr != NULL) {
  43. if(strcmp(command->commandStr, argv[1]) == 0) {
  44. printf("%s - %s\n", command->commandStr, command->helpStr);
  45. break;
  46. }
  47. command++;
  48. }
  49. }
  50. }
  51. static void laserCmd(uint8_t argc, char *argv[]) {
  52. if(argc > 1) {
  53. uint8_t state = (uint8_t)strtoul(argv[1], NULL, 10);
  54. if(state) {
  55. GPIO_ResetBits(GPIOE, GPIO_Pin_4);
  56. } else {
  57. GPIO_SetBits(GPIOE, GPIO_Pin_4);
  58. }
  59. } else {
  60. printf("Invalid arguments\n");
  61. argv[1] = argv[0];
  62. helpFn(2, argv);
  63. }
  64. }
  65. static void qikCmd(uint8_t argc, char *argv[]) {
  66. switch(argc) {
  67. case 3: {
  68. uint8_t mot = (uint8_t)strtoul(argv[1], NULL, 10);
  69. if(strcmp("coast", argv[2]) == 0) {
  70. qikSetCoast(mot);
  71. //printf("M%d coast\n", mot);
  72. }
  73. break;
  74. }
  75. case 4: {
  76. uint8_t mot = (uint8_t)strtoul(argv[1], NULL, 10);
  77. int32_t speed = strtol(argv[3], NULL, 10);
  78. uint8_t dir = 0;
  79. if(speed > 0) {
  80. dir = 1;
  81. }
  82. if(strcmp("mov", argv[2]) == 0) {
  83. // printf("Move M%d %s at speed %d\n", mot, (dir)? "Fwd" : "Rev", abs(speed));
  84. qikSetSpeed(mot, abs(speed), dir);
  85. }
  86. break;
  87. }
  88. }
  89. }
  90. static void motorCmd(uint8_t argc, char *argv[]) {
  91. switch(argc) {
  92. case 2: {
  93. if(strcmp("stop", argv[1]) == 0) {
  94. motorStop(0);
  95. motorStop(1);
  96. } else if(strcmp("dis", argv[1]) == 0) {
  97. motorDisable();
  98. } else if(strcmp("en", argv[1]) == 0) {
  99. motorEnable();
  100. } else if(strcmp("dbgon", argv[1]) == 0) {
  101. motorDebug(1);
  102. } else if(strcmp("dbgoff", argv[1]) == 0) {
  103. motorDebug(0);
  104. } else if(strcmp("center", argv[1]) == 0) {
  105. motorCenter();
  106. } else {
  107. uint8_t mot = (uint8_t)strtoul(argv[1], NULL, 10);
  108. if(mot < TOTAL_MOTORS) {
  109. printf("m %d %d\n", mot, motorGetPos(mot));
  110. }
  111. }
  112. break;
  113. }
  114. case 3: {
  115. uint8_t mot = (uint8_t)strtoul(argv[1], NULL, 10);
  116. int16_t pos = strtol(argv[2], NULL, 10);
  117. motorSetPos(mot, pos);
  118. break;
  119. }
  120. case 5: {
  121. uint8_t mot = (uint8_t)strtoul(argv[1], NULL, 10);
  122. if(strcmp("set", argv[2]) == 0) {
  123. int32_t val = strtol(argv[4], NULL, 10);
  124. pidVar_t pidVar;
  125. switch(argv[3][0]) {
  126. case 'p': {
  127. pidVar = pidP;
  128. break;
  129. }
  130. case 'i': {
  131. pidVar = pidI;
  132. break;
  133. }
  134. case 'd': {
  135. pidVar = pidD;
  136. break;
  137. }
  138. default: {
  139. pidVar = pidNone;
  140. }
  141. }
  142. if(pidVar != pidNone) {
  143. motorSetPIDVar(mot, pidVar, val);
  144. }
  145. }
  146. break;
  147. }
  148. }
  149. }
  150. //
  151. // Put any initialization code here
  152. //
  153. void consoleInit() {
  154. motorInit();
  155. // Init Laser
  156. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  157. GPIO_Init(GPIOE, &(GPIO_InitTypeDef){GPIO_Pin_4, GPIO_Mode_OUT, GPIO_OType_PP, GPIO_Speed_2MHz, GPIO_PuPd_NOPULL});
  158. GPIO_SetBits(GPIOE, GPIO_Pin_4);
  159. }
  160. //
  161. // Check to see if there is new data and process it if there is
  162. //
  163. void consoleProcess() {
  164. uint32_t inBytes = fifoSize(&usbRxFifo);
  165. if(inBytes > 0) {
  166. uint32_t newLine = 0;
  167. for(int32_t index = 0; index < inBytes; index++){
  168. if((fifoPeek(&usbRxFifo, index) == '\n') || (fifoPeek(&usbRxFifo, index) == '\r')) {
  169. newLine = index + 1;
  170. break;
  171. }
  172. }
  173. if(newLine > sizeof(cmdBuff)) {
  174. newLine = sizeof(cmdBuff) - 1;
  175. }
  176. if(newLine) {
  177. uint8_t *pBuf = (uint8_t *)cmdBuff;
  178. while(newLine--){
  179. *pBuf++ = fifoPop(&usbRxFifo);
  180. }
  181. // If it's an \r\n combination, discard the second one
  182. if((fifoPeek(&usbRxFifo, 0) == '\n') || (fifoPeek(&usbRxFifo, 0) == '\r')) {
  183. fifoPop(&usbRxFifo);
  184. }
  185. *(pBuf - 1) = 0; // String terminator
  186. argc = 0;
  187. // Get command
  188. argv[argc] = strtok(cmdBuff, " ");
  189. // Get arguments (if any)
  190. while ((argv[argc] != NULL) && (argc < sizeof(argv)/sizeof(char *))){
  191. argc++;
  192. argv[argc] = strtok(NULL, " ");
  193. }
  194. if(argc > 0) {
  195. command_t *command = commands;
  196. while(command->commandStr != NULL) {
  197. if(strcmp(command->commandStr, argv[0]) == 0) {
  198. command->fn(argc, argv);
  199. break;
  200. }
  201. command++;
  202. }
  203. if(command->commandStr == NULL) {
  204. printf("Unknown command '%s'\n", argv[0]);
  205. helpFn(1, NULL);
  206. }
  207. }
  208. }
  209. }
  210. }