ibutton_scene_read.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "ibutton_scene_read.h"
  2. #include "../ibutton_app.h"
  3. #include <dolphin/dolphin.h>
  4. static void read_callback(void* context) {
  5. furi_assert(context);
  6. iButtonApp* app = static_cast<iButtonApp*>(context);
  7. iButtonEvent event = {.type = iButtonEvent::Type::EventTypeWorkerRead};
  8. app->get_view_manager()->send_event(&event);
  9. }
  10. void iButtonSceneRead::on_enter(iButtonApp* app) {
  11. iButtonAppViewManager* view_manager = app->get_view_manager();
  12. Popup* popup = view_manager->get_popup();
  13. iButtonKey* key = app->get_key();
  14. iButtonWorker* worker = app->get_key_worker();
  15. DOLPHIN_DEED(DolphinDeedIbuttonRead);
  16. popup_set_header(popup, "iButton", 95, 26, AlignCenter, AlignBottom);
  17. popup_set_text(popup, "waiting\nfor key ...", 95, 30, AlignCenter, AlignTop);
  18. popup_set_icon(popup, 0, 5, &I_DolphinWait_61x59);
  19. view_manager->switch_to(iButtonAppViewManager::Type::iButtonAppViewPopup);
  20. ibutton_key_set_name(key, "");
  21. ibutton_worker_read_set_callback(worker, read_callback, app);
  22. ibutton_worker_read_start(worker, key);
  23. }
  24. bool iButtonSceneRead::on_event(iButtonApp* app, iButtonEvent* event) {
  25. bool consumed = false;
  26. if(event->type == iButtonEvent::Type::EventTypeWorkerRead) {
  27. consumed = true;
  28. iButtonKey* key = app->get_key();
  29. bool success = false;
  30. if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
  31. if(!ibutton_key_dallas_crc_is_valid(key)) {
  32. app->switch_to_next_scene(iButtonApp::Scene::SceneReadCRCError);
  33. } else if(!ibutton_key_dallas_is_1990_key(key)) {
  34. app->switch_to_next_scene(iButtonApp::Scene::SceneReadNotKeyError);
  35. } else {
  36. success = true;
  37. }
  38. } else {
  39. success = true;
  40. }
  41. if(success) {
  42. app->notify_success();
  43. app->notify_green_on();
  44. DOLPHIN_DEED(DolphinDeedIbuttonReadSuccess);
  45. app->switch_to_next_scene(iButtonApp::Scene::SceneReadSuccess);
  46. }
  47. } else if(event->type == iButtonEvent::Type::EventTypeTick) {
  48. consumed = true;
  49. app->notify_red_blink();
  50. }
  51. return consumed;
  52. }
  53. void iButtonSceneRead::on_exit(iButtonApp* app) {
  54. Popup* popup = app->get_view_manager()->get_popup();
  55. ibutton_worker_stop(app->get_key_worker());
  56. popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
  57. popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
  58. popup_set_icon(popup, 0, 0, NULL);
  59. }