MatrixMiniR4 1.1.5
Matrix Mini R4 Arduino Library API Documentation
Loading...
Searching...
No Matches
MiniR4_MXGesture.cpp
Go to the documentation of this file.
1
15#include "MiniR4_MXGesture.h"
16
18 uint16_t partid;
19 _pWire->begin();
20 selectBank(eBank0);
21 if (readReg(PAJ7620_ADDR_PART_ID_LOW, &partid, 2) == 0) {
22 DBG("bus data access error");
23 return ERR_DATA_BUS;
24 }
25 DBG("part id=0X");
26 DBG(partid, HEX);
27 if (partid != PAJ7620_PARTID) {
28 return ERR_IC_VERSION;
29 }
30 for (uint16_t i = 0;
31 i < sizeof(initRegisterArray) / sizeof(initRegisterArray[0]); i++) {
32 writeReg(initRegisterArray[i][0], &initRegisterArray[i][1], 1);
33 }
34 selectBank(eBank0);
35 return ERR_OK;
36}
37
38int MatrixGesture::setNormalOrGamingMode(eRateMode_t mode) {
39 mode = mode;
40 return 0;
41}
42
44 for (uint16_t i = 0; i < sizeof(gestureDescriptionsTable) /
45 sizeof(gestureDescriptionsTable[0]);
46 i++) {
47 if (gesture == gestureDescriptionsTable[i].gesture) {
48 return gestureDescriptionsTable[i].description;
49 }
50 }
51 return "";
52}
53
54void MatrixGesture::setGestureHighRate(bool b) { _gestureHighRate = b; }
55
57 readReg(PAJ7620_ADDR_GES_PS_DET_FLAG_1, &_gesture, 1);
58 _gesture = (MatrixGesture::eGesture_t)(((uint16_t)_gesture) << 8);
59 if (_gesture == eGestureWave) {
60 DBG("Wave1 Event Detected");
61 delay(GES_QUIT_TIME);
62 } else {
63 _gesture = eGestureNone;
64 readReg(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &_gesture, 1); // Read Bank_0_Reg_0x43/0x44 for gesture result.
65 _gesture = (MatrixGesture::eGesture_t)(((uint16_t)_gesture) & 0x00ff);
66 if (!_gestureHighRate) {
67 uint8_t tmp;
68 delay(GES_ENTRY_TIME);
69 readReg(PAJ7620_ADDR_GES_PS_DET_FLAG_0, &tmp, 1);
70 DBG("tmp=0x");
71 DBG(tmp, HEX);
72 DBG("_gesture=0x");
73 DBG(_gesture, HEX);
74 _gesture = (MatrixGesture::eGesture_t)(((uint16_t)_gesture) | tmp);
75 }
76 if (_gesture != eGestureNone) {
77 DBG("");
78 switch (_gesture) {
79 case eGestureRight:
80 DBG("Right Event Detected");
81 break;
82
83 case eGestureLeft:
84 DBG("Left Event Detected");
85 break;
86
87 case eGestureUp:
88 DBG("Up Event Detected");
89 break;
90
91 case eGestureDown:
92 DBG("Down Event Detected");
93 break;
94
95 case eGestureForward:
96 DBG("Forward Event Detected");
97 if (!_gestureHighRate) {
98 delay(GES_QUIT_TIME);
99 } else {
100 delay(GES_QUIT_TIME / 5);
101 }
102 break;
103
104 case eGestureBackward:
105 DBG("Backward Event Detected");
106 if (!_gestureHighRate) {
107 delay(GES_QUIT_TIME);
108 } else {
109 delay(GES_QUIT_TIME / 5);
110 }
111 break;
112
114 DBG("Clockwise Event Detected");
115 break;
116
118 DBG("anti-clockwise Event Detected");
119 break;
120
121 default:
122 uint8_t tmp;
123 readReg(PAJ7620_ADDR_GES_PS_DET_FLAG_1, &tmp, 1);
124 if (tmp) {
125 _gesture = eGestureWave;
126 DBG("Wave2 Event Detected");
127 } else {
128 switch (_gesture) {
130 DBG("LeftRight Wave Event Detected");
131 break;
133 DBG("UpDown Wave Event Detected");
134 break;
136 DBG("ForwardBackward Wave Event Detected");
137 break;
138 default:
140 DBG("Wave Disorder Event Detected");
141 break;
142 }
143 }
144 break;
145 }
146 }
147 }
148
149 // 处理 _gesture 以返回整数
150 int gestureResult = 0; // 默认为0,表示没有手势
151 switch (_gesture) {
152 case eGestureRight:
153 gestureResult = 1;
154 break;
155 case eGestureLeft:
156 gestureResult = 2;
157 break;
158 case eGestureUp:
159 gestureResult = 3;
160 break;
161 case eGestureDown:
162 gestureResult = 4;
163 break;
164 case eGestureForward:
165 gestureResult = 5;
166 break;
167 case eGestureBackward:
168 gestureResult = 6;
169 break;
171 gestureResult = 7;
172 break;
174 gestureResult = 8;
175 break;
176 case eGestureWave:
177 gestureResult = 9;
178 break;
180 gestureResult = 10;
181 break;
183 gestureResult = 11;
184 break;
186 gestureResult = 12;
187 break;
189 gestureResult = 13;
190 break;
191 default:
192 gestureResult = 0; // 如果是 eGestureNone 或其他未知手势
193 break;
194 }
195
196 return gestureResult; // 返回处理后的整数值
197}
198
199
200int MatrixGesture::selectBank(eBank_t bank) {
201 writeReg(PAJ7620_REGITER_BANK_SEL, &bank, 1);
202 return 0;
203}
204
205void MatrixGesture::writeReg(uint8_t reg, const void *pBuf, size_t size) {
206 if (pBuf == NULL) {
207 DBG("pBuf is null pointer");
208 return;
209 }
210 uint8_t *_pBuf = (uint8_t *)pBuf;
211 i2cMUXSelect();
212 _pWire->beginTransmission(_deviceAddr);
213 _pWire->write(&reg, 1);
214 for (uint16_t i = 0; i < size; i++) {
215 _pWire->write(_pBuf[i]);
216 }
217 _pWire->endTransmission();
218}
219
220uint8_t MatrixGesture::readReg(uint8_t reg, void *pBuf, size_t size) {
221 if (pBuf == NULL) {
222 DBG("pBuf is null pointer");
223 return 0;
224 }
225 uint8_t *_pBuf = (uint8_t *)pBuf;
226 i2cMUXSelect();
227 _pWire->beginTransmission(_deviceAddr);
228 _pWire->write(&reg, 1);
229 if (_pWire->endTransmission() != 0) {
230 DBG("Wire Read Error");
231 return 0;
232 }
233 _pWire->requestFrom(_deviceAddr, (uint8_t)size);
234 for (uint16_t i = 0; i < size; i++) _pBuf[i] = _pWire->read();
235 _pWire->endTransmission();
236 return size;
237}
238
240 MatrixGesture::gestureDescriptionsTable[] = {
241 {0, "None"}, // eGestureNone
242 {1, "Right"}, // eGestureRight
243 {2, "Left"}, // eGestureLeft
244 {3, "Up"}, // eGestureUp
245 {4, "Down"}, // eGestureDown
246 {5, "Forward"}, // eGestureForward
247 {6, "Backward"}, // eGestureBackward
248 {7, "Clockwise"}, // eGestureClockwise
249 {8, "Anti-Clockwise"}, // eGestureAntiClockwise
250 {9, "Wave"}, // eGestureWave
251 {10, "WaveSlowlyDisorder"}, // eGestureWaveSlowlyDisorder
252 {11, "WaveSlowlyLeftRight"}, // eGestureWaveSlowlyLeftRight
253 {12, "WaveSlowlyUpDown"}, // eGestureWaveSlowlyUpDown
254 {13, "WaveSlowlyForwardBackward"} // eGestureWaveSlowlyForwardBackward
255 };
256
257
258const uint8_t /*PROGMEM*/ MatrixGesture::initRegisterArray[219][2] = {
259 {0xEF, 0x00}, {0x32, 0x29}, {0x33, 0x01}, {0x34, 0x00}, {0x35, 0x01},
260 {0x36, 0x00}, {0x37, 0x07}, {0x38, 0x17}, {0x39, 0x06}, {0x3A, 0x12},
261 {0x3F, 0x00}, {0x40, 0x02}, {0x41, 0xFF}, {0x42, 0x01}, {0x46, 0x2D},
262 {0x47, 0x0F}, {0x48, 0x3C}, {0x49, 0x00}, {0x4A, 0x1E}, {0x4B, 0x00},
263 {0x4C, 0x20}, {0x4D, 0x00}, {0x4E, 0x1A}, {0x4F, 0x14}, {0x50, 0x00},
264 {0x51, 0x10}, {0x52, 0x00}, {0x5C, 0x02}, {0x5D, 0x00}, {0x5E, 0x10},
265 {0x5F, 0x3F}, {0x60, 0x27}, {0x61, 0x28}, {0x62, 0x00}, {0x63, 0x03},
266 {0x64, 0xF7}, {0x65, 0x03}, {0x66, 0xD9}, {0x67, 0x03}, {0x68, 0x01},
267 {0x69, 0xC8}, {0x6A, 0x40}, {0x6D, 0x04}, {0x6E, 0x00}, {0x6F, 0x00},
268 {0x70, 0x80}, {0x71, 0x00}, {0x72, 0x00}, {0x73, 0x00}, {0x74, 0xF0},
269 {0x75, 0x00}, {0x80, 0x42}, {0x81, 0x44}, {0x82, 0x04}, {0x83, 0x20},
270 {0x84, 0x20}, {0x85, 0x00}, {0x86, 0x10}, {0x87, 0x00}, {0x88, 0x05},
271 {0x89, 0x18}, {0x8A, 0x10}, {0x8B, 0x01}, {0x8C, 0x37}, {0x8D, 0x00},
272 {0x8E, 0xF0}, {0x8F, 0x81}, {0x90, 0x06}, {0x91, 0x06}, {0x92, 0x1E},
273 {0x93, 0x0D}, {0x94, 0x0A}, {0x95, 0x0A}, {0x96, 0x0C}, {0x97, 0x05},
274 {0x98, 0x0A}, {0x99, 0x41}, {0x9A, 0x14}, {0x9B, 0x0A}, {0x9C, 0x3F},
275 {0x9D, 0x33}, {0x9E, 0xAE}, {0x9F, 0xF9}, {0xA0, 0x48}, {0xA1, 0x13},
276 {0xA2, 0x10}, {0xA3, 0x08}, {0xA4, 0x30}, {0xA5, 0x19}, {0xA6, 0x10},
277 {0xA7, 0x08}, {0xA8, 0x24}, {0xA9, 0x04}, {0xAA, 0x1E}, {0xAB, 0x1E},
278 {0xCC, 0x19}, {0xCD, 0x0B}, {0xCE, 0x13}, {0xCF, 0x64}, {0xD0, 0x21},
279 {0xD1, 0x0F}, {0xD2, 0x88}, {0xE0, 0x01}, {0xE1, 0x04}, {0xE2, 0x41},
280 {0xE3, 0xD6}, {0xE4, 0x00}, {0xE5, 0x0C}, {0xE6, 0x0A}, {0xE7, 0x00},
281 {0xE8, 0x00}, {0xE9, 0x00}, {0xEE, 0x07}, {0xEF, 0x01}, {0x00, 0x1E},
282 {0x01, 0x1E}, {0x02, 0x0F}, {0x03, 0x10}, {0x04, 0x02}, {0x05, 0x00},
283 {0x06, 0xB0}, {0x07, 0x04}, {0x08, 0x0D}, {0x09, 0x0E}, {0x0A, 0x9C},
284 {0x0B, 0x04}, {0x0C, 0x05}, {0x0D, 0x0F}, {0x0E, 0x02}, {0x0F, 0x12},
285 {0x10, 0x02}, {0x11, 0x02}, {0x12, 0x00}, {0x13, 0x01}, {0x14, 0x05},
286 {0x15, 0x07}, {0x16, 0x05}, {0x17, 0x07}, {0x18, 0x01}, {0x19, 0x04},
287 {0x1A, 0x05}, {0x1B, 0x0C}, {0x1C, 0x2A}, {0x1D, 0x01}, {0x1E, 0x00},
288 {0x21, 0x00}, {0x22, 0x00}, {0x23, 0x00}, {0x25, 0x01}, {0x26, 0x00},
289 {0x27, 0x39}, {0x28, 0x7F}, {0x29, 0x08}, {0x30, 0x03}, {0x31, 0x00},
290 {0x32, 0x1A}, {0x33, 0x1A}, {0x34, 0x07}, {0x35, 0x07}, {0x36, 0x01},
291 {0x37, 0xFF}, {0x38, 0x36}, {0x39, 0x07}, {0x3A, 0x00}, {0x3E, 0xFF},
292 {0x3F, 0x00}, {0x40, 0x77}, {0x41, 0x40}, {0x42, 0x00}, {0x43, 0x30},
293 {0x44, 0xA0}, {0x45, 0x5C}, {0x46, 0x00}, {0x47, 0x00}, {0x48, 0x58},
294 {0x4A, 0x1E}, {0x4B, 0x1E}, {0x4C, 0x00}, {0x4D, 0x00}, {0x4E, 0xA0},
295 {0x4F, 0x80}, {0x50, 0x00}, {0x51, 0x00}, {0x52, 0x00}, {0x53, 0x00},
296 {0x54, 0x00}, {0x57, 0x80}, {0x59, 0x10}, {0x5A, 0x08}, {0x5B, 0x94},
297 {0x5C, 0xE8}, {0x5D, 0x08}, {0x5E, 0x3D}, {0x5F, 0x99}, {0x60, 0x45},
298 {0x61, 0x40}, {0x63, 0x2D}, {0x64, 0x02}, {0x65, 0x96}, {0x66, 0x00},
299 {0x67, 0x97}, {0x68, 0x01}, {0x69, 0xCD}, {0x6A, 0x01}, {0x6B, 0xB0},
300 {0x6C, 0x04}, {0x6D, 0x2C}, {0x6E, 0x01}, {0x6F, 0x32}, {0x71, 0x00},
301 {0x72, 0x01}, {0x73, 0x35}, {0x74, 0x00}, {0x75, 0x33}, {0x76, 0x31},
302 {0x77, 0x01}, {0x7C, 0x84}, {0x7D, 0x03}, {0x7E, 0x01}};
303
304void MatrixGesture::i2cMUXSelect() {
305 if (_ch < 0) return; // no MUX
306 _pWire->beginTransmission(ADDR_PCA954X);
307 _pWire->write((1 << _ch));
308 _pWire->endTransmission(1);
309 delayMicroseconds(300);
310}
A library for interfacing with the MATRIX Gesture Seneor PAJ7620 via I2C.
#define ERR_IC_VERSION
IC version mismatch.
#define PAJ7620_ADDR_GES_PS_DET_FLAG_1
#define ADDR_PCA954X
#define PAJ7620_ADDR_GES_PS_DET_FLAG_0
#define GES_QUIT_TIME
#define PAJ7620_PARTID
#define GES_ENTRY_TIME
When you want to recognize the Forward/Backward gestures, your gestures' reaction time must less than...
#define ERR_OK
OK.
#define PAJ7620_ADDR_PART_ID_LOW
#define ERR_DATA_BUS
Error in Data Bus.
#define PAJ7620_REGITER_BANK_SEL
#define DBG(...)
String gestureDescription(int gesture)
Get the string descritpion corresponding to the gesture number.
void setGestureHighRate(bool b)
Set gesture detection mode.
int getGesture(void)
Get gesture.
eGesture_t
gesture information
int begin(void)
init function