input_cli.c 3.6 KB

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