lf-rfid-app.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "lf-rfid-app.h"
  2. #include <furi.h>
  3. #include <api-hal.h>
  4. #include <stdarg.h>
  5. void LfrfidApp::run(void) {
  6. LfrfidEvent event;
  7. bool consumed;
  8. bool exit = false;
  9. scenes[current_scene]->on_enter(this);
  10. while(!exit) {
  11. view.receive_event(&event);
  12. consumed = scenes[current_scene]->on_event(this, &event);
  13. if(!consumed) {
  14. if(event.type == LfrfidEvent::Type::Back) {
  15. exit = switch_to_previous_scene();
  16. }
  17. }
  18. };
  19. scenes[current_scene]->on_exit(this);
  20. }
  21. LfrfidApp::LfrfidApp() {
  22. api_hal_power_insomnia_enter();
  23. }
  24. LfrfidApp::~LfrfidApp() {
  25. for(std::map<Scene, LfrfidScene*>::iterator it = scenes.begin(); it != scenes.end(); ++it) {
  26. delete it->second;
  27. scenes.erase(it);
  28. }
  29. api_hal_power_insomnia_exit();
  30. }
  31. LfrfidAppViewManager* LfrfidApp::get_view_manager() {
  32. return &view;
  33. }
  34. void LfrfidApp::switch_to_next_scene(Scene next_scene) {
  35. previous_scenes_list.push_front(current_scene);
  36. if(next_scene != Scene::Exit) {
  37. scenes[current_scene]->on_exit(this);
  38. current_scene = next_scene;
  39. scenes[current_scene]->on_enter(this);
  40. }
  41. }
  42. void LfrfidApp::search_and_switch_to_previous_scene(std::initializer_list<Scene> scenes_list) {
  43. Scene previous_scene = Scene::Start;
  44. bool scene_found = false;
  45. while(!scene_found) {
  46. previous_scene = get_previous_scene();
  47. for(Scene element : scenes_list) {
  48. if(previous_scene == element || previous_scene == Scene::Start) {
  49. scene_found = true;
  50. break;
  51. }
  52. }
  53. }
  54. scenes[current_scene]->on_exit(this);
  55. current_scene = previous_scene;
  56. scenes[current_scene]->on_enter(this);
  57. }
  58. bool LfrfidApp::switch_to_previous_scene(uint8_t count) {
  59. Scene previous_scene = Scene::Start;
  60. for(uint8_t i = 0; i < count; i++) {
  61. previous_scene = get_previous_scene();
  62. if(previous_scene == Scene::Exit) break;
  63. }
  64. if(previous_scene == Scene::Exit) {
  65. return true;
  66. } else {
  67. scenes[current_scene]->on_exit(this);
  68. current_scene = previous_scene;
  69. scenes[current_scene]->on_enter(this);
  70. return false;
  71. }
  72. }
  73. LfrfidApp::Scene LfrfidApp::get_previous_scene() {
  74. Scene scene = previous_scenes_list.front();
  75. previous_scenes_list.pop_front();
  76. return scene;
  77. }
  78. /***************************** NOTIFY *******************************/
  79. void LfrfidApp::notify_init() {
  80. // TODO open record
  81. const GpioPin* vibro_record = &vibro_gpio;
  82. hal_gpio_init(vibro_record, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  83. hal_gpio_write(vibro_record, false);
  84. }
  85. void LfrfidApp::notify_green_blink() {
  86. api_hal_light_set(LightGreen, 0xFF);
  87. delay(10);
  88. api_hal_light_set(LightGreen, 0x00);
  89. }
  90. void LfrfidApp::notify_green_on() {
  91. api_hal_light_set(LightGreen, 0xFF);
  92. }
  93. void LfrfidApp::notify_green_off() {
  94. api_hal_light_set(LightGreen, 0x00);
  95. }
  96. /*************************** TEXT STORE *****************************/
  97. char* LfrfidApp::get_text_store() {
  98. return text_store;
  99. }
  100. uint8_t LfrfidApp::get_text_store_size() {
  101. return text_store_size;
  102. }
  103. void LfrfidApp::set_text_store(const char* text...) {
  104. va_list args;
  105. va_start(args, text);
  106. vsnprintf(text_store, text_store_size, text, args);
  107. va_end(args);
  108. }
  109. RfidReader* LfrfidApp::get_reader() {
  110. return &reader;
  111. }
  112. RfidTimerEmulator* LfrfidApp::get_emulator() {
  113. return &emulator;
  114. }