aboutsummaryrefslogtreecommitdiff
path: root/users/drashna/pimoroni_trackball.c
diff options
context:
space:
mode:
Diffstat (limited to 'users/drashna/pimoroni_trackball.c')
-rw-r--r--users/drashna/pimoroni_trackball.c57
1 files changed, 47 insertions, 10 deletions
diff --git a/users/drashna/pimoroni_trackball.c b/users/drashna/pimoroni_trackball.c
index c5bb560b3..9ae094c05 100644
--- a/users/drashna/pimoroni_trackball.c
+++ b/users/drashna/pimoroni_trackball.c
@@ -1,3 +1,19 @@
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
1#include "pimoroni_trackball.h" 17#include "pimoroni_trackball.h"
2#include "i2c_master.h" 18#include "i2c_master.h"
3 19
@@ -6,7 +22,7 @@ static int16_t x_offset = 0;
6static int16_t y_offset = 0; 22static int16_t y_offset = 0;
7static int16_t h_offset = 0; 23static int16_t h_offset = 0;
8static int16_t v_offset = 0; 24static int16_t v_offset = 0;
9static float precisionSpeed = 1; 25static float precisionSpeed = 1;
10 26
11#ifndef I2C_TIMEOUT 27#ifndef I2C_TIMEOUT
12# define I2C_TIMEOUT 100 28# define I2C_TIMEOUT 100
@@ -47,17 +63,37 @@ __attribute__((weak)) void trackball_check_click(bool pressed, report_mouse_t* m
47 } 63 }
48} 64}
49 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
50float trackball_get_precision(void) { return precisionSpeed; } 76float trackball_get_precision(void) { return precisionSpeed; }
51void trackball_set_precision(float precision) { precisionSpeed = precision; } 77void trackball_set_precision(float precision) { precisionSpeed = precision; }
52bool trackball_is_scrolling(void) { return scrolling; } 78bool trackball_is_scrolling(void) { return scrolling; }
53void trackball_set_scrolling(bool scroll) { scrolling = scroll; } 79void trackball_set_scrolling(bool scroll) { scrolling = scroll; }
54 80
55__attribute__((weak)) void pointing_device_init(void) { trackball_set_rgbw(0x00,0x00,0x00,0x4F); } 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); }
56 92
57void pointing_device_task(void) { 93void pointing_device_task(void) {
58 static bool debounce; 94 static bool debounce;
59 static uint16_t debounce_timer; 95 static uint16_t debounce_timer;
60 uint8_t state[5] = {}; 96 uint8_t state[5] = {};
61 if (i2c_readReg(TRACKBALL_WRITE, 0x04, state, 5, I2C_TIMEOUT) == I2C_STATUS_SUCCESS) { 97 if (i2c_readReg(TRACKBALL_WRITE, 0x04, state, 5, I2C_TIMEOUT) == I2C_STATUS_SUCCESS) {
62 if (!state[4] && !debounce) { 98 if (!state[4] && !debounce) {
63 if (scrolling) { 99 if (scrolling) {
@@ -85,7 +121,7 @@ void pointing_device_task(void) {
85 } 121 }
86 } else { 122 } else {
87 if (state[4]) { 123 if (state[4]) {
88 debounce = true; 124 debounce = true;
89 debounce_timer = timer_read(); 125 debounce_timer = timer_read();
90 } 126 }
91 } 127 }
@@ -97,7 +133,6 @@ void pointing_device_task(void) {
97 133
98 trackball_check_click(state[4] & (1 << 7), &mouse); 134 trackball_check_click(state[4] & (1 << 7), &mouse);
99 135
100
101#ifndef PIMORONI_TRACKBALL_ROTATE 136#ifndef PIMORONI_TRACKBALL_ROTATE
102 update_member(&mouse.x, &x_offset); 137 update_member(&mouse.x, &x_offset);
103 update_member(&mouse.y, &y_offset); 138 update_member(&mouse.y, &y_offset);
@@ -110,5 +145,7 @@ void pointing_device_task(void) {
110 update_member(&mouse.v, &h_offset); 145 update_member(&mouse.v, &h_offset);
111#endif 146#endif
112 pointing_device_set_report(mouse); 147 pointing_device_set_report(mouse);
113 pointing_device_send(); 148 if (has_report_changed(mouse, pointing_device_get_report())) {
149 pointing_device_send();
150 }
114} 151}