SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
aw86224_full.ino
Go to the documentation of this file.
1
30#include <HapticDrivers.hpp>
31#include <SensorWireHelper.h>
32
33#ifndef SENSOR_SDA
34#define SENSOR_SDA 20
35#endif
36
37#ifndef SENSOR_SCL
38#define SENSOR_SCL 21
39#endif
40
42
43// LilyGo T-Display-P4 peripherals power control pins
44#ifdef ARDUINO_ESP32P4_DEV
45#include <IoExpanderDrv.hpp>
46#define IO_EXPANDER_3V3_POWER_EN 0
47#define IO_EXPANDER_5V0_POWER_EN 6
48#define IO_EXPANDER_P4_VCCA_POWER_EN 8
49#define P4_TOUCH_SDA 7
50#define P4_TOUCH_SCL 8
52#endif
53
54
56{
57 Serial.println("");
58 Serial.println("╔══════════════════════════════════════════╗");
59 Serial.println("║ AW86224 Haptic Driver Test ║");
60 Serial.println("╚══════════════════════════════════════════╝");
61 Serial.println("");
62}
63
65{
66 Serial.println("\n[TEST] Device Capabilities");
67 auto cap = haptic.getCapabilities();
68
69 Serial.print(" F0 Calibration: "); Serial.println(cap.hasF0Calibration ? "Yes" : "No");
70 Serial.print(" VBAT Compensation: "); Serial.println(cap.hasVbatCompensation ? "Yes" : "No");
71 Serial.print(" RTP Mode: "); Serial.println(cap.hasRTP ? "Yes" : "No");
72 Serial.print(" Sequence: "); Serial.println(cap.hasSequence ? "Yes" : "No");
73 Serial.print(" Break Effect: "); Serial.println(cap.hasBreakEffect ? "Yes" : "No");
74 Serial.print(" Continuous Mode: "); Serial.println(cap.hasContinuousMode ? "Yes" : "No");
75 Serial.print(" Max Effects: "); Serial.println(cap.maxEffectCount);
76 Serial.print(" Max Sequence: "); Serial.println(cap.maxSequenceLength);
77 Serial.print(" Max Duration: "); Serial.println(cap.maxDurationMs == 0 ? "Unlimited" : String(cap.maxDurationMs));
78 Serial.println("[OK] Done");
79}
80
82{
83 Serial.println("\n=== Driver Initialization ===");
84 Serial.print("Wire: SDA="); Serial.print(SENSOR_SDA); Serial.print(", SCL="); Serial.println(SENSOR_SCL);
85
86#ifdef ARDUINO_ESP32P4_DEV
87 // Enable power supply for T-Display-P4 peripherals
88 Wire1.begin(P4_TOUCH_SDA, P4_TOUCH_SCL);
89 SensorWireHelper::dumpDevices(Wire1);
90
91 if (!expander.begin(Wire1, XL9555_SLAVE_ADDRESS0)) {
92 while (1) {
93 Serial.println("Failed to find XL9555 - check your wiring!");
94 delay(1000);
95 }
96 }
97 Serial.println("Sensor Expander Initialized");
98
99 expander.pinMode(IO_EXPANDER_P4_VCCA_POWER_EN, OUTPUT);
100 expander.digitalWrite(IO_EXPANDER_P4_VCCA_POWER_EN, LOW);
101
102 expander.pinMode(IO_EXPANDER_5V0_POWER_EN, OUTPUT);
103 expander.digitalWrite(IO_EXPANDER_5V0_POWER_EN, HIGH);
104
105 expander.pinMode(IO_EXPANDER_3V3_POWER_EN, OUTPUT);
106 expander.digitalWrite(IO_EXPANDER_3V3_POWER_EN, LOW);
107
108 Wire.begin(SENSOR_SDA, SENSOR_SCL);
109 SensorWireHelper::dumpDevices(Wire);
110#endif
111
112 if (!haptic.begin(Wire, AW8624_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
113 Serial.println("[FATAL] AW86224 init failed!");
114 while (1) delay(1000);
115 }
116
117 Serial.println("[OK] Driver initialized");
118 Serial.print(" Chip Name: "); Serial.println(haptic.getChipName());
119 Serial.print(" Chip ID: 0x"); Serial.println(haptic.getChipID(), HEX);
120 Serial.print(" Actuator: "); Serial.println(haptic.getActuatorType() == HapticActuatorType::LRA ? "LRA" : "ERM");
121 Serial.print(" F0: "); Serial.print(haptic.getF0()); Serial.println(" Hz");
122 Serial.print(" VBAT: "); Serial.print(haptic.getVbat()); Serial.println(" mV");
123 Serial.print(" RAM Waveforms: "); Serial.println(haptic.getRamNum());
124 Serial.print(" F0 Cali Data: 0x"); Serial.println(haptic.getF0CaliData(), HEX);
125}
126
128{
129 Serial.println("\n[TEST] playEffect() - HapticBase Interface");
130 Serial.println(" Playing effect 1...");
132 delay(300);
133 Serial.println("[OK] Done");
134}
135
137{
138 Serial.println("\n[TEST] playEffect() - All Waveforms");
139 for (uint8_t i = 1; i <= haptic.getRamNum(); i++) {
140 Serial.print(" Effect "); Serial.print(i); Serial.println("...");
142 delay(400);
143 }
144 Serial.println("[OK] Done");
145}
146
148{
149 Serial.println("\n[TEST] shortVibration()");
150 Serial.println(" index=1, gain=0x80, loop=1");
151 haptic.shortVibration(1, 0x80, 1);
152 Serial.println("[OK] Done");
153}
154
156{
157 Serial.println("\n[TEST] shortVibration() - Different Gains");
158
159 uint8_t gains[] = {0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xFF};
160 for (size_t i = 0; i < sizeof(gains); i++) {
161 Serial.print(" Gain 0x"); Serial.print(gains[i], HEX); Serial.println("...");
162 haptic.shortVibration(1, gains[i], 1);
163 delay(300);
164 }
165 Serial.println("[OK] Done");
166}
167
169{
170 Serial.println("\n[TEST] shortVibration() - Loop Count");
171 for (uint8_t loop = 1; loop <= 5; loop++) {
172 Serial.print(" Loop "); Serial.print(loop); Serial.println("...");
173 haptic.shortVibration(1, 0x80, loop);
174 delay(500);
175 }
176 Serial.println("[OK] Done");
177}
178
180{
181 Serial.println("\n[TEST] continuousVibration() - HapticBase Interface");
182 Serial.println(" Using unified interface: 2000ms blocking");
183 haptic.setGain(0x80);
184 haptic.continuousVibration(2000, true);
185 Serial.println("[OK] Done");
186}
187
189{
190 Serial.println("\n[TEST] longVibration() - Blocking Mode");
191 Serial.println(" index=2, gain=0x80, duration=2000ms, blocking=true");
192 uint32_t start = millis();
193 haptic.longVibration(2, 0x80, 2000, true);
194 uint32_t elapsed = millis() - start;
195 Serial.print("[OK] Done (elapsed: "); Serial.print(elapsed); Serial.println(" ms)");
196}
197
199{
200 Serial.println("\n[TEST] longVibration() - Non-Blocking Mode");
201 Serial.println(" index=2, gain=0x80, duration=5000ms, blocking=false");
202 haptic.longVibration(2, 0x80, 5000, false);
203 Serial.println(" Started! Use 's' to stop or wait...");
204}
205
207{
208 Serial.println("\n[TEST] Waveform Switching (Non-Blocking)");
209 Serial.println(" Rapid switching between waveforms during vibration");
210
211 haptic.longVibration(1, 0x80, 5000, false);
212 delay(500);
213
214 for (int i = 0; i < 3; i++) {
215 for (uint8_t w = 1; w <= haptic.getRamNum(); w++) {
216 if (!haptic.isPlaying()) break;
217 Serial.print(" Switch to waveform "); Serial.print(w); Serial.println("...");
218 haptic.longVibration(w, 0x80, 1000, false);
219 delay(100);
220 }
221 }
222
223 if (haptic.isPlaying()) {
224 Serial.println(" Stopping...");
225 haptic.stop();
226 }
227 Serial.println("[OK] Done");
228}
229
231{
232 Serial.println("\n[TEST] setSequence() - HapticBase Interface");
234 haptic.setSequence(0, 1);
235 haptic.setSequence(1, 2);
236 haptic.setSequence(2, 0);
237 Serial.println(" Sequence set: [1, 2, 0]");
238 Serial.println(" Playing sequence...");
240 delay(1000);
241 Serial.println("[OK] Done");
242}
243
245{
246 Serial.println("\n[TEST] stop() - HapticBase Interface");
247 haptic.continuousVibration(5000, false);
248 Serial.println(" Started 5s vibration...");
249 delay(1000);
250 Serial.println(" Calling stop()...");
251 haptic.stop();
252 Serial.print(" isPlaying: "); Serial.println(haptic.isPlaying() ? "Yes" : "No");
253 Serial.println("[OK] Done");
254}
255
257{
258 Serial.println("\n[TEST] getStatus() - HapticBase Interface");
259 HapticStatus status = haptic.getStatus();
260 const char *statusStr = "UNKNOWN";
261 switch (status) {
262 case HapticStatus::IDLE: statusStr = "IDLE"; break;
263 case HapticStatus::PLAYING: statusStr = "PLAYING"; break;
264 case HapticStatus::PAUSED: statusStr = "PAUSED"; break;
265 case HapticStatus::CALIBRATING: statusStr = "CALIBRATING"; break;
266 case HapticStatus::ERROR: statusStr = "ERROR"; break;
267 case HapticStatus::STANDBY: statusStr = "STANDBY"; break;
268 }
269 Serial.print(" Status: "); Serial.println(statusStr);
270 Serial.println("[OK] Done");
271}
272
274{
275 Serial.println("\n[TEST] Intensity Sweep");
276 Serial.println(" Gradually increasing intensity");
277
278 for (uint16_t gain = 0x20; gain <= 0xFF; gain += 0x20) {
279 Serial.print(" Gain: 0x"); Serial.println(gain, HEX);
280 haptic.shortVibration(1, gain, 3);
281 delay(400);
282 }
283 Serial.println("[OK] Done");
284}
285
287{
288 Serial.println("\n=== Auto Demo Sequence ===");
289 delay(1000);
290
291 Serial.println("\n[1/12] Basic playEffect()...");
293 delay(800);
294
295 Serial.println("\n[2/12] Basic short vibration...");
296 haptic.shortVibration(1, 0x80, 1);
297 delay(800);
298
299 Serial.println("\n[3/12] All waveforms (short)...");
300 for (uint8_t i = 1; i <= haptic.getRamNum(); i++) {
301 Serial.print(" Wave "); Serial.print(i); Serial.println("...");
302 haptic.shortVibration(i, 0x80, 1);
303 delay(400);
304 }
305
306 Serial.println("\n[4/12] Continuous vibration (HapticBase)...");
307 haptic.continuousVibration(1500, true);
308
309 Serial.println("\n[5/12] Long vibration (2s)...");
310 haptic.longVibration(2, 0x80, 2000, true);
311
312 Serial.println("\n[6/12] Intensity sweep...");
314
315 Serial.println("\n[7/12] Non-blocking long vibration (3s)...");
316 haptic.longVibration(2, 0xA0, 3000, false);
317 uint32_t start = millis();
318 while (haptic.isPlaying() && (millis() - start < 3500)) {
319 delay(100);
320 }
321 if (haptic.isPlaying()) {
322 haptic.stop();
323 Serial.println(" Stopped by demo");
324 }
325
326 Serial.println("\n[8/12] Sequence test...");
328 delay(500);
329
330 Serial.println("\n[9/12] Status check...");
331 testStatus();
332
333 Serial.println("\n[10/12] Rapid waveform switching...");
335
336 Serial.println("\n[11/12] Gain test...");
338
339 Serial.println("\n[12/12] Final short vibration...");
340 haptic.shortVibration(1, 0xFF, 2);
341
342 Serial.println("\n=== Demo Complete ===");
343}
344
346{
347 Serial.println("\n========== Commands ==========");
348 Serial.println("Initialization:");
349 Serial.println(" i - Re-initialize driver");
350 Serial.println("");
351 Serial.println("HapticBase Interface:");
352 Serial.println(" e - playEffect(1)");
353 Serial.println(" E - playEffect() all waveforms");
354 Serial.println(" c - continuousVibration()");
355 Serial.println(" S - setSequence() test");
356 Serial.println(" p - stop() test");
357 Serial.println(" t - getStatus()");
358 Serial.println("");
359 Serial.println("AW86224 Extended:");
360 Serial.println(" 1 - Short vibration");
361 Serial.println(" a - All waveforms (short)");
362 Serial.println(" g - Different gains test");
363 Serial.println(" l - Loop count test (1-5)");
364 Serial.println(" 2 - Long vibration (blocking, 2s)");
365 Serial.println(" L - Long vibration all waveforms");
366 Serial.println(" n - Non-blocking long vibration (5s)");
367 Serial.println(" w - Waveform switching");
368 Serial.println("");
369 Serial.println("Special Tests:");
370 Serial.println(" x - Sweep: gradual intensity increase");
371 Serial.println(" d - Auto demo sequence");
372 Serial.println("");
373 Serial.println("Info:");
374 Serial.println(" k - Show capabilities");
375 Serial.println(" v - Read voltage");
376 Serial.println(" f - Read F0");
377 Serial.println(" h - Help (this message)");
378 Serial.println("==============================");
379}
380
381void setup()
382{
383 Serial.begin(115200);
384 delay(1000);
385
386 printBanner();
387 testInit();
389
390 Serial.println("\n========== Quick Test ==========");
391 Serial.println("Running auto demo sequence...");
392
393 testAutoDemo();
394
395 printHelp();
396}
397
398void loop()
399{
400 if (haptic.isPlaying()) {
402 }
403
404 if (Serial.available()) {
405 char cmd = Serial.read();
406
407 switch (cmd) {
408 case 'i':
409 testInit();
410 break;
411 case 'k':
413 break;
414 case 'e':
416 break;
417 case 'E':
419 break;
420 case 'c':
422 break;
423 case 'S':
425 break;
426 case 'p':
427 testStop();
428 break;
429 case 't':
430 testStatus();
431 break;
432 case '1':
434 break;
435 case 'a':
437 break;
438 case 'g':
440 break;
441 case 'l':
443 break;
444 case '2':
446 break;
447 case 'L':
448 for (uint8_t i = 1; i <= haptic.getRamNum(); i++) {
449 Serial.print(" Waveform "); Serial.print(i); Serial.println(", 1000ms...");
450 haptic.longVibration(i, 0x80, 1000, true);
451 delay(200);
452 }
453 Serial.println("[OK] Done");
454 break;
455 case 'n':
457 break;
458 case 'w':
460 break;
461 case 'x':
463 break;
464 case 'd':
465 testAutoDemo();
466 break;
467 case 'v':
468 Serial.print("[VBAT] "); Serial.print(haptic.getVbat()); Serial.println(" mV");
469 break;
470 case 'f':
471 Serial.print("[F0] "); Serial.print(haptic.getF0()); Serial.println(" Hz");
472 break;
473 case 'h':
474 case 'H':
475 case '?':
476 printHelp();
477 break;
478 default:
479 if (cmd != '\n' && cmd != '\r' && cmd != ' ') {
480 Serial.print("[?] Unknown: '"); Serial.print(cmd); Serial.print(" (0x"); Serial.print(cmd, HEX); Serial.println(")");
481 }
482 break;
483 }
484 }
485
486 delay(10);
487}
HapticStatus
Vibration playback status.
@ PLAYING
Currently playing.
@ PAUSED
Playback paused.
@ IDLE
Not playing.
@ CALIBRATING
Calibrating.
@ ERROR
Error state.
@ STANDBY
Standby mode.
@ LRA
Linear Resonant Actuator.
@license MIT License
@license MIT License
@license MIT License
void testCapabilities()
void testInit()
void printHelp()
void testStop()
void testShortVibrationGain()
void setup()
void testPlayEffect()
void testShortVibrationLoop()
void testWaveformSwitching()
void testSetSequence()
#define SENSOR_SCL
void testShortVibration()
void testIntensitySweep()
void testLongVibrationNonBlocking()
HapticDriver_AW86224 haptic
#define SENSOR_SDA
void testContinuousVibration()
void testLongVibrationBlocking()
void testStatus()
void testAutoDemo()
void testPlayEffectAll()
void printBanner()
void loop()
AW86224/86225 Haptic Driver Class.
bool setSequence(uint8_t slot, HapticEffectId effect) override
Set a waveform in the sequence.
uint8_t getRamNum() const
Get number of loaded waveforms.
bool isPlaying() const override
Check if haptic is currently playing.
bool setGain(uint8_t gain) override
Set the gain/amplitude (0x00 to 0xFF).
uint32_t getVbat() override
Get the battery voltage.
bool stop() override
Stop haptic playback immediately.
uint32_t getF0() const override
Get the measured resonant frequency (F0).
int8_t getF0CaliData() const
Get F0 calibration data.
void vibrationUpdate() override
Update vibration state (for non-blocking mode).
void continuousVibration(uint32_t duration_ms, bool blocking=true) override
Play a continuous vibration for specified duration.
HapticActuatorType getActuatorType() const override
Get the actuator type.
bool playEffect(HapticEffectId effect) override
Play a haptic effect.
HapticStatus getStatus() const override
Get current playback status.
bool clearSequence() override
Clear the waveform sequence.
void shortVibration(uint8_t index, uint8_t gain=0x80, uint8_t loop=1)
Play a short vibration effect.
HapticCapabilities getCapabilities() const override
Get device capabilities.
const char * getChipName() const override
Get the chip name.
void longVibration(uint8_t index, uint8_t gain=0x80, uint32_t duration_ms=1000, bool blocking=true)
Play a long continuous vibration effect.
uint8_t getChipID() const override
Get the chip ID.
bool playSequence() override
Play the waveform sequence.
bool begin(SensorCommCustom::CustomCallback callback, uint8_t addr)
Initialize the sensor using custom callback interface.
bool begin(SensorCommCustom::CustomCallback cb, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
Initialize the sensor using custom callback interface.
void pinMode(uint8_t pin, uint8_t mode)
Set the direction of a GPIO pin.
void digitalWrite(uint8_t pin, bool value)
Write a digital value to an output pin.
Concrete driver for the XL9555 16‑bit I2C GPIO expander.
#define P4_TOUCH_SCL
#define P4_TOUCH_SDA
IoExpanderPCA9570 expander
uint8_t i