lf-rfid-app.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. notification = static_cast<NotificationApp*>(furi_record_open("notification"));
  24. }
  25. LfrfidApp::~LfrfidApp() {
  26. for(std::map<Scene, LfrfidScene*>::iterator it = scenes.begin(); it != scenes.end(); ++it) {
  27. delete it->second;
  28. scenes.erase(it);
  29. }
  30. furi_record_close("notification");
  31. api_hal_power_insomnia_exit();
  32. }
  33. LfrfidAppViewManager* LfrfidApp::get_view_manager() {
  34. return &view;
  35. }
  36. void LfrfidApp::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 LfrfidApp::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 LfrfidApp::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. LfrfidApp::Scene LfrfidApp::get_previous_scene() {
  76. Scene scene = previous_scenes_list.front();
  77. previous_scenes_list.pop_front();
  78. return scene;
  79. }
  80. /***************************** NOTIFY *******************************/
  81. void LfrfidApp::notify_green_blink() {
  82. notification_message(notification, &sequence_blink_green_10);
  83. }
  84. void LfrfidApp::notify_success() {
  85. notification_message(notification, &sequence_success);
  86. }
  87. /*************************** TEXT STORE *****************************/
  88. char* LfrfidApp::get_text_store() {
  89. return text_store;
  90. }
  91. uint8_t LfrfidApp::get_text_store_size() {
  92. return text_store_size;
  93. }
  94. void LfrfidApp::set_text_store(const char* text...) {
  95. va_list args;
  96. va_start(args, text);
  97. vsnprintf(text_store, text_store_size, text, args);
  98. va_end(args);
  99. }
  100. RfidReader* LfrfidApp::get_reader() {
  101. return &reader;
  102. }
  103. RfidTimerEmulator* LfrfidApp::get_emulator() {
  104. return &emulator;
  105. }
  106. RfidWriter* LfrfidApp::get_writer() {
  107. return &writer;
  108. }