lib for working with ltc2991s

Dependents:   ece495_firmware

Fork of ltc2991_test by Logan Rooper

Revision:
2:c9e727dcd00e
Parent:
1:4e4194db7cd6
Child:
4:85017622b676
--- a/LTC2991.cpp	Tue Nov 29 03:20:35 2016 +0000
+++ b/LTC2991.cpp	Tue Dec 06 18:34:46 2016 +0000
@@ -65,7 +65,6 @@
     Library for LTC2991: 14-bit Octal I2C Voltage, Current, and Temperature Monitor
 */
 
-//#include <Arduino.h>
 #include <stdint.h>
 #include "Linduino.h"
 #include "LT_I2C.h"
@@ -74,14 +73,20 @@
 
 Serial pc2(USBTX, USBRX, 9600);
 
-//#include <Wire.h>
+LTC2991::LTC2991() {
+    lti2c = new LT_I2C();
+}
+
+LTC2991::LTC2991(PinName i2c_scl_, PinName i2c_sda_) {
+    lti2c = new LT_I2C(i2c_scl_, i2c_sda_);
+}
 
 // Reads a 14-bit adc_code from LTC2991.
-int8_t LTC2991_adc_read(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid)
+int8_t LTC2991::LTC2991_adc_read(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid)
 {
   int8_t ack = 0;
   uint16_t code;
-  ack = i2c_read_word_data(i2c_address, msb_register_address, &code);
+  ack = lti2c->i2c_read_word_data(i2c_address, msb_register_address, &code);
    
 
   *data_valid = (code >> 15) & 0x01;   // Place Data Valid Bit in *data_valid
@@ -97,7 +102,7 @@
 // Similar to LTC2991_adc_read except it repeats until the data_valid bit is set, it fails to receive an I2C acknowledge, or the timeout (in milliseconds)
 // expires. It keeps trying to read from the LTC2991 every millisecond until the data_valid bit is set (indicating new data since the previous
 // time this register was read) or until it fails to receive an I2C acknowledge (indicating an error on the I2C bus).
-int8_t LTC2991_adc_read_timeout(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid, uint16_t timeout, uint8_t status_bit)
+int8_t LTC2991::LTC2991_adc_read_timeout(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid, uint16_t timeout, uint8_t status_bit)
 {
   int8_t ack = 0;
   uint8_t reg_data;
@@ -147,7 +152,7 @@
 // to differential voltage mode, the data in the register may still correspond to the temperature reading immediately
 // after the mode change.  Flushing one reading and waiting for a new reading guarantees fresh data is received.
 // If the timeout is reached without valid data (*data_valid=1) the function exits.
-int8_t LTC2991_adc_read_new_data(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid, uint16_t timeout)
+int8_t LTC2991::LTC2991_adc_read_new_data(uint8_t i2c_address, uint8_t msb_register_address, int16_t *adc_code, int8_t *data_valid, uint16_t timeout)
 {
   int8_t ack = 0;
 
@@ -158,25 +163,25 @@
 }
 
 // Reads an 8-bit register from the LTC2991 using the standard repeated start format.
-int8_t LTC2991_register_read(uint8_t i2c_address, uint8_t register_address, uint8_t *register_data)
+int8_t LTC2991::LTC2991_register_read(uint8_t i2c_address, uint8_t register_address, uint8_t *register_data)
 {
   int8_t ack = 0;
-  ack = i2c_read_byte_data(i2c_address, register_address, register_data);
+  ack = lti2c->i2c_read_byte_data(i2c_address, register_address, register_data);
   return(ack);
 }
 
 // Write one byte to an LTC2991 register.
 // Writes to an 8-bit register inside the LTC2991 using the standard I2C repeated start format.
-int8_t LTC2991_register_write(uint8_t i2c_address, uint8_t register_address, uint8_t register_data)
+int8_t LTC2991::LTC2991_register_write(uint8_t i2c_address, uint8_t register_address, uint8_t register_data)
 {
   int8_t ack = 0;
-  ack = i2c_write_byte_data(i2c_address, register_address, register_data);
+  ack = lti2c->i2c_write_byte_data(i2c_address, register_address, register_data);
   return(ack);
 }
 
 // Used to set and clear bits in a control register.  bits_to_set will be bitwise OR'd with the register.
 // bits_to_clear will be inverted and bitwise AND'd with the register so that every location with a 1 will result in a 0 in the register.
-int8_t LTC2991_register_set_clear_bits(uint8_t i2c_address, uint8_t register_address, uint8_t bits_to_set, uint8_t bits_to_clear)
+int8_t LTC2991::LTC2991_register_set_clear_bits(uint8_t i2c_address, uint8_t register_address, uint8_t bits_to_set, uint8_t bits_to_clear)
 {
   uint8_t register_data;
   int8_t ack = 0;
@@ -189,7 +194,7 @@
 }
 
 // Calculates the LTC2991 single-ended input voltages
-float LTC2991_code_to_single_ended_voltage(int16_t adc_code, float LTC2991_single_ended_lsb)
+float LTC2991::LTC2991_code_to_single_ended_voltage(int16_t adc_code, float LTC2991_single_ended_lsb)
 {
   float voltage;
   int16_t sign = 1;
@@ -204,7 +209,7 @@
 }
 
 // Calculates the LTC2991 Vcc voltage
-float LTC2991_code_to_vcc_voltage(int16_t adc_code, float LTC2991_single_ended_lsb)
+float LTC2991::LTC2991_code_to_vcc_voltage(int16_t adc_code, float LTC2991_single_ended_lsb)
 {
   float voltage;
   int16_t sign = 1;
@@ -219,7 +224,7 @@
 }
 
 // Calculates the LTC2991 differential input voltage.
-float LTC2991_code_to_differential_voltage(int16_t adc_code, float LTC2991_differential_lsb)
+float LTC2991::LTC2991_code_to_differential_voltage(int16_t adc_code, float LTC2991_differential_lsb)
 {
   float voltage;
   int16_t sign = 1;
@@ -233,7 +238,7 @@
 }
 
 // Calculates the LTC2991 temperature
-float LTC2991_temperature(int16_t adc_code, float LTC2991_temperature_lsb, boolean unit)
+float LTC2991::LTC2991_temperature(int16_t adc_code, float LTC2991_temperature_lsb, boolean unit)
 {
   float temperature;
   adc_code = (adc_code & 0x1FFF);                               //! 1) Removes first 3 bits
@@ -250,7 +255,7 @@
 }
 
 //  Calculates the LTC2991 diode voltage
-float LTC2991_code_to_diode_voltage(int16_t adc_code, float LTC2991_diode_voltage_lsb)
+float LTC2991::LTC2991_code_to_diode_voltage(int16_t adc_code, float LTC2991_diode_voltage_lsb)
 {
   float voltage;
   adc_code = (adc_code & 0x1FFF);                               //! 1) Removes first 3 bits