brix5 / firmware / demo / QuadDRV-HwTest / QuadDRV-HwTest.ino @ master
History | View | Annotate | Download (2.563 KB)
1 | 9e4d9eb1 | Jan | // Demo Sketch for 4x TI DRV2605 and I2C MUX |
---|---|---|---|
2 | // |
||
3 | // Usage: |
||
4 | // Serial Connect w/ 9600 baud |
||
5 | // 0-3 send a buzz on channel 0-3 respectively |
||
6 | |||
7 | #include "Adafruit_DRV2605.h" |
||
8 | |||
9 | const uint8_t channelCount = 4; // belt has 3 channels |
||
10 | Adafruit_DRV2605 drvs[channelCount] = { |
||
11 | Adafruit_DRV2605(0), |
||
12 | Adafruit_DRV2605(1), |
||
13 | Adafruit_DRV2605(2), |
||
14 | Adafruit_DRV2605(3) |
||
15 | }; |
||
16 | |||
17 | uint8_t library = 6; // LRA |
||
18 | char motor = '!'; // '!' = disabled |
||
19 | cd87454c | Jan | bool debug = true; |
20 | 9e4d9eb1 | Jan | |
21 | void setup() { |
||
22 | Wire.begin(); |
||
23 | |||
24 | // wait for serial port to connect |
||
25 | cd87454c | Jan | // while (!Serial); |
26 | 9e4d9eb1 | Jan | Serial.begin(9600); |
27 | delay(250); |
||
28 | |||
29 | for (uint8_t i = 0; i < channelCount; i++) { |
||
30 | drvs[i].begin(); |
||
31 | drvs[i].selectLibrary(library); |
||
32 | drvs[i].useLRA(); |
||
33 | drvs[i].setMode(DRV2605_MODE_REALTIME); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | void loop() { |
||
38 | cd87454c | Jan | // Serial.println("Test loop"); |
39 | 9e4d9eb1 | Jan | if (Serial.available()) { |
40 | |||
41 | char cmd = Serial.read(); |
||
42 | if (debug) { |
||
43 | Serial.print("Received: "); |
||
44 | Serial.println(cmd); |
||
45 | } |
||
46 | |||
47 | uint8_t intensity = 0; |
||
48 | int duration = 0; |
||
49 | |||
50 | switch (cmd) { |
||
51 | if (debug) { |
||
52 | Serial.print("playing on channel "); |
||
53 | Serial.println(cmd); |
||
54 | } |
||
55 | |||
56 | case ('0'): |
||
57 | case ('1'): |
||
58 | case ('2'): |
||
59 | cd87454c | Jan | case ('3'): |
60 | 9e4d9eb1 | Jan | motor = cmd; |
61 | cd87454c | Jan | intensity = 127; |
62 | duration = 500; |
||
63 | 9e4d9eb1 | Jan | } |
64 | |||
65 | if (intensity > 0) { |
||
66 | // single channel |
||
67 | |||
68 | // convert ASCII channel string to int |
||
69 | uint8_t channel = ((uint8_t)motor) - 48; |
||
70 | |||
71 | if ((motor != '!') && (channel < channelCount)) { |
||
72 | playRTP(channel, intensity, duration); |
||
73 | motor = '!'; // reset to normal mode |
||
74 | } else { |
||
75 | playRTP(intensity, duration); |
||
76 | } |
||
77 | intensity = 0; |
||
78 | duration = 0; |
||
79 | } |
||
80 | |||
81 | if (debug) { |
||
82 | Serial.println(); |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | |||
87 | // all channels |
||
88 | void playRTP(uint8_t intensity, uint16_t duration) { |
||
89 | if (debug) { |
||
90 | Serial.print("Playing intensity "); |
||
91 | Serial.print(intensity); |
||
92 | Serial.print(" for "); |
||
93 | Serial.print(duration); |
||
94 | Serial.println("ms on all channels"); |
||
95 | } |
||
96 | for (uint8_t channel; channel < channelCount; channel++) { |
||
97 | drvs[channel].setRealtimeValue(intensity); |
||
98 | } |
||
99 | delay(duration); |
||
100 | for (uint8_t channel; channel < channelCount; channel++) { |
||
101 | drvs[channel].setRealtimeValue(0x00); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | // single channel |
||
106 | void playRTP(uint8_t channel, uint8_t intensity, uint16_t duration) { |
||
107 | if (debug) { |
||
108 | Serial.print("Playing intensity "); |
||
109 | Serial.print(intensity); |
||
110 | Serial.print(" for "); |
||
111 | Serial.print(duration); |
||
112 | Serial.print("ms on channel "); |
||
113 | Serial.println(channel); |
||
114 | } |
||
115 | drvs[channel].setRealtimeValue(intensity); |
||
116 | delay(duration); |
||
117 | drvs[channel].setRealtimeValue(0x00); |
||
118 | } |