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.

239 lines
6.8 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. enum State state;
  19. /* Variables used for scroll wheel functionality. */
  20. bool lohif;
  21. bool hilof;
  22. int lowA;
  23. int highA;
  24. bool cLowA;
  25. bool cHighA;
  26. int lowIndexA;
  27. int highIndexA;
  28. bool lowOverflowA;
  29. bool highOverflowA;
  30. int lowB;
  31. int highB;
  32. bool cLowB;
  33. bool cHighB;
  34. int lowIndexB;
  35. int highIndexB;
  36. bool lowOverflowB;
  37. bool highOverflowB;
  38. int scrollThresholdA;
  39. int scrollThresholdB;
  40. int arLowA[SCROLLER_AR_SIZE];
  41. int arHighA[SCROLLER_AR_SIZE];
  42. int arLowB[SCROLLER_AR_SIZE];
  43. int arHighB[SCROLLER_AR_SIZE];
  44. /* Setup function for the scroll wheel. Initializes
  45. the relevant variables. */
  46. void opt_encoder_init(void) {
  47. state = HIHI;
  48. lohif = false;
  49. hilof = false;
  50. lowA = 1023;
  51. highA = 0;
  52. cLowA = false;
  53. cHighA = false;
  54. lowIndexA = 0;
  55. highIndexA = 0;
  56. lowOverflowA = false;
  57. highOverflowA = false;
  58. lowB = 1023;
  59. highB = 0;
  60. cLowB = false;
  61. cHighB = false;
  62. lowIndexB = 0;
  63. highIndexB = 0;
  64. lowOverflowB = false;
  65. highOverflowB = false;
  66. scrollThresholdA = 0;
  67. scrollThresholdB = 0;
  68. }
  69. int opt_encoder_handler(int curA, int curB) {
  70. if (lowOverflowA == false || highOverflowA == false) calculateThresholdA(curA);
  71. if (lowOverflowB == false || highOverflowB == false) calculateThresholdB(curB);
  72. bool LO = false;
  73. bool HI = true;
  74. bool sA, sB;
  75. int ret = 0;
  76. if (curA < scrollThresholdA)
  77. sA = LO;
  78. else
  79. sA = HI;
  80. if (curB < scrollThresholdB)
  81. sB = LO;
  82. else
  83. sB = HI;
  84. if (state == HIHI) {
  85. if (sA == LO && sB == HI) {
  86. state = LOHI;
  87. if (hilof) {
  88. ret = 1;
  89. hilof = false;
  90. }
  91. } else if (sA == HI && sB == LO) {
  92. state = HILO;
  93. if (lohif) {
  94. ret = -1;
  95. lohif = false;
  96. }
  97. }
  98. }
  99. else if (state == HILO) {
  100. if (sA == HI && sB == HI) {
  101. state = HIHI;
  102. hilof = true;
  103. lohif = false;
  104. } else if (sA == LO && sB == LO) {
  105. state = LOLO;
  106. hilof = true;
  107. lohif = false;
  108. }
  109. }
  110. else if (state == LOLO) {
  111. if (sA == HI && sB == LO) {
  112. state = HILO;
  113. if (lohif) {
  114. ret = 1;
  115. lohif = false;
  116. }
  117. } else if (sA == LO && sB == HI) {
  118. state = LOHI;
  119. if (hilof) {
  120. ret = -1;
  121. hilof = false;
  122. }
  123. }
  124. }
  125. else { // state must be LOHI
  126. if (sA == HI && sB == HI) {
  127. state = HIHI;
  128. lohif = true;
  129. hilof = false;
  130. } else if (sA == LO && sB == LO) {
  131. state = LOLO;
  132. lohif = true;
  133. hilof = false;
  134. }
  135. }
  136. return ret;
  137. }
  138. void calculateThresholdA(int curA) { scrollThresholdA = calculateThreshold(curA, &lowA, &highA, &cLowA, &cHighA, arLowA, arHighA, &lowIndexA, &highIndexA, &lowOverflowA, &highOverflowA); }
  139. void calculateThresholdB(int curB) { scrollThresholdB = calculateThreshold(curB, &lowB, &highB, &cLowB, &cHighB, arLowB, arHighB, &lowIndexB, &highIndexB, &lowOverflowB, &highOverflowB); }
  140. int calculateThreshold(int cur, int* low, int* high, bool* cLow, bool* cHigh, int arLow[], int arHigh[], int* lowIndex, int* highIndex, bool* lowOverflow, bool* highOverflow) {
  141. if (cur < *low) *low = cur;
  142. if (cur > *high) *high = cur;
  143. int curThresh = thresholdEquation(*low, *high);
  144. int range = *high - *low;
  145. // The range is enforced to be over a certain limit because noise
  146. // can cause erroneous readings, making these calculations unstable.
  147. if (range >= SCROLL_THRESH_RANGE_LIM) {
  148. if (cur < curThresh) {
  149. if (*cHigh == true) {
  150. // We were just high, and now we crossed to low.
  151. // high reflects a sample of a high reading.
  152. arHigh[*highIndex] = *high;
  153. incrementIndex(highIndex, highOverflow);
  154. int midpoint = ((*high - *low) / 2) + *low;
  155. *low = midpoint;
  156. *high = midpoint;
  157. *cLow = false;
  158. *cHigh = false;
  159. } else {
  160. *cLow = true;
  161. }
  162. }
  163. if (cur > curThresh) {
  164. if (*cLow == true) {
  165. // We were just low, and now we crossed to high.
  166. // low reflects a sample of a low reading.
  167. arLow[*lowIndex] = *low;
  168. incrementIndex(lowIndex, lowOverflow);
  169. int midpoint = ((*high - *low) / 2) + *low;
  170. *low = midpoint;
  171. *high = midpoint;
  172. *cLow = false;
  173. *cHigh = false;
  174. } else {
  175. *cHigh = true;
  176. }
  177. }
  178. }
  179. int calcHigh = 0;
  180. if (*highOverflow == true) {
  181. for (int i = 0; i < SCROLLER_AR_SIZE; i++) {
  182. calcHigh += arHigh[i];
  183. }
  184. calcHigh = calcHigh / SCROLLER_AR_SIZE;
  185. } else if (*highIndex != 0) {
  186. for (int i = 0; i < *highIndex; i++) {
  187. calcHigh += arHigh[i];
  188. }
  189. calcHigh = calcHigh / *highIndex;
  190. } else {
  191. calcHigh = *high;
  192. }
  193. int calcLow = 0;
  194. if (*lowOverflow == true) {
  195. for (int i = 0; i < SCROLLER_AR_SIZE; i++) {
  196. calcLow += arLow[i];
  197. }
  198. calcLow = calcLow / SCROLLER_AR_SIZE;
  199. } else if (*lowIndex != 0) {
  200. for (int i = 0; i < *lowIndex; i++) {
  201. calcLow += arLow[i];
  202. }
  203. calcLow = calcLow / *lowIndex;
  204. } else {
  205. calcLow = *low;
  206. }
  207. return thresholdEquation(calcLow, calcHigh);
  208. }
  209. int thresholdEquation(int lo, int hi) { return ((hi - lo) / 3) + lo; }
  210. void incrementIndex(int* index, bool* ovflw) {
  211. if (*index < SCROLLER_AR_SIZE - 1)
  212. (*index)++;
  213. else {
  214. *index = 0;
  215. *ovflw = true;
  216. }
  217. }