Skip to content

Commit

Permalink
Make the index based sensors both work via more user code.
Browse files Browse the repository at this point in the history
Add some timing stuff that was used to debug the 160us delay in
qmk#15682
  • Loading branch information
uqs committed Feb 7, 2022
1 parent 2ae78c0 commit 9549b77
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 23 deletions.
38 changes: 27 additions & 11 deletions drivers/sensors/pmw3360.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "pmw3360.h"
#include "wait.h"
#include "debug.h"
#include "timer.h"
#include "print.h"
#include PMW3360_FIRMWARE_H

Expand Down Expand Up @@ -93,6 +94,8 @@ static const pin_t pins[] = PMW3360_CS_PINS;
bool _inBurst[NUMBER_OF_SENSORS] = {0};

#ifdef CONSOLE_ENABLE
static fast_timer_t init_time;

void print_byte(uint8_t byte) { dprintf("%c%c%c%c%c%c%c%c|", (byte & 0x80 ? '1' : '0'), (byte & 0x40 ? '1' : '0'), (byte & 0x20 ? '1' : '0'), (byte & 0x10 ? '1' : '0'), (byte & 0x08 ? '1' : '0'), (byte & 0x04 ? '1' : '0'), (byte & 0x02 ? '1' : '0'), (byte & 0x01 ? '1' : '0')); }
#endif
#define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
Expand Down Expand Up @@ -142,20 +145,25 @@ uint8_t pmw3360_read(int8_t index, uint8_t reg_addr) {
}

bool pmw3360_init(int8_t index) {
static const int16_t rotation[] = POINTING_DEVICE_ROTATION_pwm3360;
_Static_assert(NUMBER_OF_SENSORS == sizeof(rotation)/sizeof(rotation[0]));

#ifdef CONSOLE_ENABLE
init_time = timer_read_fast();
#endif
spi_init();
//setPinOutput(pins[index]);

//spi_stop();
//pmw3360_spi_start(index);
//spi_stop();
#if 0
// with this on, init() takes 469ms, without we're down to 164ms
setPinOutput(pins[index]);

spi_stop();
pmw3360_spi_start(index);
spi_stop();

//pmw3360_write(index, REG_Shutdown, 0xb6); // Shutdown first
//wait_ms(300);
pmw3360_write(index, REG_Shutdown, 0xb6); // Shutdown first
wait_ms(300);
#endif

// power up, need to first drive NCS high then low.
// the datasheet does not say for how long, 40us works well in practice.
pmw3360_spi_start(index);
wait_us(40);
spi_stop();
Expand All @@ -179,6 +187,9 @@ bool pmw3360_init(int8_t index) {

wait_ms(1);

#ifdef POINTING_DEVICE_ROTATION_pwm3360
static const int16_t rotation[] = POINTING_DEVICE_ROTATION_pwm3360;
_Static_assert(NUMBER_OF_SENSORS == sizeof(rotation)/sizeof(rotation[0]));
// write the 90/180/270 degree angle to REG_Control
int8_t rot = 0;
switch (rotation[index]) {
Expand All @@ -193,6 +204,7 @@ bool pmw3360_init(int8_t index) {
break;
}
pmw3360_write(index, REG_Control, rot);
#endif

// XXX data sheet only shows from -30 deg to 30deg, using values 0xe2 for -30, 0x00 for 0, 0x1e for +30
pmw3360_write(index, REG_Angle_Tune, constrain(ROTATIONAL_TRANSFORM_ANGLE, -127, 127));
Expand All @@ -208,6 +220,7 @@ bool pmw3360_init(int8_t index) {
} else {
dprintf("pmw3360 signature(s) verification failed!");
}
init_time = timer_elapsed_fast(init_time);
#endif

return init_success;
Expand Down Expand Up @@ -254,8 +267,8 @@ uint16_t pmw3360_get_cpi(void) {
if (cpival != othercpival) {
dprintf("pmw3360 cpivals differ: %d vs %d", cpival, othercpival);
}
#endif
}
#endif
return (uint16_t)((cpival + 1) & 0xFF) * CPI_STEP;
}

Expand Down Expand Up @@ -293,7 +306,8 @@ report_pmw3360_t pmw3360_read_burst(int8_t index) {

if (!_inBurst[index]) {
#ifdef CONSOLE_ENABLE
dprintf("burst on");
//dprintf("burst on\n");
dprintf("pmw3360 init took: %d\n", init_time);
#endif
pmw3360_write(index, REG_Motion_Burst, 0x00);
_inBurst[index] = true;
Expand All @@ -315,13 +329,15 @@ report_pmw3360_t pmw3360_read_burst(int8_t index) {

#ifdef CONSOLE_ENABLE
if (debug_mouse) {
/*
dprintf("sensor %d: ", index);
print_byte(motion);
print_byte(delta_x_l);
print_byte(delta_x_h);
print_byte(delta_y_l);
print_byte(delta_y_h);
dprintf("\n");
*/
}
#endif

Expand Down
55 changes: 43 additions & 12 deletions keyboards/handwired/daqtyl/daqtyl.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,54 @@
*/

#include "daqtyl.h"
#include "pointing_device.h"
#include "drivers/sensors/pmw3360.h"

#ifdef POINTING_DEVICE_ENABLE
void pointing_device_init_kb(void) {
//pointing_device_init_user();
pointing_device_set_cpi(1000);
debug_enable = true;
debug_matrix = true;
debug_mouse = true;
//debug_encoder = true;
pmw3360_init(1);
pointing_device_set_cpi(800);

pointing_device_init_user();
}

void keyboard_post_init_user(void) {
// Customise these values to desired behaviour
debug_enable=true;
debug_matrix=true;
//debug_keyboard=true;
debug_mouse=true;
// Set TCNT3 to count ticks of 4us each.
TCCR3A = 0; // TCCR0A register set to 0
TCCR3B = 0; // same with registreb B
TCNT3 = 0; // counter value to 0
// set prescaler to 64, should be 4us per tick then ...
TCCR3B |=(1<<CS31)|(1<<CS30);
}

//report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
// if (is_keyboard_master()) {
// mouse_report = pointing_device_task_user(mouse_report);
// }
// return mouse_report;
//}
static int16_t signed_sat_add16(int16_t a, int16_t b) {
int16_t res;
bool overflow = __builtin_add_overflow(a, b, &res);
if (overflow) {
// if b was positive, this will return INT16_MAX, if b was negative,
// it'll slap its negative sign bit onto INT16_MAX, returning
// INT16_MIN.
return ((uint16_t)b >> 15) + INT16_MAX;
}
return res;
}

report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
report_pmw3360_t data = pmw3360_read_burst(1);
if (data.isOnSurface && data.isMotion) {
// From quantum/pointing_device_drivers.c
#define constrain_hid(amt) ((amt) < -127 ? -127 : ((amt) > 127 ? 127 : (amt)))
mouse_report.x = constrain_hid(signed_sat_add16(mouse_report.x, data.dx));
mouse_report.y = constrain_hid(signed_sat_add16(mouse_report.y, data.dy));
}

return pointing_device_task_user(mouse_report);
}
#endif

//#ifdef POINTING_DEVICE_ENABLE
Expand Down
13 changes: 13 additions & 0 deletions keyboards/handwired/daqtyl/keymaps/default/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,23 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
};

bool encoder_update_user(uint8_t index, bool clockwise) {
static const uint16_t us_per_tick = 64 / (F_CPU/1000000);
if (index == 0) { /* First encoder */
if (clockwise) {
#ifdef CONSOLE_ENABLE
uint16_t tcnt1 = TCNT3;
pointing_device_set_cpi(100);
uint16_t tcnt2 = TCNT3;
dprintf("set_cpi took: %u ticks which is %u us\n", tcnt2-tcnt1, us_per_tick*(tcnt2-tcnt1));
#endif
tap_code(KC_P);
} else {
#ifdef CONSOLE_ENABLE
uint16_t tcnt1 = TCNT3;
uint16_t cpi = pointing_device_get_cpi();
uint16_t tcnt2 = TCNT3;
dprintf("get_cpi took: %u ticks which is %u us: cpi is %u\n", tcnt2-tcnt1, us_per_tick*(tcnt2-tcnt1), cpi);
#endif
tap_code(KC_U);
}
} else if (index == 1) { /* Second encoder */
Expand Down

0 comments on commit 9549b77

Please sign in to comment.