qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

touch_encoder.c (10017B)


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