accessor-app.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "accessor-app.h"
  2. #include <furi.h>
  3. #include <api-hal.h>
  4. #include <stdarg.h>
  5. void AccessorApp::run(void) {
  6. AccessorEvent event;
  7. bool consumed;
  8. bool exit = false;
  9. notify_init();
  10. wiegand.begin();
  11. onewire_master.start();
  12. scenes[current_scene]->on_enter(this);
  13. while(!exit) {
  14. view.receive_event(&event);
  15. consumed = scenes[current_scene]->on_event(this, &event);
  16. if(!consumed) {
  17. if(event.type == AccessorEvent::Type::Back) {
  18. exit = switch_to_previous_scene();
  19. }
  20. }
  21. };
  22. scenes[current_scene]->on_exit(this);
  23. wiegand.end();
  24. onewire_master.stop();
  25. }
  26. AccessorApp::AccessorApp()
  27. : onewire_master{&ibutton_gpio} {
  28. api_hal_power_insomnia_enter();
  29. }
  30. AccessorApp::~AccessorApp() {
  31. api_hal_power_insomnia_exit();
  32. }
  33. AccessorAppViewManager* AccessorApp::get_view_manager() {
  34. return &view;
  35. }
  36. void AccessorApp::switch_to_next_scene(Scene next_scene) {
  37. previous_scenes_list.push_front(current_scene);
  38. if(next_scene != Scene::Exit) {
  39. scenes[current_scene]->on_exit(this);
  40. current_scene = next_scene;
  41. scenes[current_scene]->on_enter(this);
  42. }
  43. }
  44. void AccessorApp::search_and_switch_to_previous_scene(std::initializer_list<Scene> scenes_list) {
  45. Scene previous_scene = Scene::Start;
  46. bool scene_found = false;
  47. while(!scene_found) {
  48. previous_scene = get_previous_scene();
  49. for(Scene element : scenes_list) {
  50. if(previous_scene == element || previous_scene == Scene::Start) {
  51. scene_found = true;
  52. break;
  53. }
  54. }
  55. }
  56. scenes[current_scene]->on_exit(this);
  57. current_scene = previous_scene;
  58. scenes[current_scene]->on_enter(this);
  59. }
  60. bool AccessorApp::switch_to_previous_scene(uint8_t count) {
  61. Scene previous_scene = Scene::Start;
  62. for(uint8_t i = 0; i < count; i++) {
  63. previous_scene = get_previous_scene();
  64. if(previous_scene == Scene::Exit) break;
  65. }
  66. if(previous_scene == Scene::Exit) {
  67. return true;
  68. } else {
  69. scenes[current_scene]->on_exit(this);
  70. current_scene = previous_scene;
  71. scenes[current_scene]->on_enter(this);
  72. return false;
  73. }
  74. }
  75. AccessorApp::Scene AccessorApp::get_previous_scene() {
  76. Scene scene = previous_scenes_list.front();
  77. previous_scenes_list.pop_front();
  78. return scene;
  79. }
  80. /***************************** NOTIFY *******************************/
  81. void AccessorApp::notify_init() {
  82. // TODO open record
  83. const GpioPin* vibro_record = &vibro_gpio;
  84. hal_gpio_init(vibro_record, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  85. hal_gpio_write(vibro_record, false);
  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. api_hal_light_set(LightGreen, 0xFF);
  96. delay(10);
  97. api_hal_light_set(LightGreen, 0x00);
  98. }
  99. void AccessorApp::notify_green_on() {
  100. api_hal_light_set(LightGreen, 0xFF);
  101. }
  102. void AccessorApp::notify_green_off() {
  103. api_hal_light_set(LightGreen, 0x00);
  104. }
  105. void AccessorApp::notify_success() {
  106. api_hal_light_set(LightBacklight, 0xFF);
  107. hal_pwm_set(0.5, 1760 / 2, &htim2, TIM_CHANNEL_2);
  108. notify_green_on();
  109. delay(100);
  110. hal_pwm_stop(&htim2, TIM_CHANNEL_2);
  111. notify_green_off();
  112. delay(100);
  113. hal_pwm_set(0.5, 1760, &htim2, TIM_CHANNEL_2);
  114. notify_green_on();
  115. delay(100);
  116. hal_pwm_stop(&htim2, TIM_CHANNEL_2);
  117. notify_green_off();
  118. }
  119. /*************************** TEXT STORE *****************************/
  120. char* AccessorApp::get_text_store() {
  121. return text_store;
  122. }
  123. uint8_t AccessorApp::get_text_store_size() {
  124. return text_store_size;
  125. }
  126. void AccessorApp::set_text_store(const char* text...) {
  127. va_list args;
  128. va_start(args, text);
  129. vsnprintf(text_store, text_store_size, text, args);
  130. va_end(args);
  131. }
  132. /*************************** APP RESOURCES *****************************/
  133. WIEGAND* AccessorApp::get_wiegand() {
  134. return &wiegand;
  135. }
  136. OneWireMaster* AccessorApp::get_one_wire() {
  137. return &onewire_master;
  138. }