Akash Vibhute / DRV2665
Revision:
0:b85fd3fdfcfa
Child:
1:e2c726c628dc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drv2665.cpp	Wed May 25 03:17:20 2016 +0000
@@ -0,0 +1,120 @@
+/**
+ *  DRV2665 library
+ *
+ *  @author Akash Vibhute
+ *  @author < akash . roboticist [at] gmail . com >
+ *  @version 0.1
+ *  @date May/24/2016
+ */
+
+#include "drv2665.h"
+#define M_PI 3.14159265358979
+
+DRV2665_DIGITAL::DRV2665_DIGITAL( PinName sda, PinName scl ):i2c_(sda, scl)
+{
+    i2c_addr = DRV2665_I2C_ADDRESS << 1;
+    wait_ms(2); //wait 2ms for i2c bus
+}
+
+void DRV2665_DIGITAL::init(uint8_t output_gain, uint8_t idle_timeout)
+{
+    write(DRV2665_CTRL_2, 0); //clear standby
+    write(DRV2665_CTRL_1, (DRV2665_DIGITAL_IN | output_gain)); //set mode digital, set gain
+    write(DRV2665_CTRL_2, idle_timeout); //set timeout period
+}
+
+void DRV2665_DIGITAL::reset()
+{
+    write(DRV2665_CTRL_2, DRV2665_DEV_RST);
+}
+
+void DRV2665_DIGITAL::outputWave(int8_t waveform[], uint8_t length)
+{
+    if(fifo_check()) {
+        for(int i=0; i<length; i++) {
+            write(DRV2665_FIFO, waveform[i]);
+            wait_us(5);
+        }
+    } else
+        return;
+}
+
+void DRV2665_DIGITAL::outputSine(uint16_t hz)
+{
+    int8_t waveform_byte;
+    uint16_t length;
+
+    //in the linux driver example, a 41 byte waveform plays back at 8kHz
+    //wavelength 41 -> wave time 125us -> frequency 8kHz
+    //hence per byte sampling time = 125us / 41 = 3.04878049
+    //ie 41 bytes :: 8kHz -> 328 bytes :: 1000Hz
+    //calculations done here are from this factor
+
+    length = (1000 * 328) / hz;
+    
+    /*
+    //If the above assumption is incorrect, it could be that the DRV plays 
+    //anything in its FIFO at a fix rate of 8ksps in that case, it would mean 
+    //a per byte play time of 125us, ie for a 100byte wavelength -> a playback
+    //time of 1.25ms (800Hz)
+    //So, 100B :: 800Hz -> 100*800B :: 1Hz
+    //For 'n' Hz :: (100*800/n) B
+
+    length = (100 * 800) / hz;
+    */
+    
+    
+    for(int i=0; i<length; i++) {
+        waveform_byte = (uint8_t)(102.0 * sin(2*M_PI*i/(length-1))); //102 is the max +ve / -ve value, typecasting in uint8 turns -ve values into 2's complement required by the driver
+        
+        write(DRV2665_FIFO, waveform_byte);
+        wait_us(5);
+
+        while(!fifo_check())
+            wait_us(5);
+    }
+}
+
+bool DRV2665_DIGITAL::fifo_check()
+{
+    uint8_t reply;
+    reply = read(DRV2665_STATUS);
+
+    if((reply & 0x01) == 0)
+        return(true);
+    else
+        return(false);
+}
+
+void DRV2665_DIGITAL::en_override(bool en)
+{
+    write(DRV2665_CTRL_2, DRV2665_BOOST_EN);
+}
+
+uint8_t DRV2665_DIGITAL::read(char reg)
+{
+    //Create a temporary buffer
+    char buff[2];
+
+    //Select the register
+    i2c_.write(i2c_addr, &reg, 1, true);
+
+    //Read the 16-bit register
+    i2c_.read(i2c_addr, buff, 2);
+
+    //Return the combined 16-bit value
+    return (buff[0] << 8) | buff[1];
+}
+
+void DRV2665_DIGITAL::write(char reg, uint8_t data)
+{
+    //Create a temporary buffer
+    char buff[2];
+
+    //Load the register address and 16-bit data
+    buff[0] = reg;
+    buff[1] = data;
+
+    //Write the data
+    i2c_.write(i2c_addr, buff, 2);
+}
\ No newline at end of file