wiegand.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "wiegand.h"
  2. const GpioPin* const pinD0 = &gpio_ext_pa4;
  3. const GpioPin* const pinD0mosfet = &gpio_ext_pb3;
  4. const GpioPin* const pinD1 = &gpio_ext_pa7;
  5. const GpioPin* const pinD1mosfet = &gpio_ext_pa6;
  6. volatile int bit_count = 0;
  7. volatile bool data[MAX_BITS];
  8. volatile uint32_t data_fall[MAX_BITS];
  9. volatile uint32_t data_rise[MAX_BITS];
  10. bool data_saved = false;
  11. bool wiegand_empty_scene_on_event(void* _ctx, SceneManagerEvent _evt) {
  12. UNUSED(_ctx);
  13. UNUSED(_evt);
  14. return false;
  15. }
  16. void wiegand_empty_scene_on_exit(void* _ctx) {
  17. UNUSED(_ctx);
  18. }
  19. void (*const basic_scenes_scene_on_enter_handlers[])(void*) = {
  20. wiegand_main_menu_scene_on_enter,
  21. wiegand_instructions_scene_on_enter,
  22. wiegand_read_scene_on_enter,
  23. wiegand_scan_scene_on_enter,
  24. wiegand_data_scene_on_enter,
  25. wiegand_save_scene_on_enter,
  26. wiegand_load_scene_on_enter,
  27. };
  28. bool (*const basic_scenes_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
  29. wiegand_main_menu_scene_on_event,
  30. wiegand_empty_scene_on_event, // instructions
  31. wiegand_empty_scene_on_event, // read
  32. wiegand_empty_scene_on_event, // scan
  33. wiegand_data_scene_on_event,
  34. wiegand_save_scene_on_event,
  35. wiegand_empty_scene_on_event, // load
  36. };
  37. void (*const basic_scenes_scene_on_exit_handlers[])(void*) = {
  38. wiegand_empty_scene_on_exit, // main_menu
  39. wiegand_empty_scene_on_exit, // instructions
  40. wiegand_read_scene_on_exit,
  41. wiegand_scan_scene_on_exit,
  42. wiegand_empty_scene_on_exit, // data
  43. wiegand_empty_scene_on_exit, // save
  44. wiegand_empty_scene_on_exit, // load
  45. };
  46. const SceneManagerHandlers basic_scenes_scene_manager_handlers = {
  47. .on_enter_handlers = basic_scenes_scene_on_enter_handlers,
  48. .on_event_handlers = basic_scenes_scene_on_event_handlers,
  49. .on_exit_handlers = basic_scenes_scene_on_exit_handlers,
  50. .scene_num = WiegandSceneCount,
  51. };
  52. bool wiegand_custom_callback(void* context, uint32_t custom_event) {
  53. furi_assert(context);
  54. App* app = context;
  55. return scene_manager_handle_custom_event(app->scene_manager, custom_event);
  56. }
  57. bool wiegand_back_event_callback(void* context) {
  58. furi_assert(context);
  59. App* app = context;
  60. return scene_manager_handle_back_event(app->scene_manager);
  61. }
  62. App* app_alloc() {
  63. App* app = malloc(sizeof(App));
  64. app->scene_manager = scene_manager_alloc(&basic_scenes_scene_manager_handlers, app);
  65. app->view_dispatcher = view_dispatcher_alloc();
  66. view_dispatcher_enable_queue(app->view_dispatcher);
  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. App* app = app_alloc();
  103. Gui* gui = furi_record_open(RECORD_GUI);
  104. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  105. scene_manager_next_scene(app->scene_manager, WiegandMainMenuScene);
  106. view_dispatcher_run(app->view_dispatcher);
  107. app_free(app);
  108. return 0;
  109. }