ibutton_scene_emulate.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "ibutton_scene_emulate.h"
  2. #include "../ibutton_app.h"
  3. #include "../ibutton_view_manager.h"
  4. #include "../ibutton_event.h"
  5. #include "../ibutton_key.h"
  6. #include <dolphin/dolphin.h>
  7. #include <callback-connector.h>
  8. void iButtonSceneEmulate::on_enter(iButtonApp* app) {
  9. iButtonAppViewManager* view_manager = app->get_view_manager();
  10. Popup* popup = view_manager->get_popup();
  11. iButtonKey* key = app->get_key();
  12. uint8_t* key_data = key->get_data();
  13. const char* key_name = key->get_name();
  14. uint8_t line_count = 2;
  15. DOLPHIN_DEED(DolphinDeedIbuttonEmulate);
  16. // check that stored key has name
  17. if(strcmp(key_name, "") != 0) {
  18. app->set_text_store("emulating\n%s", key_name);
  19. line_count = 2;
  20. } else {
  21. // if not, show key data
  22. switch(key->get_key_type()) {
  23. case iButtonKeyType::KeyDallas:
  24. app->set_text_store(
  25. "emulating\n%02X %02X %02X %02X\n%02X %02X %02X %02X",
  26. key_data[0],
  27. key_data[1],
  28. key_data[2],
  29. key_data[3],
  30. key_data[4],
  31. key_data[5],
  32. key_data[6],
  33. key_data[7]);
  34. line_count = 3;
  35. break;
  36. case iButtonKeyType::KeyCyfral:
  37. app->set_text_store("emulating\n%02X %02X", key_data[0], key_data[1]);
  38. line_count = 2;
  39. break;
  40. case iButtonKeyType::KeyMetakom:
  41. app->set_text_store(
  42. "emulating\n%02X %02X %02X %02X",
  43. key_data[0],
  44. key_data[1],
  45. key_data[2],
  46. key_data[3]);
  47. line_count = 2;
  48. break;
  49. }
  50. }
  51. switch(line_count) {
  52. case 3:
  53. popup_set_header(popup, "iButton", 82, 18, AlignCenter, AlignBottom);
  54. popup_set_text(popup, app->get_text_store(), 82, 22, AlignCenter, AlignTop);
  55. break;
  56. default:
  57. popup_set_header(popup, "iButton", 82, 24, AlignCenter, AlignBottom);
  58. popup_set_text(popup, app->get_text_store(), 82, 28, AlignCenter, AlignTop);
  59. break;
  60. }
  61. popup_set_icon(popup, 2, 10, &I_iButtonKey_49x44);
  62. view_manager->switch_to(iButtonAppViewManager::Type::iButtonAppViewPopup);
  63. app->get_key_worker()->start_emulate(app->get_key());
  64. }
  65. bool iButtonSceneEmulate::on_event(iButtonApp* app, iButtonEvent* event) {
  66. bool consumed = false;
  67. if(event->type == iButtonEvent::Type::EventTypeTick) {
  68. consumed = true;
  69. if(app->get_key_worker()->emulated()) {
  70. app->notify_yellow_blink();
  71. } else {
  72. app->notify_red_blink();
  73. }
  74. }
  75. return consumed;
  76. }
  77. void iButtonSceneEmulate::on_exit(iButtonApp* app) {
  78. app->get_key_worker()->stop_emulate();
  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, 0, 0, NULL);
  83. }