input_cli.c 3.7 KB

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