My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

event_loop.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /******************
  2. * event_loop.cpp *
  3. ******************/
  4. /****************************************************************************
  5. * Written By Mark Pelletier 2017 - Aleph Objects, Inc. *
  6. * Written By Marcio Teixeira 2018 - Aleph Objects, Inc. *
  7. * *
  8. * This program is free software: you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation, either version 3 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * To view a copy of the GNU General Public License, go to the following *
  19. * location: <https://www.gnu.org/licenses/>. *
  20. ****************************************************************************/
  21. #include "ftdi_extended.h"
  22. #if ENABLED(FTDI_EXTENDED)
  23. using namespace FTDI;
  24. enum {
  25. UNPRESSED = 0x00
  26. };
  27. tiny_timer_t touch_timer;
  28. UIData::flags_t UIData::flags;
  29. uint8_t pressed_tag = UNPRESSED;
  30. uint8_t UIData::get_persistent_data_mask() {
  31. // A bit mask for flags that should be stored to the EEPROM.
  32. // Others are considered temporarily values that need not be
  33. // saved.
  34. constexpr flags_t persistent_flags = {
  35. bits: {
  36. touch_start_sound: true,
  37. touch_end_sound: true,
  38. touch_repeat_sound: true,
  39. show_animations: true
  40. }
  41. };
  42. return persistent_flags.value;
  43. }
  44. void UIData::reset_persistent_data() {
  45. // Default values for persistent data
  46. constexpr flags_t default_flags = {
  47. bits: {
  48. touch_start_sound: true,
  49. touch_end_sound: true,
  50. touch_repeat_sound: true,
  51. show_animations: true,
  52. touch_debouncing: false,
  53. ignore_unpress: false
  54. }
  55. };
  56. flags.value = default_flags.value;
  57. }
  58. uint8_t UIData::get_persistent_data() {
  59. return flags.value & get_persistent_data_mask();
  60. }
  61. void UIData::set_persistent_data(uint8_t value) {
  62. flags.value = value & get_persistent_data_mask();
  63. }
  64. void UIData::enable_touch_sounds(bool enabled) {
  65. UIData::flags.bits.touch_start_sound = enabled;
  66. UIData::flags.bits.touch_end_sound = enabled;
  67. UIData::flags.bits.touch_repeat_sound = enabled;
  68. }
  69. bool UIData::touch_sounds_enabled() {
  70. return UIData::flags.bits.touch_start_sound || UIData::flags.bits.touch_end_sound || UIData::flags.bits.touch_repeat_sound;
  71. }
  72. void UIData::enable_animations(bool enabled) {
  73. UIData::flags.bits.show_animations = enabled;
  74. }
  75. bool UIData::animations_enabled() {
  76. return UIData::flags.bits.show_animations;
  77. }
  78. namespace FTDI {
  79. uint8_t EventLoop::get_pressed_tag() {
  80. return pressed_tag;
  81. }
  82. bool EventLoop::is_touch_held() {
  83. return pressed_tag != 0;
  84. }
  85. /**
  86. * process_events(): Process events from the touch panel.
  87. *
  88. * This function consists of a state machine that accomplishes the following:
  89. *
  90. * - Reads the tag register from the touch panel
  91. * - Dispatches onTouchStart and onTouchEnd events to the active screen.
  92. * - Handles auto-repetition by sending onTouchHeld to the active screen periodically.
  93. * - Plays touch feedback "click" sounds when appropriate.
  94. * - Performs debouncing to suppress spurious touch events.
  95. */
  96. void EventLoop::process_events() {
  97. // If the LCD is processing commands, don't check
  98. // for tags since they may be changing and could
  99. // cause spurious events.
  100. if (!touch_timer.elapsed(TOUCH_UPDATE_INTERVAL) || CLCD::CommandFifo::is_processing()) {
  101. return;
  102. }
  103. const uint8_t tag = CLCD::get_tag();
  104. switch (pressed_tag) {
  105. case UNPRESSED:
  106. if (tag != 0) {
  107. #if ENABLED(TOUCH_UI_DEBUG)
  108. SERIAL_ECHO_MSG("Touch start: ", tag);
  109. #endif
  110. pressed_tag = tag;
  111. current_screen.onRefresh();
  112. // When the user taps on a button, activate the onTouchStart handler
  113. const uint8_t lastScreen = current_screen.getScreen();
  114. if (current_screen.onTouchStart(tag)) {
  115. touch_timer.start();
  116. if (UIData::flags.bits.touch_start_sound) sound.play(press_sound);
  117. }
  118. // In the case in which a touch event triggered a new screen to be
  119. // drawn, we don't issue a touchEnd since it would be sent to the
  120. // wrong screen.
  121. UIData::flags.bits.ignore_unpress = (lastScreen != current_screen.getScreen());
  122. }
  123. else {
  124. touch_timer.start();
  125. }
  126. break;
  127. default: // PRESSED
  128. if (!UIData::flags.bits.touch_debouncing) {
  129. if (tag == pressed_tag) {
  130. // The user is holding down a button.
  131. if (touch_timer.elapsed(1000 / TOUCH_REPEATS_PER_SECOND)) {
  132. if (current_screen.onTouchHeld(tag)) {
  133. current_screen.onRefresh();
  134. if (UIData::flags.bits.touch_repeat_sound) sound.play(repeat_sound);
  135. }
  136. touch_timer.start();
  137. }
  138. }
  139. else if (tag == 0) {
  140. touch_timer.start();
  141. UIData::flags.bits.touch_debouncing = true;
  142. }
  143. }
  144. else {
  145. // Debouncing...
  146. if (tag == pressed_tag) {
  147. // If while debouncing, we detect a press, then cancel debouncing.
  148. UIData::flags.bits.touch_debouncing = false;
  149. }
  150. else if (touch_timer.elapsed(DEBOUNCE_PERIOD)) {
  151. UIData::flags.bits.touch_debouncing = false;
  152. if (UIData::flags.bits.ignore_unpress) {
  153. UIData::flags.bits.ignore_unpress = false;
  154. pressed_tag = UNPRESSED;
  155. break;
  156. }
  157. if (UIData::flags.bits.touch_end_sound) sound.play(unpress_sound);
  158. #if ENABLED(TOUCH_UI_DEBUG)
  159. SERIAL_ECHO_MSG("Touch end: ", pressed_tag);
  160. #endif
  161. const uint8_t saved_pressed_tag = pressed_tag;
  162. pressed_tag = UNPRESSED;
  163. current_screen.onTouchEnd(saved_pressed_tag);
  164. current_screen.onRefresh();
  165. }
  166. }
  167. break;
  168. } // switch (pressed_tag)
  169. } // processEvents()
  170. void EventLoop::setup() {
  171. CLCD::init();
  172. DLCache::init();
  173. UIData::reset_persistent_data();
  174. current_screen.start();
  175. }
  176. void EventLoop::loop() {
  177. sound.onIdle();
  178. /**
  179. * Guard against re-entry of UI methods, which can
  180. * crash. Re-entry can happen because some functions
  181. * (e.g. planner.synchronize) call idle().
  182. */
  183. if (!UIData::flags.bits.prevent_reentry) {
  184. UIData::flags.bits.prevent_reentry = true;
  185. current_screen.onIdle();
  186. process_events();
  187. UIData::flags.bits.prevent_reentry = false;
  188. }
  189. }
  190. } // namespace FTDI
  191. #endif // FTDI_EXTENDED