wiegand.c 4.4 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_set_event_callback_context(app->view_dispatcher, app);
  68. view_dispatcher_set_custom_event_callback(app->view_dispatcher, wiegand_custom_callback);
  69. view_dispatcher_set_navigation_event_callback(
  70. app->view_dispatcher, wiegand_back_event_callback);
  71. app->submenu = submenu_alloc();
  72. view_dispatcher_add_view(
  73. app->view_dispatcher, WiegandSubmenuView, submenu_get_view(app->submenu));
  74. app->widget = widget_alloc();
  75. view_dispatcher_add_view(
  76. app->view_dispatcher, WiegandWidgetView, widget_get_view(app->widget));
  77. app->text_input = text_input_alloc();
  78. view_dispatcher_add_view(
  79. app->view_dispatcher, WiegandTextInputView, text_input_get_view(app->text_input));
  80. app->dialogs = furi_record_open(RECORD_DIALOGS);
  81. app->file_path = furi_string_alloc();
  82. app->timer = furi_timer_alloc(wiegand_timer_callback, FuriTimerTypePeriodic, app);
  83. return app;
  84. }
  85. void app_free(void* context) {
  86. App* app = context;
  87. furi_assert(app);
  88. view_dispatcher_remove_view(app->view_dispatcher, WiegandTextInputView);
  89. text_input_free(app->text_input);
  90. view_dispatcher_remove_view(app->view_dispatcher, WiegandWidgetView);
  91. widget_free(app->widget);
  92. view_dispatcher_remove_view(app->view_dispatcher, WiegandSubmenuView);
  93. submenu_free(app->submenu);
  94. scene_manager_free(app->scene_manager);
  95. view_dispatcher_free(app->view_dispatcher);
  96. furi_timer_free(app->timer);
  97. furi_record_close(RECORD_DIALOGS);
  98. free(app);
  99. }
  100. int wiegand_app(void* p) {
  101. UNUSED(p);
  102. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  103. expansion_disable(expansion);
  104. App* app = app_alloc();
  105. Gui* gui = furi_record_open(RECORD_GUI);
  106. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  107. scene_manager_next_scene(app->scene_manager, WiegandMainMenuScene);
  108. view_dispatcher_run(app->view_dispatcher);
  109. app_free(app);
  110. expansion_enable(expansion);
  111. furi_record_close(RECORD_EXPANSION);
  112. return 0;
  113. }