SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
SensorPawA350.cpp
Go to the documentation of this file.
1
31#include "../../../SensorBuildOpt.h"
32#if !SENSORLIB_EXCLUDE_PAWA350
33
34#include "SensorPawA350.hpp"
35#include "SensorPawA350_Reg.hpp"
36
37using namespace Paw350Regs;
38
39const char *SensorPawA350::getChipName() const
40{
41 return "PAW-A350";
42}
43
45{
46 return readReg(REG_PRODUCT_ID);
47}
48
50{
51 return readReg(REG_REVISION_ID);
52}
53
55{
56 int ret = writeReg(REG_SOFT_RESET, SOFTRESET_VAL);
57 if (ret != 0) return false;
58 return getProductID() == PAW_A350_DEFAULT_PID;
59}
60
62{
63 int val = readReg(REG_EVENT);
64 if (val < 0) return false;
65 status = (val & MASK_MOTION) ? MotionStatus::MOTION_DETECTED : MotionStatus::NO_MOTION;
66 return true;
67}
68
70{
71 uint8_t buffer[2] = {0};
72 buffer[0] = readReg(REG_DELTA_X);
73 buffer[1] = readReg(REG_DELTA_Y);
74 data.delta_x = static_cast<int8_t>(buffer[0]);
75 data.delta_y = static_cast<int8_t>(buffer[1]);
76 writeReg(REG_EVENT, 0x00);
77 return true;
78}
79
81{
82 return writeReg(REG_CPI_SEL, static_cast<uint8_t>(cpi)) == 0;
83}
84
86{
87 int val = readReg(REG_CPI_SEL);
88 if (val < 0) return CpiResolution::CPI_500;
89 return static_cast<CpiResolution>(val & MASK_CPI_SEL);
90}
91
93{
94 return writeReg(REG_RUN_DOWNSHIFT, rd) == 0;
95}
96
98{
99 return readReg(REG_RUN_DOWNSHIFT);
100}
101
103{
104 // Note: Register 0x11 has a hardware read issue on some PAW-A350 variants
105 // where reading back the written value returns 0. As a workaround, we use an
106 // internal cache to track the setting. The register write is still attempted.
107 bool rslt = writeReg(REG_REST1_PERIOD, period) == 0;
108 if (rslt) {
109 cached_rest1_period = period;
110 }
111 return rslt;
112}
113
115{
116 // Returns cached value because register 0x11 readback returns 0 on some sensors.
117 // Use setRest1Period() to update this cached value.
118 return cached_rest1_period;
119}
120
122{
123 return writeReg(REG_REST1_DOWNSHIFT, r1d) == 0;
124}
125
127{
128 return readReg(REG_REST1_DOWNSHIFT);
129}
130
132{
133 return writeReg(REG_REST2_PERIOD, period) == 0;
134}
135
137{
138 return readReg(REG_REST2_PERIOD);
139}
140
142{
143 return writeReg(REG_REST2_DOWNSHIFT, r2d) == 0;
144}
145
147{
148 return readReg(REG_REST2_DOWNSHIFT);
149}
150
152{
153 return writeReg(REG_REST3_PERIOD, period) == 0;
154}
155
157{
158 return readReg(REG_REST3_PERIOD);
159}
160
161bool SensorPawA350::setLedOn(bool enable)
162{
163 int val = readReg(REG_LED_CTRL);
164 if (val < 0) return false;
165 return writeReg(REG_LED_CTRL, enable ? (val | MASK_LED_ON) : (val & ~MASK_LED_ON)) == 0;
166}
167
169{
170 int val = readReg(REG_LED_CTRL);
171 return (val >= 0) && (val & MASK_LED_ON);
172}
173
175{
176 int val = readReg(REG_LED_CTRL);
177 if (val < 0) return false;
178 return writeReg(REG_LED_CTRL, (val & ~MASK_LED_DRV) | (current & MASK_LED_DRV)) == 0;
179}
180
182{
183 int val = readReg(REG_LED_CTRL);
184 return (val >= 0) ? (val & MASK_LED_DRV) : -1;
185}
186
188{
189 int hi = writeReg(REG_SHUTTER_MAX_HI, (value >> 8) & 0xFF);
190 int lo = writeReg(REG_SHUTTER_MAX_LO, value & 0xFF);
191 return hi == 0 && lo == 0;
192}
193
195{
196 int hi = readReg(REG_SHUTTER_MAX_HI);
197 int lo = readReg(REG_SHUTTER_MAX_LO);
198 if (hi < 0 || lo < 0) return -1;
199 return (hi << 8) | lo;
200}
201
203{
204 int val = readReg(REG_MOTION_CTRL);
205 if (val < 0) return false;
206 return writeReg(REG_MOTION_CTRL, (val & ~MASK_MOTION_INT_THRES) | (threshold & MASK_MOTION_INT_THRES)) == 0;
207}
208
210{
211 int val = readReg(REG_MOTION_CTRL);
212 return (val >= 0) ? (val & MASK_MOTION_INT_THRES) : -1;
213}
214
216{
217 int val = readReg(REG_FPD_FLAG);
218 return val >= 0;
219}
220
222{
223 MotionStatus status;
224 return checkMotion(status) && status == MotionStatus::MOTION_DETECTED;
225}
226
228{
229 int rd = getRunDownshiftTime();
230 return (rd > 0) ? (rd * 8 * 8) : 0;
231}
232
234{
235 int r1r = getRest1Period();
236 return (r1r > 0) ? ((r1r + 1) * 10) : 0;
237}
238
240{
241 int r1d = getRest1DownshiftTime();
242 int period = getRest1Period();
243 return (r1d > 0 && period > 0) ? (r1d * 16 * (period + 1) * 10) : 0;
244}
245
247{
248 int r2r = getRest2Period();
249 return (r2r > 0) ? ((r2r + 1) * 10) : 0;
250}
251
253{
254 int r2d = getRest2DownshiftTime();
255 int period = getRest2Period();
256 return (r2d > 0 && period > 0) ? (r2d * 128 * (period + 1) * 10) : 0;
257}
258
260{
261 int r3r = getRest3Period();
262 return (r3r > 0) ? ((r3r + 1) * 10) : 0;
263}
264
265bool SensorPawA350::initImpl(uint8_t param)
266{
267 (void)param;
268 int pid = getProductID();
269 if (pid != PAW_A350_DEFAULT_PID) {
270 return false;
271 }
273 return true;
274}
275
276#endif
@license MIT License
@license MIT License
int readReg(uint8_t reg) const
int writeReg(uint8_t reg, uint8_t val)
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.
uint8_t cached_rest1_period
int getProductID() const
Get Product ID.
int getRest2DownshiftTime() const
Get Rest2 to Rest3 downshift time.
bool isMotionDetected()
Quick check if motion detected.
bool initImpl(uint8_t param) override
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.
CpiResolution
Count Per Inch (CPI) Resolution.
@ CPI_500
500 CPI - Default resolution
bool setMotionInterruptThreshold(uint8_t threshold)
Set motion interrupt threshold.
uint32_t getRest2DownshiftTimeMs() const
Get Rest2 downshift time in milliseconds.
bool clearFingerState()
Clear finger presence detect flag.
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.
@ NO_MOTION
No motion detected.
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.
int8_t delta_x
X-axis movement (-128 to +127 counts)
int8_t delta_y
Y-axis movement (-128 to +127 counts)