summaryrefslogtreecommitdiff
path: root/keyboards/rgbkb/common/touch_encoder.c
blob: 18037b8fb57d53a6a3f86fa97c2feafad13aedf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <https://github.com/XScorpion2> wrote this file.  As long as you retain this
 * notice you can do whatever you want with this stuff. If we meet some day, and
 * you think this stuff is worth it, you can buy me a beer in return. Ryan Caltabiano
 * ----------------------------------------------------------------------------
 */

#include "i2c_master.h"
#include "keyboard.h"
#include "touch_encoder.h"
#include "print.h"
#include "wait.h"
#include "timer.h"

// for memcpy
#include <string.h>
#include <transactions.h>

#define I2C_ADDRESS 0x1C
#define CALIBRATION_BIT 0x80
#define OVERFLOW_BIT 0x40
#define SLIDER_BIT 0x02

#ifndef TOUCH_UPDATE_INTERVAL
#   define TOUCH_UPDATE_INTERVAL 33
#endif

enum {  // QT2120 registers
    QT_CHIP_ID = 0,
    QT_FIRMWARE_VERSION,
    QT_DETECTION_STATUS,
    QT_KEY_STATUS,
    QT_SLIDER_POSITION = 5,
    QT_CALIBRATE,
    QT_RESET,
    QT_LP,
    QT_TTD,
    QT_ATD,
    QT_DI,
    QT_TRD,
    QT_DHT,
    QT_SLIDER_OPTION,
    QT_CHARDE_TIME,
    QT_KEY0_DTHR,
    QT_KEY1_DTHR,
    QT_KEY2_DTHR,
    QT_KEY3_DTHR,
    QT_KEY4_DTHR,
    QT_KEY5_DTHR,
    QT_KEY6_DTHR,
    QT_KEY7_DTHR,
    QT_KEY8_DTHR,
    QT_KEY9_DTHR,
    QT_KEY10_DTHR,
    QT_KEY11_DTHR,
    QT_KEY0_CTRL,
    QT_KEY1_CTRL,
    QT_KEY2_CTRL,
    QT_KEY3_CTRL,
    QT_KEY4_CTRL,
    QT_KEY5_CTRL,
    QT_KEY6_CTRL,
    QT_KEY7_CTRL,
    QT_KEY8_CTRL,
    QT_KEY9_CTRL,
    QT_KEY10_CTRL,
    QT_KEY11_CTRL,
    QT_KEY0_PULSE_SCALE,
    QT_KEY1_PULSE_SCALE,
    QT_KEY2_PULSE_SCALE,
    QT_KEY3_PULSE_SCALE,
    QT_KEY4_PULSE_SCALE,
    QT_KEY5_PULSE_SCALE,
    QT_KEY6_PULSE_SCALE,
    QT_KEY7_PULSE_SCALE,
    QT_KEY8_PULSE_SCALE,
    QT_KEY9_PULSE_SCALE,
    QT_KEY10_PULSE_SCALE,
    QT_KEY11_PULSE_SCALE,
    QT_KEY0_SIGNAL,
    QT_KEY1_SIGNAL     = 54,
    QT_KEY2_SIGNAL     = 56,
    QT_KEY3_SIGNAL     = 58,
    QT_KEY4_SIGNAL     = 60,
    QT_KEY5_SIGNAL     = 62,
    QT_KEY6_SIGNAL     = 64,
    QT_KEY7_SIGNAL     = 66,
    QT_KEY8_SIGNAL     = 68,
    QT_KEY9_SIGNAL     = 70,
    QT_KEY10_SIGNAL    = 72,
    QT_KEY11_SIGNAL    = 74,
    QT_KEY0_REFERENCE  = 76,
    QT_KEY1_REFERENCE  = 78,
    QT_KEY2_REFERENCE  = 80,
    QT_KEY3_REFERENCE  = 82,
    QT_KEY4_REFERENCE  = 84,
    QT_KEY5_REFERENCE  = 86,
    QT_KEY6_REFERENCE  = 88,
    QT_KEY7_REFERENCE  = 90,
    QT_KEY8_REFERENCE  = 92,
    QT_KEY9_REFERENCE  = 94,
    QT_KEY10_REFERENCE = 96,
    QT_KEY11_REFERENCE = 98,
};

bool     touch_initialized  = false;
bool     touch_disabled = false;
uint8_t  touch_handness = 0;
// touch_raw & touch_processed store the Detection Status, Key Status (x2), and Slider Position values
uint8_t  touch_raw[4]       = { 0 };
uint8_t  touch_processed[4] = { 0 };

uint16_t touch_timer        = 0;
uint16_t touch_update_timer = 0;

// For split transport only
typedef struct {
    uint8_t position;
    uint8_t taps;
} slave_touch_status_t;

bool touch_slave_init = false;
slave_touch_status_t touch_slave_state = { 0, 0 };

static bool write_register8(uint8_t address, uint8_t data) {
    i2c_status_t status = i2c_write_register((I2C_ADDRESS << 1), address, &data, sizeof(data), I2C_TIMEOUT);
    if (status != I2C_STATUS_SUCCESS) {
        xprintf("write_register8 %d failed %d\n", address, status);
    }
    return status == I2C_STATUS_SUCCESS;
}

static bool read_register(uint8_t address, uint8_t* data, uint16_t length) {
    i2c_status_t status = i2c_read_register((I2C_ADDRESS << 1), address, data, length, I2C_TIMEOUT);
    if (status != I2C_STATUS_SUCCESS) {
        xprintf("read_register %d failed %d\n", address, status);
        return false;
    }
    return true;
}

void touch_encoder_init(void) {
    i2c_init();

    touch_handness = is_keyboard_left() ? 0 : 1;

    // Set QT to slider mode
    touch_initialized = write_register8(QT_SLIDER_OPTION, 0x80);
    touch_initialized &= write_register8(QT_TTD, 4);  // Toward Drift - 20 @ 3.2s
    touch_initialized &= write_register8(QT_ATD, 1);  // Away Drift - 5 @ 0.8s
    touch_initialized &= write_register8(QT_DI, 4);   // Detection Integrator - 4
    touch_initialized &= write_register8(QT_TRD, 0);  // Touch Recall - 48
    touch_encoder_calibrate();
}

__attribute__((weak)) bool touch_encoder_tapped_kb(uint8_t index, uint8_t section) { return touch_encoder_tapped_user(index, section); }
__attribute__((weak)) bool touch_encoder_update_kb(uint8_t index, bool clockwise) { return touch_encoder_update_user(index, clockwise); }

__attribute__((weak)) bool touch_encoder_tapped_user(uint8_t index, uint8_t section) { return true; }
__attribute__((weak)) bool touch_encoder_update_user(uint8_t index, bool clockwise) { return true; }

static void touch_encoder_update_tapped(void) {
    // Started touching, being counter for TOUCH_TERM
    if (touch_processed[0] & SLIDER_BIT) {
        touch_timer = timer_read() + TOUCH_TERM;
        return;
    }

    // Touch held too long, bail
    if (timer_expired(timer_read(), touch_timer)) return;

    uint8_t section = touch_processed[3] / (UINT8_MAX / TOUCH_SEGMENTS + 1);
    xprintf("tap %d %d\n", touch_handness, section);
    if (is_keyboard_master()) {
        if (!touch_disabled) {
            touch_encoder_tapped_kb(touch_handness, section);
        }
    }
    else {
        touch_slave_state.taps ^= (1 << section);
    }
}

static void touch_encoder_update_position_common(uint8_t* position, uint8_t raw, uint8_t index) {
    int8_t delta = (*position - raw) / TOUCH_RESOLUTION;
    bool clockwise = raw > *position;
    if (delta == 0) return;

    // Don't store raw directly, as we want to ensure any remainder is kept and used next time this is called
    *position -= delta * TOUCH_RESOLUTION;
    xprintf("pos %d %d\n", index, raw);
    //uint8_t u_delta   = delta < 0 ? -delta : delta;
    if (!touch_disabled) {
        //for (uint8_t i = 0; i < u_delta; i++)
            touch_encoder_update_kb(index, clockwise);
    }
}

static void touch_encoder_update_position(void) {
    // If the user touchs and moves enough, expire touch_timer faster and do encoder position logic instead
    if (!timer_expired(timer_read(), touch_timer)) {
        if ((uint8_t)(touch_raw[3] - touch_processed[3]) <= TOUCH_DEADZONE) return;
        touch_timer = timer_read();
    }

    if (is_keyboard_master()) {
        touch_encoder_update_position_common(&touch_processed[3], touch_raw[3], touch_handness);
    }
    else {
        touch_slave_state.position = touch_raw[3];
    }
}

void touch_encoder_update_slave(slave_touch_status_t slave_state) {
    if (!touch_slave_init) {
        touch_slave_state = slave_state;
        touch_slave_init = true;
        return;
    }

    if (touch_slave_state.position != slave_state.position) {
        // Did a new slide event start?
        uint8_t mask = (1 << 7);
        if ((touch_slave_state.taps & mask) != (slave_state.taps & mask)) {
            touch_slave_state.position = slave_state.position;
        }
        touch_encoder_update_position_common(&touch_slave_state.position, slave_state.position, !touch_handness);
    }

    if (touch_slave_state.taps != slave_state.taps) {
        if (!touch_disabled) {
            for (uint8_t section = 0; section < TOUCH_SEGMENTS; section++) {
                uint8_t mask = (1 << section);
                if ((touch_slave_state.taps & mask) != (slave_state.taps & mask)) {
                    xprintf("tap %d %d\n", !touch_handness, section);
                    touch_encoder_tapped_kb(!touch_handness, section);
                }
            }
        }
        touch_slave_state.taps = slave_state.taps;
    }
}

void touch_encoder_update(int8_t transaction_id) {
#if TOUCH_UPDATE_INTERVAL > 0
    if (!timer_expired(timer_read(), touch_update_timer)) return;
    touch_update_timer = timer_read() + TOUCH_UPDATE_INTERVAL;
#endif

    if (is_keyboard_master()) {
        slave_touch_status_t slave_state;
        if (transaction_rpc_exec(transaction_id, sizeof(bool), &touch_disabled, sizeof(slave_touch_status_t), &slave_state)) {
            if (memcmp(&touch_slave_state, &slave_state, sizeof(slave_touch_status_t)))
                touch_encoder_update_slave(slave_state);
        }
    }

    if (!touch_initialized) return;

    read_register(QT_DETECTION_STATUS, &touch_raw[0], sizeof(touch_raw));
    touch_processed[1] = touch_raw[1];
    touch_processed[2] = touch_raw[2];

    if (touch_raw[0] != touch_processed[0]) {
        uint8_t delta = touch_raw[0] ^ touch_processed[0];
        touch_processed[0]  = touch_raw[0];
        // When calibrating, normal sensor behavior is supended
        if (delta & CALIBRATION_BIT) {
            xprintf("calibration %d\n", touch_processed[0] >> 7 & 1);
        }
        if (delta & OVERFLOW_BIT) {
            xprintf("overflow %d\n", touch_processed[0] >> 6 & 1);
        }
        if (delta & SLIDER_BIT) {
            touch_processed[3] = touch_raw[3];
            if (!is_keyboard_master()) {
                touch_slave_state.position = touch_raw[3];
                touch_slave_state.taps ^= (1 << 7);
            }
            touch_encoder_update_tapped();
        }
    }

    if ((touch_raw[0] & SLIDER_BIT) && touch_processed[3] != touch_raw[3]) {
        touch_encoder_update_position();
    }
}

void touch_encoder_calibrate(void) {
    if (!touch_initialized) return;
    write_register8(QT_CALIBRATE, 0x01);
}

bool touch_encoder_is_calibrating(void) {
    return touch_raw[0] & CALIBRATION_BIT;
}

void touch_encoder_toggle(void) {
    touch_disabled = !touch_disabled;
}

bool touch_encoder_is_on(void) {
    return !touch_disabled;
}

void touch_encoder_slave_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
    touch_disabled = *(bool*)initiator2target_buffer;
    memcpy(target2initiator_buffer, &touch_slave_state, sizeof(slave_touch_status_t));
}