irda.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #include <furi.h>
  2. #include <api-hal.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include "irda_nec.h"
  6. #include "irda_samsung.h"
  7. #include "irda_protocols.h"
  8. #include "irda-decoder/irda-decoder.h"
  9. typedef enum {
  10. EventTypeTick,
  11. EventTypeKey,
  12. EventTypeRX,
  13. } EventType;
  14. typedef struct {
  15. bool edge;
  16. uint32_t lasted;
  17. } RXValue;
  18. typedef struct {
  19. union {
  20. InputEvent input;
  21. RXValue rx;
  22. } value;
  23. EventType type;
  24. } AppEvent;
  25. typedef struct {
  26. IrDAProtocolType protocol;
  27. uint32_t address;
  28. uint32_t command;
  29. } IrDAPacket;
  30. #define IRDA_PACKET_COUNT 8
  31. typedef struct {
  32. uint8_t mode_id;
  33. uint16_t carrier_freq;
  34. uint8_t carrier_duty_cycle_id;
  35. uint8_t packet_id;
  36. IrDAPacket packets[IRDA_PACKET_COUNT];
  37. } State;
  38. typedef void (*ModeInput)(AppEvent*, State*);
  39. typedef void (*ModeRender)(Canvas*, State*);
  40. void input_carrier(AppEvent* event, State* state);
  41. void render_carrier(Canvas* canvas, State* state);
  42. void input_packet(AppEvent* event, State* state);
  43. void render_packet(Canvas* canvas, State* state);
  44. typedef struct {
  45. ModeRender render;
  46. ModeInput input;
  47. } Mode;
  48. const Mode modes[] = {
  49. {.render = render_carrier, .input = input_carrier},
  50. {.render = render_packet, .input = input_packet},
  51. };
  52. const float duty_cycles[] = {0.1, 0.25, 0.333, 0.5, 1.0};
  53. void render_carrier(Canvas* canvas, State* state) {
  54. canvas_set_font(canvas, FontSecondary);
  55. canvas_draw_str(canvas, 2, 25, "carrier mode >");
  56. canvas_draw_str(canvas, 2, 37, "? /\\ freq | \\/ duty cycle");
  57. {
  58. char buf[24];
  59. sprintf(buf, "frequency: %u Hz", state->carrier_freq);
  60. canvas_draw_str(canvas, 2, 50, buf);
  61. sprintf(
  62. buf, "duty cycle: %d/1000", (int)(duty_cycles[state->carrier_duty_cycle_id] * 1000));
  63. canvas_draw_str(canvas, 2, 62, buf);
  64. }
  65. }
  66. void input_carrier(AppEvent* event, State* state) {
  67. if(event->value.input.key == InputKeyOk) {
  68. if(event->value.input.type == InputTypePress) {
  69. irda_pwm_set(duty_cycles[state->carrier_duty_cycle_id], state->carrier_freq);
  70. } else if(event->value.input.type == InputTypeRelease) {
  71. irda_pwm_stop();
  72. }
  73. }
  74. if(event->value.input.type == InputTypeShort && event->value.input.key == InputKeyUp) {
  75. if(state->carrier_freq < 45000) {
  76. state->carrier_freq += 1000;
  77. } else {
  78. state->carrier_freq = 33000;
  79. }
  80. }
  81. if(event->value.input.type == InputTypeShort && event->value.input.key == InputKeyDown) {
  82. uint8_t duty_cycles_count = sizeof(duty_cycles) / sizeof(duty_cycles[0]);
  83. if(state->carrier_duty_cycle_id < (duty_cycles_count - 1)) {
  84. state->carrier_duty_cycle_id++;
  85. } else {
  86. state->carrier_duty_cycle_id = 0;
  87. }
  88. }
  89. }
  90. void render_packet(Canvas* canvas, State* state) {
  91. canvas_set_font(canvas, FontSecondary);
  92. canvas_draw_str(canvas, 2, 25, "< packet mode");
  93. canvas_draw_str(canvas, 2, 37, "? /\\ \\/ packet");
  94. {
  95. const char* protocol;
  96. switch(state->packets[state->packet_id].protocol) {
  97. case IRDA_NEC:
  98. protocol = "NEC";
  99. break;
  100. case IRDA_SAMSUNG:
  101. protocol = "SAMS";
  102. break;
  103. case IRDA_UNKNOWN:
  104. default:
  105. protocol = "UNK";
  106. break;
  107. }
  108. char buf[24];
  109. sprintf(
  110. buf,
  111. "P[%d]: %s 0x%lX 0x%lX",
  112. state->packet_id,
  113. protocol,
  114. state->packets[state->packet_id].address,
  115. state->packets[state->packet_id].command);
  116. canvas_draw_str(canvas, 2, 50, buf);
  117. }
  118. }
  119. void input_packet(AppEvent* event, State* state) {
  120. if(event->value.input.key == InputKeyOk) {
  121. if(event->value.input.type == InputTypeShort) {
  122. switch(state->packets[state->packet_id].protocol) {
  123. case IRDA_NEC:
  124. ir_nec_send(
  125. state->packets[state->packet_id].address,
  126. state->packets[state->packet_id].command);
  127. break;
  128. case IRDA_SAMSUNG:
  129. ir_samsung_send(
  130. state->packets[state->packet_id].address,
  131. state->packets[state->packet_id].command);
  132. break;
  133. default:
  134. break;
  135. }
  136. }
  137. }
  138. if(event->value.input.type == InputTypeShort && event->value.input.key == InputKeyDown) {
  139. if(state->packet_id < (IRDA_PACKET_COUNT - 1)) {
  140. state->packet_id++;
  141. };
  142. }
  143. if(event->value.input.type == InputTypeShort && event->value.input.key == InputKeyUp) {
  144. if(state->packet_id > 0) {
  145. state->packet_id--;
  146. };
  147. }
  148. }
  149. static void render_callback(Canvas* canvas, void* ctx) {
  150. State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
  151. if(state != NULL) {
  152. canvas_clear(canvas);
  153. canvas_set_color(canvas, ColorBlack);
  154. canvas_set_font(canvas, FontPrimary);
  155. canvas_draw_str(canvas, 2, 12, "irda test");
  156. modes[state->mode_id].render(canvas, state);
  157. release_mutex((ValueMutex*)ctx, state);
  158. }
  159. }
  160. static void input_callback(InputEvent* input_event, void* ctx) {
  161. osMessageQueueId_t event_queue = ctx;
  162. AppEvent event;
  163. event.type = EventTypeKey;
  164. event.value.input = *input_event;
  165. osMessageQueuePut(event_queue, &event, 0, 0);
  166. }
  167. void irda_timer_capture_callback(void* htim, void* comp_ctx) {
  168. TIM_HandleTypeDef* _htim = (TIM_HandleTypeDef*)htim;
  169. osMessageQueueId_t event_queue = (osMessageQueueId_t)comp_ctx;
  170. if(_htim->Instance == TIM2) {
  171. AppEvent event;
  172. event.type = EventTypeRX;
  173. uint32_t channel;
  174. if(_htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {
  175. // falling event
  176. event.value.rx.edge = false;
  177. channel = TIM_CHANNEL_1;
  178. } else if(_htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {
  179. // rising event
  180. event.value.rx.edge = true;
  181. channel = TIM_CHANNEL_2;
  182. } else {
  183. // not our event
  184. return;
  185. }
  186. event.value.rx.lasted = HAL_TIM_ReadCapturedValue(_htim, channel);
  187. __HAL_TIM_SET_COUNTER(_htim, 0);
  188. osMessageQueuePut(event_queue, &event, 0, 0);
  189. }
  190. }
  191. void init_packet(
  192. State* state,
  193. uint8_t index,
  194. IrDAProtocolType protocol,
  195. uint32_t address,
  196. uint32_t command) {
  197. if(index >= IRDA_PACKET_COUNT) return;
  198. state->packets[index].protocol = protocol;
  199. state->packets[index].address = address;
  200. state->packets[index].command = command;
  201. }
  202. int32_t irda(void* p) {
  203. osMessageQueueId_t event_queue = osMessageQueueNew(32, sizeof(AppEvent), NULL);
  204. State _state;
  205. uint8_t mode_count = sizeof(modes) / sizeof(modes[0]);
  206. uint8_t duty_cycles_count = sizeof(duty_cycles) / sizeof(duty_cycles[0]);
  207. _state.carrier_duty_cycle_id = duty_cycles_count - 2;
  208. _state.carrier_freq = 36000;
  209. _state.mode_id = 0;
  210. _state.packet_id = 0;
  211. for(uint8_t i = 0; i < IRDA_PACKET_COUNT; i++) {
  212. init_packet(&_state, i, IRDA_UNKNOWN, 0, 0);
  213. }
  214. init_packet(&_state, 0, IRDA_NEC, 0xFF00, 0x11);
  215. init_packet(&_state, 1, IRDA_NEC, 0xF708, 0x59);
  216. init_packet(&_state, 2, IRDA_NEC, 0xFF00, 0x10);
  217. init_packet(&_state, 3, IRDA_NEC, 0xFF00, 0x15);
  218. init_packet(&_state, 4, IRDA_NEC, 0xFF00, 0x25);
  219. init_packet(&_state, 5, IRDA_SAMSUNG, 0xE0E, 0xF30C);
  220. init_packet(&_state, 6, IRDA_SAMSUNG, 0xE0E, 0xF40D);
  221. init_packet(&_state, 7, IRDA_SAMSUNG, 0xE0E, 0xF50E);
  222. ValueMutex state_mutex;
  223. if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
  224. printf("cannot create mutex\r\n");
  225. return 255;
  226. }
  227. ViewPort* view_port = view_port_alloc();
  228. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  229. view_port_input_callback_set(view_port, input_callback, event_queue);
  230. // Open GUI and register view_port
  231. Gui* gui = furi_record_open("gui");
  232. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  233. // setup irda rx timer
  234. tim_irda_rx_init();
  235. // add timer capture interrupt
  236. api_interrupt_add(irda_timer_capture_callback, InterruptTypeTimerCapture, event_queue);
  237. IrDADecoder* decoder = alloc_decoder();
  238. AppEvent event;
  239. while(1) {
  240. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, 500);
  241. if(event_status == osOK) {
  242. if(event.type == EventTypeKey) {
  243. State* state = (State*)acquire_mutex_block(&state_mutex);
  244. // press events
  245. if(event.value.input.type == InputTypeShort &&
  246. event.value.input.key == InputKeyBack) {
  247. api_interrupt_remove(irda_timer_capture_callback, InterruptTypeTimerCapture);
  248. release_mutex(&state_mutex, state);
  249. // remove all view_ports create by app
  250. gui_remove_view_port(gui, view_port);
  251. view_port_free(view_port);
  252. // free decoder
  253. free_decoder(decoder);
  254. delete_mutex(&state_mutex);
  255. osMessageQueueDelete(event_queue);
  256. // exit
  257. return 0;
  258. }
  259. if(event.value.input.type == InputTypeShort &&
  260. event.value.input.key == InputKeyLeft) {
  261. if(state->mode_id > 0) {
  262. state->mode_id--;
  263. }
  264. }
  265. if(event.value.input.type == InputTypeShort &&
  266. event.value.input.key == InputKeyRight) {
  267. if(state->mode_id < (mode_count - 1)) {
  268. state->mode_id++;
  269. }
  270. }
  271. modes[state->mode_id].input(&event, state);
  272. release_mutex(&state_mutex, state);
  273. view_port_update(view_port);
  274. } else if(event.type == EventTypeRX) {
  275. IrDADecoderOutputData out;
  276. const uint8_t out_data_length = 4;
  277. uint8_t out_data[out_data_length];
  278. out.data_length = out_data_length;
  279. out.data = out_data;
  280. api_hal_light_set(LightRed, event.value.rx.edge ? 0x00 : 0xFF);
  281. bool decoded =
  282. process_decoder(decoder, event.value.rx.edge, &event.value.rx.lasted, 1, &out);
  283. if(decoded) {
  284. // save only if we in packet mode
  285. State* state = (State*)acquire_mutex_block(&state_mutex);
  286. if(state->mode_id == 1) {
  287. if(out.protocol == IRDA_NEC) {
  288. printf("P=NEC ");
  289. printf("A=0x%02X%02X ", out_data[1], out_data[0]);
  290. printf("C=0x%02X ", out_data[2]);
  291. if(out.flags & IRDA_REPEAT) {
  292. printf("R");
  293. }
  294. printf("\r\n");
  295. state->packets[state->packet_id].protocol = IRDA_NEC;
  296. state->packets[state->packet_id].address = out_data[1] << 8 |
  297. out_data[0];
  298. state->packets[state->packet_id].command = out_data[2];
  299. } else {
  300. printf("Unknown protocol\r\n");
  301. }
  302. }
  303. release_mutex(&state_mutex, state);
  304. view_port_update(view_port);
  305. // blink anyway
  306. api_hal_light_set(LightGreen, 0xFF);
  307. api_hal_light_set(LightGreen, 0x00);
  308. }
  309. }
  310. } else {
  311. // event timeout
  312. }
  313. }
  314. }