Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SX1276GenericLib USBDevice
Fork of NonPingPong_PICO_LoRa_LP0 by
AO32_lib.cpp
00001 00002 #include "mbed.h" 00003 #include "AO32_lib.h" 00004 00005 // ***************************************************************************** 00006 // AO32_write_register(char, char, char) writes single byte to AO32 00007 // char I2C address 00008 // char AO32 register address 00009 // char data byte to be writen 00010 // returns 0 on success ACK, 1 on NACK 00011 // ***************************************************************************** 00012 00013 int AO32_write_register(I2C *i2c, char I2C_add, char reg_add, char byte){ 00014 char data[2]; // char type ranges from 0 to 255 (8 bytes) 00015 int error; 00016 data[0] = reg_add; 00017 data[1] = byte; 00018 error = i2c->write(I2C_add,data,2); 00019 //if(DEBUG)db.printf("wr[%02X %02X %d]\r\n", data[0], data[1], error); 00020 return error; 00021 00022 } 00023 00024 /// **************************************************************************** 00025 // AO32_write_register(char, char, char *, int) writes multiple bytes to AO32 00026 // char I2C address 00027 // char AO32 register address 00028 // char * data vector of bytes to be written 00029 // int number of bytes to write 00030 // returns 0 on success ACK, 1 on NACK 00031 // ***************************************************************************** 00032 00033 int AO32_write_register(I2C *i2c, char I2C_add, char reg_add, char *bytes, int n){ 00034 int i; 00035 //set start address 00036 char data[16]; 00037 int error; 00038 data[0] = reg_add; 00039 for(i=1;i<=n;i++){ 00040 data[i] = bytes[i-1]; 00041 } 00042 error = i2c->write(I2C_add,data,n+1); // send n bytes of data 00043 00044 return error; 00045 } 00046 00047 // ***************************************************************************** 00048 // AO32_read_register(char, char, char *) reads single byte from AO32 00049 // char I2C address 00050 // char AO32 register address 00051 // char * data vector for read bytes to be stored in 00052 // returns 0 on success, 1 on fail 00053 // ***************************************************************************** 00054 00055 int AO32_read_register(I2C *i2c, char I2C_add, char reg_add, char *bytes){ 00056 int error; 00057 error = i2c->write(I2C_add,®_add,1,1); 00058 if(error)return error; 00059 error = i2c->read(I2C_add,bytes,1); 00060 //if(DEBUG)db.printf("rr e[%d]\r\n",error); 00061 return error; 00062 } 00063 00064 // ***************************************************************************** 00065 // AO32_read_register(char, char, char *, int) reads byte(s) from AO32 00066 // char I2C address 00067 // char OT07 register address 00068 // char * data vector for read bytes to be stored in 00069 // int number of bytes to read 00070 // returns 0 on success, 1 on fail 00071 // ***************************************************************************** 00072 00073 int AO32_read_register(I2C *i2c, char I2C_add, char reg_add, char *bytes, int n){ 00074 int error; 00075 error = i2c->write(I2C_add,®_add,1,1); 00076 if(error)return error; 00077 error = i2c->read(I2C_add,bytes,n); 00078 //if(DEBUG)db.printf("rr e[%d]\r\n",error); 00079 return error; 00080 } 00081 00082 //****************************************************************************** 00083 // get_OCV(char) read Open Circuit Voltage from AO32 device register 00084 // char I2C address 00085 // returns VOCMeas VOCvalue = Open-Circuit Voltage Measurement in Volt 00086 // status = Status of OCV read. 0 for success, 1 for error 00087 //****************************************************************************** 00088 VOCMeas get_OCV(I2C *i2c, char I2C_add) { 00089 00090 char data[2]; // only needs 1 byte 00091 double voltage; 00092 00093 // Read lux value, 2 bytes 00094 int error = AO32_read_register(i2c, I2C_add, AO32_VOC, data); 00095 00096 // Calculate Open Circuit Voltage from data 00097 voltage = int(data[0]) / 100; 00098 00099 VOCMeas resp; 00100 resp.VOCvalue = voltage; 00101 resp.status = error; // 1 for nack/error. 0 for ack/success 00102 return resp; 00103 } 00104 00105 //****************************************************************************** 00106 // get_Harvest(char) read harvesting counts from AO32 device register 00107 // char I2C address 00108 // returns HarvCnt harvCount = Harvester Count "LX Pulses" in decimal 00109 // status = Status of harvesting count read. 0 for success, 1 for error 00110 //****************************************************************************** 00111 HarvCnt get_Harvest(I2C *i2c, char I2C_add) { 00112 00113 char data[2]; 00114 int countHi; 00115 int countLo; 00116 int counts; 00117 00118 // Read harvesting counts, 2 bytes 00119 int error = AO32_read_register(i2c, I2C_add, AO32_HARV_H, data, 2); // burst read AO32_HARV_H and AO32_HARV_L 00120 00121 // Calculate harvesting counts from data 00122 // countHi = int(data[0] << 8); // might cause trouble, * 256 instead? 00123 countHi = int(data[0]) * 256; 00124 countLo = int(data[1]); 00125 counts = countHi + countLo; 00126 00127 HarvCnt resp; 00128 resp.harvCount = counts; 00129 resp.status = error; // 1 for nack/error. 0 for ack/success 00130 return resp; 00131 } 00132 00133 double calc_OCV(char *data) { 00134 00135 // data is an array of size 2, only needs the first byte 00136 00137 double voltage = (double)(data[0]) / 100; 00138 00139 return voltage; 00140 00141 } 00142 00143 int calc_Harvest(char *data) { 00144 00145 // data is an array of size 2 00146 00147 int countHi = int(data[0]) * 256; 00148 int countLo = int(data[1]); 00149 int harvest_counts = countHi + countLo; 00150 00151 return harvest_counts; 00152 00153 }
Generated on Wed Jul 13 2022 08:39:54 by
