no comment
Dependents: MAX30100_FirstTry MAX30100_V04
Fork of MAX30100 by
MAX30100.cpp@0:c8da8e2afe09, 2017-03-27 (annotated)
- Committer:
- Ferszt
- Date:
- Mon Mar 27 23:21:28 2017 +0000
- Revision:
- 0:c8da8e2afe09
- Child:
- 1:e96604eb8062
arduino to mbed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ferszt | 0:c8da8e2afe09 | 1 | /* |
Ferszt | 0:c8da8e2afe09 | 2 | Library for the Maxim MAX30100 pulse oximetry system |
Ferszt | 0:c8da8e2afe09 | 3 | Connor Huffine/Kontakt |
Ferszt | 0:c8da8e2afe09 | 4 | February 2016 |
Ferszt | 0:c8da8e2afe09 | 5 | */ |
Ferszt | 0:c8da8e2afe09 | 6 | |
Ferszt | 0:c8da8e2afe09 | 7 | #include "mbed.h" |
Ferszt | 0:c8da8e2afe09 | 8 | #include "MAX30100.h" |
Ferszt | 0:c8da8e2afe09 | 9 | #include "I2C_Driver.h" |
Ferszt | 0:c8da8e2afe09 | 10 | #include "wire.h" |
Ferszt | 0:c8da8e2afe09 | 11 | |
Ferszt | 0:c8da8e2afe09 | 12 | //TwoWire wire1 = TwoWire(NRF_TWI0); |
Ferszt | 0:c8da8e2afe09 | 13 | |
Ferszt | 0:c8da8e2afe09 | 14 | MAX30100::MAX30100(){ |
Ferszt | 0:c8da8e2afe09 | 15 | |
Ferszt | 0:c8da8e2afe09 | 16 | } |
Ferszt | 0:c8da8e2afe09 | 17 | |
Ferszt | 0:c8da8e2afe09 | 18 | void MAX30100::setLEDs(pulseWidth pw, ledCurrent red, ledCurrent ir){ |
Ferszt | 0:c8da8e2afe09 | 19 | uint8_t reg = I2CreadByte(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG); |
Ferszt | 0:c8da8e2afe09 | 20 | reg = reg & 0xFC; // Set LED_PW to 00 |
Ferszt | 0:c8da8e2afe09 | 21 | I2CwriteByte(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG, reg | pw); // Mask LED_PW |
Ferszt | 0:c8da8e2afe09 | 22 | I2CwriteByte(MAX30100_ADDRESS, MAX30100_LED_CONFIG, (red<<4) | ir); // write LED configs |
Ferszt | 0:c8da8e2afe09 | 23 | } |
Ferszt | 0:c8da8e2afe09 | 24 | |
Ferszt | 0:c8da8e2afe09 | 25 | void MAX30100::setSPO2(sampleRate sr){ |
Ferszt | 0:c8da8e2afe09 | 26 | uint8_t reg = I2CreadByte(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG); |
Ferszt | 0:c8da8e2afe09 | 27 | reg = reg & 0xE3; // Set SPO2_SR to 000 |
Ferszt | 0:c8da8e2afe09 | 28 | I2CwriteByte(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG, reg | (sr<<2)); // Mask SPO2_SR |
Ferszt | 0:c8da8e2afe09 | 29 | reg = I2CreadByte(MAX30100_ADDRESS, MAX30100_MODE_CONFIG); |
Ferszt | 0:c8da8e2afe09 | 30 | reg = reg & 0xf8; // Set Mode to 000 |
Ferszt | 0:c8da8e2afe09 | 31 | I2CwriteByte(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG, reg | 0x03); // Mask MODE |
Ferszt | 0:c8da8e2afe09 | 32 | } |
Ferszt | 0:c8da8e2afe09 | 33 | |
Ferszt | 0:c8da8e2afe09 | 34 | int MAX30100::getNumSamp(void){ |
Ferszt | 0:c8da8e2afe09 | 35 | uint8_t wrPtr = I2CreadByte(MAX30100_ADDRESS, MAX30100_FIFO_WR_PTR); |
Ferszt | 0:c8da8e2afe09 | 36 | uint8_t rdPtr = I2CreadByte(MAX30100_ADDRESS, MAX30100_FIFO_RD_PTR); |
Ferszt | 0:c8da8e2afe09 | 37 | return (abs( 16 + wrPtr - rdPtr ) % 16); |
Ferszt | 0:c8da8e2afe09 | 38 | } |
Ferszt | 0:c8da8e2afe09 | 39 | |
Ferszt | 0:c8da8e2afe09 | 40 | void MAX30100::readSensor(void){ |
Ferszt | 0:c8da8e2afe09 | 41 | uint8_t temp[4] = {0}; // Temporary buffer for read values |
Ferszt | 0:c8da8e2afe09 | 42 | I2CreadBytes(MAX30100_ADDRESS, MAX30100_FIFO_DATA, &temp[0], 4); // Read four times from the FIFO |
Ferszt | 0:c8da8e2afe09 | 43 | IR = (temp[0]<<8) | temp[1]; // Combine values to get the actual number |
Ferszt | 0:c8da8e2afe09 | 44 | RED = (temp[2]<<8) | temp[3]; // Combine values to get the actual number |
Ferszt | 0:c8da8e2afe09 | 45 | } |
Ferszt | 0:c8da8e2afe09 | 46 | |
Ferszt | 0:c8da8e2afe09 | 47 | void MAX30100::shutdown(void){ |
Ferszt | 0:c8da8e2afe09 | 48 | uint8_t reg = I2CreadByte(MAX30100_ADDRESS, MAX30100_MODE_CONFIG); // Get the current register |
Ferszt | 0:c8da8e2afe09 | 49 | I2CwriteByte(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, reg | 0x80); // mask the SHDN bit |
Ferszt | 0:c8da8e2afe09 | 50 | } |
Ferszt | 0:c8da8e2afe09 | 51 | |
Ferszt | 0:c8da8e2afe09 | 52 | void MAX30100::reset(void){ |
Ferszt | 0:c8da8e2afe09 | 53 | uint8_t reg = I2CreadByte(MAX30100_ADDRESS, MAX30100_MODE_CONFIG); // Get the current register |
Ferszt | 0:c8da8e2afe09 | 54 | I2CwriteByte(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, reg | 0x40); // mask the RESET bit |
Ferszt | 0:c8da8e2afe09 | 55 | } |
Ferszt | 0:c8da8e2afe09 | 56 | |
Ferszt | 0:c8da8e2afe09 | 57 | void MAX30100::startup(void){ |
Ferszt | 0:c8da8e2afe09 | 58 | uint8_t reg = I2CreadByte(MAX30100_ADDRESS, MAX30100_MODE_CONFIG); // Get the current register |
Ferszt | 0:c8da8e2afe09 | 59 | I2CwriteByte(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, reg & 0x7F); // mask the SHDN bit |
Ferszt | 0:c8da8e2afe09 | 60 | } |
Ferszt | 0:c8da8e2afe09 | 61 | |
Ferszt | 0:c8da8e2afe09 | 62 | int MAX30100::getRevID(void){ |
Ferszt | 0:c8da8e2afe09 | 63 | return I2CreadByte(MAX30100_ADDRESS, MAX30100_REV_ID); |
Ferszt | 0:c8da8e2afe09 | 64 | } |
Ferszt | 0:c8da8e2afe09 | 65 | |
Ferszt | 0:c8da8e2afe09 | 66 | int MAX30100::getPartID(void){ |
Ferszt | 0:c8da8e2afe09 | 67 | return I2CreadByte(MAX30100_ADDRESS, MAX30100_PART_ID); |
Ferszt | 0:c8da8e2afe09 | 68 | } |
Ferszt | 0:c8da8e2afe09 | 69 | |
Ferszt | 0:c8da8e2afe09 | 70 | void MAX30100::begin(pulseWidth pw, ledCurrent ir, sampleRate sr){ |
Ferszt | 0:c8da8e2afe09 | 71 | I2CwriteByte(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, 0x03); // Heart rate only |
Ferszt | 0:c8da8e2afe09 | 72 | I2CwriteByte(MAX30100_ADDRESS, MAX30100_LED_CONFIG, 0XFF); |
Ferszt | 0:c8da8e2afe09 | 73 | I2CwriteByte(MAX30100_ADDRESS, MAX30100_LED_CONFIG, ir<<4|0x0f); |
Ferszt | 0:c8da8e2afe09 | 74 | I2CwriteByte(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG, (sr<<2)|pw); |
Ferszt | 0:c8da8e2afe09 | 75 | } |
Ferszt | 0:c8da8e2afe09 | 76 | |
Ferszt | 0:c8da8e2afe09 | 77 | //void MAX30100::printRegisters(void){ |
Ferszt | 0:c8da8e2afe09 | 78 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_INT_STATUS), BIN); |
Ferszt | 0:c8da8e2afe09 | 79 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_INT_ENABLE), BIN); |
Ferszt | 0:c8da8e2afe09 | 80 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_FIFO_WR_PTR), BIN); |
Ferszt | 0:c8da8e2afe09 | 81 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_OVRFLOW_CTR), BIN); |
Ferszt | 0:c8da8e2afe09 | 82 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_FIFO_RD_PTR), BIN); |
Ferszt | 0:c8da8e2afe09 | 83 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_FIFO_DATA), BIN); |
Ferszt | 0:c8da8e2afe09 | 84 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_MODE_CONFIG), BIN); |
Ferszt | 0:c8da8e2afe09 | 85 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG), BIN); |
Ferszt | 0:c8da8e2afe09 | 86 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_LED_CONFIG), BIN); |
Ferszt | 0:c8da8e2afe09 | 87 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_TEMP_INTG), BIN); |
Ferszt | 0:c8da8e2afe09 | 88 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_TEMP_FRAC), BIN); |
Ferszt | 0:c8da8e2afe09 | 89 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_REV_ID), BIN); |
Ferszt | 0:c8da8e2afe09 | 90 | // Serial.println(I2CreadByte(MAX30100_ADDRESS, MAX30100_PART_ID), BIN); |
Ferszt | 0:c8da8e2afe09 | 91 | //} |
Ferszt | 0:c8da8e2afe09 | 92 | |
Ferszt | 0:c8da8e2afe09 | 93 | // Wire.h read and write protocols |
Ferszt | 0:c8da8e2afe09 | 94 | void MAX30100::I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data) |
Ferszt | 0:c8da8e2afe09 | 95 | { |
Ferszt | 0:c8da8e2afe09 | 96 | //wire1.beginTransmission(address); // Initialize the Tx buffer |
Ferszt | 0:c8da8e2afe09 | 97 | //wire1.write(subAddress); // Put slave register address in Tx buffer |
Ferszt | 0:c8da8e2afe09 | 98 | //wire1.write(data); // Put data in Tx buffer |
Ferszt | 0:c8da8e2afe09 | 99 | //wire1.endTransmission(); // Send the Tx buffer |
Ferszt | 0:c8da8e2afe09 | 100 | write_i2c(subAddress, &data, 1, 0xAE); |
Ferszt | 0:c8da8e2afe09 | 101 | } |
Ferszt | 0:c8da8e2afe09 | 102 | |
Ferszt | 0:c8da8e2afe09 | 103 | uint8_t MAX30100::I2CreadByte(uint8_t address, uint8_t subAddress) |
Ferszt | 0:c8da8e2afe09 | 104 | { |
Ferszt | 0:c8da8e2afe09 | 105 | uint8_t data; // `data` will store the register data |
Ferszt | 0:c8da8e2afe09 | 106 | //wire.beginTransmission(address); // Initialize the Tx buffer |
Ferszt | 0:c8da8e2afe09 | 107 | //wire.write(subAddress); // Put slave register address in Tx buffer |
Ferszt | 0:c8da8e2afe09 | 108 | //wire.endTransmission(false); // Send the Tx buffer, but send a restart to keep connection alive |
Ferszt | 0:c8da8e2afe09 | 109 | //wire.requestFrom(address, (uint8_t) 1); // Read one byte from slave register address |
Ferszt | 0:c8da8e2afe09 | 110 | //data = Wire.read(); // Fill Rx buffer with result |
Ferszt | 0:c8da8e2afe09 | 111 | read_i2c(subAddress, &data, 1, 0xAE); |
Ferszt | 0:c8da8e2afe09 | 112 | return data; // Return data read from slave register |
Ferszt | 0:c8da8e2afe09 | 113 | } |
Ferszt | 0:c8da8e2afe09 | 114 | |
Ferszt | 0:c8da8e2afe09 | 115 | void MAX30100::I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count) |
Ferszt | 0:c8da8e2afe09 | 116 | { |
Ferszt | 0:c8da8e2afe09 | 117 | //Wire.beginTransmission(address); // Initialize the Tx buffer |
Ferszt | 0:c8da8e2afe09 | 118 | // Next send the register to be read. OR with 0x80 to indicate multi-read. |
Ferszt | 0:c8da8e2afe09 | 119 | //Wire.write(subAddress); // Put slave register address in Tx buffer |
Ferszt | 0:c8da8e2afe09 | 120 | //Wire.endTransmission(false); // Send the Tx buffer, but send a restart to keep connection alive |
Ferszt | 0:c8da8e2afe09 | 121 | //uint8_t i = 0; |
Ferszt | 0:c8da8e2afe09 | 122 | //Wire.requestFrom(address, count); // Read bytes from slave register address |
Ferszt | 0:c8da8e2afe09 | 123 | //while (Wire.available()) |
Ferszt | 0:c8da8e2afe09 | 124 | //{ |
Ferszt | 0:c8da8e2afe09 | 125 | // dest[i++] = Wire.read(); // Put read results in the Rx buffer |
Ferszt | 0:c8da8e2afe09 | 126 | //} |
Ferszt | 0:c8da8e2afe09 | 127 | read_i2c(subAddress, dest, count, 0xAE); |
Ferszt | 0:c8da8e2afe09 | 128 | } |