SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
drv2605_effect.ino
Go to the documentation of this file.
1
29#include <HapticDrivers.hpp>
30
31#ifndef SENSOR_SDA
32#define SENSOR_SDA 3
33#endif
34
35#ifndef SENSOR_SCL
36#define SENSOR_SCL 2
37#endif
38
40
41static const char *effectList[] = {
42 "1 - Strong Click 100%", "2 - Strong Click 60%", "3 - Strong Click 30%",
43 "4 - Sharp Click 100%", "5 - Sharp Click 60%", "6 - Sharp Click 30%",
44 "7 - Soft Bump 100%", "8 - Soft Bump 60%", "9 - Soft Bump 30%",
45 "10 - Double Click 100%", "11 - Double Click 60%", "12 - Triple Click 100%",
46 "13 - Soft Fuzz 60%", "14 - Strong Buzz 100%", "15 - Alert 750ms 100%",
47 "16 - Alert 1000ms 100%"
48};
49
51{
52 Serial.println("");
53 Serial.println("╔══════════════════════════════════════════╗");
54 Serial.println("║ DRV2605 Haptic Driver Test ║");
55 Serial.println("╚══════════════════════════════════════════╝");
56 Serial.println("");
57}
58
60{
61 Serial.println("\n========== Commands ==========");
62 Serial.println("1-16 : Play effect (ERM Library 1)");
63 Serial.println("e : List all effects (1-117)");
64 Serial.println("l 1-6 : Select ROM library");
65 Serial.println("a : Toggle ERM/LRA mode");
66 Serial.println("s : Stop playback");
67 Serial.println("r : RTP test (0-255)");
68 Serial.println("c : Calibrate");
69 Serial.println("h : Help");
70 Serial.println("==============================");
71}
72
73void setup()
74{
75 Serial.begin(115200);
76 delay(500);
77
79
80 Serial.println("\n=== Driver Initialization ===");
81
82 if (!haptic.begin(Wire, DRV2605_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
83 Serial.println("[FATAL] DRV2605 init failed!");
84 while (1) delay(1000);
85 }
86
87 Serial.println("[OK] Driver initialized");
88 Serial.print(" Chip Name: "); Serial.println(haptic.getChipName());
89 Serial.print(" Chip ID: 0x"); Serial.println(haptic.getChipID(), HEX);
90 Serial.print(" Actuator: "); Serial.println(haptic.getActuatorType() == HapticActuatorType::LRA ? "LRA" : "ERM");
91 Serial.print(" VBAT: "); Serial.print(haptic.getVbat()); Serial.println(" mV");
92 Serial.print(" Mode: "); Serial.println(static_cast<uint8_t>(haptic.getMode()));
93 printHelp();
94}
95
96void loop()
97{
98 if (Serial.available()) {
99 char cmd = Serial.read();
100
101 if (cmd >= '1' && cmd <= '9') {
102 uint8_t idx = cmd - '1';
103 Serial.print("Playing: "); Serial.println(effectList[idx]);
104 haptic.playEffect(idx + 1);
105 } else if (cmd == 'e' || cmd == 'E') {
106 Serial.println("\n--- Effect List (1-117) ---");
107 Serial.println("ERM Library 1: Strong/Sharp/Soft Click, Double/Triple Click, Buzz");
108 Serial.println("ERM Library 2-5: Various click patterns");
109 Serial.println("ERM Library 6: LRA Library (use 'l 6')");
110 Serial.println("Send number 1-117 to play effect");
111 } else if (cmd == 'l' || cmd == 'L') {
112 Serial.read(); // skip space
113 while (Serial.available() == 0) delay(10);
114 char lib = Serial.read();
115 uint8_t libNum = lib - '0';
116 if (libNum >= 1 && libNum <= 6) {
117 haptic.selectLibrary(libNum);
118 Serial.print("Library set to: "); Serial.println(libNum);
119 }
120 } else if (cmd == 'a' || cmd == 'A') {
121 auto type = haptic.getActuatorType();
123 Serial.print("Actuator: "); Serial.println(haptic.getActuatorType() == HapticActuatorType::ERM ? "ERM" : "LRA");
124 } else if (cmd == 's' || cmd == 'S') {
125 haptic.stop();
126 Serial.println("Stopped");
127 } else if (cmd == 'r' || cmd == 'R') {
129 Serial.println("RTP Mode: Send 0-255 for intensity");
130 while (Serial.available() == 0) delay(10);
131 int val = Serial.parseInt();
132 if (val >= 0 && val <= 255) {
133 haptic.setRealtimeValue(static_cast<uint8_t>(val));
134 Serial.print("RTP Value: "); Serial.println(val);
135 }
137 } else if (cmd == 'c' || cmd == 'C') {
138 Serial.println("Calibrating...");
139 if (haptic.calibrate()) {
140 Serial.println("[OK] Calibration done");
141 } else {
142 Serial.println("[FAIL] Calibration failed");
143 }
144 } else if (cmd == 'h' || cmd == 'H' || cmd == '?') {
145 printHelp();
146 } else if (cmd >= '0' && cmd <= '9') {
147 // Multi-digit effect number
148 String numStr = String(cmd);
149 while (Serial.available()) {
150 char c = Serial.peek();
151 if (c >= '0' && c <= '9') {
152 numStr += Serial.read();
153 } else {
154 Serial.read(); // consume non-digit
155 break;
156 }
157 }
158 uint8_t effectNum = numStr.toInt();
159 if (effectNum >= 1 && effectNum <= 117) {
160 Serial.print("Playing effect: "); Serial.println(effectNum);
161 haptic.playEffect(effectNum);
162 } else {
163 Serial.print("Invalid: "); Serial.print(effectNum); Serial.println(" (range 1-117)");
164 }
165 } else if (cmd != '\n' && cmd != '\r' && cmd != ' ') {
166 Serial.print("[?] Unknown: '"); Serial.println(cmd);
167 }
168 }
169
170 delay(10);
171}
@ LRA
Linear Resonant Actuator.
@ ERM
Eccentric Rotating Mass motor.
@ INTERNAL_TRIGGER
Play by selecting waveforms/effects and calling run()
@ REAL_TIME_PLAYBACK
RTP mode (direct drive value)
@license MIT License
Driver for the TI DRV2605/DRV2604 haptic motor controller.
uint32_t getVbat() override
Get the battery voltage.
bool setActuatorType(HapticActuatorType type) override
Set the actuator type.
HapticActuatorType getActuatorType() const override
Get the actuator type.
bool calibrate() override
Perform auto-calibration.
void selectLibrary(uint8_t lib)
Select the ROM waveform library.
bool setMode(HapticMode mode) override
Set the operating mode.
uint8_t getChipID() const override
Get the chip ID.
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 setRealtimeValue(uint8_t value) override
Set RTP (real-time playback) value.
bool begin(SensorCommCustom::CustomCallback cb, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
Initialize the sensor using custom callback interface.
void printHelp()
void setup()
HapticDriver_DRV2605 haptic
#define SENSOR_SCL
#define SENSOR_SDA
void printBanner()
void loop()