ibutton_scene_read.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "../ibutton_i.h"
  2. #include <dolphin/dolphin.h>
  3. static void ibutton_scene_read_callback(void* context) {
  4. iButton* ibutton = context;
  5. view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventWorkerRead);
  6. }
  7. void ibutton_scene_read_on_enter(void* context) {
  8. iButton* ibutton = context;
  9. Popup* popup = ibutton->popup;
  10. iButtonKey* key = ibutton->key;
  11. iButtonWorker* worker = ibutton->key_worker;
  12. DOLPHIN_DEED(DolphinDeedIbuttonRead);
  13. popup_set_header(popup, "iButton", 95, 26, AlignCenter, AlignBottom);
  14. popup_set_text(popup, "Waiting\nfor key ...", 95, 30, AlignCenter, AlignTop);
  15. popup_set_icon(popup, 0, 5, &I_DolphinWait_61x59);
  16. view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewPopup);
  17. string_set_str(ibutton->file_path, IBUTTON_APP_FOLDER);
  18. ibutton_worker_read_set_callback(worker, ibutton_scene_read_callback, ibutton);
  19. ibutton_worker_read_start(worker, key);
  20. ibutton_notification_message(ibutton, iButtonNotificationMessageReadStart);
  21. }
  22. bool ibutton_scene_read_on_event(void* context, SceneManagerEvent event) {
  23. iButton* ibutton = context;
  24. SceneManager* scene_manager = ibutton->scene_manager;
  25. bool consumed = false;
  26. if(event.type == SceneManagerEventTypeTick) {
  27. consumed = true;
  28. } else if(event.type == SceneManagerEventTypeCustom) {
  29. consumed = true;
  30. if(event.event == iButtonCustomEventWorkerRead) {
  31. bool success = false;
  32. iButtonKey* key = ibutton->key;
  33. if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
  34. if(!ibutton_key_dallas_crc_is_valid(key)) {
  35. scene_manager_next_scene(scene_manager, iButtonSceneReadCRCError);
  36. } else if(!ibutton_key_dallas_is_1990_key(key)) {
  37. scene_manager_next_scene(scene_manager, iButtonSceneReadNotKeyError);
  38. } else {
  39. success = true;
  40. }
  41. } else {
  42. success = true;
  43. }
  44. if(success) {
  45. ibutton_notification_message(ibutton, iButtonNotificationMessageSuccess);
  46. ibutton_notification_message(ibutton, iButtonNotificationMessageGreenOn);
  47. DOLPHIN_DEED(DolphinDeedIbuttonReadSuccess);
  48. scene_manager_next_scene(scene_manager, iButtonSceneReadSuccess);
  49. }
  50. }
  51. }
  52. return consumed;
  53. }
  54. void ibutton_scene_read_on_exit(void* context) {
  55. iButton* ibutton = context;
  56. Popup* popup = ibutton->popup;
  57. ibutton_worker_stop(ibutton->key_worker);
  58. popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
  59. popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
  60. popup_set_icon(popup, 0, 0, NULL);
  61. ibutton_notification_message(ibutton, iButtonNotificationMessageBlinkStop);
  62. }