taku hachisu / DRV2605

Dependents:   sonar_vibrator

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DRV2605.cpp Source File

DRV2605.cpp

00001 #include "DRV2605.h"
00002 
00003 DRV2605::DRV2605(I2C &i2c, ActuatorType at, BrakeFactor bf, LoopGain lg,
00004                  uint8_t rv, uint8_t ocv, AutoCalTime act, uint8_t dt,
00005                  SampleTime st, uint8_t bt, uint8_t it)
00006 {
00007     _i2c =  &i2c;
00008 
00009     // After powerup, wait at least 250 µs before the DRV2605 device accepts
00010     // I2C commands
00011     wait_us(250);
00012 
00013     // Assert the EN pin (logic high).
00014     // This is skipped because EN pin is connected to Vdd
00015 
00016     // To remove the device from standby mode
00017     mode(Mode_AUTO_CAL);
00018 
00019     // Auto calibratoin procedure
00020     // Populate the input parameters required by the auto-calibration engine:
00021     // Feedback Control Register;
00022     i2cWriteByte(FEEDBACK_CONTROL, i2cReadByte(FEEDBACK_CONTROL) | at | bf | lg);
00023 
00024     // Rated Voltage Register;
00025     i2cWriteByte(RATED_VOLTAGE, rv);
00026 
00027     // Overdrive Clamp Voltage Register;
00028     i2cWriteByte(OVERDRIVE_CLAMP_VOLTAGE, ocv);
00029 
00030     // Control4 Register;
00031     i2cWriteByte(CONTROL4, i2cReadByte(FEEDBACK_CONTROL) | act);
00032 
00033     // Control1 Register;
00034     i2cWriteByte(CONTROL1, i2cReadByte(FEEDBACK_CONTROL) | dt);
00035 
00036     // Control2 Register (only for LRA)
00037     if(at == LRA)
00038         i2cWriteByte(CONTROL2, i2cReadByte(FEEDBACK_CONTROL) | st | (bt << 2) | it);
00039 
00040     // Start auto- calibration
00041     i2cWriteByte(GO, 0x01);
00042     // Wait for calibration to complete
00043     while(i2cReadByte(GO));
00044 }
00045 
00046 void DRV2605::i2cWriteByte(char reg, char value)
00047 {
00048     char buff[2] = {reg, value};
00049     _i2c->write(DRV2605_SLAVE_ADDR<<1, buff, 2);
00050 }
00051 
00052 uint8_t DRV2605::i2cReadByte(char reg)
00053 {
00054     char result;                                    // Temp result storage
00055     _i2c->write(DRV2605_SLAVE_ADDR<<1, &reg, 1, true);
00056     _i2c->read(DRV2605_SLAVE_ADDR<<1, &result, 1);
00057 
00058     return result;
00059 }
00060 
00061 void DRV2605::mode(uint8_t mode)
00062 {
00063     i2cWriteByte(MODE, mode);
00064 }
00065 
00066 uint8_t DRV2605::diagnostics()
00067 {
00068     mode(Mode_DIAG);
00069     i2cWriteByte(GO, 1);
00070     while(i2cReadByte(GO));
00071 
00072     return i2cReadByte(STATUS);
00073 }
00074 
00075 void DRV2605::dataFormatRTP(bool isSigned)
00076 {
00077     // Control3 Register (0x1D)
00078     if(isSigned)
00079         i2cWriteByte(CONTROL3, i2cReadByte(CONTROL3) & ~(1 << 3));
00080     else
00081         i2cWriteByte(CONTROL3, i2cReadByte(CONTROL3) | (1 << 3));
00082 }
00083 
00084 void DRV2605::setLibrary(uint8_t lib)
00085 {
00086     i2cWriteByte(LIBRARY_SELECTION, lib);     // Select ROM Library
00087 }
00088 
00089 void DRV2605::setWaveform(int effect)
00090 {
00091     i2cWriteByte(WAVEFORM_SEQUENCER_1, effect);          // Load waveform index to play
00092     i2cWriteByte(WAVEFORM_SEQUENCER_2, 0);               // Insert stop condition so we don't play other registers if filled
00093 }
00094 
00095 void DRV2605::setWaveformSequence(int effect1, int effect2,
00096                                   int effect3, int effect4,
00097                                   int effect5, int effect6,
00098                                   int effect7, int effect8)
00099 {
00100     i2cWriteByte(WAVEFORM_SEQUENCER_1, effect1);
00101     i2cWriteByte(WAVEFORM_SEQUENCER_2, effect2);
00102     i2cWriteByte(WAVEFORM_SEQUENCER_3, effect3);
00103     i2cWriteByte(WAVEFORM_SEQUENCER_4, effect4);
00104     i2cWriteByte(WAVEFORM_SEQUENCER_5, effect5);
00105     i2cWriteByte(WAVEFORM_SEQUENCER_6, effect6);
00106     i2cWriteByte(WAVEFORM_SEQUENCER_7, effect7);
00107     i2cWriteByte(WAVEFORM_SEQUENCER_8, effect8);
00108 }
00109 
00110 void DRV2605::play()
00111 {
00112     i2cWriteByte(GO, 1);
00113 }
00114 
00115 void DRV2605::stop()
00116 {
00117     i2cWriteByte(GO, 0);
00118 }
00119 
00120 void DRV2605::playRtp(uint8_t rtp)
00121 {
00122     i2cWriteByte(REAL_TIME_PLAYBACK, rtp);
00123 }
00124 
00125 float DRV2605::batteryVoltage(void)
00126 {
00127     return (float)i2cReadByte(VBAT_VOLTAGE_MONITOR) / 255.0 * 5.6;
00128 }