irda_app.c 14 KB

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