touch_encoder.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * ----------------------------------------------------------------------------
  3. * "THE BEER-WARE LICENSE" (Revision 42):
  4. * <https://github.com/XScorpion2> wrote this file. As long as you retain this
  5. * notice you can do whatever you want with this stuff. If we meet some day, and
  6. * you think this stuff is worth it, you can buy me a beer in return. Ryan Caltabiano
  7. * ----------------------------------------------------------------------------
  8. */
  9. #include "i2c_master.h"
  10. #include "keyboard.h"
  11. #include "touch_encoder.h"
  12. #include "print.h"
  13. #include "wait.h"
  14. #include "timer.h"
  15. // for memcpy
  16. #include <string.h>
  17. #include <transactions.h>
  18. #define I2C_ADDRESS 0x1C
  19. #define CALIBRATION_BIT 0x80
  20. #define OVERFLOW_BIT 0x40
  21. #define SLIDER_BIT 0x02
  22. #ifndef TOUCH_UPDATE_INTERVAL
  23. # define TOUCH_UPDATE_INTERVAL 33
  24. #endif
  25. enum { // QT2120 registers
  26. QT_CHIP_ID = 0,
  27. QT_FIRMWARE_VERSION,
  28. QT_DETECTION_STATUS,
  29. QT_KEY_STATUS,
  30. QT_SLIDER_POSITION = 5,
  31. QT_CALIBRATE,
  32. QT_RESET,
  33. QT_LP,
  34. QT_TTD,
  35. QT_ATD,
  36. QT_DI,
  37. QT_TRD,
  38. QT_DHT,
  39. QT_SLIDER_OPTION,
  40. QT_CHARDE_TIME,
  41. QT_KEY0_DTHR,
  42. QT_KEY1_DTHR,
  43. QT_KEY2_DTHR,
  44. QT_KEY3_DTHR,
  45. QT_KEY4_DTHR,
  46. QT_KEY5_DTHR,
  47. QT_KEY6_DTHR,
  48. QT_KEY7_DTHR,
  49. QT_KEY8_DTHR,
  50. QT_KEY9_DTHR,
  51. QT_KEY10_DTHR,
  52. QT_KEY11_DTHR,
  53. QT_KEY0_CTRL,
  54. QT_KEY1_CTRL,
  55. QT_KEY2_CTRL,
  56. QT_KEY3_CTRL,
  57. QT_KEY4_CTRL,
  58. QT_KEY5_CTRL,
  59. QT_KEY6_CTRL,
  60. QT_KEY7_CTRL,
  61. QT_KEY8_CTRL,
  62. QT_KEY9_CTRL,
  63. QT_KEY10_CTRL,
  64. QT_KEY11_CTRL,
  65. QT_KEY0_PULSE_SCALE,
  66. QT_KEY1_PULSE_SCALE,
  67. QT_KEY2_PULSE_SCALE,
  68. QT_KEY3_PULSE_SCALE,
  69. QT_KEY4_PULSE_SCALE,
  70. QT_KEY5_PULSE_SCALE,
  71. QT_KEY6_PULSE_SCALE,
  72. QT_KEY7_PULSE_SCALE,
  73. QT_KEY8_PULSE_SCALE,
  74. QT_KEY9_PULSE_SCALE,
  75. QT_KEY10_PULSE_SCALE,
  76. QT_KEY11_PULSE_SCALE,
  77. QT_KEY0_SIGNAL,
  78. QT_KEY1_SIGNAL = 54,
  79. QT_KEY2_SIGNAL = 56,
  80. QT_KEY3_SIGNAL = 58,
  81. QT_KEY4_SIGNAL = 60,
  82. QT_KEY5_SIGNAL = 62,
  83. QT_KEY6_SIGNAL = 64,
  84. QT_KEY7_SIGNAL = 66,
  85. QT_KEY8_SIGNAL = 68,
  86. QT_KEY9_SIGNAL = 70,
  87. QT_KEY10_SIGNAL = 72,
  88. QT_KEY11_SIGNAL = 74,
  89. QT_KEY0_REFERENCE = 76,
  90. QT_KEY1_REFERENCE = 78,
  91. QT_KEY2_REFERENCE = 80,
  92. QT_KEY3_REFERENCE = 82,
  93. QT_KEY4_REFERENCE = 84,
  94. QT_KEY5_REFERENCE = 86,
  95. QT_KEY6_REFERENCE = 88,
  96. QT_KEY7_REFERENCE = 90,
  97. QT_KEY8_REFERENCE = 92,
  98. QT_KEY9_REFERENCE = 94,
  99. QT_KEY10_REFERENCE = 96,
  100. QT_KEY11_REFERENCE = 98,
  101. };
  102. bool touch_initialized = false;
  103. bool touch_disabled = false;
  104. uint8_t touch_handness = 0;
  105. // touch_raw & touch_processed store the Detection Status, Key Status (x2), and Slider Position values
  106. uint8_t touch_raw[4] = { 0 };
  107. uint8_t touch_processed[4] = { 0 };
  108. uint16_t touch_timer = 0;
  109. uint16_t touch_update_timer = 0;
  110. // For split transport only
  111. typedef struct {
  112. uint8_t position;
  113. uint8_t taps;
  114. } slave_touch_status_t;
  115. bool touch_slave_init = false;
  116. slave_touch_status_t touch_slave_state = { 0, 0 };
  117. static bool write_register8(uint8_t address, uint8_t data) {
  118. i2c_status_t status = i2c_writeReg((I2C_ADDRESS << 1), address, &data, sizeof(data), I2C_TIMEOUT);
  119. if (status != I2C_STATUS_SUCCESS) {
  120. xprintf("write_register8 %d failed %d\n", address, status);
  121. }
  122. return status == I2C_STATUS_SUCCESS;
  123. }
  124. static bool read_register(uint8_t address, uint8_t* data, uint16_t length) {
  125. i2c_status_t status = i2c_readReg((I2C_ADDRESS << 1), address, data, length, I2C_TIMEOUT);
  126. if (status != I2C_STATUS_SUCCESS) {
  127. xprintf("read_register %d failed %d\n", address, status);
  128. return false;
  129. }
  130. return true;
  131. }
  132. void touch_encoder_init(void) {
  133. i2c_init();
  134. touch_handness = is_keyboard_left() ? 0 : 1;
  135. // Set QT to slider mode
  136. touch_initialized = write_register8(QT_SLIDER_OPTION, 0x80);
  137. touch_initialized &= write_register8(QT_TTD, 4); // Toward Drift - 20 @ 3.2s
  138. touch_initialized &= write_register8(QT_ATD, 1); // Away Drift - 5 @ 0.8s
  139. touch_initialized &= write_register8(QT_DI, 4); // Detection Integrator - 4
  140. touch_initialized &= write_register8(QT_TRD, 0); // Touch Recall - 48
  141. touch_encoder_calibrate();
  142. }
  143. __attribute__((weak)) bool touch_encoder_tapped_kb(uint8_t index, uint8_t section) { return touch_encoder_tapped_user(index, section); }
  144. __attribute__((weak)) bool touch_encoder_update_kb(uint8_t index, bool clockwise) { return touch_encoder_update_user(index, clockwise); }
  145. __attribute__((weak)) bool touch_encoder_tapped_user(uint8_t index, uint8_t section) { return true; }
  146. __attribute__((weak)) bool touch_encoder_update_user(uint8_t index, bool clockwise) { return true; }
  147. static void touch_encoder_update_tapped(void) {
  148. // Started touching, being counter for TOUCH_TERM
  149. if (touch_processed[0] & SLIDER_BIT) {
  150. touch_timer = timer_read() + TOUCH_TERM;
  151. return;
  152. }
  153. // Touch held too long, bail
  154. if (timer_expired(timer_read(), touch_timer)) return;
  155. uint8_t section = touch_processed[3] / (UINT8_MAX / TOUCH_SEGMENTS + 1);
  156. xprintf("tap %d %d\n", touch_handness, section);
  157. if (is_keyboard_master()) {
  158. if (!touch_disabled) {
  159. touch_encoder_tapped_kb(touch_handness, section);
  160. }
  161. }
  162. else {
  163. touch_slave_state.taps ^= (1 << section);
  164. }
  165. }
  166. static void touch_encoder_update_position_common(uint8_t* position, uint8_t raw, uint8_t index) {
  167. int8_t delta = (*position - raw) / TOUCH_RESOLUTION;
  168. bool clockwise = raw > *position;
  169. if (delta == 0) return;
  170. // Don't store raw directly, as we want to ensure any remainder is kept and used next time this is called
  171. *position -= delta * TOUCH_RESOLUTION;
  172. xprintf("pos %d %d\n", index, raw);
  173. //uint8_t u_delta = delta < 0 ? -delta : delta;
  174. if (!touch_disabled) {
  175. //for (uint8_t i = 0; i < u_delta; i++)
  176. touch_encoder_update_kb(index, clockwise);
  177. }
  178. }
  179. static void touch_encoder_update_position(void) {
  180. // If the user touchs and moves enough, expire touch_timer faster and do encoder position logic instead
  181. if (!timer_expired(timer_read(), touch_timer)) {
  182. if ((uint8_t)(touch_raw[3] - touch_processed[3]) <= TOUCH_DEADZONE) return;
  183. touch_timer = timer_read();
  184. }
  185. if (is_keyboard_master()) {
  186. touch_encoder_update_position_common(&touch_processed[3], touch_raw[3], touch_handness);
  187. }
  188. else {
  189. touch_slave_state.position = touch_raw[3];
  190. }
  191. }
  192. void touch_encoder_update_slave(slave_touch_status_t slave_state) {
  193. if (!touch_slave_init) {
  194. touch_slave_state = slave_state;
  195. touch_slave_init = true;
  196. return;
  197. }
  198. if (touch_slave_state.position != slave_state.position) {
  199. // Did a new slide event start?
  200. uint8_t mask = (1 << 7);
  201. if ((touch_slave_state.taps & mask) != (slave_state.taps & mask)) {
  202. touch_slave_state.position = slave_state.position;
  203. }
  204. touch_encoder_update_position_common(&touch_slave_state.position, slave_state.position, !touch_handness);
  205. }
  206. if (touch_slave_state.taps != slave_state.taps) {
  207. if (!touch_disabled) {
  208. for (uint8_t section = 0; section < TOUCH_SEGMENTS; section++) {
  209. uint8_t mask = (1 << section);
  210. if ((touch_slave_state.taps & mask) != (slave_state.taps & mask)) {
  211. xprintf("tap %d %d\n", !touch_handness, section);
  212. touch_encoder_tapped_kb(!touch_handness, section);
  213. }
  214. }
  215. }
  216. touch_slave_state.taps = slave_state.taps;
  217. }
  218. }
  219. void touch_encoder_update(int8_t transaction_id) {
  220. #if TOUCH_UPDATE_INTERVAL > 0
  221. if (!timer_expired(timer_read(), touch_update_timer)) return;
  222. touch_update_timer = timer_read() + TOUCH_UPDATE_INTERVAL;
  223. #endif
  224. if (is_keyboard_master()) {
  225. slave_touch_status_t slave_state;
  226. if (transaction_rpc_exec(transaction_id, sizeof(bool), &touch_disabled, sizeof(slave_touch_status_t), &slave_state)) {
  227. if (memcmp(&touch_slave_state, &slave_state, sizeof(slave_touch_status_t)))
  228. touch_encoder_update_slave(slave_state);
  229. }
  230. }
  231. if (!touch_initialized) return;
  232. read_register(QT_DETECTION_STATUS, &touch_raw[0], sizeof(touch_raw));
  233. touch_processed[1] = touch_raw[1];
  234. touch_processed[2] = touch_raw[2];
  235. if (touch_raw[0] != touch_processed[0]) {
  236. uint8_t delta = touch_raw[0] ^ touch_processed[0];
  237. touch_processed[0] = touch_raw[0];
  238. // When calibrating, normal sensor behavior is supended
  239. if (delta & CALIBRATION_BIT) {
  240. xprintf("calibration %d\n", touch_processed[0] >> 7 & 1);
  241. }
  242. if (delta & OVERFLOW_BIT) {
  243. xprintf("overflow %d\n", touch_processed[0] >> 6 & 1);
  244. }
  245. if (delta & SLIDER_BIT) {
  246. touch_processed[3] = touch_raw[3];
  247. if (!is_keyboard_master()) {
  248. touch_slave_state.position = touch_raw[3];
  249. touch_slave_state.taps ^= (1 << 7);
  250. }
  251. touch_encoder_update_tapped();
  252. }
  253. }
  254. if ((touch_raw[0] & SLIDER_BIT) && touch_processed[3] != touch_raw[3]) {
  255. touch_encoder_update_position();
  256. }
  257. }
  258. void touch_encoder_calibrate(void) {
  259. if (!touch_initialized) return;
  260. write_register8(QT_CALIBRATE, 0x01);
  261. }
  262. bool touch_encoder_is_calibrating(void) {
  263. return touch_raw[0] & CALIBRATION_BIT;
  264. }
  265. void touch_encoder_toggle(void) {
  266. touch_disabled = !touch_disabled;
  267. }
  268. bool touch_encoder_is_on(void) {
  269. return !touch_disabled;
  270. }
  271. void touch_encoder_slave_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
  272. touch_disabled = *(bool*)initiator2target_buffer;
  273. memcpy(target2initiator_buffer, &touch_slave_state, sizeof(slave_touch_status_t));
  274. }