Library for MAX30101 SpO2 and heart rate sensor
Dependents: HeartRate HeartRate proj final_project_ee119 ... more
Diff: MAX30101.cpp
- Revision:
- 5:be1dde31fe49
- Parent:
- 3:a2effad05c99
- Child:
- 7:ce122c27358e
diff -r d1b50bd4065a -r be1dde31fe49 MAX30101.cpp --- a/MAX30101.cpp Thu May 04 23:46:46 2017 +0000 +++ b/MAX30101.cpp Fri May 05 00:25:21 2017 +0000 @@ -260,50 +260,47 @@ //***************************************************************************** -int32_t MAX30101::readFIFO(uint8_t numLeds, uint8_t *data, uint16_t &readBytes) +int32_t MAX30101::readFIFO(LedChannels_e numLeds, uint8_t *data, uint16_t &readBytes) { int32_t result = -1; readBytes = 0; - if((numLeds > 0) && (numLeds < 4)) + //Get write pointer + result = readRegister(FIFO_WritePointer, m_fifoWritePtr); + if(result == 0) { - //Get write pointer - result = readRegister(FIFO_WritePointer, m_fifoWritePtr); + //Get read pointer + result = readRegister(FIFO_ReadPointer, m_fifoReadPtr); if(result == 0) { - //Get read pointer - result = readRegister(FIFO_ReadPointer, m_fifoReadPtr); + //Calculate num bytes to read + if(m_fifoWritePtr > m_fifoReadPtr) + { + m_fifoNumBytes = ((m_fifoWritePtr - m_fifoReadPtr) * + (BYTES_PER_CH * numLeds)); + } + else + { + m_fifoNumBytes = (((32 - m_fifoReadPtr) + m_fifoWritePtr) * + (BYTES_PER_CH * numLeds)); + } + + //temporary buffer for data + char local_data[m_fifoNumBytes]; + local_data[0] = FIFO_DataRegister; + + //Set fifo data ptr + result = m_i2cBus.write(I2C_W_ADRS, local_data, 1, true); if(result == 0) { - //Calculate num bytes to read - if(m_fifoWritePtr > m_fifoReadPtr) - { - m_fifoNumBytes = ((m_fifoWritePtr - m_fifoReadPtr) * - (BYTES_PER_CH * numLeds)); - } - else - { - m_fifoNumBytes = (((32 - m_fifoReadPtr) + m_fifoWritePtr) * - (BYTES_PER_CH * numLeds)); - } - - //temporary buffer for data - char local_data[m_fifoNumBytes]; - local_data[0] = FIFO_DataRegister; - - //Set fifo data ptr - result = m_i2cBus.write(I2C_W_ADRS, local_data, 1, true); + //read fifo + result = m_i2cBus.read(I2C_R_ADRS, local_data, m_fifoNumBytes); if(result == 0) { - //read fifo - result = m_i2cBus.read(I2C_R_ADRS, local_data, m_fifoNumBytes); - if(result == 0) - { - //move data to user buffer - memcpy(data, local_data, m_fifoNumBytes); - readBytes = m_fifoNumBytes; - } + //move data to user buffer + memcpy(data, local_data, m_fifoNumBytes); + readBytes = m_fifoNumBytes; } } }