Skip to content

Commit 12eb644

Browse files
drashnagompaKarlK90
authored
Add support for PAW3204 Optical Sensor (#17669)
Co-authored-by: gompa <gompa@h-bomb.nl> Co-authored-by: Stefan Kerkmann <karlk90@pm.me>
1 parent 6992efb commit 12eb644

File tree

6 files changed

+282
-1
lines changed

6 files changed

+282
-1
lines changed

builddefs/common_features.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ ifeq ($(strip $(MOUSEKEY_ENABLE)), yes)
126126
SRC += $(QUANTUM_DIR)/mousekey.c
127127
endif
128128

129-
VALID_POINTING_DEVICE_DRIVER_TYPES := adns5050 adns9800 analog_joystick cirque_pinnacle_i2c cirque_pinnacle_spi pmw3360 pmw3389 pimoroni_trackball custom
129+
VALID_POINTING_DEVICE_DRIVER_TYPES := adns5050 adns9800 analog_joystick cirque_pinnacle_i2c cirque_pinnacle_spi paw3204 pmw3360 pmw3389 pimoroni_trackball custom
130130
ifeq ($(strip $(POINTING_DEVICE_ENABLE)), yes)
131131
ifeq ($(filter $(POINTING_DEVICE_DRIVER),$(VALID_POINTING_DEVICE_DRIVER_TYPES)),)
132132
$(call CATASTROPHIC_ERROR,Invalid POINTING_DEVICE_DRIVER,POINTING_DEVICE_DRIVER="$(POINTING_DEVICE_DRIVER)" is not a valid pointing device type)

docs/feature_pointing_device.md

+18
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ Also see the `POINTING_DEVICE_TASK_THROTTLE_MS`, which defaults to 10ms when usi
136136

137137
**`POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE`** is not specific to Cirque trackpad; any pointing device with a lift/contact status can integrate this gesture into its driver. e.g. PMW3360 can use Lift_Stat from Motion register. Note that `POINTING_DEVICE_MOTION_PIN` cannot be used with this feature; continuous polling of `pointing_device_get_report()` is needed to generate glide reports.
138138

139+
### PAW 3204 Sensor
140+
141+
To use the paw 3204 sensor, add this to your `rules.mk`
142+
143+
```make
144+
POINTING_DEVICE_DRIVER = paw3204
145+
```
146+
147+
The paw 3204 sensor uses a serial type protocol for communication, and requires an additional light source.
148+
149+
| Setting | Description |
150+
|--------------------|---------------------------------------------------------------------|
151+
|`PAW3204_SCLK_PIN` | (Required) The pin connected to the clock pin of the sensor. |
152+
|`PAW3204_SDIO_PIN` | (Required) The pin connected to the data pin of the sensor. |
153+
154+
The CPI range is 400-1600, with supported values of (400, 500, 600, 800, 1000, 1200 and 1600). Defaults to 1000 CPI.
155+
156+
139157
### Pimoroni Trackball
140158

141159
To use the Pimoroni Trackball module, add this to your `rules.mk`:

drivers/sensors/paw3204.c

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/* Copyright 2021 Gompa (@Gompa)
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+
// https://github.com/shinoaliceKabocha/choco60_track/tree/master/keymaps/default
18+
19+
#include "paw3204.h"
20+
#include "wait.h"
21+
#include "debug.h"
22+
#include "gpio.h"
23+
24+
#define REG_PID1 0x00
25+
#define REG_PID2 0x01
26+
#define REG_STAT 0x02
27+
#define REG_X 0x03
28+
#define REG_Y 0x04
29+
30+
#define REG_SETUP 0x06
31+
#define REG_IMGQUAL 0x07
32+
#define REG_IMGREC 0x0E
33+
#define REG_IMGTRASH 0x0D
34+
35+
#define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
36+
37+
// CPI values
38+
enum cpi_values {
39+
CPI400, // 0b000
40+
CPI500, // 0b001
41+
CPI600, // 0b010
42+
CPI800, // 0b011
43+
CPI1000, // 0b100
44+
CPI1200, // 0b101
45+
CPI1600, // 0b110
46+
};
47+
48+
uint8_t paw3204_serial_read(void);
49+
void paw3204_serial_write(uint8_t reg_addr);
50+
uint8_t paw3204_read_reg(uint8_t reg_addr);
51+
void paw3204_write_reg(uint8_t reg_addr, uint8_t data);
52+
53+
void paw3204_init(void) {
54+
setPinOutput(PAW3204_SCLK_PIN); // setclockpin to output
55+
setPinInputHigh(PAW3204_SDIO_PIN); // set datapin input high
56+
57+
paw3204_write_reg(REG_SETUP, 0x86); // reset sensor and set 1600cpi
58+
wait_us(5);
59+
60+
paw3204_read_reg(0x00); // read id
61+
paw3204_read_reg(0x01); // read id2
62+
// PAW3204_write_reg(REG_SETUP,0x06); // dont reset sensor and set cpi 1600
63+
paw3204_write_reg(REG_IMGTRASH, 0x32); // write image trashhold
64+
}
65+
66+
uint8_t paw3204_serial_read(void) {
67+
setPinInput(PAW3204_SDIO_PIN);
68+
uint8_t byte = 0;
69+
70+
for (uint8_t i = 0; i < 8; ++i) {
71+
writePinLow(PAW3204_SCLK_PIN);
72+
wait_us(1);
73+
74+
byte = (byte << 1) | readPin(PAW3204_SDIO_PIN);
75+
76+
writePinHigh(PAW3204_SCLK_PIN);
77+
wait_us(1);
78+
}
79+
80+
return byte;
81+
}
82+
83+
void paw3204_serial_write(uint8_t data) {
84+
writePinLow(PAW3204_SDIO_PIN);
85+
setPinOutput(PAW3204_SDIO_PIN);
86+
87+
for (int8_t b = 7; b >= 0; b--) {
88+
writePinLow(PAW3204_SCLK_PIN);
89+
if (data & (1 << b)) {
90+
writePinHigh(PAW3204_SDIO_PIN);
91+
} else {
92+
writePinLow(PAW3204_SDIO_PIN);
93+
}
94+
writePinHigh(PAW3204_SCLK_PIN);
95+
}
96+
97+
wait_us(4);
98+
}
99+
100+
report_paw3204_t paw3204_read(void) {
101+
report_paw3204_t data = {0};
102+
103+
data.isMotion = paw3204_read_reg(REG_STAT) & (1 << 7); // check for motion only (bit 7 in field)
104+
data.x = (int8_t)paw3204_read_reg(REG_X);
105+
data.y = (int8_t)paw3204_read_reg(REG_Y);
106+
107+
return data;
108+
}
109+
110+
void paw3204_write_reg(uint8_t reg_addr, uint8_t data) {
111+
paw3204_serial_write(0b10000000 | reg_addr);
112+
paw3204_serial_write(data);
113+
}
114+
115+
uint8_t paw3204_read_reg(uint8_t reg_addr) {
116+
paw3204_serial_write(reg_addr);
117+
wait_us(5);
118+
return paw3204_serial_read();
119+
}
120+
121+
void paw3204_set_cpi(uint16_t cpi) {
122+
uint8_t cpival = CPI1000;
123+
if (cpi <= 450) {
124+
cpival = CPI400;
125+
} else if (cpi <= 550) {
126+
cpival = CPI500;
127+
} else if (cpi <= 700) {
128+
cpival = CPI600;
129+
} else if (cpi <= 900) {
130+
cpival = CPI800;
131+
} else if (cpi <= 1100) {
132+
cpival = CPI1000;
133+
} else if (cpi <= 1400) {
134+
cpival = CPI1200;
135+
} else if (cpi > 1400) {
136+
cpival = CPI1600;
137+
}
138+
paw3204_write_reg(REG_SETUP, cpival);
139+
}
140+
141+
uint16_t paw3204_get_cpi(void) {
142+
uint16_t cpival = 1000;
143+
144+
switch (paw3204_read_reg(REG_SETUP) & 0b111) {
145+
case CPI400:
146+
cpival = 400;
147+
break;
148+
case CPI500:
149+
cpival = 500;
150+
break;
151+
case CPI600:
152+
cpival = 600;
153+
break;
154+
case CPI800:
155+
cpival = 800;
156+
break;
157+
case CPI1000:
158+
cpival = 1000;
159+
break;
160+
case CPI1200:
161+
cpival = 1200;
162+
break;
163+
case CPI1600:
164+
cpival = 1600;
165+
break;
166+
}
167+
return cpival;
168+
}
169+
170+
uint8_t read_pid_paw3204(void) {
171+
return paw3204_read_reg(REG_PID1);
172+
}

drivers/sensors/paw3204.h

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Copyright 2021 Gompa (@Gompa)
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 <stdint.h>
20+
#include <stdbool.h>
21+
22+
#ifndef PAW3204_SCLK_PIN
23+
# error "No clock pin defined -- missing PAW3204_SCLK_PIN"
24+
#endif
25+
#ifndef PAW3204_SDIO_PIN
26+
# error "No data pin defined -- missing PAW3204_SDIO_PIN"
27+
#endif
28+
29+
typedef struct {
30+
int16_t x;
31+
int16_t y;
32+
bool isMotion;
33+
} report_paw3204_t;
34+
35+
/**
36+
* @brief Initializes the sensor so it is in a working state and ready to
37+
* be polled for data.
38+
*
39+
* @return true Initialization was a success
40+
* @return false Initialization failed, do not proceed operation
41+
*/
42+
void paw3204_init(void);
43+
44+
/**
45+
* @brief Reads and clears the current delta, and motion register values on the
46+
* given sensor.
47+
*
48+
* @return pmw33xx_report_t Current values of the sensor, if errors occurred all
49+
* fields are set to zero
50+
*/
51+
52+
report_paw3204_t paw3204_read(void);
53+
/**
54+
* @brief Sets the given CPI value the sensor. CPI is often refereed to
55+
* as the sensors sensitivity. Values outside of the allowed range are
56+
* constrained into legal values.
57+
*
58+
* @param cpi CPI value to set
59+
*/
60+
void paw3204_set_cpi(uint16_t cpi);
61+
62+
/**
63+
* @brief Gets the currently set CPI value from the sensor. CPI is often
64+
* refereed to as the sensors sensitivity.
65+
*
66+
* @return uint16_t Current CPI value of the sensor
67+
*/
68+
uint16_t paw3204_get_cpi(void);

quantum/pointing_device.h

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
3333
# include "drivers/sensors/cirque_pinnacle.h"
3434
# include "drivers/sensors/cirque_pinnacle_gestures.h"
3535
# include "pointing_device_gestures.h"
36+
#elif defined(POINTING_DEVICE_DRIVER_paw3204)
37+
# include "drivers/sensors/paw3204.h"
3638
#elif defined(POINTING_DEVICE_DRIVER_pimoroni_trackball)
3739
# include "i2c_master.h"
3840
# include "drivers/sensors/pimoroni_trackball.h"

quantum/pointing_device_drivers.c

+21
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define CONSTRAIN_HID_XY(amt) ((amt) < XY_REPORT_MIN ? XY_REPORT_MIN : ((amt) > XY_REPORT_MAX ? XY_REPORT_MAX : (amt)))
2727

2828
// get_report functions should probably be moved to their respective drivers.
29+
2930
#if defined(POINTING_DEVICE_DRIVER_adns5050)
3031
report_mouse_t adns5050_get_report(report_mouse_t mouse_report) {
3132
report_adns5050_t data = adns5050_read_burst();
@@ -198,7 +199,27 @@ const pointing_device_driver_t pointing_device_driver = {
198199
.get_cpi = cirque_pinnacle_get_cpi
199200
};
200201
// clang-format on
202+
#elif defined(POINTING_DEVICE_DRIVER_paw3204)
203+
204+
report_mouse_t paw3204_get_report(report_mouse_t mouse_report) {
205+
report_paw3204_t data = paw3204_read();
206+
if (data.isMotion) {
207+
# ifdef CONSOLE_ENABLE
208+
dprintf("Raw ] X: %d, Y: %d\n", data.x, data.y);
209+
# endif
210+
211+
mouse_report.x = data.x;
212+
mouse_report.y = data.y;
213+
}
201214

215+
return mouse_report;
216+
}
217+
const pointing_device_driver_t pointing_device_driver = {
218+
.init = paw3204_init,
219+
.get_report = paw3204_get_report,
220+
.set_cpi = paw3204_set_cpi,
221+
.get_cpi = paw3204_get_cpi,
222+
};
202223
#elif defined(POINTING_DEVICE_DRIVER_pimoroni_trackball)
203224

204225
mouse_xy_report_t pimoroni_trackball_adapt_values(clamp_range_t* offset) {

0 commit comments

Comments
 (0)