irda.c 15 KB

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