wiegand.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "wiegand.h"
  2. #include <expansion/expansion.h>
  3. const GpioPin* const pinD0 = &gpio_ext_pa4;
  4. const GpioPin* const pinD0mosfet = &gpio_ext_pb3;
  5. const GpioPin* const pinD1 = &gpio_ext_pa7;
  6. const GpioPin* const pinD1mosfet = &gpio_ext_pa6;
  7. volatile int bit_count = 0;
  8. volatile bool data[MAX_BITS];
  9. volatile uint32_t data_fall[MAX_BITS];
  10. volatile uint32_t data_rise[MAX_BITS];
  11. bool data_saved = false;
  12. bool wiegand_empty_scene_on_event(void* _ctx, SceneManagerEvent _evt) {
  13. UNUSED(_ctx);
  14. UNUSED(_evt);
  15. return false;
  16. }
  17. void wiegand_empty_scene_on_exit(void* _ctx) {
  18. UNUSED(_ctx);
  19. }
  20. void (*const basic_scenes_scene_on_enter_handlers[])(void*) = {
  21. wiegand_main_menu_scene_on_enter,
  22. wiegand_instructions_scene_on_enter,
  23. wiegand_read_scene_on_enter,
  24. wiegand_scan_scene_on_enter,
  25. wiegand_data_scene_on_enter,
  26. wiegand_save_scene_on_enter,
  27. wiegand_load_scene_on_enter,
  28. };
  29. bool (*const basic_scenes_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
  30. wiegand_main_menu_scene_on_event,
  31. wiegand_empty_scene_on_event, // instructions
  32. wiegand_empty_scene_on_event, // read
  33. wiegand_empty_scene_on_event, // scan
  34. wiegand_data_scene_on_event,
  35. wiegand_save_scene_on_event,
  36. wiegand_empty_scene_on_event, // load
  37. };
  38. void (*const basic_scenes_scene_on_exit_handlers[])(void*) = {
  39. wiegand_empty_scene_on_exit, // main_menu
  40. wiegand_empty_scene_on_exit, // instructions
  41. wiegand_read_scene_on_exit,
  42. wiegand_scan_scene_on_exit,
  43. wiegand_empty_scene_on_exit, // data
  44. wiegand_empty_scene_on_exit, // save
  45. wiegand_empty_scene_on_exit, // load
  46. };
  47. const SceneManagerHandlers basic_scenes_scene_manager_handlers = {
  48. .on_enter_handlers = basic_scenes_scene_on_enter_handlers,
  49. .on_event_handlers = basic_scenes_scene_on_event_handlers,
  50. .on_exit_handlers = basic_scenes_scene_on_exit_handlers,
  51. .scene_num = WiegandSceneCount,
  52. };
  53. bool wiegand_custom_callback(void* context, uint32_t custom_event) {
  54. furi_assert(context);
  55. App* app = context;
  56. return scene_manager_handle_custom_event(app->scene_manager, custom_event);
  57. }
  58. bool wiegand_back_event_callback(void* context) {
  59. furi_assert(context);
  60. App* app = context;
  61. return scene_manager_handle_back_event(app->scene_manager);
  62. }
  63. App* app_alloc() {
  64. App* app = malloc(sizeof(App));
  65. app->scene_manager = scene_manager_alloc(&basic_scenes_scene_manager_handlers, app);
  66. app->view_dispatcher = view_dispatcher_alloc();
  67. view_dispatcher_enable_queue(app->view_dispatcher);
  68. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  69. view_dispatcher_set_custom_event_callback(app->view_dispatcher, wiegand_custom_callback);
  70. view_dispatcher_set_navigation_event_callback(
  71. app->view_dispatcher, wiegand_back_event_callback);
  72. app->submenu = submenu_alloc();
  73. view_dispatcher_add_view(
  74. app->view_dispatcher, WiegandSubmenuView, submenu_get_view(app->submenu));
  75. app->widget = widget_alloc();
  76. view_dispatcher_add_view(
  77. app->view_dispatcher, WiegandWidgetView, widget_get_view(app->widget));
  78. app->text_input = text_input_alloc();
  79. view_dispatcher_add_view(
  80. app->view_dispatcher, WiegandTextInputView, text_input_get_view(app->text_input));
  81. app->dialogs = furi_record_open(RECORD_DIALOGS);
  82. app->file_path = furi_string_alloc();
  83. app->timer = furi_timer_alloc(wiegand_timer_callback, FuriTimerTypePeriodic, app);
  84. return app;
  85. }
  86. void app_free(void* context) {
  87. App* app = context;
  88. furi_assert(app);
  89. view_dispatcher_remove_view(app->view_dispatcher, WiegandTextInputView);
  90. text_input_free(app->text_input);
  91. view_dispatcher_remove_view(app->view_dispatcher, WiegandWidgetView);
  92. widget_free(app->widget);
  93. view_dispatcher_remove_view(app->view_dispatcher, WiegandSubmenuView);
  94. submenu_free(app->submenu);
  95. scene_manager_free(app->scene_manager);
  96. view_dispatcher_free(app->view_dispatcher);
  97. furi_timer_free(app->timer);
  98. furi_record_close(RECORD_DIALOGS);
  99. free(app);
  100. }
  101. int wiegand_app(void* p) {
  102. UNUSED(p);
  103. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  104. expansion_disable(expansion);
  105. App* app = app_alloc();
  106. Gui* gui = furi_record_open(RECORD_GUI);
  107. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  108. scene_manager_next_scene(app->scene_manager, WiegandMainMenuScene);
  109. view_dispatcher_run(app->view_dispatcher);
  110. app_free(app);
  111. expansion_enable(expansion);
  112. furi_record_close(RECORD_EXPANSION);
  113. return 0;
  114. }