Library for MAX30101 SpO2 and heart rate sensor
Dependents: HeartRate HeartRate proj final_project_ee119 ... more
Diff: MAX30101.cpp
- Revision:
- 3:a2effad05c99
- Parent:
- 2:0db1f3bec727
- Child:
- 5:be1dde31fe49
--- a/MAX30101.cpp Mon May 01 21:00:34 2017 +0000 +++ b/MAX30101.cpp Thu May 04 23:44:48 2017 +0000 @@ -259,6 +259,60 @@ } +//***************************************************************************** +int32_t MAX30101::readFIFO(uint8_t 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 read pointer + result = readRegister(FIFO_ReadPointer, m_fifoReadPtr); + 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); + 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; + } + } + } + } + } + + return result; +} + + //***************************************************************************** int32_t MAX30101::writeRegister(Registers_e reg, uint8_t value) {