No Description
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.

sm_value.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include <Arduino.h>
  2. #include "config.h"
  3. #include "config_pins.h"
  4. #include "lcd.h"
  5. #include "states.h"
  6. template <typename T>
  7. StateValue<T>::StateValue(State *_parent, T &_value, T _min, T _max) : State(_parent), value(_value) {
  8. min = _min;
  9. max = _max;
  10. heading = NULL;
  11. text = NULL;
  12. onEnterFunc = NULL;
  13. updateFunc = NULL;
  14. updateLiveFunc = NULL;
  15. }
  16. template <typename T>
  17. void StateValue<T>::setMax(T _max) {
  18. max = _max;
  19. }
  20. template <typename T>
  21. void StateValue<T>::onUpdate(UpdateFuncPtr func) {
  22. updateFunc = func;
  23. }
  24. template <typename T>
  25. void StateValue<T>::onLiveUpdate(UpdateFuncPtr func) {
  26. updateLiveFunc = func;
  27. }
  28. template <typename T>
  29. void StateValue<T>::display(void) {
  30. lcd_clear();
  31. if (heading == NULL) {
  32. lcd_set_heading(getTitle());
  33. } else {
  34. lcd_set_heading(heading);
  35. }
  36. String s = String(min) + F(" .. ") + String(value) + F(" .. ") + String(max);
  37. if (text != NULL) {
  38. s = text + String(F("\n")) + s;
  39. }
  40. lcd_set_text(s.c_str());
  41. }
  42. template <typename T>
  43. void StateValue<T>::enterState(void) {
  44. if (onEnterFunc != NULL) {
  45. onEnterFunc();
  46. }
  47. display();
  48. }
  49. template <typename T>
  50. void StateValue<T>::inState(StateMachineInput smi) {
  51. if (smi.encoder != 0) {
  52. float vf = smi.encoder;
  53. vf *= 1.0 + ((float)smi.rpm / ENCODER_RPM_VALUE_FACTOR);
  54. int v = vf;
  55. value -= v;
  56. if (value < min) {
  57. value = min;
  58. }
  59. if (value > max) {
  60. value = max;
  61. }
  62. if (updateLiveFunc != NULL) {
  63. updateLiveFunc(value);
  64. }
  65. display();
  66. }
  67. if (smi.click) {
  68. if (updateFunc != NULL) {
  69. updateFunc(value);
  70. }
  71. if (getChild() != NULL) {
  72. states_go_to(getChild());
  73. } else if (getParent() != NULL) {
  74. states_go_to(getParent());
  75. }
  76. }
  77. }
  78. template class StateValue<int>;
  79. template class StateValue<float>;
  80. // --------------------------------------
  81. template <typename T, size_t N>
  82. StateValues<T, N>::StateValues(State *_parent) : State(_parent) {
  83. heading = NULL;
  84. onEnterFunc = NULL;
  85. updateFunc = NULL;
  86. updateLiveFunc = NULL;
  87. pos = 0;
  88. off = 0;
  89. editing = false;
  90. }
  91. template <typename T, size_t N>
  92. void StateValues<T, N>::setData(size_t index, const char *name, T *value, T min, T max) {
  93. if (index >= N) {
  94. return;
  95. }
  96. values[index] = value;
  97. mins[index] = min;
  98. maxs[index] = max;
  99. texts[index] = name;
  100. }
  101. template <typename T, size_t N>
  102. void StateValues<T, N>::onUpdate(UpdateFuncPtr func) {
  103. updateFunc = func;
  104. }
  105. template <typename T, size_t N>
  106. void StateValues<T, N>::onLiveUpdate(UpdateFuncPtr func) {
  107. updateLiveFunc = func;
  108. }
  109. template <typename T, size_t N>
  110. void StateValues<T, N>::display(void) {
  111. lcd_clear();
  112. if (heading == NULL) {
  113. lcd_set_heading(getTitle());
  114. } else {
  115. lcd_set_heading(heading);
  116. }
  117. for (size_t i = off; (i < (N + 1)) && (i < (off + lcd_text_lines())); i++) {
  118. String s;
  119. if (i == pos) {
  120. if (editing) {
  121. s = F("# ");
  122. } else {
  123. s = F("> ");
  124. }
  125. } else {
  126. s = F(" ");
  127. }
  128. if (i < N) {
  129. s += texts[i] + String(F(" ")) + String(*(values[i])) + F(" (") + String(mins[i]) + F("/") + String(maxs[i]) + F(")");
  130. } else {
  131. if (getChild() != NULL) {
  132. s += F("Continue");
  133. } else {
  134. s += F("Done");
  135. }
  136. }
  137. lcd_set_menu_text(i - off, s.c_str(), 2);
  138. }
  139. }
  140. template <typename T, size_t N>
  141. void StateValues<T, N>::enterState(void) {
  142. pos = 0;
  143. if (onEnterFunc != NULL) {
  144. onEnterFunc();
  145. }
  146. display();
  147. }
  148. template <typename T, size_t N>
  149. void StateValues<T, N>::inState(StateMachineInput smi) {
  150. if (editing) {
  151. if (smi.encoder != 0) {
  152. float vf = smi.encoder;
  153. vf *= 1.0 + ((float)smi.rpm / ENCODER_RPM_VALUE_FACTOR);
  154. int v = vf;
  155. *(values[pos]) -= v;
  156. if (*(values[pos]) < mins[pos]) {
  157. *(values[pos]) = mins[pos];
  158. }
  159. if (*(values[pos]) > maxs[pos]) {
  160. *(values[pos]) = maxs[pos];
  161. }
  162. if (updateLiveFunc != NULL) {
  163. updateLiveFunc(pos, *(values[pos]));
  164. }
  165. display();
  166. }
  167. if (smi.click) {
  168. editing = false;
  169. display();
  170. }
  171. } else {
  172. if (smi.encoder != 0) {
  173. int tmp = pos;
  174. tmp -= smi.encoder;
  175. while (tmp < 0) {
  176. tmp += N + 1;
  177. }
  178. while (tmp >= N + 1) {
  179. tmp -= N + 1;
  180. }
  181. while (tmp < off) {
  182. off--;
  183. }
  184. while (tmp >= (off + lcd_text_lines())) {
  185. off++;
  186. }
  187. pos = tmp;
  188. display();
  189. }
  190. if (smi.click) {
  191. if (pos < N) {
  192. editing = true;
  193. display();
  194. } else {
  195. if (updateFunc != NULL) {
  196. for (size_t i = 0; i < N; i++) {
  197. updateFunc(i, *(values[i]));
  198. }
  199. }
  200. if (getChild() != NULL) {
  201. states_go_to(getChild());
  202. } else if (getParent() != NULL) {
  203. states_go_to(getParent());
  204. }
  205. }
  206. }
  207. }
  208. }
  209. template class StateValues<uint8_t, 2>;
  210. template class StateValues<float, 2>;
  211. template class StateValues<float, 12>;