accessor-app.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "accessor-app.h"
  2. #include <furi.h>
  3. #include <furi-hal.h>
  4. #include <stdarg.h>
  5. void AccessorApp::run(void) {
  6. AccessorEvent event;
  7. bool consumed;
  8. bool exit = false;
  9. wiegand.begin();
  10. onewire_master.start();
  11. scenes[current_scene]->on_enter(this);
  12. while(!exit) {
  13. view.receive_event(&event);
  14. consumed = scenes[current_scene]->on_event(this, &event);
  15. if(!consumed) {
  16. if(event.type == AccessorEvent::Type::Back) {
  17. exit = switch_to_previous_scene();
  18. }
  19. }
  20. };
  21. scenes[current_scene]->on_exit(this);
  22. wiegand.end();
  23. onewire_master.stop();
  24. }
  25. AccessorApp::AccessorApp()
  26. : onewire_master{&ibutton_gpio} {
  27. furi_hal_power_insomnia_enter();
  28. notification = static_cast<NotificationApp*>(furi_record_open("notification"));
  29. notify_init();
  30. furi_hal_power_enable_otg();
  31. }
  32. AccessorApp::~AccessorApp() {
  33. furi_hal_power_disable_otg();
  34. furi_record_close("notification");
  35. furi_hal_power_insomnia_exit();
  36. }
  37. AccessorAppViewManager* AccessorApp::get_view_manager() {
  38. return &view;
  39. }
  40. void AccessorApp::switch_to_next_scene(Scene next_scene) {
  41. previous_scenes_list.push_front(current_scene);
  42. if(next_scene != Scene::Exit) {
  43. scenes[current_scene]->on_exit(this);
  44. current_scene = next_scene;
  45. scenes[current_scene]->on_enter(this);
  46. }
  47. }
  48. void AccessorApp::search_and_switch_to_previous_scene(std::initializer_list<Scene> scenes_list) {
  49. Scene previous_scene = Scene::Start;
  50. bool scene_found = false;
  51. while(!scene_found) {
  52. previous_scene = get_previous_scene();
  53. for(Scene element : scenes_list) {
  54. if(previous_scene == element || previous_scene == Scene::Start) {
  55. scene_found = true;
  56. break;
  57. }
  58. }
  59. }
  60. scenes[current_scene]->on_exit(this);
  61. current_scene = previous_scene;
  62. scenes[current_scene]->on_enter(this);
  63. }
  64. bool AccessorApp::switch_to_previous_scene(uint8_t count) {
  65. Scene previous_scene = Scene::Start;
  66. for(uint8_t i = 0; i < count; i++) {
  67. previous_scene = get_previous_scene();
  68. if(previous_scene == Scene::Exit) break;
  69. }
  70. if(previous_scene == Scene::Exit) {
  71. return true;
  72. } else {
  73. scenes[current_scene]->on_exit(this);
  74. current_scene = previous_scene;
  75. scenes[current_scene]->on_enter(this);
  76. return false;
  77. }
  78. }
  79. AccessorApp::Scene AccessorApp::get_previous_scene() {
  80. Scene scene = previous_scenes_list.front();
  81. previous_scenes_list.pop_front();
  82. return scene;
  83. }
  84. /***************************** NOTIFY *******************************/
  85. void AccessorApp::notify_init() {
  86. GPIO_InitTypeDef GPIO_InitStruct = {0};
  87. GPIO_InitStruct.Pin = PB3_Pin;
  88. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  89. GPIO_InitStruct.Pull = GPIO_NOPULL;
  90. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  91. GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
  92. HAL_GPIO_Init(PB3_GPIO_Port, &GPIO_InitStruct);
  93. }
  94. void AccessorApp::notify_green_blink() {
  95. notification_message(notification, &sequence_blink_green_10);
  96. }
  97. void AccessorApp::notify_success() {
  98. notification_message(notification, &sequence_success);
  99. hal_pwm_set(0.5, 1760 / 2, &htim2, TIM_CHANNEL_2);
  100. delay(100);
  101. hal_pwm_stop(&htim2, TIM_CHANNEL_2);
  102. delay(100);
  103. hal_pwm_set(0.5, 1760, &htim2, TIM_CHANNEL_2);
  104. delay(100);
  105. hal_pwm_stop(&htim2, TIM_CHANNEL_2);
  106. }
  107. /*************************** TEXT STORE *****************************/
  108. char* AccessorApp::get_text_store() {
  109. return text_store;
  110. }
  111. uint8_t AccessorApp::get_text_store_size() {
  112. return text_store_size;
  113. }
  114. void AccessorApp::set_text_store(const char* text...) {
  115. va_list args;
  116. va_start(args, text);
  117. vsnprintf(text_store, text_store_size, text, args);
  118. va_end(args);
  119. }
  120. /*************************** APP RESOURCES *****************************/
  121. WIEGAND* AccessorApp::get_wiegand() {
  122. return &wiegand;
  123. }
  124. OneWireMaster* AccessorApp::get_one_wire() {
  125. return &onewire_master;
  126. }