aboutsummaryrefslogtreecommitdiff
path: root/users
diff options
context:
space:
mode:
authorDrashna Jaelre <drashna@live.com>2021-06-21 13:00:11 -0700
committerGitHub <noreply@github.com>2021-06-21 21:00:11 +0100
commit6901411bca0760ceb8acbe1f0c95feaed9d2aaeb (patch)
treefdcc973f99e6dab858455f6eebd380a12ecc8675 /users
parentb491c7994aa1931fd000cc93a1bf70453d462c40 (diff)
downloadqmk_firmware-6901411bca0760ceb8acbe1f0c95feaed9d2aaeb.tar.gz
qmk_firmware-6901411bca0760ceb8acbe1f0c95feaed9d2aaeb.zip
Move optical sensor code to drivers folder (#13044)
Diffstat (limited to 'users')
-rw-r--r--users/drashna/drashna.h2
-rw-r--r--users/drashna/pimoroni_trackball.c151
-rw-r--r--users/drashna/pimoroni_trackball.h35
-rw-r--r--users/drashna/rules.mk2
4 files changed, 2 insertions, 188 deletions
diff --git a/users/drashna/drashna.h b/users/drashna/drashna.h
index e66f10657..e37c73bb2 100644
--- a/users/drashna/drashna.h
+++ b/users/drashna/drashna.h
@@ -33,7 +33,7 @@
33# include "oled_stuff.h" 33# include "oled_stuff.h"
34#endif 34#endif
35#if defined(PIMORONI_TRACKBALL_ENABLE) 35#if defined(PIMORONI_TRACKBALL_ENABLE)
36# include "pimoroni_trackball.h" 36# include "drivers/sensors/pimoroni_trackball.h"
37#endif 37#endif
38 38
39/* Define layer names */ 39/* Define layer names */
diff --git a/users/drashna/pimoroni_trackball.c b/users/drashna/pimoroni_trackball.c
deleted file mode 100644
index a6ca6c996..000000000
--- a/users/drashna/pimoroni_trackball.c
+++ /dev/null
@@ -1,151 +0,0 @@
1/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "pimoroni_trackball.h"
18#include "i2c_master.h"
19
20static uint8_t scrolling = 0;
21static int16_t x_offset = 0;
22static int16_t y_offset = 0;
23static int16_t h_offset = 0;
24static int16_t v_offset = 0;
25static float precisionSpeed = 1;
26
27#ifndef I2C_TIMEOUT
28# define I2C_TIMEOUT 100
29#endif
30#ifndef MOUSE_DEBOUNCE
31# define MOUSE_DEBOUNCE 5
32#endif
33
34void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
35 uint8_t data[] = {0x00, red, green, blue, white};
36 i2c_transmit(TRACKBALL_ADDRESS << 1, data, sizeof(data), I2C_TIMEOUT);
37}
38
39int16_t mouse_offset(uint8_t positive, uint8_t negative, int16_t scale) {
40 int16_t offset = (int16_t)positive - (int16_t)negative;
41 int16_t magnitude = (int16_t)(scale * offset * offset * precisionSpeed);
42 return offset < 0 ? -magnitude : magnitude;
43}
44
45void update_member(int8_t* member, int16_t* offset) {
46 if (*offset > 127) {
47 *member = 127;
48 *offset -= 127;
49 } else if (*offset < -127) {
50 *member = -127;
51 *offset += 127;
52 } else {
53 *member = *offset;
54 *offset = 0;
55 }
56}
57
58__attribute__((weak)) void trackball_check_click(bool pressed, report_mouse_t* mouse) {
59 if (pressed) {
60 mouse->buttons |= MOUSE_BTN1;
61 } else {
62 mouse->buttons &= ~MOUSE_BTN1;
63 }
64}
65
66void trackball_register_button(bool pressed, enum mouse_buttons button) {
67 report_mouse_t currentReport = pointing_device_get_report();
68 if (pressed) {
69 currentReport.buttons |= button;
70 } else {
71 currentReport.buttons &= ~button;
72 }
73 pointing_device_set_report(currentReport);
74}
75
76float trackball_get_precision(void) { return precisionSpeed; }
77void trackball_set_precision(float precision) { precisionSpeed = precision; }
78bool trackball_is_scrolling(void) { return scrolling; }
79void trackball_set_scrolling(bool scroll) { scrolling = scroll; }
80
81bool has_report_changed (report_mouse_t first, report_mouse_t second) {
82 return !(
83 (!first.buttons && first.buttons == second.buttons) &&
84 (!first.x && first.x == second.x) &&
85 (!first.y && first.y == second.y) &&
86 (!first.h && first.h == second.h) &&
87 (!first.v && first.v == second.v) );
88}
89
90
91__attribute__((weak)) void pointing_device_init(void) { trackball_set_rgbw(0x00, 0x00, 0x00, 0x4F); }
92
93void pointing_device_task(void) {
94 static bool debounce;
95 static uint16_t debounce_timer;
96 uint8_t state[5] = {};
97 if (i2c_readReg(TRACKBALL_ADDRESS << 1, 0x04, state, 5, I2C_TIMEOUT) == I2C_STATUS_SUCCESS) {
98 if (!state[4] && !debounce) {
99 if (scrolling) {
100#ifdef PIMORONI_TRACKBALL_INVERT_X
101 h_offset += mouse_offset(state[2], state[3], 1);
102#else
103 h_offset -= mouse_offset(state[2], state[3], 1);
104#endif
105#ifdef PIMORONI_TRACKBALL_INVERT_Y
106 v_offset += mouse_offset(state[1], state[0], 1);
107#else
108 v_offset -= mouse_offset(state[1], state[0], 1);
109#endif
110 } else {
111#ifdef PIMORONI_TRACKBALL_INVERT_X
112 x_offset -= mouse_offset(state[2], state[3], 5);
113#else
114 x_offset += mouse_offset(state[2], state[3], 5);
115#endif
116#ifdef PIMORONI_TRACKBALL_INVERT_Y
117 y_offset -= mouse_offset(state[1], state[0], 5);
118#else
119 y_offset += mouse_offset(state[1], state[0], 5);
120#endif
121 }
122 } else {
123 if (state[4]) {
124 debounce = true;
125 debounce_timer = timer_read();
126 }
127 }
128 }
129
130 if (timer_elapsed(debounce_timer) > MOUSE_DEBOUNCE) debounce = false;
131
132 report_mouse_t mouse = pointing_device_get_report();
133
134 trackball_check_click(state[4] & (1 << 7), &mouse);
135
136#ifndef PIMORONI_TRACKBALL_ROTATE
137 update_member(&mouse.x, &x_offset);
138 update_member(&mouse.y, &y_offset);
139 update_member(&mouse.h, &h_offset);
140 update_member(&mouse.v, &v_offset);
141#else
142 update_member(&mouse.x, &y_offset);
143 update_member(&mouse.y, &x_offset);
144 update_member(&mouse.h, &v_offset);
145 update_member(&mouse.v, &h_offset);
146#endif
147 pointing_device_set_report(mouse);
148 if (has_report_changed(mouse, pointing_device_get_report())) {
149 pointing_device_send();
150 }
151}
diff --git a/users/drashna/pimoroni_trackball.h b/users/drashna/pimoroni_trackball.h
deleted file mode 100644
index a30fb0bb8..000000000
--- a/users/drashna/pimoroni_trackball.h
+++ /dev/null
@@ -1,35 +0,0 @@
1/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#pragma once
18
19#include "quantum.h"
20#include "pointing_device.h"
21
22#ifndef TRACKBALL_ADDRESS
23# define TRACKBALL_ADDRESS 0x0A
24#endif
25#define TRACKBALL_WRITE ((TRACKBALL_ADDRESS << 1) | I2C_WRITE)
26#define TRACKBALL_READ ((TRACKBALL_ADDRESS << 1) | I2C_READ)
27
28void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white);
29void trackball_check_click(bool pressed, report_mouse_t *mouse);
30void trackball_register_button(bool pressed, enum mouse_buttons button);
31
32float trackball_get_precision(void);
33void trackball_set_precision(float precision);
34bool trackball_is_scrolling(void);
35void trackball_set_scrolling(bool scroll);
diff --git a/users/drashna/rules.mk b/users/drashna/rules.mk
index b79051508..fa4fb2420 100644
--- a/users/drashna/rules.mk
+++ b/users/drashna/rules.mk
@@ -74,7 +74,7 @@ endif
74ifeq ($(strip $(PIMORONI_TRACKBALL_ENABLE)), yes) 74ifeq ($(strip $(PIMORONI_TRACKBALL_ENABLE)), yes)
75 POINTING_DEVICE_ENABLE := yes 75 POINTING_DEVICE_ENABLE := yes
76 OPT_DEFS += -DPIMORONI_TRACKBALL_ENABLE 76 OPT_DEFS += -DPIMORONI_TRACKBALL_ENABLE
77 SRC += pimoroni_trackball.c 77 SRC += drivers/sensors/pimoroni_trackball.c
78 QUANTUM_LIB_SRC += i2c_master.c 78 QUANTUM_LIB_SRC += i2c_master.c
79endif 79endif
80 80