My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

event_loop.cpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #ifdef 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 supress 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_START();
  109. SERIAL_ECHOLNPAIR("Touch start: ", tag);
  110. #endif
  111. pressed_tag = tag;
  112. current_screen.onRefresh();
  113. // When the user taps on a button, activate the onTouchStart handler
  114. const uint8_t lastScreen = current_screen.getScreen();
  115. if (current_screen.onTouchStart(tag)) {
  116. touch_timer.start();
  117. if (UIData::flags.bits.touch_start_sound) sound.play(press_sound);
  118. }
  119. // In the case in which a touch event triggered a new screen to be
  120. // drawn, we don't issue a touchEnd since it would be sent to the
  121. // wrong screen.
  122. UIData::flags.bits.ignore_unpress = (lastScreen != current_screen.getScreen());
  123. }
  124. else {
  125. touch_timer.start();
  126. }
  127. break;
  128. default: // PRESSED
  129. if (!UIData::flags.bits.touch_debouncing) {
  130. if (tag == pressed_tag) {
  131. // The user is holding down a button.
  132. if (touch_timer.elapsed(1000 / TOUCH_REPEATS_PER_SECOND)) {
  133. if (current_screen.onTouchHeld(tag)) {
  134. current_screen.onRefresh();
  135. if (UIData::flags.bits.touch_repeat_sound) sound.play(repeat_sound);
  136. }
  137. touch_timer.start();
  138. }
  139. }
  140. else if (tag == 0) {
  141. touch_timer.start();
  142. UIData::flags.bits.touch_debouncing = true;
  143. }
  144. }
  145. else {
  146. // Debouncing...
  147. if (tag == pressed_tag) {
  148. // If while debouncing, we detect a press, then cancel debouncing.
  149. UIData::flags.bits.touch_debouncing = false;
  150. }
  151. else if (touch_timer.elapsed(DEBOUNCE_PERIOD)) {
  152. UIData::flags.bits.touch_debouncing = false;
  153. if (UIData::flags.bits.ignore_unpress) {
  154. UIData::flags.bits.ignore_unpress = false;
  155. pressed_tag = UNPRESSED;
  156. break;
  157. }
  158. if (UIData::flags.bits.touch_end_sound) sound.play(unpress_sound);
  159. #if ENABLED(TOUCH_UI_DEBUG)
  160. SERIAL_ECHO_START();
  161. SERIAL_ECHOLNPAIR("Touch end: ", pressed_tag);
  162. #endif
  163. const uint8_t saved_pressed_tag = pressed_tag;
  164. pressed_tag = UNPRESSED;
  165. current_screen.onTouchEnd(saved_pressed_tag);
  166. current_screen.onRefresh();
  167. }
  168. }
  169. break;
  170. } // switch (pressed_tag)
  171. } // processEvents()
  172. void EventLoop::setup() {
  173. CLCD::init();
  174. DLCache::init();
  175. UIData::reset_persistent_data();
  176. current_screen.start();
  177. }
  178. void EventLoop::loop() {
  179. sound.onIdle();
  180. /**
  181. * Guard against re-entry of UI methods, which can
  182. * crash. Re-entry can happen because some functions
  183. * (e.g. planner.synchronize) call idle().
  184. */
  185. if (!UIData::flags.bits.prevent_reentry) {
  186. UIData::flags.bits.prevent_reentry = true;
  187. current_screen.onIdle();
  188. process_events();
  189. UIData::flags.bits.prevent_reentry = false;
  190. }
  191. }
  192. } // namespace FTDI
  193. #endif // FTDI_EXTENDED