SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
HapticDriver_AW86224.hpp
Go to the documentation of this file.
1
31#pragma once
32
33#include "../SensorBuildOpt.h"
34#if !SENSORLIB_EXCLUDE_HAPTIC_AW86224
35
37#include "HapticBase.hpp"
38
44static constexpr uint8_t AW8624_SLAVE_ADDRESS = (0x58);
45
70{
71public:
72
78 enum class PlayMode : uint8_t {
79 STANDBY = 0,
80 RAM = 1,
81 RAM_LOOP = 2,
82 CONT = 3,
83 RTP = 4
84 };
85
91 enum class SramSize : uint8_t {
92 SRAM_1K = 0,
93 SRAM_2K = 1,
94 SRAM_3K = 2
95 };
96
102 enum class ChipID : uint8_t {
103 AW86223 = 1,
104 AW86224 = 2,
105 AW86225 = 3,
106 AW86214 = 4
107 };
108
114 enum class PwmMode : uint8_t {
115 PWM_48K = 0,
116 PWM_24K = 1,
117 PWM_12K = 2
118 };
119
120 //=========================================================================
121 // Constructor & Initialization
122 //=========================================================================
123
131
132 //=========================================================================
133 // HapticBase Interface - Lifecycle
134 //=========================================================================
135
144 void setPins(uint8_t resetPin, uint8_t intPin);
145
146 void end() override;
147 bool isReady() const override;
148 const char *getChipName() const override;
149
150 //=========================================================================
151 // HapticBase Interface - Playback Control
152 //=========================================================================
153
154 bool run() override;
155 bool stop() override;
156 bool isPlaying() const override;
157 HapticStatus getStatus() const override;
158
159 //=========================================================================
160 // HapticBase Interface - Gain Control
161 //=========================================================================
162
163 bool setGain(uint8_t gain) override;
164 uint8_t getGain() const override;
165
166 //=========================================================================
167 // HapticBase Interface - Effect Playback
168 //=========================================================================
169
170 bool playEffect(HapticEffectId effect) override;
171 bool playEffectAsync(HapticEffectId effect) override;
172 void continuousVibration(uint32_t duration_ms, bool blocking = true) override;
173 void vibrationUpdate() override;
174
175 //=========================================================================
176 // HapticBase Interface - Waveform Sequence
177 //=========================================================================
178
179 bool setSequence(uint8_t slot, HapticEffectId effect) override;
180 bool clearSequence() override;
181 bool playSequence() override;
182
183 //=========================================================================
184 // HapticBase Interface - Device Information
185 //=========================================================================
186
187 uint8_t getChipID() const override;
188 HapticCapabilities getCapabilities() const override;
189
190 //=========================================================================
191 // HapticBase Interface - Calibration
192 //=========================================================================
193
194 bool calibrate() override;
195 bool needsCalibration() const override;
196 uint32_t getF0() const override;
197 uint32_t getVbat() override;
198
199 //=========================================================================
200 // HapticBase Interface - Mode Control
201 //=========================================================================
202
203 bool setMode(HapticMode mode) override;
204 HapticMode getMode() const override;
205 HapticActuatorType getActuatorType() const override;
206 bool setActuatorType(HapticActuatorType type) override;
207
208 //=========================================================================
209 // HapticBase Interface - RTP
210 //=========================================================================
211
212 bool setRealtimeValue(uint8_t value) override;
213
214 //=========================================================================
215 // Core Playback Control
216 //=========================================================================
217
224 void softReset();
225
226 //=========================================================================
227 // Vibration Effects
228 //=========================================================================
229
252 void shortVibration(uint8_t index, uint8_t gain = 0x80, uint8_t loop = 1);
253
289 void longVibration(uint8_t index, uint8_t gain = 0x80, uint32_t duration_ms = 1000, bool blocking = true);
290
306 void stopVibration();
307
330 //=========================================================================
331 // Waveform Configuration
332 //=========================================================================
333
348 void setWaveSeq(uint8_t wav, uint8_t seq);
349
364 void setWaveLoop(uint8_t wav, uint8_t loop);
365
371 void setPlayMode(PlayMode mode);
372
380 void setTrimLRA(uint8_t val);
381
382 //=========================================================================
383 // RTP (Real-Time Playback) Mode
384 //=========================================================================
385
394 void setRtpData(uint8_t *data, uint32_t len);
395
414 bool playRtp(const uint8_t *data, uint32_t len, uint8_t gain = 0x80);
415
416 //=========================================================================
417 // Calibration Functions
418 //=========================================================================
419
429 bool loadRamData(const uint8_t *data, uint32_t len);
430
431 //=========================================================================
432 // Status & Information Getters
433 //=========================================================================
434
439 uint8_t getRamNum() const
440 {
441 return _ramNum;
442 }
443
448 bool isRamInit() const
449 {
450 return _ramInit;
451 }
452
457 uint32_t getVbatValue() const
458 {
459 return _vbat;
460 }
461
467 {
468 return _playMode;
469 }
470
475 int8_t getF0CaliData() const
476 {
477 return _f0_cali_data;
478 }
479
484 uint32_t getLraValue() const
485 {
486 return _lra;
487 }
488
489 //=========================================================================
490 // Parameter Setters (for advanced configuration)
491 //=========================================================================
492
497 void setF0Pre(uint32_t val)
498 {
499 _f0_pre = val;
500 }
501 uint32_t getF0Pre() const
502 {
503 return _f0_pre;
504 }
505
510 void setLraVrms(uint32_t val)
511 {
512 _lra_vrms = val;
513 }
514 uint32_t getLraVrms() const
515 {
516 return _lra_vrms;
517 }
518
523 void setContBrkTime(uint8_t val)
524 {
525 _cont_brk_time = val;
526 }
527 uint8_t getContBrkTime() const
528 {
529 return _cont_brk_time;
530 }
531
536 void setContBrkGain(uint8_t val)
537 {
538 _cont_brk_gain = val;
539 }
540 uint8_t getContBrkGain() const
541 {
542 return _cont_brk_gain;
543 }
544
549 void setContDrv1Lvl(uint8_t val)
550 {
551 _cont_drv1_lvl = val;
552 }
553 uint8_t getContDrv1Lvl() const
554 {
555 return _cont_drv1_lvl;
556 }
557
562 void setContDrv1Time(uint8_t val)
563 {
564 _cont_drv1_time = val;
565 }
566 uint8_t getContDrv1Time() const
567 {
568 return _cont_drv1_time;
569 }
570
575 void setContDrv2Time(uint8_t val)
576 {
577 _cont_drv2_time = val;
578 }
579 uint8_t getContDrv2Time() const
580 {
581 return _cont_drv2_time;
582 }
583
588 void setContTrackMargin(uint8_t val)
589 {
590 _cont_track_margin = val;
591 }
592 uint8_t getContTrackMargin() const
593 {
594 return _cont_track_margin;
595 }
596
601 void setD2sGain(uint8_t val)
602 {
603 _d2s_gain = val;
604 }
605 uint8_t getD2sGain() const
606 {
607 return _d2s_gain;
608 }
609
614 void setPwmcfg4Prtime(uint8_t val)
615 {
616 _pwmcfg4_prtime = val;
617 }
618 uint8_t getPwmcfg4Prtime() const
619 {
620 return _pwmcfg4_prtime;
621 }
622
627 void setPwmcfg3Prlvl(uint8_t val)
628 {
629 _pwmcfg3_prlvl = val;
630 }
631 uint8_t getPwmcfg3Prlvl() const
632 {
633 return _pwmcfg3_prlvl;
634 }
635
640 void setAutoBrkEnabled(bool val)
641 {
642 _auto_brk_enabled = val;
643 }
644 bool getAutoBrkEnabled() const
645 {
646 return _auto_brk_enabled;
647 }
648
653 void setF0CaliPercent(uint8_t val)
654 {
655 _f0_cali_percent = val;
656 }
657 uint8_t getF0CaliPercent() const
658 {
659 return _f0_cali_percent;
660 }
661
666 bool isVibrating() const
667 {
668 return isPlaying();
669 }
670
671private:
672 // Internal helper functions
673 bool waitRtpGo(uint32_t timeout_ms);
674
681 void printRegs();
682
683 // RAM initialization
684 void ramInit(bool enable);
685 void playStop();
686 void reset();
687 bool checkQualify();
688 bool offsetCalibration();
689 int selectD2sGain(uint8_t reg);
690 bool measureF0();
691 bool measureVbat();
692 void getLraResistance();
693 void setSramSize(SramSize size);
694 void setFifoAddr();
695 void setBaseAddr();
696 void setRamAddr();
697 bool getBaseAddr(const uint8_t *data, uint32_t len);
698 void setRepeatSeq(uint8_t seq);
699 void autoBrkConfig(bool enable);
700 void setPwm(PwmMode mode);
701 void playMode(PlayMode mode);
702 void config();
703 void protectConfig(uint8_t prtime, uint8_t prlvl);
704 void miscParaInit();
705 void trigInit();
706 void contConfig();
707 void interruptSetup();
708 void vbatModeConfig(uint8_t flag);
709 void calculateCaliData();
710 bool parseChipID();
711 void hapticInit();
712 void f0Calibration();
713 bool judgeF0WithinRange();
714 uint8_t getGlobalState() const;
715 bool waitStandby(uint32_t timeout_ms = 100);
716 int getIrqState();
717 void irqClear();
718 uint8_t rtpGetFifoAfs();
719 void setRtpAei(bool enable);
720 void cancelVibration();
721 void ram_vbat_comp(bool flag);
722
723 // Calculation formulas
724 inline uint8_t drv2_lvl_formula(uint32_t f0, uint32_t vrms)
725 {
726 uint32_t base = (f0 < 1800) ? 1809920 : 1990912;
727 return static_cast<uint8_t>((base / 1000 * vrms) / 61000);
728 }
729
730 bool initImpl(uint8_t param);
731
732 void afterInitSuccess(uint8_t param) override
733 {
734 setReady(true);
735 }
736
737 inline uint32_t vbat_formula(uint32_t value)
738 {
739 return (6100 * (value) / 1024);
740 }
741
742 inline uint32_t f0_formula(uint32_t value)
743 {
744 return (384000 * 10 / (value));
745 }
746
747 inline uint32_t rl_formula(uint32_t value, uint32_t gain)
748 {
749 return (((value) * 678 * 100) / (1024 * (gain)));
750 }
751
752 inline int os_formula(int value, int d2s_gain)
753 {
754 return (2440 * (value - 512) / (1024 * (d2s_gain + 1)));
755 }
756
757 // Member variables
758 int _rstPin;
759 int _intPin;
760 uint8_t _chipID;
761 PlayMode _playMode;
762 uint8_t _gain;
763 uint32_t _f0;
764 uint32_t _vbat;
765 uint32_t _lra;
766 bool _ramInit;
767 uint8_t _cont_drv2_lvl;
768 int8_t _f0_cali_data;
769 uint32_t _f0_pre;
770 uint16_t _ramBaseAddr;
771 uint8_t _ramNum;
772 uint32_t _lra_vrms;
773 uint8_t _cont_brk_time;
774 uint8_t _cont_brk_gain;
775 uint8_t _cont_drv1_lvl;
776 uint8_t _cont_drv1_time;
777 uint8_t _cont_drv2_time;
778 uint8_t _cont_track_margin;
779 uint8_t _d2s_gain;
780 uint8_t _pwmcfg4_prtime;
781 uint8_t _pwmcfg3_prlvl;
782 bool _auto_brk_enabled;
783 uint8_t _f0_cali_percent;
784 uint32_t _duration;
785 uint32_t _vibrationStartTime;
786 bool _isCalibrated;
787};
788
789#endif
@license MIT License
HapticStatus
Vibration playback status.
HapticActuatorType
Actuator type.
HapticMode
Generic haptic operating mode.
uint16_t HapticEffectId
Haptic effect ID type.
@license MIT License
void loop()
Abstract base class for haptic drivers.
void setReady(bool ready)
Set ready state.
AW86224/86225 Haptic Driver Class.
void end() override
Deinitialize the haptic driver and release resources.
void setContDrv2Time(uint8_t val)
Set continuous mode drive time 2.
void setPlayMode(PlayMode mode)
Set playback mode.
bool setSequence(uint8_t slot, HapticEffectId effect) override
Set a waveform in the sequence.
void setContDrv1Lvl(uint8_t val)
Set continuous mode drive level 1.
void setPwmcfg4Prtime(uint8_t val)
Set protection time.
bool playEffectAsync(HapticEffectId effect) override
Play a haptic effect (non-blocking).
uint32_t getLraValue() const
Get LRA resistance.
void setWaveLoop(uint8_t wav, uint8_t loop)
Set waveform loop count.
@ PWM_12K
12kHz PWM rate (default)
uint8_t getRamNum() const
Get number of loaded waveforms.
void stopVibration()
Stop any ongoing vibration.
void setWaveSeq(uint8_t wav, uint8_t seq)
Update vibration state (for non-blocking mode)
bool calibrate() override
Perform auto-calibration.
bool isPlaying() const override
Check if haptic is currently playing.
void setF0Pre(uint32_t val)
Set expected F0 (resonant frequency)
void setTrimLRA(uint8_t val)
Set LRA frequency trim value.
bool setGain(uint8_t gain) override
Set the gain/amplitude (0x00 to 0xFF).
uint32_t getVbat() override
Get the battery voltage.
bool loadRamData(const uint8_t *data, uint32_t len)
Load RAM waveform data.
void setPins(uint8_t resetPin, uint8_t intPin)
setPins
bool stop() override
Stop haptic playback immediately.
bool setMode(HapticMode mode) override
Set the operating mode.
uint32_t getF0() const override
Get the measured resonant frequency (F0).
HapticMode getMode() const override
Get the current operating mode.
int8_t getF0CaliData() const
Get F0 calibration data.
void setRtpData(uint8_t *data, uint32_t len)
Set RTP waveform data.
bool playRtp(const uint8_t *data, uint32_t len, uint8_t gain=0x80)
Play RTP (Real-Time Playback) waveform.
void setContBrkGain(uint8_t val)
Set continuous mode break gain.
bool setActuatorType(HapticActuatorType type) override
Set the actuator type.
uint8_t getGain() const override
Get the current gain value.
bool setRealtimeValue(uint8_t value) override
Set RTP (real-time playback) value.
bool isReady() const override
Check if the driver is initialized and ready.
void setLraVrms(uint32_t val)
Set LRA rated voltage.
void setContDrv1Time(uint8_t val)
Set continuous mode drive time 1.
void softReset()
Perform soft reset.
void setContTrackMargin(uint8_t val)
Set continuous mode track margin.
void vibrationUpdate() override
Update vibration state (for non-blocking mode).
void setAutoBrkEnabled(bool val)
Enable/disable auto break.
PlayMode getPlayMode() const
Get current play mode.
void setF0CaliPercent(uint8_t val)
Set F0 calibration tolerance percentage.
uint32_t getVbatValue() const
Get current voltage reading.
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 run() override
Start haptic playback.
bool isVibrating() const
Check if vibration is currently active (alias for isPlaying)
bool clearSequence() override
Clear the waveform sequence.
HapticDriver_AW86224()
Default constructor.
void shortVibration(uint8_t index, uint8_t gain=0x80, uint8_t loop=1)
Play a short vibration effect.
SramSize
SRAM size selection.
bool isRamInit() const
Check if RAM is initialized.
HapticCapabilities getCapabilities() const override
Get device capabilities.
void setD2sGain(uint8_t val)
Set D2S gain (digital to simulation gain)
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.
PlayMode
Playback mode enumeration.
@ CONT
Continuous mode with F0 tracking.
@ RAM_LOOP
RAM loop mode, continuous playback.
@ RAM
RAM playback mode, single play.
@ STANDBY
Standby mode, minimal power consumption.
@ RTP
Real-time playback mode.
bool needsCalibration() const override
Check if calibration is needed.
uint8_t getChipID() const override
Get the chip ID.
void setPwmcfg3Prlvl(uint8_t val)
Set protection level.
void setContBrkTime(uint8_t val)
Set continuous mode break time.
bool playSequence() override
Play the waveform sequence.
Device capability flags.