Skip to content

Commit 26f6969

Browse files
committed
Fixes a number of array related bugs
* Some functions were calling for pointers and others for arrays, so they have all been changed to arrays to be consistent * Many of the array related functions were returning pointers to the very arrays being passed to it - this is completely unneccessary and so have been changed to return the pass/fail status of the retreival * Added another version of readMultipleBytes that accepts uint8_t instead of uint32_t type arrays
1 parent df89a99 commit 26f6969

File tree

2 files changed

+142
-83
lines changed

2 files changed

+142
-83
lines changed

src/SparkFun_Bio_Sensor_Hub_Library.cpp

Lines changed: 89 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,11 @@ uint8_t SparkFun_Bio_Sensor_Hub::getMcuType() {
608608
int32_t SparkFun_Bio_Sensor_Hub::getBootloaderInf() {
609609

610610
int32_t bootVers = 0;
611-
int32_t revNum[4] = {0};
612-
readMultipleBytes(BOOTLOADER_INFO, BOOTLOADER_VERS, 0x00, 4, revNum);
611+
const size_t sizeOfRev = 4;
612+
int32_t revNum[sizeOfRev] = {};
613+
uint8_t status = readMultipleBytes(BOOTLOADER_INFO, BOOTLOADER_VERS, 0x00, 4, revNum);
613614

614-
if( revNum[1] != SUCCESS )
615+
if(!status)
615616
return ERR_UNKNOWN;
616617
else {
617618
bootVers |= (int32_t(revNum[1]) << 16);
@@ -785,7 +786,7 @@ uint8_t SparkFun_Bio_Sensor_Hub::readRegisterAccel(uint8_t regAddr) {
785786
sensorAttr SparkFun_Bio_Sensor_Hub::getAfeAttributesMAX30101() {
786787

787788
sensorAttr maxAttr;
788-
uint8_t tempArray[2];
789+
uint8_t tempArray[2] {};
789790

790791
readFillArray(READ_ATTRIBUTES_AFE, RETRIEVE_AFE_MAX30101, 2, tempArray);
791792

@@ -805,7 +806,7 @@ sensorAttr SparkFun_Bio_Sensor_Hub::getAfeAttributesMAX30101() {
805806
sensorAttr SparkFun_Bio_Sensor_Hub::getAfeAttributesAccelerometer() {
806807

807808
sensorAttr maxAttr;
808-
uint8_t tempArray[2];
809+
uint8_t tempArray[2] {};
809810

810811
readFillArray(READ_ATTRIBUTES_AFE, RETRIEVE_AFE_ACCELEROMETER, 2, tempArray);
811812

@@ -819,22 +820,23 @@ sensorAttr SparkFun_Bio_Sensor_Hub::getAfeAttributesAccelerometer() {
819820
// Family Byte: DUMP_REGISTERS (0x43), Index Byte: DUMP_REGISTER_MAX30101 (0x03)
820821
// This function returns all registers and register values sequentially of the
821822
// MAX30101 sensor: register zero and register value zero to register n and
822-
// register value n.
823-
uint8_t* SparkFun_Bio_Sensor_Hub::dumpRegisterMAX30101(uint8_t numReg, uint8_t regArray[255]) {
823+
// register value n. There are 36 registers in this case.
824+
uint8_t SparkFun_Bio_Sensor_Hub::dumpRegisterMAX30101(uint8_t regArray[]) {
824825

825-
readFillArray(DUMP_REGISTERS, DUMP_REGISTER_MAX30101, numReg, regArray);
826-
return regArray;
826+
uint8_t numOfBytes = 36;
827+
uint8_t status = readFillArray(DUMP_REGISTERS, DUMP_REGISTER_MAX30101, numOfBytes, regArray);
828+
return status;
827829

828830
}
829831

830832
// Family Byte: DUMP_REGISTERS (0x43), Index Byte: DUMP_REGISTER_ACCELEROMETER (0x04)
831833
// This function returns all registers and register values sequentially of the
832834
// Accelerometer: register zero and register value zero to register n and
833835
// register value n.
834-
uint8_t* SparkFun_Bio_Sensor_Hub::dumpRegisterAccelerometer(uint8_t numReg, uint8_t regArray[]) {
836+
uint8_t SparkFun_Bio_Sensor_Hub::dumpRegisterAccelerometer(uint8_t numReg, uint8_t regArray[]) {
835837

836-
readFillArray(DUMP_REGISTERS, DUMP_REGISTER_ACCELEROMETER, numReg, regArray); //Fake read amount
837-
return regArray;
838+
uint8_t status = readFillArray(DUMP_REGISTERS, DUMP_REGISTER_ACCELEROMETER, numReg, regArray); //Fake read amount
839+
return status;
838840

839841
}
840842

@@ -973,14 +975,14 @@ uint8_t SparkFun_Bio_Sensor_Hub::readAlgoSamples() {
973975
// This function reads the maximum age for the wrist heart rate monitor
974976
// (WHRM) algorithm. It returns three uint32_t integers that are
975977
// multiplied by 100,000.
976-
int32_t* SparkFun_Bio_Sensor_Hub::readMaximFastCoef(int32_t coefArr[3]) {
978+
uint8_t SparkFun_Bio_Sensor_Hub::readMaximFastCoef(int32_t coefArr[3]) {
977979

978-
uint8_t numOfReads = 3;
979-
readMultipleBytes( READ_ALGORITHM_CONFIG, READ_MAX_FAST_COEF, READ_MAX_FAST_COEF_ID, numOfReads, coefArr );
980+
const size_t numOfReads = 3;
981+
uint8_t status = readMultipleBytes( READ_ALGORITHM_CONFIG, READ_MAX_FAST_COEF, READ_MAX_FAST_COEF_ID, numOfReads, coefArr );
980982
coefArr[0] = coefArr[0] * 100000;
981983
coefArr[1] = coefArr[1] * 100000;
982984
coefArr[2] = coefArr[2] * 100000;
983-
return coefArr;
985+
return status;
984986
}
985987

986988
// Family Byte: ENABLE_ALGORITHM (0x52), Index Byte:
@@ -1138,14 +1140,14 @@ uint8_t SparkFun_Bio_Sensor_Hub::isPatientBPMedication(uint8_t medication){
11381140
if (medication != 0x01 || medication != 0x00)
11391141
return INCORR_PARAM;
11401142

1141-
uint8_t status = _writeByte(CHANGE_ALGORITHM_CONFIG, BPT_CONFIG, BPT_MEDICATION, medication);
1143+
uint8_t status = writeByte(CHANGE_ALGORITHM_CONFIG, BPT_CONFIG, BPT_MEDICATION, medication);
11421144
return status;
11431145

11441146
}
11451147

11461148
uint8_t SparkFun_Bio_Sensor_Hub::isPatientBPMedication(){
11471149

1148-
uint8_t medication = _readByte(CHANGE_ALGORITHM_CONFIG, BPT_CONFIG, BPT_MEDICATION);
1150+
uint8_t medication = readByte(CHANGE_ALGORITHM_CONFIG, BPT_CONFIG, BPT_MEDICATION);
11491151
return medication;
11501152

11511153
}
@@ -1173,15 +1175,18 @@ uint8_t SparkFun_Bio_Sensor_Hub::writeDiastolicVals(uint8_t diasVal1, uint8_t di
11731175

11741176
}
11751177

1176-
uint8_t SparkFun_Bio_Sensor_Hub::readDiastolicVals(uint8_t ){
1178+
uint8_t SparkFun_Bio_Sensor_Hub::readDiastolicVals(uint8_t userProvidedArray[]){
11771179

1180+
const size_t numDiasVals = 3;
1181+
uint8_t status = readMultipleBytes(CHANGE_ALGORITHM_CONFIG, BPT_CONFIG, DIASTOLIC_VALUE, numDiasVals, userProvidedArray);
1182+
return status;
11781183

11791184
}
11801185

11811186
uint8_t SparkFun_Bio_Sensor_Hub::writeBPTAlgoData(uint8_t bptCalibData[]){
11821187

11831188
const size_t numCalibVals = 824;
1184-
uint8_t status = writeBytes(CHANGE_ALGORITHM_CONFIG, BPT_CONFIG, BPT_CALIB_DATA, bptCalibData, numCalibVals)
1189+
uint8_t status = writeBytes(CHANGE_ALGORITHM_CONFIG, BPT_CONFIG, BPT_CALIB_DATA, bptCalibData, numCalibVals);
11851190
return status;
11861191

11871192
}
@@ -1216,7 +1221,11 @@ uint8_t SparkFun_Bio_Sensor_Hub::writeSP02AlgoCoef(int32_t intA, int32_t intB, i
12161221

12171222
}
12181223

1219-
uint8_t SparkFun_Bio_Sensor_Hub::readBPTAlgoCoef(){ //overload for read and write
1224+
uint8_t SparkFun_Bio_Sensor_Hub::readSP02AlgoCoef(){ // Have the user provide their own array here and pass the pointer to it
1225+
1226+
uint8_t status = readLongByte(CHANGE_ALGORITHM_CONFIG, BPT_CALIB_DATA, AGC_SP02_COEFS);
1227+
return status;
1228+
12201229
}
12211230

12221231
//-------------------Private Functions-----------------------
@@ -1318,7 +1327,7 @@ uint8_t SparkFun_Bio_Sensor_Hub::writeByte(uint8_t _familyByte, uint8_t _indexBy
13181327
// register address and register value as parameters. Again there is the write
13191328
// of the specific bytes followed by a read to confirm positive transmission.
13201329
uint8_t SparkFun_Bio_Sensor_Hub::writeLongBytes(uint8_t _familyByte, uint8_t _indexByte,\
1321-
uint8_t _writeByte, const int32_t _writeVal[], const size_t _size)
1330+
uint8_t _writeByte, int32_t _writeVal[], const size_t _size)
13221331
{
13231332

13241333
_i2cPort->beginTransmission(_address);
@@ -1348,7 +1357,7 @@ uint8_t SparkFun_Bio_Sensor_Hub::writeLongBytes(uint8_t _familyByte, uint8_t _in
13481357
// register address and register value as parameters. Again there is the write
13491358
// of the specific bytes followed by a read to confirm positive transmission.
13501359
uint8_t SparkFun_Bio_Sensor_Hub::writeBytes(uint8_t _familyByte, uint8_t _indexByte,\
1351-
uint8_t _writeByte, const uint8_t _writeVal[], size_t _size)
1360+
uint8_t _writeByte, uint8_t _writeVal[], size_t _size)
13521361
{
13531362

13541363
_i2cPort->beginTransmission(_address);
@@ -1426,8 +1435,8 @@ uint8_t SparkFun_Bio_Sensor_Hub::readByte(uint8_t _familyByte, uint8_t _indexBy
14261435

14271436
}
14281437

1429-
uint8_t* SparkFun_Bio_Sensor_Hub::readFillArray(uint8_t _familyByte, uint8_t _indexByte,\
1430-
uint8_t arraySize, uint8_t array[] )
1438+
uint8_t SparkFun_Bio_Sensor_Hub::readFillArray(uint8_t _familyByte, uint8_t _indexByte,\
1439+
uint8_t _numOfReads, uint8_t array[] )
14311440
{
14321441

14331442
uint8_t statusByte;
@@ -1438,27 +1447,27 @@ uint8_t* SparkFun_Bio_Sensor_Hub::readFillArray(uint8_t _familyByte, uint8_t _in
14381447
_i2cPort->endTransmission();
14391448
delay(CMD_DELAY);
14401449

1441-
_i2cPort->requestFrom(_address, static_cast<uint8_t>(arraySize + sizeof(statusByte)));
1442-
statusByte = _i2cPort->read(); // Got it
1443-
if( statusByte ){// SUCCESS (0x00)
1444-
for(uint8_t i = 0; i < arraySize; i++){
1450+
_i2cPort->requestFrom(_address, static_cast<uint8_t>(_numOfReads + sizeof(statusByte)));
1451+
statusByte = _i2cPort->read();
1452+
if( statusByte ){// SUCCESS: 0x00
1453+
for(size_t i = 0; i < _numOfReads; i++){
14451454
array[i] = 0;
14461455
}
1447-
return array;
1456+
return statusByte;
14481457
}
14491458

1450-
for(uint8_t i = 0; i < arraySize; i++){
1459+
for(size_t i = 0; i < _numOfReads; i++){
14511460
array[i] = _i2cPort->read();
14521461
}
1453-
return array; // If good then return the array.
1462+
return statusByte;
14541463

14551464
}
14561465
// This function handles all read commands or stated another way, all information
14571466
// requests. It starts a request by writing the family byte, an index byte, and
14581467
// a write byte and then then delays 60 microseconds, during which the MAX32664
14591468
// retrieves the requested information. An I-squared-C request is then issued,
14601469
// and the information is read. This differs from the above read commands in
1461-
// that it returns a 16 bit integer instead of 8.
1470+
// that it returns a 16 bit integer instead of a single byte.
14621471
uint16_t SparkFun_Bio_Sensor_Hub::readIntByte(uint8_t _familyByte, uint8_t _indexByte,\
14631472
uint8_t _writeByte )
14641473
{
@@ -1490,7 +1499,7 @@ uint16_t SparkFun_Bio_Sensor_Hub::readIntByte(uint8_t _familyByte, uint8_t _inde
14901499
// a write byte and then then delays 60 microseconds, during which the MAX32664
14911500
// retrieves the requested information. An I-squared-C request is then issued,
14921501
// and the information is read. This differs from the above read commands in
1493-
// that it returns a 4 byte (uint32_t) integer instead of 8.
1502+
// that it returns three, four byte (uint32_t) integer instead of a single byte.
14941503
uint32_t SparkFun_Bio_Sensor_Hub::readLongByte(uint8_t _familyByte, uint8_t _indexByte,\
14951504
uint8_t _writeByte)
14961505
{
@@ -1525,9 +1534,9 @@ uint32_t SparkFun_Bio_Sensor_Hub::readLongByte(uint8_t _familyByte, uint8_t _ind
15251534
// retrieves the requested information. An I-squared-C request is then issued,
15261535
// and the information is read. This function is very similar to the one above
15271536
// except it returns three uint32_t bytes instead of one.
1528-
int32_t* SparkFun_Bio_Sensor_Hub::readMultipleBytes(uint8_t _familyByte, uint8_t _indexByte,\
1529-
uint8_t _writeByte, uint8_t _numOfReads,\
1530-
int32_t* array)
1537+
uint8_t SparkFun_Bio_Sensor_Hub::readMultipleBytes(uint8_t _familyByte, uint8_t _indexByte,\
1538+
uint8_t _writeByte, const size_t _numOfReads,\
1539+
int32_t userArray[])
15311540
{
15321541

15331542
uint8_t statusByte;
@@ -1542,21 +1551,51 @@ int32_t* SparkFun_Bio_Sensor_Hub::readMultipleBytes(uint8_t _familyByte, uint8_t
15421551
_i2cPort->requestFrom(_address, static_cast<uint8_t>(sizeof(int32_t) * _numOfReads + sizeof(statusByte)));
15431552
statusByte = _i2cPort->read();
15441553
if( statusByte ){ // Pass through if SUCCESS (0x00).
1545-
for(uint8_t i = 0; i < (sizeof(int32_t) * _numOfReads); i++){
1546-
array[i] = 0;
1547-
array[i] = 0;
1548-
array[i] = 0;
1549-
array[i] = 0;
1554+
for(size_t i = 0; i < (sizeof(int32_t) * _numOfReads); i++){
1555+
userArray[i] = 0;
1556+
userArray[i] = 0;
1557+
userArray[i] = 0;
1558+
userArray[i] = 0;
15501559
}
1551-
return array;
1560+
return statusByte;
15521561
}
1562+
1563+
else
1564+
return statusByte;
1565+
}
15531566

1554-
for(uint8_t i = 0; i < (sizeof(int32_t) * _numOfReads); i++){
1555-
array[i] |= (_i2cPort->read() << 24);
1556-
array[i] |= (_i2cPort->read() << 16);
1557-
array[i] |= (_i2cPort->read() << 8);
1558-
array[i] |= _i2cPort->read();
1559-
}
1560-
return array;
1567+
// This function handles all read commands or stated another way, all information
1568+
// requests. It starts a request by writing the family byte, an index byte, and
1569+
// a write byte and then then delays 60 microseconds, during which the MAX32664
1570+
// retrieves the requested information. An I-squared-C request is then issued,
1571+
// and the information is read. This function is very similar to the one above
1572+
// except it returns three uint32_t bytes instead of one.
1573+
uint8_t SparkFun_Bio_Sensor_Hub::readMultipleBytes(uint8_t _familyByte, uint8_t _indexByte,\
1574+
uint8_t _writeByte, const size_t _numOfReads,\
1575+
uint8_t userArray[])
1576+
{
1577+
1578+
uint8_t statusByte;
1579+
1580+
_i2cPort->beginTransmission(_address);
1581+
_i2cPort->write(_familyByte);
1582+
_i2cPort->write(_indexByte);
1583+
_i2cPort->write(_writeByte);
1584+
_i2cPort->endTransmission();
1585+
delay(CMD_DELAY);
15611586

1587+
_i2cPort->requestFrom(_address, static_cast<uint8_t>(sizeof(int32_t) * _numOfReads + sizeof(statusByte)));
1588+
statusByte = _i2cPort->read();
1589+
if( statusByte ){ // Pass through if SUCCESS (0x00).
1590+
for(size_t i = 0; i < (sizeof(uint8_t) * _numOfReads); i++){
1591+
userArray[i] = 0;
1592+
userArray[i] = 0;
1593+
userArray[i] = 0;
1594+
userArray[i] = 0;
1595+
}
1596+
return statusByte;
1597+
}
1598+
1599+
else
1600+
return statusByte;
15621601
}

0 commit comments

Comments
 (0)