Statistics
| Branch: | Revision:

brix5 / firmware / demo / QuadDRV-HwTest / QuadDRV-HwTest.ino @ 9e4d9eb1

History | View | Annotate | Download (2.609 KB)

1
// 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
bool debug = false;
20

    
21
void setup() {
22
  Wire.begin();
23
  
24
  // wait for serial port to connect
25
  while (!Serial); 
26
  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
  if (Serial.available()) {
39
    
40
    char cmd = Serial.read();
41
    if (debug) {
42
      Serial.print("Received: ");
43
      Serial.println(cmd);
44
    }
45

    
46
    uint8_t intensity = 0;
47
    int duration = 0;
48

    
49
    switch (cmd) {
50
      if (debug) {
51
          Serial.print("playing on channel ");
52
          Serial.println(cmd);
53
      }
54
      case ('0'):
55

    
56
      // single motor: play 'rare' case
57
      case ('0'):
58
      case ('1'):
59
      case ('2'):
60
      case ('3'): // disabled for 3 motors
61
        motor = cmd;
62
        intensity = 255;
63
        duration = 200;
64
    }
65

    
66
    if (intensity > 0) {
67
      // single channel
68

    
69
      // convert ASCII channel string to int
70
      uint8_t channel = ((uint8_t)motor) - 48;
71

    
72
      if ((motor != '!') && (channel < channelCount)) {
73
        playRTP(channel, intensity, duration);
74
        motor = '!'; // reset to normal mode
75
      } else {
76
        playRTP(intensity, duration);
77
      }
78
      intensity = 0;
79
      duration = 0;
80
    }
81

    
82
    if (debug) {
83
      Serial.println();
84
    }
85
  }
86
}
87

    
88
// all channels
89
void playRTP(uint8_t intensity, uint16_t duration) {
90
  if (debug) {
91
    Serial.print("Playing intensity ");
92
    Serial.print(intensity);
93
    Serial.print(" for ");
94
    Serial.print(duration);
95
    Serial.println("ms on all channels");
96
  }
97
  for (uint8_t channel; channel < channelCount; channel++) {
98
    drvs[channel].setRealtimeValue(intensity);
99
  }
100
  delay(duration);
101
  for (uint8_t channel; channel < channelCount; channel++) {
102
    drvs[channel].setRealtimeValue(0x00);
103
  }
104
}
105

    
106
// single channel
107
void playRTP(uint8_t channel, uint8_t intensity, uint16_t duration) {
108
  if (debug) {
109
    Serial.print("Playing intensity ");
110
    Serial.print(intensity);
111
    Serial.print(" for ");
112
    Serial.print(duration);
113
    Serial.print("ms on channel ");
114
    Serial.println(channel);
115
  }
116
  drvs[channel].setRealtimeValue(intensity);
117
  delay(duration);
118
  drvs[channel].setRealtimeValue(0x00);
119
}