logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

opt_encoder_default.c (7400B)


  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) {
  146. scrollThresholdA = calculateThreshold(curA, &lowA, &highA, &cLowA, &cHighA, arLowA, arHighA, &lowIndexA, &highIndexA, &lowOverflowA, &highOverflowA);
  147. }
  148. void calculateThresholdB(int curB) {
  149. scrollThresholdB = calculateThreshold(curB, &lowB, &highB, &cLowB, &cHighB, arLowB, arHighB, &lowIndexB, &highIndexB, &lowOverflowB, &highOverflowB);
  150. }
  151. int calculateThreshold(int cur, int* low, int* high, bool* cLow, bool* cHigh, int arLow[], int arHigh[], int* lowIndex, int* highIndex, bool* lowOverflow, bool* highOverflow) {
  152. if (cur < *low) *low = cur;
  153. if (cur > *high) *high = cur;
  154. int curThresh = thresholdEquation(*low, *high);
  155. int range = *high - *low;
  156. // The range is enforced to be over a certain limit because noise
  157. // can cause erroneous readings, making these calculations unstable.
  158. if (range >= SCROLL_THRESH_RANGE_LIM) {
  159. if (cur < curThresh) {
  160. if (*cHigh == true) {
  161. // We were just high, and now we crossed to low.
  162. // high reflects a sample of a high reading.
  163. arHigh[*highIndex] = *high;
  164. incrementIndex(highIndex, highOverflow);
  165. int midpoint = ((*high - *low) / 2) + *low;
  166. *low = midpoint;
  167. *high = midpoint;
  168. *cLow = false;
  169. *cHigh = false;
  170. } else {
  171. *cLow = true;
  172. }
  173. }
  174. if (cur > curThresh) {
  175. if (*cLow == true) {
  176. // We were just low, and now we crossed to high.
  177. // low reflects a sample of a low reading.
  178. arLow[*lowIndex] = *low;
  179. incrementIndex(lowIndex, lowOverflow);
  180. int midpoint = ((*high - *low) / 2) + *low;
  181. *low = midpoint;
  182. *high = midpoint;
  183. *cLow = false;
  184. *cHigh = false;
  185. } else {
  186. *cHigh = true;
  187. }
  188. }
  189. }
  190. int calcHigh = 0;
  191. if (*highOverflow == true) {
  192. for (int i = 0; i < SCROLLER_AR_SIZE; i++) {
  193. calcHigh += arHigh[i];
  194. }
  195. calcHigh = calcHigh / SCROLLER_AR_SIZE;
  196. } else if (*highIndex != 0) {
  197. for (int i = 0; i < *highIndex; i++) {
  198. calcHigh += arHigh[i];
  199. }
  200. calcHigh = calcHigh / *highIndex;
  201. } else {
  202. calcHigh = *high;
  203. }
  204. int calcLow = 0;
  205. if (*lowOverflow == true) {
  206. for (int i = 0; i < SCROLLER_AR_SIZE; i++) {
  207. calcLow += arLow[i];
  208. }
  209. calcLow = calcLow / SCROLLER_AR_SIZE;
  210. } else if (*lowIndex != 0) {
  211. for (int i = 0; i < *lowIndex; i++) {
  212. calcLow += arLow[i];
  213. }
  214. calcLow = calcLow / *lowIndex;
  215. } else {
  216. calcLow = *low;
  217. }
  218. return thresholdEquation(calcLow, calcHigh);
  219. }
  220. int thresholdEquation(int lo, int hi) {
  221. return ((hi - lo) / 3) + lo;
  222. }
  223. void incrementIndex(int* index, bool* ovflw) {
  224. if (*index < SCROLLER_AR_SIZE - 1)
  225. (*index)++;
  226. else {
  227. *index = 0;
  228. *ovflw = true;
  229. }
  230. }