lf-rfid.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "flipper_v2.h"
  2. typedef enum {
  3. EventTypeTick,
  4. EventTypeKey,
  5. } EventType;
  6. typedef struct {
  7. union {
  8. InputEvent input;
  9. } value;
  10. EventType type;
  11. } AppEvent;
  12. typedef struct {
  13. uint32_t freq_khz;
  14. bool on;
  15. } State;
  16. static void render_callback(CanvasApi* canvas, void* ctx) {
  17. State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
  18. canvas->clear(canvas);
  19. canvas->set_color(canvas, ColorBlack);
  20. canvas->set_font(canvas, FontPrimary);
  21. canvas->draw_str(canvas, 2, 12, "LF RFID");
  22. canvas->draw_str(canvas, 2, 24, state->on ? "ON" : "OFF");
  23. char buf[12];
  24. sprintf(buf, "%d kHz", (int)state->freq_khz);
  25. canvas->draw_str(canvas, 2, 36, buf);
  26. release_mutex((ValueMutex*)ctx, state);
  27. }
  28. static void input_callback(InputEvent* input_event, void* ctx) {
  29. osMessageQueueId_t event_queue = (QueueHandle_t)ctx;
  30. AppEvent event;
  31. event.type = EventTypeKey;
  32. event.value.input = *input_event;
  33. osMessageQueuePut(event_queue, &event, 0, 0);
  34. }
  35. extern TIM_HandleTypeDef TIM_C;
  36. void em4100_emulation(uint8_t* data, GpioPin* pin);
  37. void prepare_data(uint32_t ID, uint32_t VENDOR, uint8_t* data);
  38. void lf_rfid_workaround(void* p) {
  39. osMessageQueueId_t event_queue = osMessageQueueNew(1, sizeof(AppEvent), NULL);
  40. // create pin
  41. GpioPin pull_pin = {.pin = RFID_PULL_Pin, .port = RFID_PULL_GPIO_Port};
  42. // TODO open record
  43. GpioPin* pull_pin_record = &pull_pin;
  44. gpio_init(pull_pin_record, GpioModeOutputPushPull);
  45. uint8_t emulation_data[64];
  46. prepare_data(4378151, 01, emulation_data);
  47. State _state;
  48. _state.freq_khz = 125;
  49. _state.on = false;
  50. ValueMutex state_mutex;
  51. if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
  52. printf("cannot create mutex\n");
  53. furiac_exit(NULL);
  54. }
  55. Widget* widget = widget_alloc();
  56. widget_draw_callback_set(widget, render_callback, &state_mutex);
  57. widget_input_callback_set(widget, input_callback, event_queue);
  58. // Open GUI and register widget
  59. GuiApi* gui = (GuiApi*)furi_open("gui");
  60. if(gui == NULL) {
  61. printf("gui is not available\n");
  62. furiac_exit(NULL);
  63. }
  64. gui->add_widget(gui, widget, GuiLayerFullscreen);
  65. AppEvent event;
  66. while(1) {
  67. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, 100);
  68. State* state = (State*)acquire_mutex_block(&state_mutex);
  69. if(event_status == osOK) {
  70. if(event.type == EventTypeKey) {
  71. // press events
  72. if(event.value.input.state && event.value.input.input == InputBack) {
  73. hal_pwmn_stop(&TIM_C, TIM_CHANNEL_1); // TODO: move to furiac_onexit
  74. gpio_init(pull_pin_record, GpioModeInput);
  75. // TODO remove all widgets create by app
  76. widget_enabled_set(widget, false);
  77. furiac_exit(NULL);
  78. }
  79. if(event.value.input.state && event.value.input.input == InputUp) {
  80. state->freq_khz += 10;
  81. }
  82. if(event.value.input.state && event.value.input.input == InputDown) {
  83. state->freq_khz -= 10;
  84. }
  85. if(event.value.input.state && event.value.input.input == InputLeft) {
  86. }
  87. if(event.value.input.state && event.value.input.input == InputRight) {
  88. }
  89. if(event.value.input.state && event.value.input.input == InputOk) {
  90. state->on = !state->on;
  91. }
  92. }
  93. } else {
  94. // event timeout
  95. }
  96. hal_pwmn_set(
  97. state->on ? 0.5 : 0.0, (float)(state->freq_khz * 1000), &LFRFID_TIM, LFRFID_CH);
  98. if(!state->on) {
  99. em4100_emulation(emulation_data, pull_pin_record);
  100. } else {
  101. gpio_write(pull_pin_record, false);
  102. }
  103. // common code, for example, force update UI
  104. widget_update(widget);
  105. release_mutex(&state_mutex, state);
  106. }
  107. }