M5Unit-GESTURE 0.0.2 git rev:39d7f12
Loading...
Searching...
No Matches
unit_PAJ7620U2.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_GESTURE_UNIT_PAJ7620U2_HPP
11#define M5_UNIT_GESTURE_UNIT_PAJ7620U2_HPP
12
13#include <M5UnitComponent.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
15#include <array>
16#include <memory>
17
18namespace m5 {
19namespace unit {
20
25namespace paj7620u2 {
32enum class Gesture : uint16_t {
33 None,
34 Up = 1U << 0,
35 Down = 1U << 1,
36 Left = 1U << 2,
37 Right = 1U << 3,
38 Forward = 1U << 4,
39 Backward = 1U << 5,
40 Clockwise = 1U << 6,
41 CounterClockwise = 1U << 7,
42 Wave = 1U << 8,
43 Approach = 1U << 9,
44 HasObject = 1U << 10,
45 WakeupTrigger = 1U << 11,
46 Confirm = 1U << 12,
47 Abort = 1U << 13,
48 Reserve = 1U << 14,
49 NoObject = 1U << 15,
50};
51
56enum class Mode : uint8_t {
57 Gesture,
58 Proximity,
59 Cursor,
60};
61
66enum class Frequency : int8_t {
67 Unknown = -1,
68 Normal,
69 Gaming,
70};
71
76struct Data {
77 Data()
78 {
79 }
80 // Common
81 // [0,1]:gesture
82 // Proximity
83 // [2]:proximity [3]:approach
84 // Cirosr
85 // [2,3]:X [4,5]:Y
86 std::array<uint8_t, 2 + 4> raw{};
87 Gesture data_gesture{};
88 Mode data_mode{};
89 union {
90 struct {
91 uint8_t proximity_brightness;
92 bool proximity_approach;
93 };
94 struct {
95 uint16_t cursor_x{}, cursor_y{};
96 };
97 };
98
100 inline Mode mode() const
101 {
102 return data_mode;
103 }
106 {
107 return data_gesture;
108 }
111
112 inline uint8_t brightness() const
113 {
114 return (data_mode == Mode::Proximity) ? proximity_brightness : 0;
115 }
117 inline bool approach() const
118 {
119 return (data_mode == Mode::Proximity) ? proximity_approach : false;
120 }
124
125 inline uint16_t cursorX() const
126 {
127 return (data_mode == Mode::Cursor) ? cursor_x : 0xFFFF;
128 }
130 inline uint16_t cursorY() const
131 {
132 return (data_mode == Mode::Cursor) ? cursor_y : 0xFFFF;
133 }
135};
136
137} // namespace paj7620u2
138
143class UnitPAJ7620U2 : public Component, public PeriodicMeasurementAdapter<UnitPAJ7620U2, paj7620u2::Data> {
144 M5_UNIT_COMPONENT_HPP_BUILDER(UnitPAJ7620U2, 0x73);
145
146public:
151 struct config_t {
153 bool start_periodic{true};
155 paj7620u2::Mode mode{paj7620u2::Mode::Gesture};
157 paj7620u2::Frequency frequency{paj7620u2::Frequency::Normal};
159 bool hflip{false};
161 bool vflip{true};
163 uint8_t rotation{0};
165 bool store_on_change{true};
166 };
167
168 explicit UnitPAJ7620U2(const uint8_t addr = DEFAULT_ADDRESS)
169 : Component(addr), _data{new m5::container::CircularBuffer<paj7620u2::Data>(1)}
170 {
171 auto ccfg = component_config();
172 ccfg.clock = 400 * 1000U;
173 component_config(ccfg);
174 }
175 virtual ~UnitPAJ7620U2()
176 {
177 }
178
179 virtual bool begin() override;
180 virtual void update(const bool force = false) override;
181
184
186 {
187 return _cfg;
188 }
190 inline void config(const config_t& cfg)
191 {
192 _cfg = cfg;
193 }
195
198
200 {
201 return !empty() ? oldest().gesture() : paj7620u2::Gesture::None;
202 }
204 inline uint8_t brightness() const
205 {
206 return !empty() ? oldest().brightness() : 0;
207 }
209 inline bool approach() const
210 {
211 return !empty() ? oldest().approach() : false;
212 }
214 uint16_t cursorX() const
215 {
216 return !empty() ? oldest().cursorX() : 0xFFFF;
217 }
219 uint16_t cursorY() const
220 {
221 return !empty() ? oldest().cursorY() : 0xFFFF;
222 }
224
227
232 bool startPeriodicMeasurement(const uint32_t intervalMs = 0)
233 {
234 return PeriodicMeasurementAdapter<UnitPAJ7620U2, paj7620u2::Data>::startPeriodicMeasurement(intervalMs);
235 }
244 const uint32_t intervalMs)
245 {
246 return PeriodicMeasurementAdapter<UnitPAJ7620U2, paj7620u2::Data>::startPeriodicMeasurement(intervalMs);
247 }
253 {
254 return PeriodicMeasurementAdapter<UnitPAJ7620U2, paj7620u2::Data>::stopPeriodicMeasurement();
255 }
257
263 inline uint8_t rotation() const
264 {
265 return _rotation;
266 }
278 void setRotate(const uint8_t rot)
279 {
280 _rotation = rot & 0x03;
281 }
282
285 {
286 return _frequency;
287 }
293 bool readFrequency(uint8_t& raw);
306
309
321 bool readObjectCenter(uint16_t& x, uint16_t& y);
327 bool readObjectSize(uint16_t& sz);
333 bool existsObject(bool& exists)
334 {
335 uint8_t e{};
336 if (readNoObjectCount(e)) {
337 exists = (e == 0);
338 return true;
339 }
340 return false;
341 }
347 bool readNoObjectCount(uint8_t& cnt);
353 bool readNoMotionCount(uint8_t& cnt);
355
358
364 bool readProximity(uint8_t& brightness, uint8_t& approach);
365
374 bool readApproachThreshold(uint8_t& high, uint8_t& low);
383 bool writeApproachThreshold(const uint8_t high, const uint8_t low);
385
388
395 bool readCursor(uint16_t& x, uint16_t& y);
397
400
401 inline paj7620u2::Mode mode() const
402 {
403 return _mode;
404 }
410 bool writeMode(const paj7620u2::Mode m);
412
414 bool readHorizontalFlip(bool& flip);
416 bool readVerticalFlip(bool& flip);
418 bool writeHorizontalFlip(const bool flip);
420 bool writeVerticalFlip(const bool flip);
421
424
429 bool enable(const bool flag);
431 inline bool enable()
432 {
433 return enable(true);
434 }
436 inline bool disable()
437 {
438 return enable(false);
439 }
441 bool suspend();
443 bool resume();
445
446protected:
447 bool start_periodic_measurement(const uint32_t intervalMs = 0);
448 bool start_periodic_measurement(const paj7620u2::Mode mode, const paj7620u2::Frequency freq,
449 const uint32_t intervalMs);
450 bool stop_periodic_measurement();
451
452 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitPAJ7620U2, paj7620u2::Data);
453
454 bool select_bank(const uint8_t bank, const bool force = false);
455
456 bool read_banked_register(const uint16_t reg, uint8_t* buf, const size_t len);
457 bool read_banked_register8(const uint16_t reg, uint8_t& value);
458 bool read_banked_register16(const uint16_t reg, uint16_t& value);
459 bool write_banked_register(const uint16_t reg, const uint8_t* buf, const size_t len);
460 bool write_banked_register8(const uint16_t reg, const uint8_t value);
461 bool write_banked_register16(const uint16_t reg, const uint16_t value);
462
463 bool update_gesture(paj7620u2::Data& d);
464 bool update_proximity(paj7620u2::Data& d);
465 bool update_cursor(paj7620u2::Data& d);
466
467 bool read_gesture(paj7620u2::Data& d);
468 bool read_proximity(paj7620u2::Data& d);
469 bool read_cursor(paj7620u2::Data& d);
470
471 bool was_wakeup();
472 bool read_chip_id(uint16_t& id);
473 bool read_version(uint8_t& version);
474
475protected:
476 uint8_t _current_bank{0xFF};
477 paj7620u2::Mode _mode{};
478 paj7620u2::Frequency _frequency{};
479 uint8_t _rotation{};
480
481 std::unique_ptr<m5::container::CircularBuffer<paj7620u2::Data>> _data{};
482 config_t _cfg{};
483};
484
486namespace paj7620u2 {
487namespace command {
488
489// Bnak 0/1
490constexpr uint8_t BANK_SEL{0xEF};
491// high byte:bank low byte: register
492// Bank 0
493constexpr uint16_t PART_ID_LOW{0x0000};
494constexpr uint16_t PART_ID_HIGH{0x0001};
495constexpr uint16_t VERSION_ID{0x0002};
496constexpr uint16_t SW_SUSPEND_ENL{0x0003};
497
498constexpr uint16_t CURSOR_CLAMP_CENTER_X_LOW{0X003B};
499constexpr uint16_t CURSOR_CLAMP_CENTER_X_HIGH{0X003C};
500constexpr uint16_t CURSOR_CLAMP_CENTER_Y_LOW{0X003D};
501constexpr uint16_t CURSOR_CLAMP_CENTER_Y_HIGH{0X003E};
502
503constexpr uint16_t INT_FLAG_1{0x0043};
504constexpr uint16_t INT_FLAG_2{0x0044};
505
506const uint16_t R_POX_UB{0X0069};
507const uint16_t R_POX_LB{0X006A};
508constexpr uint16_t S_STATE{0x006B};
509constexpr uint16_t S_AVGY{0x006C};
510
511constexpr uint16_t OBJECT_CENTER_X_LOW{0X00AC};
512constexpr uint16_t OBJECT_CENTER_X_HIGH{0X00AD};
513constexpr uint16_t OBJECT_CENTER_Y_LOW{0X00AE};
514constexpr uint16_t OBJECT_CENTER_Y_HIGH{0X00AF};
515
516constexpr uint16_t OBJECT_AVG_Y{0X00B0};
517constexpr uint16_t OBJECT_SIZE_LOW{0x00B1};
518constexpr uint16_t OBJECT_SIZE_HIGH{0x00B2};
519constexpr uint16_t WAVE_ABORT_COUNT{0x00B7};
520constexpr uint16_t NO_OBJECT_COUNT{0x00B8};
521constexpr uint16_t NO_MOTION_COUNT{0x00B9};
522
523constexpr uint16_t VEL_X_LOW{0x00C3};
524constexpr uint16_t VEL_X_HIGH{0x00C4};
525constexpr uint16_t VEL_Y_LOW{0x00C3};
526constexpr uint16_t VEL_Y_HIGH{0x00C4};
527
528// Bank1
529constexpr uint16_t LS_COMP_DAVG_V{0x0104};
530constexpr uint16_t R_REF_CLK_CNT_LOW{0x0165};
531constexpr uint16_t R_TG_ENH{0x0172};
532} // namespace command
533} // namespace paj7620u2
535
536} // namespace unit
537} // namespace m5
538
539#endif
PAJ7620U2 unit.
Definition unit_PAJ7620U2.hpp:143
bool readProximity(uint8_t &brightness, uint8_t &approach)
Read proximity.
Definition unit_PAJ7620U2.cpp:516
bool readNoObjectCount(uint8_t &cnt)
State counter with no objects detected.
Definition unit_PAJ7620U2.cpp:498
paj7620u2::Frequency frequency() const
Gets the inner frequency.
Definition unit_PAJ7620U2.hpp:284
bool startPeriodicMeasurement(const paj7620u2::Mode mode, const paj7620u2::Frequency freq, const uint32_t intervalMs)
Start periodic measurement.
Definition unit_PAJ7620U2.hpp:243
uint8_t brightness() const
Oldest brightness if Proximity mode.
Definition unit_PAJ7620U2.hpp:204
bool stopPeriodicMeasurement()
Stop periodic measurement.
Definition unit_PAJ7620U2.hpp:252
bool writeApproachThreshold(const uint8_t high, const uint8_t low)
Write the threshold for detect approach.
Definition unit_PAJ7620U2.cpp:636
void setRotate(const uint8_t rot)
Set the roptation.
Definition unit_PAJ7620U2.hpp:278
bool resume()
resume from suspended
Definition unit_PAJ7620U2.cpp:563
bool readGesture(paj7620u2::Gesture &gesture)
Read gesture.
Definition unit_PAJ7620U2.cpp:487
config_t config()
Gets the configration.
Definition unit_PAJ7620U2.hpp:185
void config(const config_t &cfg)
Set the configration.
Definition unit_PAJ7620U2.hpp:190
bool enable()
enable sensor
Definition unit_PAJ7620U2.hpp:431
bool writeVerticalFlip(const bool flip)
Write the vertical flipping.
Definition unit_PAJ7620U2.cpp:673
bool disable()
disable sensor
Definition unit_PAJ7620U2.hpp:436
bool readApproachThreshold(uint8_t &high, uint8_t &low)
Read the threshold for detect approach.
Definition unit_PAJ7620U2.cpp:631
bool readCursor(uint16_t &x, uint16_t &y)
Cursor position.
Definition unit_PAJ7620U2.cpp:541
bool readNoMotionCount(uint8_t &cnt)
State counter which a non-moving object is detected.
Definition unit_PAJ7620U2.cpp:504
bool readVerticalFlip(bool &flip)
Read the vertical flipping.
Definition unit_PAJ7620U2.cpp:652
bool suspend()
suspend
Definition unit_PAJ7620U2.cpp:558
bool writeHorizontalFlip(const bool flip)
Write the horizontal flipping.
Definition unit_PAJ7620U2.cpp:663
paj7620u2::Mode mode() const
Gets the detection mode.
Definition unit_PAJ7620U2.hpp:401
bool startPeriodicMeasurement(const uint32_t intervalMs=0)
Start periodic measurement in the current settings.
Definition unit_PAJ7620U2.hpp:232
bool writeFrequency(const paj7620u2::Frequency f)
Write the frequency.
Definition unit_PAJ7620U2.cpp:592
bool approach() const
Oldest approach status if Proximity mode.
Definition unit_PAJ7620U2.hpp:209
bool existsObject(bool &exists)
Is the object present within the detection range?
Definition unit_PAJ7620U2.hpp:333
bool readObjectCenter(uint16_t &x, uint16_t &y)
Object center position.
Definition unit_PAJ7620U2.cpp:528
bool readFrequency(uint8_t &raw)
Read the raw frequency.
Definition unit_PAJ7620U2.cpp:568
uint8_t rotation() const
Get the rotation.
Definition unit_PAJ7620U2.hpp:263
bool readHorizontalFlip(bool &flip)
Read the horizontal flipping.
Definition unit_PAJ7620U2.cpp:641
bool writeMode(const paj7620u2::Mode m)
Write the detection mode.
Definition unit_PAJ7620U2.cpp:601
uint16_t cursorY() const
Oldest cursor Y if Cursor mode.
Definition unit_PAJ7620U2.hpp:219
bool readObjectSize(uint16_t &sz)
Read gesture object size.
Definition unit_PAJ7620U2.cpp:510
uint16_t cursorX() const
Oldest cursor X if Cursor mode.
Definition unit_PAJ7620U2.hpp:214
paj7620u2::Gesture gesture() const
Oldest gesture.
Definition unit_PAJ7620U2.hpp:199
Top level namespace of M5stack.
For PAJ7620U2.
Unit-related namespace.
Settings for begin.
Definition unit_PAJ7620U2.hpp:151
bool vflip
Flip verticl if start on bein.
Definition unit_PAJ7620U2.hpp:161
paj7620u2::Mode mode
Mode if start on bein.
Definition unit_PAJ7620U2.hpp:155
bool hflip
Flip horizontal if start on bein.
Definition unit_PAJ7620U2.hpp:159
bool start_periodic
Start periodic measurement on begin?
Definition unit_PAJ7620U2.hpp:153
uint8_t rotation
Rotation if start on begin.
Definition unit_PAJ7620U2.hpp:163
paj7620u2::Frequency frequency
Frequency if start on bein.
Definition unit_PAJ7620U2.hpp:157
bool store_on_change
Store only when value is a change.
Definition unit_PAJ7620U2.hpp:165
Measurement data group.
Definition unit_PAJ7620U2.hpp:76
Mode mode() const
Gets the operation mode.
Definition unit_PAJ7620U2.hpp:100
bool approach() const
Detect the approach?
Definition unit_PAJ7620U2.hpp:117
uint16_t cursorX() const
Gets the cursor X the any object.
Definition unit_PAJ7620U2.hpp:125
uint8_t brightness() const
Gets the brightness.
Definition unit_PAJ7620U2.hpp:112
Gesture gesture() const
Gets the gesture (Common)
Definition unit_PAJ7620U2.hpp:105
uint16_t cursorY() const
Gets the cursor Y the any object.
Definition unit_PAJ7620U2.hpp:130
Frequency
Frequency Operating frequency.
Definition unit_PAJ7620U2.hpp:66
Gesture
Gesture type.
Definition unit_PAJ7620U2.hpp:32
@ NoObject
No object (cursor mode)
@ Approach
Approach (proximity mode)
@ HasObject
Has object (cursor mode)
@ Forward
Closer to the sensor.
@ Confirm
Confirm (confirm mode)
@ Abort
Abort (confirm mode)
@ CounterClockwise
Counter clock wise.
@ WakeupTrigger
Wakeup (trigger mode)
@ Backward
Away from the sensor.
Mode
Operation Mode.
Definition unit_PAJ7620U2.hpp:56
@ Cursor
Detect XY cordinate.
@ Proximity
Detect proximity.