Dependencies: SX1276GenericLib USBDevice
Fork of PICO_LP1 by
Diff: AO32Lib/AO32_lib.cpp
- Revision:
- 3:85fc843a9d7d
- Child:
- 5:9e751733a6f3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AO32Lib/AO32_lib.cpp Mon Oct 12 21:55:22 2020 +0000 @@ -0,0 +1,163 @@ + +#include "mbed.h" +#include "AO32_lib.h" + +// ***************************************************************************** +// AO32_write_register(char, char, char) writes single byte to AO32 +// char I2C address +// char AO32 register address +// char data byte to be writen +// returns 0 on success ACK, 1 on NACK +// ***************************************************************************** + +int AO32_write_register(I2C *i2c, char I2C_add, char reg_add, char byte){ + char data[2]; // char type ranges from 0 to 255 (8 bytes) + int error; + data[0] = reg_add; + data[1] = byte; + error = i2c->write(I2C_add,data,2); + //if(DEBUG)db.printf("wr[%02X %02X %d]\r\n", data[0], data[1], error); + return error; + +} + +/// **************************************************************************** +// AO32_write_register(char, char, char *, int) writes multiple bytes to AO32 +// char I2C address +// char AO32 register address +// char * data vector of bytes to be written +// int number of bytes to write +// returns 0 on success ACK, 1 on NACK +// ***************************************************************************** + +int AO32_write_register(I2C *i2c, char I2C_add, char reg_add, char *bytes, int n){ + int i; + //set start address + char data[16]; + int error; + data[0] = reg_add; + for(i=1;i<=n;i++){ + data[i] = bytes[i-1]; + } + error = i2c->write(I2C_add,data,n+1); // send n bytes of data + + return error; +} + +// ***************************************************************************** +// AO32_read_register(char, char, char *) reads single byte from AO32 +// char I2C address +// char AO32 register address +// char * data vector for read bytes to be stored in +// returns 0 on success, 1 on fail +// ***************************************************************************** + +int AO32_read_register(I2C *i2c, char I2C_add, char reg_add, char *bytes){ + int error; + error = i2c->write(I2C_add,®_add,1,1); + if(error)return error; + error = i2c->read(I2C_add,bytes,1); + //if(DEBUG)db.printf("rr e[%d]\r\n",error); + return error; +} + +// ***************************************************************************** +// AO32_read_register(char, char, char *, int) reads byte(s) from AO32 +// char I2C address +// char OT07 register address +// char * data vector for read bytes to be stored in +// int number of bytes to read +// returns 0 on success, 1 on fail +// ***************************************************************************** + +int AO32_read_register(I2C *i2c, char I2C_add, char reg_add, char *bytes, int n){ + int error; + error = i2c->write(I2C_add,®_add,1,1); + if(error)return error; + error = i2c->read(I2C_add,bytes,n); + //if(DEBUG)db.printf("rr e[%d]\r\n",error); + return error; +} + +//****************************************************************************** +// get_OCV(char) read Open Circuit Voltage from AO32 device register +// char I2C address +// returns VOCMeas VOCvalue = Open-Circuit Voltage Measurement in Volt +// status = Status of OCV read. 0 for success, 1 for error +//****************************************************************************** +VOCMeas get_OCV(I2C *i2c, char I2C_add) { + + char data[2]; // only needs 1 byte + double voltage; + + // Read lux value, 2 bytes + int error = AO32_read_register(i2c, I2C_add, AO32_VOC, data); + + // Calculate Open Circuit Voltage from data + voltage = int(data[0]) / 100; + + VOCMeas resp; + resp.VOCvalue = voltage; + resp.status = error; // 1 for nack/error. 0 for ack/success + return resp; +} + +//****************************************************************************** +// get_Harvest(char) read harvesting counts from AO32 device register +// char I2C address +// returns HarvCnt harvCount = Harvester Count "LX Pulses" in decimal +// status = Status of harvesting count read. 0 for success, 1 for error +//****************************************************************************** +HarvCnt get_Harvest(I2C *i2c, char I2C_add) { + + char data[2]; + int countHi; + int countLo; + int counts; + + // Read harvesting counts, 2 bytes + int error = AO32_read_register(i2c, I2C_add, AO32_HARV_H, data, 2); // burst read AO32_HARV_H and AO32_HARV_L + + // Calculate harvesting counts from data + countHi = int(data[0] << 8); // might cause trouble, * 256 instead? + countLo = int(data[1]); + counts = countHi + countLo; + + HarvCnt resp; + resp.harvCount = counts; + resp.status = error; // 1 for nack/error. 0 for ack/success + return resp; +} + + +//****************************************************************************** +// get_luxvalue(char) read lux value from AO32 device register +// char I2C address +// returns LuxResponse luxValue = light intensity in lux +// status = register read result +//****************************************************************************** + +//LuxResponse get_luxvalue(I2C *i2c, char I2C_add){ +// char data[2]; // 2 bytes of raw Lux Register +// double lux; +//// int count; +// +// // Read lux value, 2 bytes +// int error = AO32_read_lux_register(i2c, I2C_add, AO32_LUX_HI, data); +// +// //calculate lux from data +//// count = (int)(data[0]*256 + data[1]); +//// if (count >= 32768)count = count - 65536; // 2s comp +//// T = (double)count*0.005; +// +// int exponent; +// int mantissa; +// exponent = data[0] >> 4; +// mantissa = (data[0] << 4) + data[1]; +// lux = 0.045 * mantissa * pow(2, exponent); +// +// LuxResponse resp; +// resp.luxValue = lux; +// resp.status = error; // 1 for nack/error. 0 for ack/success +// return resp; +//} \ No newline at end of file