ibutton_scene_read.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
  30. if(!ibutton_key_dallas_crc_is_valid(key)) {
  31. app->switch_to_next_scene(iButtonApp::Scene::SceneReadCRCError);
  32. } else if(!ibutton_key_dallas_is_1990_key(key)) {
  33. app->switch_to_next_scene(iButtonApp::Scene::SceneReadNotKeyError);
  34. } else {
  35. app->switch_to_next_scene(iButtonApp::Scene::SceneReadSuccess);
  36. }
  37. } else {
  38. app->switch_to_next_scene(iButtonApp::Scene::SceneReadSuccess);
  39. }
  40. } else if(event->type == iButtonEvent::Type::EventTypeTick) {
  41. consumed = true;
  42. app->notify_red_blink();
  43. }
  44. return consumed;
  45. }
  46. void iButtonSceneRead::on_exit(iButtonApp* app) {
  47. Popup* popup = app->get_view_manager()->get_popup();
  48. ibutton_worker_stop(app->get_key_worker());
  49. popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
  50. popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
  51. popup_set_icon(popup, 0, 0, NULL);
  52. }