ibutton-app.cpp 5.1 KB

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