SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
pawa350_motion.ino
Go to the documentation of this file.
1
45
46#ifndef SENSOR_SDA
47#define SENSOR_SDA 1
48#endif
49
50#ifndef SENSOR_SCL
51#define SENSOR_SCL 2
52#endif
53
54#ifndef SENSOR_IRQ
55#define SENSOR_IRQ 43
56#endif
57
58#ifndef SENSOR_RST
59#define SENSOR_RST 44
60#endif
61
62#ifndef SENSOR_SHUTDOWN
63#define SENSOR_SHUTDOWN 48
64#endif
65
66#ifndef SENSOR_FPD_INT
67#define SENSOR_FPD_INT 18
68#endif
69
71
72volatile bool motionIrqTriggered = false;
73volatile bool fingerStateChanged = false;
74
75void setup()
76{
77 bool rslt;
78
79 Serial.begin(115200);
80 while (!Serial);
81
82
83#if SENSOR_SHUTDOWN != -1
84 // Sensor Power On Pin,HIGH is active
85 pinMode(SENSOR_SHUTDOWN, OUTPUT);
86 digitalWrite(SENSOR_SHUTDOWN, HIGH);
87#endif
88#if SENSOR_RST != -1
89 // Sensor Reset Pin, LOW is reset sensor
90 pinMode(SENSOR_RST, OUTPUT);
91 digitalWrite(SENSOR_RST, HIGH);
92#endif
93#if SENSOR_FPD_INT != -1
94 // Sensor Finger Presence Detection Pin, HIGH is active
95 pinMode(SENSOR_FPD_INT, INPUT);
96#endif
97 // Sensor Motion Interrupt Pin, LOW is active
98 pinMode(SENSOR_IRQ, INPUT);
99 attachInterrupt(SENSOR_IRQ, +[]() {
100 motionIrqTriggered = true;
101 }, FALLING);
102
103 rslt = sensor.begin(Wire, PAW_A350_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL);
104 if (!rslt) {
105 Serial.println("PAW-A350 sensor initialization failed!");
106 while (1) {
107 Serial.println("Ensure sensor is connected and I2C wiring is correct");
108 delay(2000);
109 }
110 }
111
112 Serial.print(sensor.getChipName());
113 Serial.println(" initialized successfully");
114
115 rslt = sensor.softReset();
116 if (!rslt) {
117 Serial.println("Software reset failed!");
118 while (1);
119 }
120 Serial.println("Software reset successful");
121
122 Serial.println("\n========== ID Test ==========");
123 int pid = sensor.getProductID();
124 int rev = sensor.getRevisionID();
125 Serial.print("Product ID: 0x");
126 Serial.print(pid, HEX);
127 Serial.print(" (expected 0x88)");
128 Serial.println(pid == 0x88 ? " [OK]" : " [FAIL]");
129 Serial.print("Revision ID: 0x");
130 Serial.println(rev, HEX);
131
132 Serial.println("\n========== CPI Test ==========");
134 Serial.print("Set CPI 125: ");
135 Serial.println(rslt ? "OK" : "FAILED");
136 Serial.print("Get CPI: ");
137 Serial.print((int)sensor.getCpi());
138 Serial.println(sensor.getCpi() == SensorPawA350::CpiResolution::CPI_125 ? " [OK]" : " [FAIL]");
139
141 Serial.print("Set CPI 250: ");
142 Serial.println(rslt ? "OK" : "FAILED");
143 Serial.print("Get CPI: ");
144 Serial.print((int)sensor.getCpi());
145 Serial.println(sensor.getCpi() == SensorPawA350::CpiResolution::CPI_250 ? " [OK]" : " [FAIL]");
146
148 Serial.print("Set CPI 500: ");
149 Serial.println(rslt ? "OK" : "FAILED");
150 Serial.print("Get CPI: ");
151 Serial.print((int)sensor.getCpi());
152 Serial.println(sensor.getCpi() == SensorPawA350::CpiResolution::CPI_500 ? " [OK]" : " [FAIL]");
153
155 Serial.print("Set CPI 750: ");
156 Serial.println(rslt ? "OK" : "FAILED");
157 Serial.print("Get CPI: ");
158 Serial.print((int)sensor.getCpi());
159 Serial.println(sensor.getCpi() == SensorPawA350::CpiResolution::CPI_750 ? " [OK]" : " [FAIL]");
160
162 Serial.print("Set CPI 1000: ");
163 Serial.println(rslt ? "OK" : "FAILED");
164 Serial.print("Get CPI: ");
165 Serial.print((int)sensor.getCpi());
166 Serial.println(sensor.getCpi() == SensorPawA350::CpiResolution::CPI_1000 ? " [OK]" : " [FAIL]");
167
169 Serial.print("Set CPI 1250: ");
170 Serial.println(rslt ? "OK" : "FAILED");
171 Serial.print("Get CPI: ");
172 Serial.print((int)sensor.getCpi());
173 Serial.println(sensor.getCpi() == SensorPawA350::CpiResolution::CPI_1250 ? " [OK]" : " [FAIL]");
174
175 Serial.println("\n========== Power Management Test ==========");
176 Serial.println("-- Run Downshift --");
177 rslt = sensor.setRunDownshiftTime(8);
178 Serial.print("Set RunDownshift(8): ");
179 Serial.println(rslt ? "OK" : "FAILED");
180 Serial.print("Get RunDownshift: ");
181 Serial.print(sensor.getRunDownshiftTime());
182 Serial.println(sensor.getRunDownshiftTime() == 8 ? " [OK]" : " [FAIL]");
183 Serial.print("Get RunDownshiftMs: ");
184 Serial.print(sensor.getRunDownshiftTimeMs());
185 Serial.println(sensor.getRunDownshiftTimeMs() == 512 ? " [OK]" : " [FAIL]");
186
187 rslt = sensor.setRunDownshiftTime(4);
188 Serial.print("Set RunDownshift(4): ");
189 Serial.println(rslt ? "OK" : "FAILED");
190
191 Serial.println("-- Rest1 Period --");
192 rslt = sensor.setRest1Period(9);
193 Serial.print("Set Rest1Period(9): ");
194 Serial.println(rslt ? "OK" : "FAILED");
195 Serial.print("Get Rest1Period: ");
196 Serial.print(sensor.getRest1Period());
197 Serial.println(sensor.getRest1Period() == 9 ? " [OK]" : " [FAIL]");
198 Serial.print("Get Rest1PeriodMs: ");
199 Serial.print(sensor.getRest1PeriodMs());
200 Serial.println(sensor.getRest1PeriodMs() == 100 ? " [OK]" : " [FAIL]");
201
202 rslt = sensor.setRest1Period(1);
203 Serial.print("Set Rest1Period(1): ");
204 Serial.println(rslt ? "OK" : "FAILED");
205
206 Serial.println("-- Rest1 Downshift --");
207 rslt = sensor.setRest1DownshiftTime(50);
208 Serial.print("Set Rest1Downshift(50): ");
209 Serial.println(rslt ? "OK" : "FAILED");
210 Serial.print("Get Rest1Downshift: ");
211 Serial.print(sensor.getRest1DownshiftTime());
212 Serial.println(sensor.getRest1DownshiftTime() == 50 ? " [OK]" : " [FAIL]");
213
214 rslt = sensor.setRest1DownshiftTime(31);
215 Serial.print("Set Rest1Downshift(31): ");
216 Serial.println(rslt ? "OK" : "FAILED");
217
218 Serial.println("-- Rest2 Period --");
219 rslt = sensor.setRest2Period(19);
220 Serial.print("Set Rest2Period(19): ");
221 Serial.println(rslt ? "OK" : "FAILED");
222 Serial.print("Get Rest2Period: ");
223 Serial.print(sensor.getRest2Period());
224 Serial.println(sensor.getRest2Period() == 19 ? " [OK]" : " [FAIL]");
225 Serial.print("Get Rest2PeriodMs: ");
226 Serial.print(sensor.getRest2PeriodMs());
227 Serial.println(sensor.getRest2PeriodMs() == 200 ? " [OK]" : " [FAIL]");
228
229 rslt = sensor.setRest2Period(9);
230 Serial.print("Set Rest2Period(9): ");
231 Serial.println(rslt ? "OK" : "FAILED");
232
233 Serial.println("-- Rest2 Downshift --");
234 rslt = sensor.setRest2DownshiftTime(100);
235 Serial.print("Set Rest2Downshift(100): ");
236 Serial.println(rslt ? "OK" : "FAILED");
237 Serial.print("Get Rest2Downshift: ");
238 Serial.print(sensor.getRest2DownshiftTime());
239 Serial.println(sensor.getRest2DownshiftTime() == 100 ? " [OK]" : " [FAIL]");
240
241 rslt = sensor.setRest2DownshiftTime(47);
242 Serial.print("Set Rest2Downshift(47): ");
243 Serial.println(rslt ? "OK" : "FAILED");
244
245 Serial.println("-- Rest3 Period --");
246 rslt = sensor.setRest3Period(99);
247 Serial.print("Set Rest3Period(99): ");
248 Serial.println(rslt ? "OK" : "FAILED");
249 Serial.print("Get Rest3Period: ");
250 Serial.print(sensor.getRest3Period());
251 Serial.println(sensor.getRest3Period() == 99 ? " [OK]" : " [FAIL]");
252 Serial.print("Get Rest3PeriodMs: ");
253 Serial.print(sensor.getRest3PeriodMs());
254 Serial.println(sensor.getRest3PeriodMs() == 1000 ? " [OK]" : " [FAIL]");
255
256 rslt = sensor.setRest3Period(49);
257 Serial.print("Set Rest3Period(49): ");
258 Serial.println(rslt ? "OK" : "FAILED");
259
260 Serial.println("\n========== LED Control Test ==========");
261 rslt = sensor.setLedOn(true);
262 Serial.print("Set LedOn(true): ");
263 Serial.println(rslt ? "OK" : "FAILED");
264 Serial.print("Get LedOn: ");
265 Serial.print(sensor.getLedOn() ? "true" : "false");
266 Serial.println(sensor.getLedOn() ? " [OK]" : " [FAIL]");
267
268 rslt = sensor.setLedOn(false);
269 Serial.print("Set LedOn(false): ");
270 Serial.println(rslt ? "OK" : "FAILED");
271 Serial.print("Get LedOn: ");
272 Serial.print(sensor.getLedOn() ? "true" : "false");
273 Serial.println(!sensor.getLedOn() ? " [OK]" : " [FAIL]");
274
275 Serial.println("-- LED Drive Current --");
276 rslt = sensor.setLedDriveCurrent(0);
277 Serial.print("Set LedDriveCurrent(0=13mA): ");
278 Serial.println(rslt ? "OK" : "FAILED");
279 Serial.print("Get LedDriveCurrent: ");
280 Serial.print(sensor.getLedDriveCurrent());
281 Serial.println(sensor.getLedDriveCurrent() == 0 ? " [OK]" : " [FAIL]");
282
283 rslt = sensor.setLedDriveCurrent(2);
284 Serial.print("Set LedDriveCurrent(2=9.6mA): ");
285 Serial.println(rslt ? "OK" : "FAILED");
286 Serial.print("Get LedDriveCurrent: ");
287 Serial.print(sensor.getLedDriveCurrent());
288 Serial.println(sensor.getLedDriveCurrent() == 2 ? " [OK]" : " [FAIL]");
289
290 rslt = sensor.setLedDriveCurrent(7);
291 Serial.print("Set LedDriveCurrent(7=27mA): ");
292 Serial.println(rslt ? "OK" : "FAILED");
293 Serial.print("Get LedDriveCurrent: ");
294 Serial.print(sensor.getLedDriveCurrent());
295 Serial.println(sensor.getLedDriveCurrent() == 7 ? " [OK]" : " [FAIL]");
296
297 Serial.println("\n========== Shutter Test ==========");
298 rslt = sensor.setShutterMax(2000);
299 Serial.print("Set ShutterMax(2000): ");
300 Serial.println(rslt ? "OK" : "FAILED");
301 Serial.print("Get ShutterMax: ");
302 Serial.print(sensor.getShutterMax());
303 Serial.println(sensor.getShutterMax() == 2000 ? " [OK]" : " [FAIL]");
304
305 rslt = sensor.setShutterMax(2929);
306 Serial.print("Set ShutterMax(2929): ");
307 Serial.println(rslt ? "OK" : "FAILED");
308 Serial.print("Get ShutterMax: ");
309 Serial.print(sensor.getShutterMax());
310 Serial.println(sensor.getShutterMax() == 2929 ? " [OK]" : " [FAIL]");
311
312 Serial.println("\n========== Motion Interrupt Test ==========");
313 for (int i = 0; i <= 7; i++) {
315 Serial.print("Set threshold(");
316 Serial.print(i);
317 Serial.print("): ");
318 Serial.print(rslt ? "OK" : "FAILED");
319 Serial.print(" Get: ");
320 Serial.print(sensor.getMotionInterruptThreshold());
321 Serial.println(sensor.getMotionInterruptThreshold() == i ? " [OK]" : " [FAIL]");
322 }
323
324 Serial.println("\n========== Power Timing Summary ==========");
325 Serial.print("Run downshift: ");
326 Serial.print(sensor.getRunDownshiftTimeMs());
327 Serial.println(" ms");
328 Serial.print("Rest1 period: ");
329 Serial.print(sensor.getRest1PeriodMs());
330 Serial.println(" ms");
331 Serial.print("Rest1 downshift: ");
332 Serial.print(sensor.getRest1DownshiftTimeMs() / 1000);
333 Serial.println(" s");
334 Serial.print("Rest2 period: ");
335 Serial.print(sensor.getRest2PeriodMs());
336 Serial.println(" ms");
337 Serial.print("Rest2 downshift: ");
338 Serial.print(sensor.getRest2DownshiftTimeMs() / 60000);
339 Serial.println(" min");
340 Serial.print("Rest3 period: ");
341 Serial.print(sensor.getRest3PeriodMs());
342 Serial.println(" ms");
343
346
347 Serial.println("\n========== Motion Detection Started ==========");
348 Serial.println("Move finger across sensor to detect motion");
349 Serial.println("------------------------------------------");
350}
351
352void loop()
353{
354 uint32_t currentTime = millis();
355 static uint32_t lastPrintTime = 0;
356 static int32_t cumulativeX = 0;
357 static int32_t cumulativeY = 0;
358
359#if SENSOR_FPD_INT != -1
360 static uint32_t proximityTriggerPrintInterval = 0;
361 // Simply bring your finger close to the sensor to trigger it.
362 if (digitalRead(SENSOR_FPD_INT) == HIGH) {
363 if (millis() - proximityTriggerPrintInterval >= 1000) {
364 Serial.println("Finger proximity sensor triggered.");
365 proximityTriggerPrintInterval = millis();
366 }
367 }
368#endif
369
370 if (motionIrqTriggered) {
371 motionIrqTriggered = false;
372
374 if (sensor.checkMotion(status)) {
377 if (sensor.getMotionData(data)) {
378 cumulativeX += data.delta_x;
379 cumulativeY += data.delta_y;
380
381 if (currentTime - lastPrintTime >= 100) {
382 float inchesX = (float)cumulativeX / 1000.0f;
383 float inchesY = (float)cumulativeY / 1000.0f;
384
385 Serial.print("Motion: DX=");
386 Serial.print(data.delta_x);
387 Serial.print(", DY=");
388 Serial.print(data.delta_y);
389 Serial.print(", CumX=");
390 Serial.print(cumulativeX);
391 Serial.print(", CumY=");
392 Serial.print(cumulativeY);
393 Serial.print(", InchX=");
394 Serial.print(inchesX, 3);
395 Serial.print(", InchY=");
396 Serial.print(inchesY, 3);
397 Serial.print(", Time=");
398 Serial.println(currentTime);
399
400 lastPrintTime = currentTime;
401 }
402 }
403 }
404 }
405 }
406}
@license MIT License
bool begin(SensorCommCustom::CustomCallback cb, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
Initialize the sensor using custom callback interface.
PAW-A350 Optical Finger Navigation Sensor Driver.
uint32_t getRest2PeriodMs() const
Get Rest2 period in milliseconds.
bool checkMotion(MotionStatus &status)
Check motion status.
bool getLedOn() const
Get LED always-on status.
uint32_t getRest1PeriodMs() const
Get Rest1 period in milliseconds.
int getProductID() const
Get Product ID.
int getRest2DownshiftTime() const
Get Rest2 to Rest3 downshift time.
bool getMotionData(MotionData &data)
Get motion delta data.
bool setLedDriveCurrent(uint8_t current)
Set LED drive current.
int getRest1DownshiftTime() const
Get Rest1 to Rest2 downshift time.
int getRevisionID() const
Get Revision ID.
int getRest1Period() const
Get Rest1 frame period.
bool setRest1Period(uint8_t period)
Set Rest1 frame period.
@ CPI_125
125 CPI - Lowest resolution, largest range
@ CPI_500
500 CPI - Default resolution
@ CPI_1000
1000 CPI - Recommended for most applications
@ CPI_1250
1250 CPI - Highest resolution
bool setMotionInterruptThreshold(uint8_t threshold)
Set motion interrupt threshold.
uint32_t getRest2DownshiftTimeMs() const
Get Rest2 downshift time in milliseconds.
uint32_t getRunDownshiftTimeMs() const
Get Run downshift time in milliseconds.
int getRest2Period() const
Get Rest2 frame period.
int getRunDownshiftTime() const
Get Run to Rest1 downshift time.
CpiResolution getCpi() const
Get current CPI resolution.
bool setRest3Period(uint8_t period)
Set Rest3 frame period.
bool setLedOn(bool enable)
Enable/disable LED always-on mode.
int getRest3Period() const
Get Rest3 frame period.
const char * getChipName() const
Get chip name.
MotionStatus
Motion Detection Status.
@ MOTION_DETECTED
Motion occurred, data ready for reading.
int getMotionInterruptThreshold() const
Get motion interrupt threshold.
bool setShutterMax(uint16_t value)
Set shutter maximum open time.
bool setRest2Period(uint8_t period)
Set Rest2 frame period.
int getShutterMax() const
Get shutter maximum open time.
uint32_t getRest3PeriodMs() const
Get Rest3 period in milliseconds.
bool setRunDownshiftTime(uint8_t rd)
Set Run to Rest1 downshift time.
bool setRest2DownshiftTime(uint8_t r2d)
Set Rest2 to Rest3 downshift time.
bool setRest1DownshiftTime(uint8_t r1d)
Set Rest1 to Rest2 downshift time.
bool setCpi(CpiResolution cpi)
Set CPI resolution.
int getLedDriveCurrent() const
Get LED drive current setting.
uint32_t getRest1DownshiftTimeMs() const
Get Rest1 downshift time in milliseconds.
bool softReset()
Perform software reset.
volatile bool fingerStateChanged
#define SENSOR_SHUTDOWN
void setup()
#define SENSOR_SCL
volatile bool motionIrqTriggered
#define SENSOR_RST
#define SENSOR_FPD_INT
SensorPawA350 sensor
#define SENSOR_SDA
#define SENSOR_IRQ
void loop()
uint8_t i
int8_t delta_x
X-axis movement (-128 to +127 counts)
int8_t delta_y
Y-axis movement (-128 to +127 counts)