ibutton-scene-write.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "ibutton-scene-write.h"
  2. #include "../ibutton-app.h"
  3. #include "../ibutton-view-manager.h"
  4. #include "../ibutton-event.h"
  5. #include "../ibutton-key.h"
  6. void iButtonSceneWrite::on_enter(iButtonApp* app) {
  7. iButtonAppViewManager* view_manager = app->get_view_manager();
  8. Popup* popup = view_manager->get_popup();
  9. iButtonKey* key = app->get_key();
  10. uint8_t* key_data = key->get_data();
  11. const char* key_name = key->get_name();
  12. uint8_t line_count = 2;
  13. // check that stored key has name
  14. if(strcmp(key_name, "") != 0) {
  15. app->set_text_store("writing\n%s", key_name);
  16. line_count = 2;
  17. } else {
  18. // if not, show key data
  19. switch(key->get_key_type()) {
  20. case iButtonKeyType::KeyDallas:
  21. app->set_text_store(
  22. "writing\n%02X %02X %02X %02X\n%02X %02X %02X %02X",
  23. key_data[0],
  24. key_data[1],
  25. key_data[2],
  26. key_data[3],
  27. key_data[4],
  28. key_data[5],
  29. key_data[6],
  30. key_data[7]);
  31. line_count = 3;
  32. break;
  33. case iButtonKeyType::KeyCyfral:
  34. app->set_text_store("writing\n%02X %02X", key_data[0], key_data[1]);
  35. line_count = 2;
  36. break;
  37. case iButtonKeyType::KeyMetakom:
  38. app->set_text_store(
  39. "writing\n%02X %02X %02X %02X", key_data[0], key_data[1], key_data[2], key_data[3]);
  40. line_count = 2;
  41. break;
  42. }
  43. }
  44. switch(line_count) {
  45. case 3:
  46. popup_set_header(popup, "iButton", 92, 18, AlignCenter, AlignBottom);
  47. popup_set_text(popup, app->get_text_store(), 92, 22, AlignCenter, AlignTop);
  48. break;
  49. default:
  50. popup_set_header(popup, "iButton", 92, 24, AlignCenter, AlignBottom);
  51. popup_set_text(popup, app->get_text_store(), 92, 28, AlignCenter, AlignTop);
  52. break;
  53. }
  54. popup_set_icon(popup, 10, 10, I_iButtonKey_49x44);
  55. view_manager->switch_to(iButtonAppViewManager::Type::iButtonAppViewPopup);
  56. app->get_key_worker()->start_write();
  57. }
  58. bool iButtonSceneWrite::on_event(iButtonApp* app, iButtonEvent* event) {
  59. bool consumed = false;
  60. if(event->type == iButtonEvent::Type::EventTypeTick) {
  61. consumed = true;
  62. KeyWriter::Error result = app->get_key_worker()->write(app->get_key());
  63. switch(result) {
  64. case KeyWriter::Error::SAME_KEY:
  65. case KeyWriter::Error::OK:
  66. app->switch_to_next_scene(iButtonApp::Scene::SceneWriteSuccess);
  67. break;
  68. case KeyWriter::Error::NO_DETECT:
  69. app->notify_red_blink();
  70. break;
  71. case KeyWriter::Error::CANNOT_WRITE:
  72. app->notify_yellow_blink();
  73. break;
  74. }
  75. }
  76. return consumed;
  77. }
  78. void iButtonSceneWrite::on_exit(iButtonApp* app) {
  79. Popup* popup = app->get_view_manager()->get_popup();
  80. popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
  81. popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
  82. popup_set_icon(popup, -1, -1, I_DolphinWait_61x59);
  83. app->get_key_worker()->stop_write();
  84. }