ibutton-app.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "ibutton-app.h"
  2. #include <stdarg.h>
  3. void iButtonApp::run(void) {
  4. iButtonEvent event;
  5. bool consumed;
  6. bool exit = false;
  7. scenes[current_scene]->on_enter(this);
  8. while(!exit) {
  9. view.receive_event(&event);
  10. consumed = scenes[current_scene]->on_event(this, &event);
  11. if(!consumed) {
  12. if(event.type == iButtonEvent::Type::EventTypeBack) {
  13. exit = switch_to_previous_scene();
  14. }
  15. }
  16. };
  17. scenes[current_scene]->on_exit(this);
  18. }
  19. iButtonApp::iButtonApp() {
  20. notify_init();
  21. api_hal_power_insomnia_enter();
  22. key_worker = new KeyWorker(&ibutton_gpio);
  23. sd_ex_api = static_cast<SdCard_Api*>(furi_record_open("sdcard-ex"));
  24. fs_api = static_cast<FS_Api*>(furi_record_open("sdcard"));
  25. // we need random
  26. srand(DWT->CYCCNT);
  27. }
  28. iButtonApp::~iButtonApp() {
  29. furi_record_close("sdcard-ex");
  30. furi_record_close("sdcard");
  31. api_hal_power_insomnia_exit();
  32. }
  33. iButtonAppViewManager* iButtonApp::get_view_manager() {
  34. return &view;
  35. }
  36. void iButtonApp::switch_to_next_scene(Scene next_scene) {
  37. previous_scenes_list.push_front(current_scene);
  38. if(next_scene != Scene::SceneExit) {
  39. scenes[current_scene]->on_exit(this);
  40. current_scene = next_scene;
  41. scenes[current_scene]->on_enter(this);
  42. }
  43. }
  44. void iButtonApp::search_and_switch_to_previous_scene(std::initializer_list<Scene> scenes_list) {
  45. Scene previous_scene = Scene::SceneStart;
  46. bool scene_found = false;
  47. while(!scene_found) {
  48. previous_scene = get_previous_scene();
  49. for(Scene element : scenes_list) {
  50. if(previous_scene == element || previous_scene == Scene::SceneStart) {
  51. scene_found = true;
  52. break;
  53. }
  54. }
  55. }
  56. scenes[current_scene]->on_exit(this);
  57. current_scene = previous_scene;
  58. scenes[current_scene]->on_enter(this);
  59. }
  60. bool iButtonApp::switch_to_previous_scene(uint8_t count) {
  61. Scene previous_scene = Scene::SceneStart;
  62. for(uint8_t i = 0; i < count; i++) {
  63. previous_scene = get_previous_scene();
  64. if(previous_scene == Scene::SceneExit) break;
  65. }
  66. if(previous_scene == Scene::SceneExit) {
  67. return true;
  68. } else {
  69. scenes[current_scene]->on_exit(this);
  70. current_scene = previous_scene;
  71. scenes[current_scene]->on_enter(this);
  72. return false;
  73. }
  74. }
  75. iButtonApp::Scene iButtonApp::get_previous_scene() {
  76. Scene scene = previous_scenes_list.front();
  77. previous_scenes_list.pop_front();
  78. return scene;
  79. }
  80. const GpioPin* iButtonApp::get_ibutton_pin() {
  81. // TODO open record
  82. return &ibutton_gpio;
  83. }
  84. KeyWorker* iButtonApp::get_key_worker() {
  85. return key_worker;
  86. }
  87. iButtonKey* iButtonApp::get_key() {
  88. return &key;
  89. }
  90. SdCard_Api* iButtonApp::get_sd_ex_api() {
  91. return sd_ex_api;
  92. }
  93. FS_Api* iButtonApp::get_fs_api() {
  94. return fs_api;
  95. }
  96. char* iButtonApp::get_file_name() {
  97. return file_name;
  98. }
  99. uint8_t iButtonApp::get_file_name_size() {
  100. return file_name_size;
  101. }
  102. void iButtonApp::notify_init() {
  103. // TODO open record
  104. const GpioPin* vibro_record = &vibro_gpio;
  105. gpio_init(vibro_record, GpioModeOutputPushPull);
  106. gpio_write(vibro_record, false);
  107. }
  108. void iButtonApp::notify_green_blink() {
  109. notify_green_on();
  110. delay(10);
  111. notify_green_off();
  112. }
  113. void iButtonApp::notify_yellow_blink() {
  114. notify_red_on();
  115. notify_green_on();
  116. delay(10);
  117. notify_green_off();
  118. notify_red_off();
  119. }
  120. void iButtonApp::notify_red_blink() {
  121. notify_red_on();
  122. delay(10);
  123. notify_red_off();
  124. }
  125. void iButtonApp::notify_green_on() {
  126. api_hal_light_set(LightGreen, 0xFF);
  127. }
  128. void iButtonApp::notify_green_off() {
  129. api_hal_light_set(LightGreen, 0x00);
  130. }
  131. void iButtonApp::notify_red_on() {
  132. api_hal_light_set(LightRed, 0xFF);
  133. }
  134. void iButtonApp::notify_red_off() {
  135. api_hal_light_set(LightRed, 0x00);
  136. }
  137. void iButtonApp::notify_error() {
  138. notify_vibro_on();
  139. delay(50);
  140. notify_vibro_off();
  141. delay(100);
  142. notify_vibro_on();
  143. delay(50);
  144. notify_vibro_off();
  145. }
  146. void iButtonApp::notify_success() {
  147. notify_vibro_on();
  148. hal_pwm_set(0.5, 1760, &SPEAKER_TIM, SPEAKER_CH);
  149. delay(50);
  150. hal_pwm_stop(&SPEAKER_TIM, SPEAKER_CH);
  151. notify_vibro_off();
  152. }
  153. void iButtonApp::notify_vibro_on() {
  154. gpio_write(&vibro_gpio, true);
  155. }
  156. void iButtonApp::notify_vibro_off() {
  157. gpio_write(&vibro_gpio, false);
  158. }
  159. void iButtonApp::set_text_store(const char* text...) {
  160. va_list args;
  161. va_start(args, text);
  162. vsnprintf(text_store, text_store_size, text, args);
  163. va_end(args);
  164. }
  165. char* iButtonApp::get_text_store() {
  166. return text_store;
  167. }
  168. uint8_t iButtonApp::get_text_store_size() {
  169. return text_store_size;
  170. }
  171. void iButtonApp::generate_random_name(char* name, uint8_t max_name_size) {
  172. const uint8_t prefix_size = 9;
  173. const char* prefix[prefix_size] = {
  174. "ancient",
  175. "hollow",
  176. "strange",
  177. "disappeared",
  178. "unknown",
  179. "unthinkable",
  180. "unnamable",
  181. "nameless",
  182. "my",
  183. };
  184. const uint8_t suffix_size = 8;
  185. const char* suffix[suffix_size] = {
  186. "door",
  187. "entrance",
  188. "doorway",
  189. "entry",
  190. "portal",
  191. "entree",
  192. "opening",
  193. "crack",
  194. };
  195. sniprintf(
  196. name, max_name_size, "%s_%s", prefix[rand() % prefix_size], suffix[rand() % suffix_size]);
  197. // to upper
  198. name[0] = name[0] - 0x20;
  199. }