ibutton-scene-select-key.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "ibutton-scene-select-key.h"
  2. #include "../ibutton-app.h"
  3. #include "../ibutton-event.h"
  4. #include "../ibutton-key.h"
  5. void iButtonSceneSelectKey::on_enter(iButtonApp* app) {
  6. // Input events and views are managed by file_select
  7. bool res = app->get_sd_ex_api()->file_select(
  8. app->get_sd_ex_api()->context,
  9. "ibutton",
  10. "*",
  11. app->get_file_name(),
  12. app->get_file_name_size(),
  13. app->get_key()->get_name());
  14. // Process file_select return
  15. if(res) {
  16. // Get key file path
  17. string_t key_str;
  18. string_init_set_str(key_str, "ibutton/");
  19. string_cat_str(key_str, app->get_file_name());
  20. // Read data from file
  21. File key_file;
  22. uint8_t key_data[IBUTTON_KEY_DATA_SIZE + 1] = {};
  23. // TODO process false result from file system service
  24. app->get_fs_api()->file.open(
  25. &key_file, string_get_cstr(key_str), FSAM_READ, FSOM_OPEN_EXISTING);
  26. app->get_fs_api()->file.read(&key_file, key_data, IBUTTON_KEY_DATA_SIZE + 1);
  27. app->get_fs_api()->file.close(&key_file);
  28. string_clear(key_str);
  29. // Set key data
  30. iButtonKeyType key_type = static_cast<iButtonKeyType>(key_data[0]);
  31. if(key_type > iButtonKeyType::KeyMetakom) {
  32. app->switch_to_next_scene(iButtonApp::Scene::SceneStart);
  33. }
  34. app->get_key()->set_name(app->get_file_name());
  35. app->get_key()->set_type(key_type);
  36. app->get_key()->set_data(key_data + 1, IBUTTON_KEY_DATA_SIZE);
  37. app->switch_to_next_scene(iButtonApp::Scene::SceneSavedKeyMenu);
  38. } else {
  39. app->switch_to_previous_scene();
  40. }
  41. }
  42. bool iButtonSceneSelectKey::on_event(iButtonApp* app, iButtonEvent* event) {
  43. return false;
  44. }
  45. void iButtonSceneSelectKey::on_exit(iButtonApp* app) {
  46. }