-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointing_device_accel_via.c
190 lines (169 loc) · 7.45 KB
/
pointing_device_accel_via.c
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
// Copyright 2024 burkfers (@burkfers)
// SPDX-License-Identifier: GPL-2.0-or-later
#include "pointing_device_accel.h"
#include "pointing_device_internal.h"
#include "via.h"
_Static_assert(sizeof(g_pointing_device_accel_config) <= VIA_EEPROM_CUSTOM_CONFIG_SIZE, "Mismatch in via custom eeprom stored data");
enum via_pointing_device_channel {
id_maccel = 24,
};
enum via_pointing_device_ids {
id_pointing_device_takeoff = 1,
id_pointing_device_growth_rate = 2,
id_pointing_device_offset = 3,
id_pointing_device_limit = 4,
id_pointing_device_enabled = 5,
};
#define MACCEL_VIA_TKO_MIN 0
#define MACCEL_VIA_TKO_MAX 5
#define MACCEL_VIA_GRO_MIN 0.01
#define MACCEL_VIA_GRO_MAX 5
#define MACCEL_VIA_OFS_MIN 0
#define MACCEL_VIA_OFS_MAX 15
#define MACCEL_VIA_LMT_MIN 0
#define MACCEL_VIA_LMT_MAX 1
#define MACCEL_VIA_UINT16_MIN 0
#define MACCEL_VIA_UINT16_MAX \
60000 // Not using the full range for historic reasons. Should be changed with breaking change requiring via json
// update.
#define PROJECT(val, rmin, rmax, tmin, tmax) \
((rmax == rmin) ? tmin : ((((float)(val - rmin) / (float)(rmax - rmin)) * (float)(tmax - tmin)) + tmin))
#define PROJECT_TO_VIA(val, rmin, rmax) PROJECT(val, rmin, rmax, MACCEL_VIA_UINT16_MIN, MACCEL_VIA_UINT16_MAX)
#define PROJECT_FROM_VIA(val, tmin, tmax) PROJECT(val, MACCEL_VIA_UINT16_MIN, MACCEL_VIA_UINT16_MAX, tmin, tmax)
#define COMBINE_UINT8(one, two) (two | (one << 8))
// Handle the data received by the keyboard from the VIA menus
void pointing_device_config_set_value(uint8_t *data) {
// data = [ value_id, value_data ]
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
case id_pointing_device_takeoff:
{
uint16_t takeoff = COMBINE_UINT8(value_data[0], value_data[1]);
g_pointing_device_accel_config.takeoff =
PROJECT_FROM_VIA(takeoff, MACCEL_VIA_TKO_MIN, MACCEL_VIA_TKO_MAX);
#ifdef MACCEL_DEBUG
printf("MACCEL:via: TKO: %.3f grw: %.3f ofs: %.3f lmt: %.3f\n", g_pointing_device_accel_config.takeoff,
g_pointing_device_accel_config.growth_rate, g_pointing_device_accel_config.offset,
g_pointing_device_accel_config.limit);
#endif
break;
}
case id_pointing_device_growth_rate:
{
uint16_t growth_rate = COMBINE_UINT8(value_data[0], value_data[1]);
g_pointing_device_accel_config.growth_rate =
PROJECT_FROM_VIA(growth_rate, MACCEL_VIA_GRO_MIN, MACCEL_VIA_GRO_MAX);
#ifdef MACCEL_DEBUG
printf("MACCEL:via: tko: %.3f GRW: %.3f ofs: %.3f lmt: %.3f\n", g_pointing_device_accel_config.takeoff,
g_pointing_device_accel_config.growth_rate, g_pointing_device_accel_config.offset,
g_pointing_device_accel_config.limit);
#endif
break;
}
case id_pointing_device_offset:
{
uint16_t offset = COMBINE_UINT8(value_data[0], value_data[1]);
g_pointing_device_accel_config.offset =
PROJECT_FROM_VIA(offset, MACCEL_VIA_OFS_MIN, MACCEL_VIA_OFS_MAX);
#ifdef MACCEL_DEBUG
printf("MACCEL:via: tko: %.3f grw: %.3f OFS: %.3f lmt: %.3f\n", g_pointing_device_accel_config.takeoff,
g_pointing_device_accel_config.growth_rate, g_pointing_device_accel_config.offset,
g_pointing_device_accel_config.limit);
#endif
break;
}
case id_pointing_device_limit:
{
uint16_t limit = COMBINE_UINT8(value_data[0], value_data[1]);
g_pointing_device_accel_config.limit = PROJECT_FROM_VIA(limit, MACCEL_VIA_LMT_MIN, MACCEL_VIA_LMT_MAX);
#ifdef MACCEL_DEBUG
printf("MACCEL:via: tko: %.3f grw: %.3f ofs: %.3f LMT: %.3f\n", g_pointing_device_accel_config.takeoff,
g_pointing_device_accel_config.growth_rate, g_pointing_device_accel_config.offset,
g_pointing_device_accel_config.limit);
#endif
break;
}
case id_pointing_device_enabled:
{
g_pointing_device_accel_config.enabled = value_data[0];
break;
}
}
}
// Handle the data sent by the keyboard to the VIA menus
void pointing_device_config_get_value(uint8_t *data) {
// data = [ value_id, value_data ]
uint8_t *value_id = &(data[0]);
uint8_t *value_data = &(data[1]);
switch (*value_id) {
case id_pointing_device_takeoff:
{
// uint16_t takeoff = (g_pointing_device_accel_config.takeoff - 0.5) / (4.5 / 6) * 10000;
uint16_t takeoff =
PROJECT_TO_VIA(g_pointing_device_accel_config.takeoff, MACCEL_VIA_TKO_MIN, MACCEL_VIA_TKO_MAX);
value_data[0] = takeoff >> 8;
value_data[1] = takeoff & 0xFF;
break;
}
case id_pointing_device_growth_rate:
{
uint16_t growth_rate =
PROJECT_TO_VIA(g_pointing_device_accel_config.growth_rate, MACCEL_VIA_GRO_MIN, MACCEL_VIA_GRO_MAX);
value_data[0] = growth_rate >> 8;
value_data[1] = growth_rate & 0xFF;
break;
}
case id_pointing_device_offset:
{
uint16_t offset =
PROJECT_TO_VIA(g_pointing_device_accel_config.offset, MACCEL_VIA_OFS_MIN, MACCEL_VIA_OFS_MAX);
value_data[0] = offset >> 8;
value_data[1] = offset & 0xFF;
break;
}
case id_pointing_device_limit:
{
uint16_t limit =
PROJECT_TO_VIA(g_pointing_device_accel_config.limit, MACCEL_VIA_LMT_MIN, MACCEL_VIA_LMT_MAX);
value_data[0] = limit >> 8;
value_data[1] = limit & 0xFF;
break;
}
case id_pointing_device_enabled:
{
value_data[0] = g_pointing_device_accel_config.enabled;
break;
}
}
}
// Save the data to persistent memory after changes are made
__attribute__((weak)) void pointing_device_config_update(pointing_device_accel_config_t *config) {
via_update_custom_config(config, 0, sizeof(pointing_device_accel_config_t));
}
__attribute__((weak)) void pointing_device_config_read(pointing_device_accel_config_t *config) {
via_read_custom_config(&g_pointing_device_accel_config, 0, sizeof(g_pointing_device_accel_config));
}
__attribute__((weak, alias("via_custom_value_command_accel"))) void via_custom_value_command_kb(uint8_t *data, uint8_t length) {
uint8_t *command_id = &(data[0]);
uint8_t *channel_id = &(data[1]);
uint8_t *value_and_data = &(data[2]);
if (*channel_id == id_maccel) {
switch (*command_id) {
case id_custom_set_value:
pointing_device_config_set_value(value_and_data);
break;
case id_custom_get_value:
pointing_device_config_get_value(value_and_data);
break;
case id_custom_save:
pointing_device_config_update(&g_pointing_device_accel_config);
break;
default:
*command_id = id_unhandled;
break;
}
return;
}
*command_id = id_unhandled;
}