29#include "../SensorBuildOpt.h"
30#if !SENSORLIB_EXCLUDE_HAPTIC_DRV2605
44 for (
int i = 0;
i < 8;
i++) {
76 return writeReg(DRV2605_REG_GO, 1) == 0;
81 return writeReg(DRV2605_REG_GO, 0) == 0;
86 int value =
readReg(DRV2605_REG_GO);
87 return value > 0 && value != -1;
124 if (_isStandby)
return false;
147 if (slot >= 8)
return false;
148 _waveformSeq[slot] =
static_cast<uint8_t
>(effect);
155 for (uint8_t
i = 0;
i < 8;
i++) {
186 int chipID =
readReg(DRV2605_REG_STATUS);
187 if (chipID < 0)
return 0;
188 return static_cast<uint8_t
>(chipID >> 5);
244 if (
writeReg(DRV2605_REG_MODE, value) == 0) {
256 int value =
readReg(DRV2605_REG_MODE);
265 return writeReg(DRV2605_REG_RTPIN, value) == 0;
270 int value =
readReg(DRV2605_REG_RTPIN);
271 return value >= 0 ?
static_cast<uint8_t
>(value) : 0;
287 int value =
readReg(DRV2605_REG_LRARESON);
288 if (value < 0)
return 0;
290 uint8_t period =
static_cast<uint8_t
>(value & 0x7F);
291 if (period == 0)
return 0;
293 return static_cast<uint32_t
>(1000000 / (period * 75));
298 int vbat =
readReg(DRV2605_REG_VBAT);
302 uint32_t mv = (
static_cast<float>(vbat) * 5.6 / 255) * 1000;
308 writeReg(
static_cast<uint8_t
>(DRV2605_REG_WAVESEQ1 + slot), w);
313 _currentLibrary = lib;
319 int value =
readReg(DRV2605_REG_GO);
330 writeReg(DRV2605_REG_RATEDV, value);
335 writeReg(DRV2605_REG_CLAMPV, value);
343 uint32_t start =
hal->millis();
344 while (!
isDone() && (
hal->millis() - start < 1000)) {
349 int comp =
readReg(DRV2605_REG_AUTOCALCOMP);
350 int backEmf =
readReg(DRV2605_REG_AUTOCALEMP);
354 if (comp >= 0 && backEmf >= 0) {
364 int value =
readReg(DRV2605_REG_CONTROL2);
371 writeReg(DRV2605_REG_CONTROL2,
static_cast<uint8_t
>(value));
377 writeReg(DRV2605_REG_OVERDRIVE, cycles);
387 int value =
readReg(DRV2605_REG_CONTROL3);
394 writeReg(DRV2605_REG_CONTROL3,
static_cast<uint8_t
>(value));
406 writeReg(DRV2605_REG_CONTROL4, value);
411 int value =
readReg(DRV2605_REG_LRARESON);
412 return value >= 0 ?
static_cast<uint8_t
>(value & 0x7F) : 0;
415bool HapticDriver_DRV2605::initImpl(uint8_t param)
419 int chipID =
readReg(DRV2605_REG_STATUS);
427 case DRV2604_CHIP_ID:
428 case DRV2605_CHIP_ID:
429 case DRV2604L_CHIP_ID:
430 case DRV2605L_CHIP_ID:
431 case DRV2605X_CHIP_ID:
444 writeReg(DRV2605_REG_SUSTAINPOS, 0);
445 writeReg(DRV2605_REG_SUSTAINNEG, 0);
447 writeReg(DRV2605_REG_AUDIOMAX, 0x64);
449 int value =
readReg(DRV2605_REG_FEEDBACK);
453 writeReg(DRV2605_REG_FEEDBACK,
static_cast<uint8_t
>(value & 0x7F));
455 int control3 =
readReg(DRV2605_REG_CONTROL3);
456 if (control3 == -1) {
459 writeReg(DRV2605_REG_CONTROL3,
static_cast<uint8_t
>(control3 | 0x20));
HapticStatus
Vibration playback status.
@ PLAYING
Currently playing.
HapticActuatorType
Actuator type.
@ LRA
Linear Resonant Actuator.
@ ERM
Eccentric Rotating Mass motor.
HapticMode
Generic haptic operating mode.
@ INTERNAL_TRIGGER
Play by selecting waveforms/effects and calling run()
@ EXT_TRIGGER_LEVEL
External trigger on level (pin state)
@ AUTO_CALIBRATE
Auto-calibration mode.
@ DIAGNOSTICS
Diagnostics mode.
@ PWM_ANALOG
PWM or analog input drive.
@ AUDIO_TO_VIBE
Audio input to vibration.
@ EXT_TRIGGER_EDGE
External trigger on edge (pin interrupt)
@ REAL_TIME_PLAYBACK
RTP mode (direct drive value)
uint16_t HapticEffectId
Haptic effect ID type.
std::unique_ptr< SensorHal > hal
Abstract base class for haptic drivers.
void setReady(bool ready)
Set ready state.
bool isDone() const
Check if playback is done.
uint32_t getVbat() override
Get the battery voltage.
void setAutoBrake(bool enable)
Enable/disable automatic braking.
void setRtpOverdrive(bool enable)
Enable/disable RTP overdrive.
bool setActuatorType(HapticActuatorType type) override
Set the actuator type.
HapticActuatorType getActuatorType() const override
Get the actuator type.
bool calibrate() override
Perform auto-calibration.
uint32_t getF0() const override
Get the measured resonant frequency (F0).
void setCtrl4(uint8_t value)
Set the control4 register value.
bool run() override
Start haptic playback.
~HapticDriver_DRV2605() override
Destructor.
void setRatedVoltage(uint8_t value)
Set the rated voltage for auto-calibration.
bool isReady() const override
Check if the driver is initialized and ready.
bool playEffectAsync(HapticEffectId effect) override
Play a haptic effect (non-blocking).
void selectLibrary(uint8_t lib)
Select the ROM waveform library.
void setBrakeTime(uint8_t time)
Set the brake time offset.
bool isPlaying() const override
Check if haptic is currently playing.
void end() override
Deinitialize the driver and release resources.
void continuousVibration(uint32_t duration_ms, bool blocking=true) override
Play a continuous vibration for specified duration.
void emergencyStop() override
Emergency stop - stops all vibration immediately.
int autoCal()
Perform auto-calibration and return compensation result.
void setOverdriveClamp(uint8_t value)
Set the overdrive clamp voltage for auto-calibration.
void setOverdriveTime(uint8_t cycles)
Set the number of overdrive cycles.
uint8_t getRealtimeValue() const
Get the current RTP value.
bool setMode(HapticMode mode) override
Set the operating mode.
void vibrationUpdate() override
Update vibration state (for non-blocking mode).
bool setSequence(uint8_t slot, HapticEffectId effect) override
Set a waveform in the sequence.
HapticStatus getStatus() const override
Get current playback status.
uint8_t getChipID() const override
Get the chip ID.
bool setGain(uint8_t gain) override
Set the gain/amplitude.
bool playSequence() override
Play the waveform sequence.
uint8_t getGain() const override
Get the current gain value.
void setERMLRA(bool erm)
Enable/disable ERM/LRA mode.
bool playEffect(HapticEffectId effect) override
Play a haptic effect.
const char * getChipName() const override
Get the chip name.
HapticMode getMode() const override
Get the current operating mode.
bool stop() override
Stop haptic playback immediately.
bool clearSequence() override
Clear the waveform sequence.
HapticDriver_DRV2605()
Construct an uninitialized driver instance.
void trigger()
Trigger a single playback cycle.
HapticCapabilities getCapabilities() const override
Get device capabilities.
bool setRealtimeValue(uint8_t value) override
Set RTP (real-time playback) value.
uint8_t getLRAFrequency() const
Read the LRA resonance period (for LRA calibration).
bool needsCalibration() const override
Check if calibration is needed.
void setWaveform(uint8_t slot, uint8_t w)
Set a waveform entry in the playback sequence table.
int readReg(uint8_t reg) const
int writeReg(uint8_t reg, uint8_t val)
int updateBits(uint8_t reg, uint8_t mask, uint8_t value_shifted)
uint8_t maxSequenceLength
Maximum waveform sequence length.
uint8_t hasF0Calibration
Supports F0 (resonant frequency) calibration.
uint16_t maxDurationMs
Maximum vibration duration in ms (0 = unlimited)
uint8_t hasAutoCalibration
Supports auto-calibration.
uint8_t hasVbatCompensation
Supports automatic voltage compensation.
uint8_t hasRTP
Supports Real-Time Playback mode.
uint8_t hasBreakEffect
Supports brake/break effects.
uint8_t hasOverdrive
Supports overdrive effects.
uint8_t maxEffectCount
Maximum number of effect IDs supported.
uint8_t hasContinuousMode
Supports continuous vibration mode.
uint8_t hasSequence
Supports waveform sequencing.