Thanks for the quick reply
OK, let's start with the getting the return into it's own register.
uint16_t GaugeStatus = 0; // This should be perfectly fine for holding the return.
To get the 0x40 and the 0x85 into one variable as 0x8540
The block read is more complicated than that because it first reads the command and determines how much data is being returned by specific commands and then uses that to know how many times to read data on the following read cycle.
I think the read block is fine since it will always return the correct amount of data from the read command which can be 2 to 4 bytes long. I just need to figure out how to that the returned data and pass it back to a global variable.
Here is the ReadBlock Function code that works fine:
// pass a pointer to a char[] that can take up to 33 chars
// will return the length of the string received
int BQ20Z45::readStringB(uint8_t address, char* result)
{
int pos = 0;
int len;
// Read the length of the string
Wire.beginTransmission(BQ20Z45_Address); //Setup Write to Gauge
Wire.write(0x44); //Manufacturer Block Read
Wire.write(0x02); //How Many Bytes to expect Next
Wire.write(address); //Manufacturer Access Command - Example 0x01 Device Type
Wire.write(0x00); //First Byte of Command
Wire.endTransmission(true); //Repeated Start = True
Wire.beginTransmission(BQ20Z45_Address); //Setup Write to Gauge
Wire.write(0x44); //Manufacturer Block Read
Wire.endTransmission(true); //Repeated Start
Wire.requestFrom(BQ20Z45_Address, 1, true); //Setup Reading of Returned Data
len = Wire.read(); // length of the string thats about to return
len++; // plus one to allow for the length byte on the reread
// if len > 32 then the it will be truncated to 32 by requestFrom
len = Wire.requestFrom(BQ20Z45_Address, len, true); // readRequest returns # bytes actually read
len--; // we won't move the first 3 bytes as its not part of the string
len--;
len--;
if (len > 0)
{
Wire.read();
Wire.read();
Wire.read();
for (pos = 0; pos < len; pos++)
result[pos] = Wire.read();
}
result[pos] = '\0'; // append the zero terminator
return len;
}
This is the function that calls that ReadBlock function and passes the correct command address to the function. Both lines of code are located in teh .cpp file:
// Class Methods Below
int BQ20Z45::GaugingStatus(char *result)
{
return readStringB(BQ20Z45_GaugingStatus, result);
}
Here is the .H file code if it makes a difference.
#ifndef BQ20Z45_h
#define BQ20Z45_h
#define BQ20Z45_Address 0x0B
#define BQ20Z45_ManBlockAccess 0x44
#define BQ20Z45_ManAccess 0x00
#define BQ20Z45_GaugingStatus 0x56
class BQ20Z45
{
public:
int GaugingStatus(char *result);
protected:
private:
int readStringB(uint8_t address, char* result);
};
#endif
Is there any way to set this up so the Block Read function passes the gathered data into a global variable by modifying the code above in the .H and .CPP files?
It looks like I need to change the result to a different data type for this to work correctly but I'm going to need some guidance to help get that setup correctly of course