Library for TI's DRV 2605

Dependents:   sonar_vibrator

Revision:
0:d47ac72f4048
Child:
1:0747096351be
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DRV2605.h	Fri Nov 10 09:34:15 2017 +0000
@@ -0,0 +1,225 @@
+#ifndef DRV2605_H
+#define DRV2605_H
+
+#include "mbed.h"
+
+/**  Modifined from Bryce WilliamsBryce Williams's code
+ *  References:
+ *  http://www.ti.com/product/DRV2605L/description&lpos=Middle_Container&lid=Alternative_Devices
+ *  http://www.ti.com/lit/ds/symlink/drv2605l.pdf (Datasheet)
+ *  http://www.ti.com/lit/an/sloa189/sloa189.pdf (Setup Guide; SLOA189)
+ *  https://developer.mbed.org/users/electromotivated/code/DRV2605/
+ */
+
+// DRV2605 Slave Address
+#define DRV2605_SLAVE_ADDR 0x5A
+
+// DRV2605 Registers
+#define STATUS                               0x00
+#define MODE                                 0x01
+#define REAL_TIME_PLAYBACK                   0x02
+#define LIBRARY_SELECTION                    0x03
+#define WAVEFORM_SEQUENCER_1                 0x04
+#define WAVEFORM_SEQUENCER_2                 0x05
+#define WAVEFORM_SEQUENCER_3                 0x06
+#define WAVEFORM_SEQUENCER_4                 0x07
+#define WAVEFORM_SEQUENCER_5                 0x08
+#define WAVEFORM_SEQUENCER_6                 0x09
+#define WAVEFORM_SEQUENCER_7                 0x0A
+#define WAVEFORM_SEQUENCER_8                 0x0B
+#define GO                                   0x0C
+#define OVERDRIVE_TIME_OFFSET                0x0D
+#define POSITIVE_SUSTAIN_TIME_OFFSET         0x0E
+#define NEGATIVE_SUSTAIN_TIME_OFFSET         0x0F
+#define BRAKE_TIME_OFFSET                    0x10
+#define AUDIO_TO_VIBE_CONTROL                0x11
+#define AUDIO_TO_VIBE_MINIMUM_INPUT_LEVEL    0x12
+#define AUDIO_TO_VIBE_MAXIMUM_INPUT_LEVEL    0x13
+#define AUDIO_TO_VIBE_MINIMUM_OUPUT_DRIVE    0x14
+#define AUDIO_TO_VIBE_MAXIMUM_OUTPUT_DRIVE   0x15
+#define RATED_VOLTAGE                        0x16
+#define OVERDRIVE_CLAMP_VOLTAGE              0x17
+#define AUTO_CALIBRATION_COMPENSATION_RESULT 0x18
+#define AUTO_CALIBRATION_BACK_EMF_RESULT     0x19
+#define FEEDBACK_CONTROL                     0x1A
+#define CONTROL                              0x1B
+#define CONTROL2                             0x1C
+#define CONTROL3                             0x1D
+#define CONTROL4                             0x1E
+#define CONTROL5                             0x1F
+#define LRA_OPEN_LOOP_PERIOD                 0x20
+#define VBAT_VOLTAGE_MONITOR                 0x21
+#define LRA_RESONANCE_PERIOD                 0x22
+
+// Modes defines the possible modes of the DRV2605L (Register Addr: 0x01)
+#define Mode_INTERNAL_TRIG   0x00 // Waveforms fired by Setting GO bit in Register 0x0C
+#define Mode_EXTERNAL_EDGE   0x01 // Rising Edge on IN/TRIG pin set GO Bit.
+#define Mode_EXTERNAL_LEVEL  0x02 // GO bit follows state of edge on IN/TRIG pin.
+#define Mode_PWM_ANALOG      0x03 // PWM or Analog Signal accepted at IN/TRIG pin.
+#define Mode_AUDIO_TO_VIBE   0x04 // An AC-coupled audio signal is accepted at the IN/TRIG pin.
+#define Mode_RTP             0x05 // Real- Time Playback
+#define Mode_DIAG            0x06 // Set to perform actuator diagnostics
+#define Mode_AUTO_CAL        0x07 // Set to perform auto calibration of device for actuator
+#define Mode_STANDBY         0x40 // Set Device to Software Standby (Low- Power Mode)
+#define Mode_RESET           0x80 // Reset Device (equivalent of power cycling the device)
+
+// FeedBack_Controls Fields Bitmasks (Register Addr: 0x1A)
+#define Actuator_ERM            0x00
+#define Actuator_LRA            0x80
+#define Brake_FACTORTx1         0x00
+#define Brake_FACTORTx2         0x10
+#define Brake_FACTORTx3         0x20
+#define Brake_FACTORTx4         0x40
+#define Brake_FACTORTx6         0x80
+#define Brake_FACTORTx8         0x50
+#define Brake_FACTORTx16        0x60
+#define Brake_FACTORTxDISABLE   0x70
+#define Loop_GainLOW            0x00
+#define Loop_GainMED            0x04
+#define Loop_GainHIGH           0x08
+#define Loop_GainVERYHIGH       0x0C
+
+// ROM Waveform Library Selections
+#define LibraryEMPTY            0x00
+#define LibraryA                0x01
+#define LibraryB                0x02
+#define LibraryC                0x03
+#define LibraryD                0x04
+#define LibraryE                0x05
+#define LibraryLRA              0x06
+#define LibraryF                0x07
+
+
+/** Library for the TI DRV2605
+ *
+ */
+
+class DRV2605
+{
+
+public:
+    /**
+    * Create a DRV2605 object
+    *
+    * @param &i2c pointer of I2C object
+    * @param isERM Set the actuator type
+    */
+    DRV2605(I2C &i2c, bool isERM);
+
+    /**
+    * Write value to specified register of device
+    *
+    * @param reg      The device register to write
+    * @param value    The value to write to the register
+    */
+    void i2cWriteByte(char reg, char value);
+
+    /**
+    * Read value from register of device
+    * @param reg  The device register to read
+    * @return     The result
+    */
+    uint8_t i2cReadByte(char reg);
+
+    /**
+    * Place device into specified mode
+    * @param mode     The mode to place device into
+    */
+    void mode(char mode);
+
+    /**
+    * Runs diagnostics on the Actuator and Device and returns the results.
+    * The results indicate if an actuator is detected, over- current events,
+    * etc. Refer to STATUS Register (0x00) in device datasheet for more
+    * description register values.
+    *
+    * Note: This should be run if the user is having trouble getting the actuator
+    * to work.
+    *
+    * @return The results of the diagnostics (i.e. Status Reg (0x00))
+    */
+    uint8_t diagnostics();
+    
+    
+    /**
+    * Set ROM Library
+    *
+    * Note: This should be run before setWaveform function
+    * @param lib Set library (see library's macro)
+    */
+    void setLibrary(char lib);
+
+    /**
+    * Play single waveform from ROM Library
+    *
+    * Note: This should be run after setLibrary function
+    * @param effect  The Waveform Effect Library Index value to play
+    * (valid values are 1 to 123)
+    */
+    void setWaveform(int effect);
+
+    /**
+    * Load Wave Sequence into DRV2605 Sequence Registers
+    *
+    * Note: This should be run before setWaveform function
+    * @param effect1... effect8   The effect to play. Valid inputs are 
+    * 0 to 123;
+    * 0: Stop Condition,
+    * 1- 123: Waveform Index
+    */
+    void setWaveformSequence(int effect1 = 0, int effect2 = 0,
+                                int effect3 = 0, int effect4 = 0,
+                                int effect5 = 0, int effect6 = 0,
+                                int effect7 = 0, int effect8 = 0);
+
+    /**
+    *    Plays the currently loaded waveform or waveform sequence.
+    */
+    void play();
+    
+    /**
+    *    Stops the currently loaded waveform or waveform sequence.
+    */
+    void stop();
+
+    /**
+    * Real time play back
+    *
+    * @param rtp  intensity of the vibration (0 - 255)
+    */
+    void playRtp(char rtp);
+
+    /**
+        TODO: Add Closed Loop Calibration
+
+        Run basic DRV2605L Auto- Calibration as detailed in Section 2 of
+        the Device Setup Guide for OPEN- LOOP ONLY.
+        This must be done before using the device in closed- loop mode
+        (unless cal has been done before with values stored in non-volatile
+         mem; see datasheet for more info).
+
+        NOTE: It is NOT recommended to store cal values into device
+        non-volatile memory as this can be done only once. Thus do not
+        use this feature unless the device is being used in a final
+        project AND values have been confirmed to result in satisfactory
+        performance).
+
+        This uses many of the default device register values such as
+        the default DRIVE_TIME .
+
+        @param actuator_peak_voltage The maximum/peak voltage rating of the actuator
+    */
+    uint8_t auto_cal_open_loop(float actuator_peak_voltage);
+
+    /**
+    * Battery voltage monitor
+    *
+    * @return battery voltage
+    */
+    float battery_voltage(void);
+
+private:
+    I2C *_i2c;
+};
+
+#endif
\ No newline at end of file