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.

248 lines
7.2 KiB

  1. /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  2. * Copyright 2020 Ploopy Corporation
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "opt_encoder.h"
  18. #include <stdbool.h>
  19. enum State { HIHI, HILO, LOLO, LOHI };
  20. enum State state;
  21. /* Variables used for scroll wheel functionality. */
  22. bool lohif;
  23. bool hilof;
  24. int lowA;
  25. int highA;
  26. bool cLowA;
  27. bool cHighA;
  28. int lowIndexA;
  29. int highIndexA;
  30. bool lowOverflowA;
  31. bool highOverflowA;
  32. int lowB;
  33. int highB;
  34. bool cLowB;
  35. bool cHighB;
  36. int lowIndexB;
  37. int highIndexB;
  38. bool lowOverflowB;
  39. bool highOverflowB;
  40. int scrollThresholdA;
  41. int scrollThresholdB;
  42. int arLowA[SCROLLER_AR_SIZE];
  43. int arHighA[SCROLLER_AR_SIZE];
  44. int arLowB[SCROLLER_AR_SIZE];
  45. int arHighB[SCROLLER_AR_SIZE];
  46. void calculateThresholdA(int curA);
  47. void calculateThresholdB(int curB);
  48. int calculateThreshold(int cur, int* low, int* high, bool* cLow, bool* cHigh, int arLow[], int arHigh[], int* lowIndex, int* highIndex, bool* lowOverflow, bool* highOverflow);
  49. int thresholdEquation(int lo, int hi);
  50. void incrementIndex(int* index, bool* ovflw);
  51. /* Setup function for the scroll wheel. Initializes
  52. the relevant variables. */
  53. void opt_encoder_init(void) {
  54. state = HIHI;
  55. lohif = false;
  56. hilof = false;
  57. lowA = 1023;
  58. highA = 0;
  59. cLowA = false;
  60. cHighA = false;
  61. lowIndexA = 0;
  62. highIndexA = 0;
  63. lowOverflowA = false;
  64. highOverflowA = false;
  65. lowB = 1023;
  66. highB = 0;
  67. cLowB = false;
  68. cHighB = false;
  69. lowIndexB = 0;
  70. highIndexB = 0;
  71. lowOverflowB = false;
  72. highOverflowB = false;
  73. scrollThresholdA = 0;
  74. scrollThresholdB = 0;
  75. }
  76. int8_t opt_encoder_handler(uint16_t curA, uint16_t curB) {
  77. if (lowOverflowA == false || highOverflowA == false) calculateThresholdA(curA);
  78. if (lowOverflowB == false || highOverflowB == false) calculateThresholdB(curB);
  79. bool LO = false;
  80. bool HI = true;
  81. bool sA, sB;
  82. int ret = 0;
  83. if (curA < scrollThresholdA)
  84. sA = LO;
  85. else
  86. sA = HI;
  87. if (curB < scrollThresholdB)
  88. sB = LO;
  89. else
  90. sB = HI;
  91. if (state == HIHI) {
  92. if (sA == LO && sB == HI) {
  93. state = LOHI;
  94. if (hilof) {
  95. ret = 1;
  96. hilof = false;
  97. }
  98. } else if (sA == HI && sB == LO) {
  99. state = HILO;
  100. if (lohif) {
  101. ret = -1;
  102. lohif = false;
  103. }
  104. }
  105. }
  106. else if (state == HILO) {
  107. if (sA == HI && sB == HI) {
  108. state = HIHI;
  109. hilof = true;
  110. lohif = false;
  111. } else if (sA == LO && sB == LO) {
  112. state = LOLO;
  113. hilof = true;
  114. lohif = false;
  115. }
  116. }
  117. else if (state == LOLO) {
  118. if (sA == HI && sB == LO) {
  119. state = HILO;
  120. if (lohif) {
  121. ret = 1;
  122. lohif = false;
  123. }
  124. } else if (sA == LO && sB == HI) {
  125. state = LOHI;
  126. if (hilof) {
  127. ret = -1;
  128. hilof = false;
  129. }
  130. }
  131. }
  132. else { // state must be LOHI
  133. if (sA == HI && sB == HI) {
  134. state = HIHI;
  135. lohif = true;
  136. hilof = false;
  137. } else if (sA == LO && sB == LO) {
  138. state = LOLO;
  139. lohif = true;
  140. hilof = false;
  141. }
  142. }
  143. return ret;
  144. }
  145. void calculateThresholdA(int curA) { scrollThresholdA = calculateThreshold(curA, &lowA, &highA, &cLowA, &cHighA, arLowA, arHighA, &lowIndexA, &highIndexA, &lowOverflowA, &highOverflowA); }
  146. void calculateThresholdB(int curB) { scrollThresholdB = calculateThreshold(curB, &lowB, &highB, &cLowB, &cHighB, arLowB, arHighB, &lowIndexB, &highIndexB, &lowOverflowB, &highOverflowB); }
  147. int calculateThreshold(int cur, int* low, int* high, bool* cLow, bool* cHigh, int arLow[], int arHigh[], int* lowIndex, int* highIndex, bool* lowOverflow, bool* highOverflow) {
  148. if (cur < *low) *low = cur;
  149. if (cur > *high) *high = cur;
  150. int curThresh = thresholdEquation(*low, *high);
  151. int range = *high - *low;
  152. // The range is enforced to be over a certain limit because noise
  153. // can cause erroneous readings, making these calculations unstable.
  154. if (range >= SCROLL_THRESH_RANGE_LIM) {
  155. if (cur < curThresh) {
  156. if (*cHigh == true) {
  157. // We were just high, and now we crossed to low.
  158. // high reflects a sample of a high reading.
  159. arHigh[*highIndex] = *high;
  160. incrementIndex(highIndex, highOverflow);
  161. int midpoint = ((*high - *low) / 2) + *low;
  162. *low = midpoint;
  163. *high = midpoint;
  164. *cLow = false;
  165. *cHigh = false;
  166. } else {
  167. *cLow = true;
  168. }
  169. }
  170. if (cur > curThresh) {
  171. if (*cLow == true) {
  172. // We were just low, and now we crossed to high.
  173. // low reflects a sample of a low reading.
  174. arLow[*lowIndex] = *low;
  175. incrementIndex(lowIndex, lowOverflow);
  176. int midpoint = ((*high - *low) / 2) + *low;
  177. *low = midpoint;
  178. *high = midpoint;
  179. *cLow = false;
  180. *cHigh = false;
  181. } else {
  182. *cHigh = true;
  183. }
  184. }
  185. }
  186. int calcHigh = 0;
  187. if (*highOverflow == true) {
  188. for (int i = 0; i < SCROLLER_AR_SIZE; i++) {
  189. calcHigh += arHigh[i];
  190. }
  191. calcHigh = calcHigh / SCROLLER_AR_SIZE;
  192. } else if (*highIndex != 0) {
  193. for (int i = 0; i < *highIndex; i++) {
  194. calcHigh += arHigh[i];
  195. }
  196. calcHigh = calcHigh / *highIndex;
  197. } else {
  198. calcHigh = *high;
  199. }
  200. int calcLow = 0;
  201. if (*lowOverflow == true) {
  202. for (int i = 0; i < SCROLLER_AR_SIZE; i++) {
  203. calcLow += arLow[i];
  204. }
  205. calcLow = calcLow / SCROLLER_AR_SIZE;
  206. } else if (*lowIndex != 0) {
  207. for (int i = 0; i < *lowIndex; i++) {
  208. calcLow += arLow[i];
  209. }
  210. calcLow = calcLow / *lowIndex;
  211. } else {
  212. calcLow = *low;
  213. }
  214. return thresholdEquation(calcLow, calcHigh);
  215. }
  216. int thresholdEquation(int lo, int hi) { return ((hi - lo) / 3) + lo; }
  217. void incrementIndex(int* index, bool* ovflw) {
  218. if (*index < SCROLLER_AR_SIZE - 1)
  219. (*index)++;
  220. else {
  221. *index = 0;
  222. *ovflw = true;
  223. }
  224. }