flipper_rfidbeacon.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // CC0 1.0 Universal (CC0 1.0)
  2. // Public Domain Dedication
  3. // https://github.com/nmrr
  4. #include <stdio.h>
  5. #include <furi.h>
  6. #include <gui/gui.h>
  7. #include <input/input.h>
  8. #include <notification/notification_messages.h>
  9. #include <furi_hal_rfid.h>
  10. typedef enum {
  11. EventTypeInput,
  12. ClockEventTypeTick,
  13. } EventType;
  14. typedef struct {
  15. EventType type;
  16. InputEvent input;
  17. } EventApp;
  18. const char CW_char[36] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  19. const uint8_t CW_size[36] = {2, 4, 4, 3, 1, 4, 3, 4, 2, 4, 3, 4, 2, 2, 3, 4, 4, 3, 3, 1, 3, 4, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
  20. const uint8_t CW_value[36] = {0b01000000, 0b10000000, 0b10100000, 0b10000000, 0b00000000, 0b00100000, 0b11000000, 0b00000000, 0b00000000, 0b01110000, 0b10100000, 0b01000000, 0b11000000, 0b10000000, 0b11100000, 0b01100000, 0b11010000, 0b01000000, 0b00000000, 0b10000000, 0b00100000, 0b00010000, 0b01100000, 0b10010000, 0b10110000, 0b11000000, 0b11111000, 0b01111000, 0b00111000, 0b00011000, 0b00001000, 0b00000000, 0b10000000, 0b11000000, 0b11100000, 0b11110000};
  21. typedef struct {
  22. FuriMutex* mutex;
  23. uint8_t status;
  24. uint8_t enableCW_mutex;
  25. } mutexStruct;
  26. static void draw_callback(Canvas* canvas, void* ctx)
  27. {
  28. furi_assert(ctx);
  29. mutexStruct* mutexVal = ctx;
  30. mutexStruct mutexDraw;
  31. furi_mutex_acquire(mutexVal->mutex, FuriWaitForever);
  32. memcpy(&mutexDraw, mutexVal, sizeof(mutexStruct));
  33. furi_mutex_release(mutexVal->mutex);
  34. canvas_set_font(canvas, FontPrimary);
  35. canvas_draw_str_aligned(canvas, 64, 5, AlignCenter, AlignCenter, "RFID Beacon");
  36. char buffer[8];
  37. snprintf(buffer, sizeof(buffer), "%c", CW_char[mutexVal->status]);
  38. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, buffer);
  39. if (mutexVal->enableCW_mutex == 0) canvas_draw_str_aligned(canvas, 64, 59, AlignCenter, AlignCenter, "OFF");
  40. else canvas_draw_str_aligned(canvas, 64, 59, AlignCenter, AlignCenter, "On the air");
  41. }
  42. static void input_callback(InputEvent* input_event, void* ctx)
  43. {
  44. furi_assert(ctx);
  45. FuriMessageQueue* event_queue = ctx;
  46. EventApp event = {.type = EventTypeInput, .input = *input_event};
  47. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  48. }
  49. static void clock_tick(void* ctx) {
  50. furi_assert(ctx);
  51. FuriMessageQueue* queue = ctx;
  52. EventApp event = {.type = ClockEventTypeTick};
  53. furi_message_queue_put(queue, &event, 0);
  54. }
  55. static uint8_t RFID_STATUS = 0;
  56. void RFID_ON(NotificationApp* notification)
  57. {
  58. if (RFID_STATUS == 0)
  59. {
  60. notification_message(notification, &sequence_set_only_red_255);
  61. furi_hal_rfid_tim_read_start(125000, 0.5);
  62. RFID_STATUS = 1;
  63. }
  64. }
  65. void RFID_OFF(NotificationApp* notification)
  66. {
  67. if (RFID_STATUS == 1)
  68. {
  69. notification_message(notification, &sequence_reset_red);
  70. furi_hal_rfid_tim_read_stop();
  71. RFID_STATUS = 0;
  72. }
  73. }
  74. int32_t flipper_rfidbeacon_app()
  75. {
  76. EventApp event;
  77. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp));
  78. mutexStruct mutexVal;
  79. mutexVal.status = 0;
  80. mutexVal.enableCW_mutex = 0;
  81. mutexVal.mutex= furi_mutex_alloc(FuriMutexTypeNormal);
  82. if(!mutexVal.mutex)
  83. {
  84. furi_message_queue_free(event_queue);
  85. return 255;
  86. }
  87. ViewPort* view_port = view_port_alloc();
  88. view_port_draw_callback_set(view_port, draw_callback, &mutexVal.mutex);
  89. view_port_input_callback_set(view_port, input_callback, event_queue);
  90. Gui* gui = furi_record_open(RECORD_GUI);
  91. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  92. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  93. FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
  94. furi_timer_start(timer, 100);
  95. uint8_t enableCW = 0;
  96. uint8_t letterState = 0;
  97. uint8_t letterPosition = 0;
  98. uint8_t letterChosen = 0;
  99. // 1 : pause, 2 : dot, 3 : dash
  100. uint8_t draw = 0;
  101. while(1)
  102. {
  103. furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
  104. uint8_t screenRefresh = 0;
  105. if(event.type == EventTypeInput)
  106. {
  107. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong)
  108. {
  109. break;
  110. }
  111. else if(event.input.key == InputKeyUp && event.input.type == InputTypeLong)
  112. {
  113. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  114. if (enableCW == 0)
  115. {
  116. enableCW = 1;
  117. letterPosition = 0;
  118. draw = 0;
  119. }
  120. else enableCW = 0;
  121. mutexVal.enableCW_mutex = enableCW;
  122. screenRefresh = 1;
  123. furi_mutex_release(mutexVal.mutex);
  124. }
  125. else if(event.input.key == InputKeyLeft && event.input.type == InputTypeShort)
  126. {
  127. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  128. if (mutexVal.status != 0) mutexVal.status--;
  129. else mutexVal.status = 35;
  130. screenRefresh = 1;
  131. furi_mutex_release(mutexVal.mutex);
  132. }
  133. else if(event.input.key == InputKeyRight && event.input.type == InputTypeShort)
  134. {
  135. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  136. if (mutexVal.status != 35) mutexVal.status++;
  137. else mutexVal.status = 0;
  138. screenRefresh = 1;
  139. furi_mutex_release(mutexVal.mutex);
  140. }
  141. }
  142. else if (event.type == ClockEventTypeTick)
  143. {
  144. if (enableCW == 1)
  145. {
  146. if (draw == 0)
  147. {
  148. if (letterPosition == 0)
  149. {
  150. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  151. letterChosen = mutexVal.status;
  152. furi_mutex_release(mutexVal.mutex);
  153. }
  154. if (letterPosition == CW_size[letterChosen])
  155. {
  156. draw = 1;
  157. letterPosition = 0;
  158. }
  159. else
  160. {
  161. uint8_t mask = 0b10000000;
  162. mask >>= letterPosition;
  163. if ((CW_value[letterChosen] & mask) != 0) draw = 3;
  164. else draw = 2;
  165. letterState = 0;
  166. letterPosition++;
  167. }
  168. }
  169. // PAUSE
  170. if (draw == 1)
  171. {
  172. letterState++;
  173. if (letterState == 30)
  174. {
  175. letterState = 0;
  176. draw = 0;
  177. }
  178. }
  179. // DOT
  180. else if (draw == 2)
  181. {
  182. if (letterState == 0)
  183. {
  184. RFID_ON(notification);
  185. letterState = 1;
  186. }
  187. else
  188. {
  189. RFID_OFF(notification);
  190. draw = 0;
  191. letterState = 0;
  192. }
  193. }
  194. // DASH
  195. else if (draw == 3)
  196. {
  197. if (letterState == 0)
  198. {
  199. RFID_ON(notification);
  200. letterState = 1;
  201. }
  202. else
  203. {
  204. if (letterState != 3) letterState++;
  205. else
  206. {
  207. RFID_OFF(notification);
  208. draw = 0;
  209. letterState = 0;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. if (screenRefresh == 1) view_port_update(view_port);
  216. }
  217. RFID_OFF(notification);
  218. furi_timer_free(timer);
  219. furi_message_queue_free(event_queue);
  220. furi_record_close(RECORD_NOTIFICATION);
  221. gui_remove_view_port(gui, view_port);
  222. view_port_free(view_port);
  223. furi_record_close(RECORD_GUI);
  224. return 0;
  225. }