console.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "mcc_generated_files/mcc.h"
  2. #include "serial.h"
  3. #include "rn4870/rn4870.h"
  4. #include "rn4870/hid_defs.h"
  5. #include <stdlib.h>
  6. static char cmd[48];
  7. static int idx;
  8. static void process_command(void) {
  9. if (!strncmp(cmd, "ble command mode", 16)) {
  10. serial_println("Trying to enter BLE command mode ...");
  11. rn4870_enter_command_mode();
  12. } else if (!strncmp(cmd, "ble list services", 17)) {
  13. serial_println("Retrieving a list of defined BLE services and their characteristics ...");
  14. rn4870_list_services();
  15. } else if (!strncmp(cmd, "ble reset services", 18)) {
  16. serial_println("Resetting defined BLE services ...");
  17. rn4870_reset_services();
  18. } else if (!strncmp(cmd, "ble add battery service", 23)) {
  19. serial_print("Trying to add service ...");
  20. rn4870_add_battery_service();
  21. } else if (!strncmp(cmd, "set battery level", 17)) {
  22. const char *p = cmd + 18;
  23. if (!p) {
  24. serial_print("You must specify the level!");
  25. return;
  26. }
  27. serial_print("Trying to set battery level to ");
  28. serial_println(p);
  29. uint8_t level = atoi(p);
  30. rn4870_set_battery_level(level);
  31. } else if (!strncmp(cmd, "ble add info service", 20)) {
  32. serial_println("Trying to add device info service ...");
  33. rn4870_add_device_info_service();
  34. } else if (!strncmp(cmd, "ble add hid service", 19)) {
  35. serial_println("Trying to add HID service ...");
  36. rn4870_add_hid_service();
  37. } else if (!strncmp(cmd, "ble set gap appearance", 22)) {
  38. const char *code = cmd + 23;
  39. if (!code) {
  40. serial_println("You must specify the code.");
  41. return;
  42. }
  43. serial_print("Setting GAP appearance to 0x");
  44. serial_println(code);
  45. rn4870_set_gap_appearance(code);
  46. } else if (!strncmp(cmd, "ble init hid", 12)) {
  47. serial_println("Trying to init HID service ...");
  48. rn4870_init_hid_param();
  49. } else if (!strncmp(cmd, "ble read characteristic", 23)) {
  50. char *handle = cmd + 24;
  51. serial_print("Trying to read characteristic ");
  52. serial_println(handle);
  53. rn4870_read_characteristic(handle);
  54. } else if (!strncmp(cmd, "mouse left click", 16)) {
  55. serial_println("Sending left click ...");
  56. rn4870_hid_mouse_click(MOUSE_LEFT_CLICK);
  57. } else if (!strncmp(cmd, "mouse right click", 17)) {
  58. serial_println("Sending right click ...");
  59. rn4870_hid_mouse_click(MOUSE_RIGHT_CLICK);
  60. } else {
  61. serial_print("Did not understand command: ");
  62. serial_println(cmd);
  63. }
  64. }
  65. static void process_rx_char(char ch) {
  66. if (ch == '\r') {
  67. } else if (ch == '\n') {
  68. cmd[idx] = '\0';
  69. idx = 0;
  70. process_command();
  71. } else {
  72. cmd[idx++] = ch;
  73. if (idx >= sizeof (cmd) - 1) idx = 0;
  74. }
  75. }
  76. void console_monitor(void) {
  77. if (EUSART1_is_rx_ready()) {
  78. char ch = EUSART1_Read();
  79. process_rx_char(ch);
  80. }
  81. }