ibutton_scene_read.cpp 2.5 KB

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