input_cli.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "input_i.h"
  2. #include <furi.h>
  3. #include <cli/cli.h>
  4. #include <toolbox/args.h>
  5. static void input_cli_usage() {
  6. printf("Usage:\r\n");
  7. printf("input <cmd> <args>\r\n");
  8. printf("Cmd list:\r\n");
  9. printf("\tdump\t\t\t - dump input events\r\n");
  10. printf("\tsend <key> <type>\t - send input event\r\n");
  11. }
  12. static void input_cli_dump_events_callback(const void* value, void* ctx) {
  13. furi_assert(value);
  14. furi_assert(ctx);
  15. osMessageQueueId_t input_queue = ctx;
  16. osMessageQueuePut(input_queue, value, 0, osWaitForever);
  17. }
  18. static void input_cli_dump(Cli* cli, string_t args, Input* input) {
  19. UNUSED(args);
  20. osMessageQueueId_t input_queue = osMessageQueueNew(8, sizeof(InputEvent), NULL);
  21. FuriPubSubSubscription* input_subscription =
  22. furi_pubsub_subscribe(input->event_pubsub, input_cli_dump_events_callback, input_queue);
  23. InputEvent input_event;
  24. printf("Press CTRL+C to stop\r\n");
  25. while(!cli_cmd_interrupt_received(cli)) {
  26. if(osMessageQueueGet(input_queue, &input_event, NULL, 100) == osOK) {
  27. printf(
  28. "key: %s type: %s\r\n",
  29. input_get_key_name(input_event.key),
  30. input_get_type_name(input_event.type));
  31. }
  32. }
  33. furi_pubsub_unsubscribe(input->event_pubsub, input_subscription);
  34. osMessageQueueDelete(input_queue);
  35. }
  36. static void input_cli_send_print_usage() {
  37. printf("Invalid arguments. Usage:\r\n");
  38. printf("\tinput send <key> <type>\r\n");
  39. printf("\t\t <key>\t - one of 'up', 'down', 'left', 'right', 'back', 'ok'\r\n");
  40. printf("\t\t <type>\t - one of 'press', 'release', 'short', 'long'\r\n");
  41. }
  42. static void input_cli_send(Cli* cli, string_t args, Input* input) {
  43. UNUSED(cli);
  44. InputEvent event;
  45. string_t key_str;
  46. string_init(key_str);
  47. bool parsed = false;
  48. do {
  49. // Parse Key
  50. if(!args_read_string_and_trim(args, key_str)) {
  51. break;
  52. }
  53. if(!string_cmp(key_str, "up")) {
  54. event.key = InputKeyUp;
  55. } else if(!string_cmp(key_str, "down")) {
  56. event.key = InputKeyDown;
  57. } else if(!string_cmp(key_str, "left")) {
  58. event.key = InputKeyLeft;
  59. } else if(!string_cmp(key_str, "right")) {
  60. event.key = InputKeyRight;
  61. } else if(!string_cmp(key_str, "ok")) {
  62. event.key = InputKeyOk;
  63. } else if(!string_cmp(key_str, "back")) {
  64. event.key = InputKeyBack;
  65. } else {
  66. break;
  67. }
  68. // Parse Type
  69. if(!string_cmp(args, "press")) {
  70. event.type = InputTypePress;
  71. } else if(!string_cmp(args, "release")) {
  72. event.type = InputTypeRelease;
  73. } else if(!string_cmp(args, "short")) {
  74. event.type = InputTypeShort;
  75. } else if(!string_cmp(args, "long")) {
  76. event.type = InputTypeLong;
  77. } else {
  78. break;
  79. }
  80. parsed = true;
  81. } while(false);
  82. if(parsed) {
  83. furi_pubsub_publish(input->event_pubsub, &event);
  84. } else {
  85. input_cli_send_print_usage();
  86. }
  87. string_clear(key_str);
  88. }
  89. void input_cli(Cli* cli, string_t args, void* context) {
  90. furi_assert(cli);
  91. furi_assert(context);
  92. Input* input = context;
  93. string_t cmd;
  94. string_init(cmd);
  95. do {
  96. if(!args_read_string_and_trim(args, cmd)) {
  97. input_cli_usage();
  98. break;
  99. }
  100. if(string_cmp_str(cmd, "dump") == 0) {
  101. input_cli_dump(cli, args, input);
  102. break;
  103. }
  104. if(string_cmp_str(cmd, "send") == 0) {
  105. input_cli_send(cli, args, input);
  106. break;
  107. }
  108. input_cli_usage();
  109. } while(false);
  110. string_clear(cmd);
  111. }