ibutton_scene_read.cpp 2.3 KB

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