opt_encoder.c 6.3 KB

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