brix5 / firmware / demo / QuadDRV-CompassBelt / Adafruit_DRV2605.cpp @ a2a2cf8b
History | View | Annotate | Download (3.716 KB)
1 |
/***************************************************
|
---|---|
2 |
This is a library for the Adafruit DRV2605L Haptic Driver
|
3 |
|
4 |
----> http://www.adafruit.com/products/2306
|
5 |
|
6 |
Check out the links above for our tutorials and wiring diagrams
|
7 |
This motor/haptic driver uses I2C to communicate
|
8 |
|
9 |
Adafruit invests time and resources providing this open source code,
|
10 |
please support Adafruit and open-source hardware by purchasing
|
11 |
products from Adafruit!
|
12 |
|
13 |
Written by Limor Fried/Ladyada for Adafruit Industries.
|
14 |
MIT license, all text above must be included in any redistribution
|
15 |
****************************************************/
|
16 |
|
17 |
|
18 |
#if ARDUINO >= 100 |
19 |
#include "Arduino.h" |
20 |
#else
|
21 |
#include "WProgram.h" |
22 |
#endif
|
23 |
|
24 |
#include "Adafruit_DRV2605.h" |
25 |
|
26 |
/**************************************************************************/
|
27 |
/*!
|
28 |
@brief Instantiates a new DRV2605 class
|
29 |
*/
|
30 |
/**************************************************************************/
|
31 |
// I2C, no address adjustments or pins
|
32 |
Adafruit_DRV2605::Adafruit_DRV2605(uint8_t channel) { |
33 |
_channel = channel; |
34 |
} |
35 |
|
36 |
|
37 |
/**************************************************************************/
|
38 |
/*!
|
39 |
@brief Setups the HW
|
40 |
*/
|
41 |
/**************************************************************************/
|
42 |
boolean Adafruit_DRV2605::begin() { |
43 |
uint8_t id = readRegister8(DRV2605_REG_STATUS); |
44 |
//Serial.print("Status 0x"); Serial.println(id, HEX);
|
45 |
|
46 |
writeRegister8(DRV2605_REG_MODE, 0x00); // out of standby |
47 |
|
48 |
writeRegister8(DRV2605_REG_RTPIN, 0x00); // no real-time-playback |
49 |
|
50 |
writeRegister8(DRV2605_REG_WAVESEQ1, 1); // strong click |
51 |
writeRegister8(DRV2605_REG_WAVESEQ2, 0);
|
52 |
|
53 |
writeRegister8(DRV2605_REG_OVERDRIVE, 0); // no overdrive |
54 |
|
55 |
writeRegister8(DRV2605_REG_SUSTAINPOS, 0);
|
56 |
writeRegister8(DRV2605_REG_SUSTAINNEG, 0);
|
57 |
writeRegister8(DRV2605_REG_BREAK, 0);
|
58 |
writeRegister8(DRV2605_REG_AUDIOMAX, 0x64);
|
59 |
|
60 |
// ERM open loop
|
61 |
|
62 |
// turn off N_ERM_LRA
|
63 |
writeRegister8(DRV2605_REG_FEEDBACK, readRegister8(DRV2605_REG_FEEDBACK) & 0x7F);
|
64 |
// turn on ERM_OPEN_LOOP
|
65 |
writeRegister8(DRV2605_REG_CONTROL3, readRegister8(DRV2605_REG_CONTROL3) | 0x20);
|
66 |
|
67 |
return true; |
68 |
} |
69 |
|
70 |
void Adafruit_DRV2605::setWaveform(uint8_t slot, uint8_t w) {
|
71 |
writeRegister8(DRV2605_REG_WAVESEQ1 + slot, w); |
72 |
} |
73 |
|
74 |
void Adafruit_DRV2605::selectLibrary(uint8_t lib) {
|
75 |
writeRegister8(DRV2605_REG_LIBRARY, lib); |
76 |
} |
77 |
|
78 |
void Adafruit_DRV2605::go() {
|
79 |
writeRegister8(DRV2605_REG_GO, 1);
|
80 |
} |
81 |
|
82 |
void Adafruit_DRV2605::setMode(uint8_t mode) {
|
83 |
writeRegister8(DRV2605_REG_MODE, mode); |
84 |
} |
85 |
|
86 |
void Adafruit_DRV2605::setRealtimeValue(uint8_t rtp) {
|
87 |
writeRegister8(DRV2605_REG_RTPIN, rtp); |
88 |
} |
89 |
|
90 |
/********************************************************************/
|
91 |
|
92 |
uint8_t Adafruit_DRV2605::readRegister8(uint8_t reg) { |
93 |
selectChannel(_channel); |
94 |
uint8_t x ; |
95 |
// use i2c
|
96 |
Wire.beginTransmission(DRV2605_ADDR); |
97 |
Wire.write((byte)reg); |
98 |
Wire.endTransmission(); |
99 |
Wire.requestFrom((byte)DRV2605_ADDR, (byte)1);
|
100 |
x = Wire.read(); |
101 |
// Serial.print("$"); Serial.print(reg, HEX);
|
102 |
// Serial.print(": 0x"); Serial.println(x, HEX);
|
103 |
return x;
|
104 |
} |
105 |
|
106 |
void Adafruit_DRV2605::writeRegister8(uint8_t reg, uint8_t val) {
|
107 |
selectChannel(_channel); |
108 |
// use i2c
|
109 |
Wire.beginTransmission(DRV2605_ADDR); |
110 |
Wire.write((byte)reg); |
111 |
Wire.write((byte)val); |
112 |
Wire.endTransmission(); |
113 |
} |
114 |
|
115 |
/****************/
|
116 |
|
117 |
|
118 |
// Allow users to use ERM motor or LRA motors
|
119 |
|
120 |
void Adafruit_DRV2605::useERM ()
|
121 |
{ |
122 |
writeRegister8(DRV2605_REG_FEEDBACK, readRegister8(DRV2605_REG_FEEDBACK) & 0x7F);
|
123 |
} |
124 |
|
125 |
void Adafruit_DRV2605::useLRA ()
|
126 |
{ |
127 |
writeRegister8(DRV2605_REG_FEEDBACK, readRegister8(DRV2605_REG_FEEDBACK) | 0x80);
|
128 |
} |
129 |
|
130 |
void Adafruit_DRV2605::selectChannel (uint8_t channel)
|
131 |
{ |
132 |
// choose channel, all address bits to gnd on PCA9546A/TCA9548A = 0x70
|
133 |
Wire.beginTransmission(0x77);
|
134 |
Wire.write(1 << channel);
|
135 |
Wire.endTransmission(); |
136 |
} |