cmdRx.c 950 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "uart.h"
  2. #include "stdio.h"
  3. #include "main.h"
  4. #define MAX_LEN 100
  5. void handleCmdRx() {
  6. static int mode = -1;
  7. static unsigned char str[MAX_LEN];
  8. static unsigned char *valP;
  9. static int reset = 1;
  10. if( reset == 1 ) {
  11. valP = str;
  12. mode = 0;
  13. reset = 0;
  14. }
  15. while( serialAvailable() ) {
  16. const unsigned char c = serialRead();
  17. if( !(c>='0' && c<='9') &&
  18. !(c>='a' && c<='z') &&
  19. !(c>='A' && c<='Z') &&
  20. c!='\"' && c!=',' && c!='.' && c!='{' && c!='}') {
  21. reset = 1;
  22. }
  23. else {
  24. if( mode == 0 && c=='{' ) {
  25. *(valP)++ = c;
  26. mode = 1;
  27. }
  28. else if( mode == 1 && c=='}' ) {
  29. *(valP)++ = c;
  30. *(valP)++ = '\0';
  31. int val=-1;
  32. const int no = sscanf((const char *)str,"{%d}",&val);
  33. if( no == 1 ) {
  34. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, (val==1)?GPIO_PIN_RESET:GPIO_PIN_SET);
  35. }
  36. reset=1;
  37. }
  38. else if( mode==1 ){
  39. *(valP)++ = c;
  40. }
  41. }
  42. }
  43. }