using out of the box trackball code now

This commit is contained in:
Sadek Baroudi 2021-11-01 01:58:41 -07:00
parent d9cf8e9918
commit 921540d45e
5 changed files with 51 additions and 398 deletions

View File

@ -151,7 +151,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
/* Locking resynchronize hack */
#define LOCKING_RESYNC_ENABLE
#define TRACKBALL_ORIENTATION 2
#define PIMORONI_TRACKBALL_ROTATE
#define PIMORONI_TRACKBALL_CLICK
#define PIMORONI_TRACKBALL_INVERT_X
// If using encoder type 2, uncomment this
// #define ENCODERS_B_REVERSE

View File

@ -15,6 +15,11 @@
*/
#include "sadekbaroudi.h"
#ifdef PIMORONI_TRACKBALL_ENABLE
#include "drivers/sensors/pimoroni_trackball.h"
#include "pointing_device.h"
#include "color.h"
#endif
#include QMK_KEYBOARD_H
/*
@ -228,3 +233,44 @@ void oled_task_user(void) {
}
#endif
#if !defined(RGBLIGHT_ENABLE) && defined(PIMORONI_TRACKBALL_ENABLE)
layer_state_t layer_state_set_keymap(layer_state_t state) {
switch (get_highest_layer(state)) {
case _QWERTY:
if (is_caps_lock_on) {
trackball_set_rgbw(RGB_RED, 0x00);
} else {
trackball_set_rgbw(RGB_BLUE, 0x00);
}
break;
case _NAVIGATION:
trackball_set_rgbw(RGB_GREEN, 0x00);
break;
case _SYMBOLS:
trackball_set_rgbw(RGB_PURPLE, 0x00);
break;
case _FUNCTION:
trackball_set_rgbw(RGB_YELLOW, 0x00);
break;
case _MEDIA:
trackball_set_rgbw(RGB_ORANGE, 0x00);
break;
case _MOUSE:
trackball_set_rgbw(RGB_CYAN, 0x00);
break;
case _WINNAV:
trackball_set_rgbw(RGB_WHITE, 0x00);
break;
default: // for any other layers, or the default layer
if (is_caps_lock_on) {
trackball_set_rgbw(RGB_RED, 0x00);
} else {
trackball_set_rgbw(RGB_BLUE, 0x00);
}
break;
}
return state;
}
#endif

View File

@ -1,329 +0,0 @@
#include "pimoroni_trackball.h"
#include "i2c_master.h"
#include "i2c_master.c"
#include "pointing_device.h"
#include "sadekbaroudi.h"
#ifndef TRACKBALL_NO_MATH
#include "math.h"
# ifndef TRACKBALL_ANGLE_OFFSET
# define TRACKBALL_ANGLE_OFFSET 0
# endif
#endif
#undef TRACKBALL_REVERSE_VSCROLL
#undef TRACKBALL_REVERSE_HSCROLL
#ifndef TRACKBALL_ORIENTATION
# define TRACKBALL_ORIENTATION 0
#endif
#ifndef TRACKBALL_REVERSE_VSCROLL
# define TRACKBALL_REVERSE_VSCROLL false
#endif
#ifndef TRACKBALL_REVERSE_HSCROLL
# define TRACKBALL_REVERSE_HSCROLL false
#endif
#ifndef TRACKBALL_ACCELERATION_WINDOW
# define TRACKBALL_ACCELERATION_WINDOW 50000 // ms window to increase acceleration factor
#endif
// Sadek: I commented this line out, as I don't have the interrupt pin connected
//#define TRACKBALL_INTERRUPT_PIN D3
#define TRACKBALL_TIMEOUT 5
bool scrolling = false;
bool trackball_idle = true;
uint8_t tb_brightness = 42;
void trackball_init(void) {
i2c_init();
#ifdef TRACKBALL_INTERRUPT_PIN
setPinInput(TRACKBALL_INTERRUPT_PIN);
writePinLow(TRACKBALL_INTERRUPT_PIN);
uint8_t data[] = {REG_INTERRUPT_PIN, MASK_INTERRUPT_PIN_ENABLE};
i2c_transmit(TRACKBALL_WRITE, data, 2, TB_I2C_TIMEOUT);
#endif
}
bool trackball_get_interrupt(void) {
#ifndef TRACKBALL_INTERRUPT_PIN
uint8_t data[1] = {};
i2c_readReg(TRACKBALL_WRITE, REG_INTERRUPT_PIN, data, 1, TB_I2C_TIMEOUT);
return data[0] & MASK_INTERRUPT_TRIGGERED;
#else
return !readPin(TRACKBALL_INTERRUPT_PIN);
#endif
}
void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
uint8_t data[] = {0x00, red, green, blue, white};
i2c_transmit(TRACKBALL_WRITE, data, 5, TB_I2C_TIMEOUT);
}
void trackball_read_state(uint8_t* data, uint16_t size_of_data) {
i2c_readReg(TRACKBALL_WRITE, REG_LEFT, data, size_of_data, TB_I2C_TIMEOUT);
}
void trackball_set_scrolling(bool scroll) {
scrolling = scroll;
}
trackball_state_t trackball_get_state(void) {
// up down left right button
uint8_t s[5] = {};
trackball_read_state(s, 5);
trackball_state_t state = {
#if TRACKBALL_ORIENTATION == 0
// Pimoroni text is up
.y = s[0] - s[1],
.x = s[3] - s[2],
#elif TRACKBALL_ORIENTATION == 1
// Pimoroni text is right
.y = s[3] - s[2],
.x = s[1] - s[0],
#elif TRACKBALL_ORIENTATION == 2
// Pimoroni text is down
.y = s[1] - s[0],
.x = s[2] - s[3],
#else
// Pimoroni text is left
.y = s[2] - s[3],
.x = s[0] - s[1],
#endif
.button_down = s[4] & 0x80,
.button_triggered = s[4] & 0x01,
};
#ifndef TRACKBALL_NO_MATH
state.angle_rad = atan2(state.y, state.x) + TRACKBALL_ANGLE_OFFSET;
state.vector_length = sqrt(pow(state.x, 2) + pow(state.y, 2));
state.raw_x = state.x;
state.raw_y = state.y;
state.x = (int16_t)(state.vector_length * cos(state.angle_rad));
state.y = (int16_t)(state.vector_length * sin(state.angle_rad));
#endif
return state;
}
void trackball_sleep(void) {
/* not sure how this is supposed to work */
uint8_t data[] = {REG_CTRL, MSK_CTRL_FWRITE | MSK_CTRL_SLEEP};
i2c_transmit(TRACKBALL_WRITE, data, 2, TB_I2C_TIMEOUT);
}
void trackball_set_brightness(uint8_t brightness) {
uint8_t data[4] = {};
i2c_readReg(TRACKBALL_WRITE, REG_RED, data, 4, TB_I2C_TIMEOUT);
for (int i=0; i<4; i++) {
if (data[i]) {
data[i] = brightness;
}
}
i2c_writeReg(TRACKBALL_WRITE, REG_RED, data, 4, TB_I2C_TIMEOUT);
}
#ifndef MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
void trackball_set_hsv(uint8_t hue, uint8_t sat, uint8_t brightness) {
RGB rgb = hsv_to_rgb((HSV){hue, sat, brightness});
uint8_t white = MIN(rgb.r, MIN(rgb.g, rgb.b));
rgb.r -= white;
rgb.g -= white;
rgb.b -= white;
trackball_set_rgbw(rgb.r, rgb.g, rgb.b, white);
}
__attribute__((weak)) void pointing_device_init(void) {
trackball_init();
trackball_set_rgbw(0,0,tb_brightness,0);
}
__attribute__((weak)) void process_mouse_user(report_mouse_t* mouse_report, int16_t x, int16_t y, int16_t h, int16_t v) {
mouse_report->x = x;
mouse_report->y = y;
mouse_report->h = h;
mouse_report->v = v;
}
__attribute__((weak)) void update_member(int8_t* member, int16_t* offset) {
if (*offset > 127) {
*member = 127;
*offset -= 127;
} else if (*offset < -127) {
*member = -127;
*offset += 127;
} else {
*member = *offset;
*offset = 0;
}
}
__attribute__((weak)) bool has_report_changed(report_mouse_t new, report_mouse_t old) {
return (new.buttons != old.buttons) ||
(new.x && new.x != old.x) ||
(new.y && new.y != old.y) ||
(new.h && new.h != old.h) ||
(new.v && new.v != old.v);
}
static int16_t x_offset = 0;
static int16_t y_offset = 0;
static int16_t v_offset = 0;
static int16_t h_offset = 0;
static int16_t tb_timer = 0;
uint16_t acceleration_timer = 0;
__attribute__((weak)) void process_mouse(report_mouse_t* mouse, bool fast_scroll) {
static int8_t new_x_offset = 0;
static int8_t new_y_offset = 0;
static int8_t new_v_offset = 0;
static int8_t new_h_offset = 0;
if (trackball_get_interrupt() && (!tb_timer || timer_elapsed(tb_timer) > TRACKBALL_TIMEOUT)) {
tb_timer = timer_read() | 1;
trackball_state_t state = trackball_get_state();
if (state.button_triggered) {
if(state.button_down) {
mouse->buttons |= MOUSE_BTN1;
} else {
mouse->buttons &= ~MOUSE_BTN1;
}
} else {
//--------------------------------------------------------------
//DONT WANT TO MOVE THESE BUT HERE THEY ARE
//--------------------------------------------------------------
float power = 2.5;
float var_accel = 1;
if (fast_scroll) {
var_accel = 2;
power = 3;
}
double newlen = pow(state.vector_length*var_accel, power);
//float var_accel = 2; //acceleration factor
//double newlen = pow(state.vector_length, power);
/*
if (state.vector_length > 3 && (timer_elapsed(acceleration_timer) == 0 || timer_elapsed(acceleration_timer) < TRACKBALL_ACCELERATION_WINDOW)) {
acceleration_timer = timer_read();
newlen += pow(state.vector_length*var_accel, power);
} else {
acceleration_timer = timer_read();
newlen += pow(state.vector_length, power);
}
*/
//newlen = pow(state.vector_length, power);
x_offset += (newlen * cos(state.angle_rad));
y_offset += (newlen * sin(state.angle_rad));
/*
float y_correction = 0.8;
if ( y_offset > 0 && ((newlen * sin(state.angle_rad) * y_correction == 0)) {
y_offset = 0.2;
}
else {
y_offset *= y_correction;
}
*/
#if TRACKBALL_REVERSE_VSCROLL == true
v_offset += (newlen * sin(state.angle_rad));
#else
v_offset -= (newlen * sin(state.angle_rad));
#endif
#if TRACKBALL_REVERSE_HSCROLL == true
h_offset -= (newlen * cos(state.angle_rad));
#else
h_offset += (newlen * cos(state.angle_rad));
#endif
}
}
while (x_offset || y_offset || h_offset || v_offset) {
update_member(&new_x_offset, &x_offset);
update_member(&new_y_offset, &y_offset);
update_member(&new_v_offset, &v_offset);
update_member(&new_h_offset, &h_offset);
mouse->x = new_x_offset;
mouse->y = new_y_offset;
mouse->v = new_v_offset;
mouse->h = new_h_offset;
}
}
__attribute__((weak)) void pointing_device_task(void) {
report_mouse_t mouse_report = pointing_device_get_report();
// look into whether or not I'd want this
//bool fast_scroll = (get_highest_layer(layer_state) == _SYMBOLS);
bool fast_scroll = false;
process_mouse(&mouse_report, fast_scroll);
// Logic for colors is in rgb_stuff.c
// Note that for now, if RGBLIGHT_ENABLE is not set, this won't run, so they are tied together, I can fix later
// If I ever want to use scrolling, set the rules here
if (layer_state_is(_NAVIGATION)) {
trackball_set_scrolling(true);
} else {
trackball_set_scrolling(false);
}
pointing_device_set_report(mouse_report);
pointing_device_send();
}
__attribute__((weak)) void pointing_device_send(void) {
static report_mouse_t old_report = {};
report_mouse_t mouseReport = pointing_device_get_report();
if (is_keyboard_master()) {
int8_t x = mouseReport.x, y = mouseReport.y, h = mouseReport.h, v = mouseReport.v;
mouseReport.x = 0;
mouseReport.y = 0;
mouseReport.h = 0;
mouseReport.v = 0;
if (!scrolling) {
process_mouse_user(&mouseReport, x, y, 0, 0);
} else {
process_mouse_user(&mouseReport, 0, 0, h, v);
}
if (has_report_changed(mouseReport, old_report)) {
trackball_idle = false;
host_mouse_send(&mouseReport);
} else {
trackball_idle = true;
}
} else {
if (has_report_changed(mouseReport, old_report)) {
trackball_idle = false;
} else {
trackball_idle = true;
}
}
mouseReport.x = 0;
mouseReport.y = 0;
mouseReport.v = 0;
mouseReport.h = 0;
old_report = mouseReport;
pointing_device_set_report(mouseReport);
}

View File

@ -1,67 +0,0 @@
#pragma once
#include <quantum.h>
#include "color.h"
#ifndef TRACKBALL_ADDRESS
# define TRACKBALL_ADDRESS 0x0A
#endif
#ifndef TRACKBALL_ANGLE_OFFSET
# define TRACKBALL_ANGLE_OFFSET 0
#endif
#define TRACKBALL_WRITE ((TRACKBALL_ADDRESS << 1) | I2C_WRITE)
#define TRACKBALL_READ ((TRACKBALL_ADDRESS << 1) | I2C_READ)
#define TB_I2C_TIMEOUT 100
#define REG_RED 0x00
#define REG_GREEN 0x01
#define REG_BLUE 0x02
#define REG_WHITE 0x03
#define REG_LEFT 0x04
#define REG_INTERRUPT_PIN 0xF9
#define MASK_INTERRUPT_TRIGGERED 0x01
#define MASK_INTERRUPT_PIN_ENABLE 0x02
#define REG_CTRL 0xFE
#define MSK_CTRL_SLEEP 0b00000001
#define MSK_CTRL_RESET 0b00000010
#define MSK_CTRL_FREAD 0b00000100
#define MSK_CTRL_FWRITE 0b00001000
typedef struct {
int16_t x;
int16_t y;
bool button_down;
bool button_triggered;
#ifndef TRACKBALL_NO_MATH
double vector_length;
double angle_rad;
int8_t raw_x;
int8_t raw_y;
#endif
} trackball_state_t;
void trackball_init(void);
bool trackball_get_interrupt(void);
void trackball_set_rgbw(uint8_t r, uint8_t g, uint8_t b, uint8_t w);
void trackball_read_state(uint8_t* data, uint16_t size_of_data);
void trackball_sleep(void);
void trackball_set_brightness(uint8_t brightness);
void trackball_set_hsv(uint8_t hue, uint8_t sat, uint8_t brightness);
void trackball_set_scrolling (bool scroll);
trackball_state_t trackball_get_state(void);
void pointing_device_init(void);
void process_mouse_user(report_mouse_t* mouse_report, int16_t x, int16_t y, int16_t h, int16_t v);
void update_member(int8_t* member, int16_t* offset);
bool has_report_changed(report_mouse_t new, report_mouse_t old);
void process_mouse(report_mouse_t* mouse, bool fast_scroll);
void pointing_device_task(void);
void pointing_device_send(void);

View File

@ -69,7 +69,8 @@ endif
ifeq ($(strip $(PIMORONI_TRACKBALL_ENABLE)), yes)
POINTING_DEVICE_ENABLE := yes
SRC += pimoroni_trackball.c
SRC += drivers/sensors/pimoroni_trackball.c
QUANTUM_LIB_SRC += i2c_master.c
OPT_DEFS += -DPIMORONI_TRACKBALL_ENABLE
endif