interface_task.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include <AceButton.h>
  2. #include "interface_task.h"
  3. using namespace ace_button;
  4. #define COUNT_OF(A) (sizeof(A) / sizeof(A[0]))
  5. static KnobConfig configs[] = {
  6. // int32_t num_positions;
  7. // int32_t position;
  8. // float position_width_radians;
  9. // float detent_strength_unit;
  10. // float endstop_strength_unit;
  11. // float snap_point;
  12. // char descriptor[50];
  13. {
  14. 0,
  15. 0,
  16. 10 * PI / 180,
  17. 0,
  18. 1,
  19. 1.1,
  20. "Unbounded\nNo detents",
  21. },
  22. {
  23. 11,
  24. 0,
  25. 10 * PI / 180,
  26. 0,
  27. 1,
  28. 1.1,
  29. "Bounded 0-10\nNo detents",
  30. },
  31. {
  32. 73,
  33. 0,
  34. 10 * PI / 180,
  35. 0,
  36. 1,
  37. 1.1,
  38. "Multi-rev\nNo detents",
  39. },
  40. {
  41. 2,
  42. 0,
  43. 60 * PI / 180,
  44. 1,
  45. 1,
  46. 0.55, // Note the snap point is slightly past the midpoint (0.5); compare to normal detents which use a snap point *past* the next value (i.e. > 1)
  47. "On/off\nStrong detent",
  48. },
  49. {
  50. 1,
  51. 0,
  52. 60 * PI / 180,
  53. 0.01,
  54. 0.6,
  55. 1.1,
  56. "Return-to-center",
  57. },
  58. {
  59. 256,
  60. 127,
  61. 1 * PI / 180,
  62. 0,
  63. 1,
  64. 1.1,
  65. "Fine values\nNo detents",
  66. },
  67. {
  68. 256,
  69. 127,
  70. 1 * PI / 180,
  71. 1,
  72. 1,
  73. 1.1,
  74. "Fine values\nWith detents",
  75. },
  76. {
  77. 32,
  78. 0,
  79. 8.225806452 * PI / 180,
  80. 1,
  81. 1,
  82. 1.1,
  83. "Coarse values\nStrong detents",
  84. },
  85. {
  86. 32,
  87. 0,
  88. 8.225806452 * PI / 180,
  89. 0.2,
  90. 1,
  91. 1.1,
  92. "Coarse values\nWeak detents",
  93. },
  94. };
  95. InterfaceTask::InterfaceTask(const uint8_t task_core, MotorTask& motor_task) : Task{"Interface", 2048, 1, task_core}, motor_task_(motor_task) {
  96. }
  97. InterfaceTask::~InterfaceTask() {}
  98. void InterfaceTask::run() {
  99. #if PIN_BUTTON_NEXT >= 34
  100. pinMode(PIN_BUTTON_NEXT, INPUT);
  101. #else
  102. pinMode(PIN_BUTTON_NEXT, INPUT_PULLUP);
  103. #endif
  104. AceButton button_next((uint8_t) PIN_BUTTON_NEXT);
  105. button_next.getButtonConfig()->setIEventHandler(this);
  106. #if PIN_BUTTON_PREV > -1
  107. #if PIN_BUTTON_PREV >= 34
  108. pinMode(PIN_BUTTON_PREV, INPUT);
  109. #else
  110. pinMode(PIN_BUTTON_PREV, INPUT_PULLUP);
  111. #endif
  112. AceButton button_prev((uint8_t) PIN_BUTTON_PREV);
  113. button_prev.getButtonConfig()->setIEventHandler(this);
  114. #endif
  115. motor_task_.setConfig(configs[0]);
  116. while (1) {
  117. button_next.check();
  118. #if PIN_BUTTON_PREV > -1
  119. button_prev.check();
  120. #endif
  121. if (Serial.available()) {
  122. int v = Serial.read();
  123. if (v == ' ') {
  124. changeConfig(true);
  125. }
  126. }
  127. delay(10);
  128. }
  129. }
  130. void InterfaceTask::handleEvent(AceButton* button, uint8_t event_type, uint8_t button_state) {
  131. switch (event_type) {
  132. case AceButton::kEventPressed:
  133. if (button->getPin() == PIN_BUTTON_NEXT) {
  134. changeConfig(true);
  135. }
  136. #if PIN_BUTTON_PREV > -1
  137. if (button->getPin() == PIN_BUTTON_PREV) {
  138. changeConfig(false);
  139. }
  140. #endif
  141. break;
  142. case AceButton::kEventReleased:
  143. break;
  144. }
  145. }
  146. void InterfaceTask::changeConfig(bool next) {
  147. if (next) {
  148. current_config_ = (current_config_ + 1) % COUNT_OF(configs);
  149. } else {
  150. if (current_config_ == 0) {
  151. current_config_ = COUNT_OF(configs) - 1;
  152. } else {
  153. current_config_ --;
  154. }
  155. }
  156. Serial.printf("Changing config to %d:\n%s\n", current_config_, configs[current_config_].descriptor);
  157. motor_task_.setConfig(configs[current_config_]);
  158. }