ibutton_scene_read.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. popup_set_header(popup, "iButton", 95, 26, AlignCenter, AlignBottom);
  13. popup_set_text(popup, "Waiting\nfor key ...", 95, 30, AlignCenter, AlignTop);
  14. popup_set_icon(popup, 0, 5, &I_DolphinWait_61x59);
  15. view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewPopup);
  16. furi_string_set(ibutton->file_path, IBUTTON_APP_FOLDER);
  17. ibutton_worker_read_set_callback(worker, ibutton_scene_read_callback, ibutton);
  18. ibutton_worker_read_start(worker, key);
  19. ibutton_notification_message(ibutton, iButtonNotificationMessageReadStart);
  20. }
  21. bool ibutton_scene_read_on_event(void* context, SceneManagerEvent event) {
  22. iButton* ibutton = context;
  23. SceneManager* scene_manager = ibutton->scene_manager;
  24. bool consumed = false;
  25. if(event.type == SceneManagerEventTypeTick) {
  26. consumed = true;
  27. } else if(event.type == SceneManagerEventTypeCustom) {
  28. consumed = true;
  29. if(event.event == iButtonCustomEventWorkerRead) {
  30. bool success = false;
  31. iButtonKey* key = ibutton->key;
  32. if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
  33. if(!ibutton_key_dallas_crc_is_valid(key)) {
  34. scene_manager_next_scene(scene_manager, iButtonSceneReadCRCError);
  35. } else if(!ibutton_key_dallas_is_1990_key(key)) {
  36. scene_manager_next_scene(scene_manager, iButtonSceneReadNotKeyError);
  37. } else {
  38. success = true;
  39. }
  40. } else {
  41. success = true;
  42. }
  43. if(success) {
  44. ibutton_notification_message(ibutton, iButtonNotificationMessageSuccess);
  45. scene_manager_next_scene(scene_manager, iButtonSceneReadSuccess);
  46. DOLPHIN_DEED(DolphinDeedIbuttonReadSuccess);
  47. }
  48. }
  49. }
  50. return consumed;
  51. }
  52. void ibutton_scene_read_on_exit(void* context) {
  53. iButton* ibutton = context;
  54. Popup* popup = ibutton->popup;
  55. ibutton_worker_stop(ibutton->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. ibutton_notification_message(ibutton, iButtonNotificationMessageBlinkStop);
  60. }