irda_app_old.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include <furi.h>
  2. #include <api-hal.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <cli/cli.h>
  6. #include <notification/notification-messages.h>
  7. #include <api-hal-irda.h>
  8. #include "irda.h"
  9. typedef enum {
  10. EventTypeTick,
  11. EventTypeKey,
  12. EventTypeRX,
  13. } EventType;
  14. typedef IrdaMessage IrDAPacket;
  15. typedef struct {
  16. union {
  17. InputEvent input;
  18. IrDAPacket rx;
  19. } value;
  20. EventType type;
  21. } AppEvent;
  22. //typedef struct {
  23. // IrdaProtocol protocol;
  24. // uint32_t address;
  25. // uint32_t command;
  26. //} IrDAPacket;
  27. #define IRDA_PACKET_COUNT 8
  28. typedef struct {
  29. osMessageQueueId_t cli_ir_rx_queue;
  30. Cli* cli;
  31. bool cli_cmd_is_active;
  32. } IrDAApp;
  33. typedef struct {
  34. uint8_t mode_id;
  35. uint16_t carrier_freq;
  36. uint8_t carrier_duty_cycle_id;
  37. uint8_t packet_id;
  38. IrDAPacket packets[IRDA_PACKET_COUNT];
  39. } State;
  40. typedef void (*ModeInput)(AppEvent*, State*);
  41. typedef void (*ModeRender)(Canvas*, State*);
  42. void input_carrier(AppEvent* event, State* state);
  43. void render_carrier(Canvas* canvas, State* state);
  44. void input_packet(AppEvent* event, State* state);
  45. void render_packet(Canvas* canvas, State* state);
  46. typedef struct {
  47. ModeRender render;
  48. ModeInput input;
  49. } Mode;
  50. const Mode modes[] = {
  51. {.render = render_carrier, .input = input_carrier},
  52. {.render = render_packet, .input = input_packet},
  53. };
  54. const float duty_cycles[] = {0.1, 0.25, 0.333, 0.5, 1.0};
  55. void render_carrier(Canvas* canvas, State* state) {
  56. canvas_set_font(canvas, FontSecondary);
  57. canvas_draw_str(canvas, 2, 25, "carrier mode >");
  58. canvas_draw_str(canvas, 2, 37, "? /\\ freq | \\/ duty cycle");
  59. {
  60. char buf[24];
  61. sprintf(buf, "frequency: %u Hz", state->carrier_freq);
  62. canvas_draw_str(canvas, 2, 50, buf);
  63. sprintf(
  64. buf, "duty cycle: %d/1000", (int)(duty_cycles[state->carrier_duty_cycle_id] * 1000));
  65. canvas_draw_str(canvas, 2, 62, buf);
  66. }
  67. }
  68. void input_carrier(AppEvent* event, State* state) {
  69. if(event->value.input.key == InputKeyOk) {
  70. if(event->value.input.type == InputTypePress) {
  71. api_hal_irda_pwm_set(duty_cycles[state->carrier_duty_cycle_id], state->carrier_freq);
  72. } else if(event->value.input.type == InputTypeRelease) {
  73. api_hal_irda_pwm_stop();
  74. }
  75. }
  76. if(event->value.input.type == InputTypeShort && event->value.input.key == InputKeyUp) {
  77. if(state->carrier_freq < 45000) {
  78. state->carrier_freq += 1000;
  79. } else {
  80. state->carrier_freq = 33000;
  81. }
  82. }
  83. if(event->value.input.type == InputTypeShort && event->value.input.key == InputKeyDown) {
  84. uint8_t duty_cycles_count = sizeof(duty_cycles) / sizeof(duty_cycles[0]);
  85. if(state->carrier_duty_cycle_id < (duty_cycles_count - 1)) {
  86. state->carrier_duty_cycle_id++;
  87. } else {
  88. state->carrier_duty_cycle_id = 0;
  89. }
  90. }
  91. }
  92. void render_packet(Canvas* canvas, State* state) {
  93. canvas_set_font(canvas, FontSecondary);
  94. canvas_draw_str(canvas, 2, 25, "< packet mode");
  95. canvas_draw_str(canvas, 2, 37, "? /\\ \\/ packet");
  96. {
  97. char buf[30];
  98. sprintf(
  99. buf,
  100. "P[%d]: %s 0x%lX 0x%lX",
  101. state->packet_id,
  102. irda_get_protocol_name(state->packets[state->packet_id].protocol),
  103. state->packets[state->packet_id].address,
  104. state->packets[state->packet_id].command);
  105. canvas_draw_str(canvas, 2, 50, buf);
  106. }
  107. }
  108. void input_packet(AppEvent* event, State* state) {
  109. if(event->value.input.key == InputKeyOk) {
  110. if(event->value.input.type == InputTypeShort) {
  111. IrdaMessage message = {
  112. .protocol = state->packets[state->packet_id].protocol,
  113. .address = state->packets[state->packet_id].address,
  114. .command = state->packets[state->packet_id].command,
  115. };
  116. irda_send(&message, 1);
  117. }
  118. }
  119. if(event->value.input.type == InputTypeShort && event->value.input.key == InputKeyDown) {
  120. if(state->packet_id < (IRDA_PACKET_COUNT - 1)) {
  121. state->packet_id++;
  122. };
  123. }
  124. if(event->value.input.type == InputTypeShort && event->value.input.key == InputKeyUp) {
  125. if(state->packet_id > 0) {
  126. state->packet_id--;
  127. };
  128. }
  129. }
  130. static void render_callback(Canvas* canvas, void* ctx) {
  131. State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
  132. if(state != NULL) {
  133. canvas_clear(canvas);
  134. canvas_set_color(canvas, ColorBlack);
  135. canvas_set_font(canvas, FontPrimary);
  136. canvas_draw_str(canvas, 2, 12, "irda test");
  137. modes[state->mode_id].render(canvas, state);
  138. release_mutex((ValueMutex*)ctx, state);
  139. }
  140. }
  141. static void input_callback(InputEvent* input_event, void* ctx) {
  142. osMessageQueueId_t event_queue = ctx;
  143. AppEvent event;
  144. event.type = EventTypeKey;
  145. event.value.input = *input_event;
  146. osMessageQueuePut(event_queue, &event, 0, 0);
  147. }
  148. void init_packet(
  149. State* state,
  150. uint8_t index,
  151. IrdaProtocol protocol,
  152. uint32_t address,
  153. uint32_t command) {
  154. if(index >= IRDA_PACKET_COUNT) return;
  155. state->packets[index].protocol = protocol;
  156. state->packets[index].address = address;
  157. state->packets[index].command = command;
  158. }
  159. void irda_cli_cmd_rx(Cli* cli, string_t args, void* context) {
  160. furi_assert(context);
  161. IrDAPacket packet;
  162. IrDAApp* app = context;
  163. app->cli_cmd_is_active = true;
  164. bool exit = false;
  165. printf("Reading income packets...\r\nPress Ctrl+C to abort\r\n");
  166. while(!exit) {
  167. exit = cli_cmd_interrupt_received(app->cli);
  168. osStatus status = osMessageQueueGet(app->cli_ir_rx_queue, &packet, 0, 5);
  169. if(status == osOK) {
  170. printf(
  171. "%s "
  172. "Address:0x%02X Command: 0x%02X %s\r\n",
  173. irda_get_protocol_name(packet.protocol),
  174. (uint8_t)packet.address,
  175. (uint8_t)packet.command,
  176. packet.repeat ? "R" : "");
  177. }
  178. }
  179. printf("Interrupt command received");
  180. app->cli_cmd_is_active = false;
  181. return;
  182. }
  183. void irda_cli_cmd_tx(Cli* cli, string_t args, void* context) {
  184. furi_assert(context);
  185. ValueMutex* state_mutex = context;
  186. // Read protocol name
  187. IrdaProtocol protocol;
  188. string_t protocol_str;
  189. string_init(protocol_str);
  190. size_t ws = string_search_char(args, ' ');
  191. if(ws == STRING_FAILURE) {
  192. printf("Invalid input. Use ir_tx PROTOCOL ADDRESS COMMAND");
  193. string_clear(protocol_str);
  194. return;
  195. } else {
  196. string_set_n(protocol_str, args, 0, ws);
  197. string_right(args, ws);
  198. string_strim(args);
  199. }
  200. if(!string_cmp_str(protocol_str, "NEC")) {
  201. protocol = IrdaProtocolNEC;
  202. } else if(!string_cmp_str(protocol_str, "SAMSUNG")) {
  203. protocol = IrdaProtocolSamsung32;
  204. } else {
  205. printf("Incorrect protocol. Valid protocols: `NEC`, `SAMSUNG`");
  206. string_clear(protocol_str);
  207. return;
  208. }
  209. string_clear(protocol_str);
  210. // Read address
  211. uint16_t address = strtoul(string_get_cstr(args), NULL, 16);
  212. ws = string_search_char(args, ' ');
  213. if(!(ws == 4 || ws == 6)) {
  214. printf("Invalid address format. Use 4 [0-F] hex digits in 0xXXXX or XXXX formats");
  215. return;
  216. }
  217. string_right(args, ws);
  218. string_strim(args);
  219. // Read command
  220. uint16_t command = strtoul(string_get_cstr(args), NULL, 16);
  221. ws = string_search_char(args, '\0');
  222. if(!(ws == 4 || ws == 6)) {
  223. printf("Invalid command format. Use 4 [0-F] hex digits in 0xXXXX or XXXX formats");
  224. return;
  225. }
  226. State* state = (State*)acquire_mutex(state_mutex, 25);
  227. if(state == NULL) {
  228. printf("IRDA resources busy\r\n");
  229. return;
  230. }
  231. IrdaMessage message = {
  232. .protocol = protocol,
  233. .address = address,
  234. .command = command,
  235. };
  236. irda_send(&message, 1);
  237. release_mutex(state_mutex, state);
  238. return;
  239. }
  240. typedef struct {
  241. osMessageQueueId_t event_queue;
  242. IrdaDecoderHandler* handler;
  243. } IsrContext;
  244. void irda_rx_callback(void* ctx, bool level, uint32_t duration) {
  245. IsrContext* isr_context = ctx;
  246. const IrdaMessage* message = irda_decode(isr_context->handler, level, duration);
  247. AppEvent event;
  248. event.type = EventTypeRX;
  249. if(message) {
  250. event.value.rx = *message;
  251. furi_check(osMessageQueuePut(isr_context->event_queue, &event, 0, 0) == osOK);
  252. }
  253. }
  254. int32_t irda2(void* p) {
  255. osMessageQueueId_t event_queue = osMessageQueueNew(32, sizeof(AppEvent), NULL);
  256. State _state;
  257. uint8_t mode_count = sizeof(modes) / sizeof(modes[0]);
  258. uint8_t duty_cycles_count = sizeof(duty_cycles) / sizeof(duty_cycles[0]);
  259. _state.carrier_duty_cycle_id = duty_cycles_count - 2;
  260. _state.carrier_freq = 36000;
  261. _state.mode_id = 0;
  262. _state.packet_id = 0;
  263. IrDAApp irda_app;
  264. irda_app.cli = furi_record_open("cli");
  265. irda_app.cli_ir_rx_queue = osMessageQueueNew(1, sizeof(IrDAPacket), NULL);
  266. irda_app.cli_cmd_is_active = false;
  267. NotificationApp* notification = furi_record_open("notification");
  268. for(uint8_t i = 0; i < IRDA_PACKET_COUNT; i++) {
  269. init_packet(&_state, i, 0, 0, 0);
  270. }
  271. init_packet(&_state, 0, IrdaProtocolNEC, 0x00, 0x11);
  272. init_packet(&_state, 1, IrdaProtocolNEC, 0x08, 0x59);
  273. init_packet(&_state, 2, IrdaProtocolNEC, 0x00, 0x10);
  274. init_packet(&_state, 3, IrdaProtocolNEC, 0x00, 0x15);
  275. init_packet(&_state, 4, IrdaProtocolNEC, 0x00, 0x25);
  276. init_packet(&_state, 5, IrdaProtocolSamsung32, 0x0E, 0x0C);
  277. init_packet(&_state, 6, IrdaProtocolSamsung32, 0x0E, 0x0D);
  278. init_packet(&_state, 7, IrdaProtocolSamsung32, 0x0E, 0x0E);
  279. ValueMutex state_mutex;
  280. if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
  281. printf("cannot create mutex\r\n");
  282. return 255;
  283. }
  284. ViewPort* view_port = view_port_alloc();
  285. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  286. view_port_input_callback_set(view_port, input_callback, event_queue);
  287. cli_add_command(irda_app.cli, "ir_rx", irda_cli_cmd_rx, &irda_app);
  288. cli_add_command(irda_app.cli, "ir_tx", irda_cli_cmd_tx, &state_mutex);
  289. // Open GUI and register view_port
  290. Gui* gui = furi_record_open("gui");
  291. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  292. IsrContext isr_context = {
  293. .handler = irda_alloc_decoder(),
  294. .event_queue = event_queue,
  295. };
  296. api_hal_irda_rx_irq_init();
  297. api_hal_irda_rx_irq_set_callback(irda_rx_callback, &isr_context);
  298. AppEvent event;
  299. while(1) {
  300. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, 500);
  301. if(event_status == osOK) {
  302. if(event.type == EventTypeKey) {
  303. State* state = (State*)acquire_mutex_block(&state_mutex);
  304. // press events
  305. if(event.value.input.type == InputTypeShort &&
  306. event.value.input.key == InputKeyBack) {
  307. release_mutex(&state_mutex, state);
  308. // remove all view_ports create by app
  309. gui_remove_view_port(gui, view_port);
  310. view_port_free(view_port);
  311. // free decoder
  312. delete_mutex(&state_mutex);
  313. osMessageQueueDelete(event_queue);
  314. osMessageQueueDelete(irda_app.cli_ir_rx_queue);
  315. cli_delete_command(irda_app.cli, "ir_rx");
  316. cli_delete_command(irda_app.cli, "ir_tx");
  317. furi_record_close("cli");
  318. furi_record_close("notification");
  319. api_hal_irda_rx_irq_deinit();
  320. irda_free_decoder(isr_context.handler);
  321. // exit
  322. return 0;
  323. }
  324. if(event.value.input.type == InputTypeShort &&
  325. event.value.input.key == InputKeyLeft) {
  326. if(state->mode_id > 0) {
  327. state->mode_id--;
  328. }
  329. }
  330. if(event.value.input.type == InputTypeShort &&
  331. event.value.input.key == InputKeyRight) {
  332. if(state->mode_id < (mode_count - 1)) {
  333. state->mode_id++;
  334. }
  335. }
  336. modes[state->mode_id].input(&event, state);
  337. release_mutex(&state_mutex, state);
  338. view_port_update(view_port);
  339. } else if(event.type == EventTypeRX) {
  340. notification_message(notification, &sequence_blink_red_10);
  341. // save only if we in packet mode
  342. State* state = (State*)acquire_mutex_block(&state_mutex);
  343. IrDAPacket packet = event.value.rx;
  344. if(state->mode_id == 1) {
  345. printf("P=%s ", irda_get_protocol_name(packet.protocol));
  346. printf("A=0x%02lX ", packet.address);
  347. printf("C=0x%02lX ", packet.command);
  348. if(packet.repeat) {
  349. printf("R");
  350. }
  351. printf("\r\n");
  352. // Save packet to state
  353. memcpy(&(state->packets[state->packet_id]), &packet, sizeof(IrDAPacket));
  354. }
  355. if(irda_app.cli_cmd_is_active) {
  356. // Send decoded packet to cli
  357. osMessageQueuePut(irda_app.cli_ir_rx_queue, &packet, 0, 0);
  358. }
  359. release_mutex(&state_mutex, state);
  360. view_port_update(view_port);
  361. // blink anyway
  362. notification_message(notification, &sequence_blink_green_10);
  363. }
  364. } else {
  365. // event timeout
  366. }
  367. }
  368. }