cli_commands.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "cli_commands.h"
  2. #include <api-hal.h>
  3. #include <api-hal-gpio.h>
  4. #include <rtc.h>
  5. void cli_command_help(string_t args, void* context) {
  6. (void)args;
  7. Cli* cli = context;
  8. printf("Commands we have:");
  9. furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK);
  10. CliCommandDict_it_t it;
  11. for(CliCommandDict_it(it, cli->commands); !CliCommandDict_end_p(it); CliCommandDict_next(it)) {
  12. CliCommandDict_itref_t* ref = CliCommandDict_ref(it);
  13. printf(" ");
  14. printf(string_get_cstr(ref->key));
  15. };
  16. furi_check(osMutexRelease(cli->mutex) == osOK);
  17. if(string_size(args) > 0) {
  18. cli_nl();
  19. printf("Also I have no clue what '");
  20. printf(string_get_cstr(args));
  21. printf("' is.");
  22. }
  23. }
  24. void cli_command_version(string_t args, void* context) {
  25. (void)args;
  26. (void)context;
  27. cli_print_version();
  28. }
  29. void cli_command_uuid(string_t args, void* context) {
  30. (void)args;
  31. (void)context;
  32. size_t uid_size = api_hal_uid_size();
  33. const uint8_t* uid = api_hal_uid();
  34. string_t byte_str;
  35. string_init(byte_str);
  36. string_cat_printf(byte_str, "UID:");
  37. for(size_t i = 0; i < uid_size; i++) {
  38. uint8_t uid_byte = uid[i];
  39. string_cat_printf(byte_str, "%02X", uid_byte);
  40. }
  41. printf(string_get_cstr(byte_str));
  42. }
  43. void cli_command_date(string_t args, void* context) {
  44. RTC_DateTypeDef date;
  45. RTC_TimeTypeDef time;
  46. // TODO add get_datetime to core, not use HAL here
  47. // READ ORDER MATTERS! Time then date.
  48. HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
  49. HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
  50. string_t datetime_str;
  51. string_init(datetime_str);
  52. string_cat_printf(datetime_str, "%.2d:%.2d:%.2d ", time.Hours, time.Minutes, time.Seconds);
  53. string_cat_printf(datetime_str, "%.2d-%.2d-%.2d", date.Month, date.Date, 2000 + date.Year);
  54. printf(string_get_cstr(datetime_str));
  55. string_clear(datetime_str);
  56. }
  57. void cli_command_log(string_t args, void* context) {
  58. Cli* cli = context;
  59. furi_stdglue_set_global_stdout_callback(cli_stdout_callback);
  60. printf("Press any key to stop...\r\n");
  61. cli_getc(cli);
  62. furi_stdglue_set_global_stdout_callback(NULL);
  63. }
  64. void cli_command_vibro(string_t args, void* context) {
  65. if(!string_cmp(args, "0")) {
  66. api_hal_vibro_on(false);
  67. } else if(!string_cmp(args, "1")) {
  68. api_hal_vibro_on(true);
  69. } else {
  70. printf("Wrong input");
  71. }
  72. }
  73. void cli_command_led(string_t args, void* context) {
  74. // Get first word as light name
  75. Light light;
  76. string_t light_name;
  77. string_init(light_name);
  78. size_t ws = string_search_char(args, ' ');
  79. if(ws == STRING_FAILURE) {
  80. printf("Wrong input");
  81. string_clear(light_name);
  82. return;
  83. } else {
  84. string_set_n(light_name, args, 0, ws);
  85. string_right(args, ws);
  86. string_strim(args);
  87. }
  88. // Check light name
  89. if(!string_cmp(light_name, "r")) {
  90. light = LightRed;
  91. } else if(!string_cmp(light_name, "g")) {
  92. light = LightGreen;
  93. } else if(!string_cmp(light_name, "b")) {
  94. light = LightBlue;
  95. } else if(!string_cmp(light_name, "bl")) {
  96. light = LightBacklight;
  97. } else {
  98. printf("Wrong argument");
  99. string_clear(light_name);
  100. return;
  101. }
  102. string_clear(light_name);
  103. // Read light value from the rest of the string
  104. char* end_ptr;
  105. uint32_t value = strtoul(string_get_cstr(args), &end_ptr, 0);
  106. if(!(value < 256 && *end_ptr == '\0')) {
  107. printf("Wrong argument");
  108. return;
  109. }
  110. api_hal_light_set(light, value);
  111. }
  112. void cli_command_gpio_set(string_t args, void* context) {
  113. char pin_names[][4] = {"PC0", "PC1", "PC3", "PB2", "PB3", "PA4", "PA6", "PA7"};
  114. GpioPin gpio[] = {
  115. {.port = GPIOC, .pin = LL_GPIO_PIN_0},
  116. {.port = GPIOC, .pin = LL_GPIO_PIN_1},
  117. {.port = GPIOC, .pin = LL_GPIO_PIN_3},
  118. {.port = GPIOB, .pin = LL_GPIO_PIN_2},
  119. {.port = GPIOB, .pin = LL_GPIO_PIN_3},
  120. {.port = GPIOA, .pin = LL_GPIO_PIN_4},
  121. {.port = GPIOA, .pin = LL_GPIO_PIN_6},
  122. {.port = GPIOA, .pin = LL_GPIO_PIN_7}};
  123. uint8_t num = 0;
  124. bool pin_found = false;
  125. // Get first word as pin name
  126. string_t pin_name;
  127. string_init(pin_name);
  128. size_t ws = string_search_char(args, ' ');
  129. if(ws == STRING_FAILURE) {
  130. printf("Wrong input. Correct usage: gpio_set <pin_name> <0|1>");
  131. string_clear(pin_name);
  132. return;
  133. } else {
  134. string_set_n(pin_name, args, 0, ws);
  135. string_right(args, ws);
  136. string_strim(args);
  137. }
  138. // Search correct pin name
  139. for(num = 0; num < sizeof(pin_names) / sizeof(char*); num++) {
  140. if(!string_cmp(pin_name, pin_names[num])) {
  141. pin_found = true;
  142. break;
  143. }
  144. }
  145. if(!pin_found) {
  146. printf("Wrong pin name. Available pins: ");
  147. for(uint8_t i = 0; i < sizeof(pin_names) / sizeof(char*); i++) {
  148. printf("%s ", pin_names[i]);
  149. }
  150. string_clear(pin_name);
  151. return;
  152. }
  153. string_clear(pin_name);
  154. // Read "0" or "1" as second argument to set or reset pin
  155. if(!string_cmp(args, "0")) {
  156. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  157. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  158. LL_GPIO_ResetOutputPin(gpio[num].port, gpio[num].pin);
  159. } else if(!string_cmp(args, "1")) {
  160. LL_GPIO_SetPinMode(gpio[num].port, gpio[num].pin, LL_GPIO_MODE_OUTPUT);
  161. LL_GPIO_SetPinOutputType(gpio[num].port, gpio[num].pin, LL_GPIO_OUTPUT_PUSHPULL);
  162. LL_GPIO_SetOutputPin(gpio[num].port, gpio[num].pin);
  163. } else {
  164. printf("Wrong 2nd argument. Use \"1\" to set, \"0\" to reset");
  165. }
  166. return;
  167. }
  168. void cli_commands_init(Cli* cli) {
  169. cli_add_command(cli, "help", cli_command_help, cli);
  170. cli_add_command(cli, "?", cli_command_help, cli);
  171. cli_add_command(cli, "version", cli_command_version, cli);
  172. cli_add_command(cli, "!", cli_command_version, cli);
  173. cli_add_command(cli, "uid", cli_command_uuid, cli);
  174. cli_add_command(cli, "date", cli_command_date, cli);
  175. cli_add_command(cli, "log", cli_command_log, cli);
  176. cli_add_command(cli, "vibro", cli_command_vibro, cli);
  177. cli_add_command(cli, "led", cli_command_led, cli);
  178. cli_add_command(cli, "gpio_set", cli_command_gpio_set, cli);
  179. }