Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
max30100.cpp
00001 #include "max30100.h" 00002 00003 //Set up I2C, (SDA,SCL) 00004 static I2C i2c(I2C_SDA, I2C_SCL); 00005 static Serial pc(USBTX, USBRX); // tx, rx 00006 00007 uint16_t IR = 0; // Last IR reflectance datapoint 00008 uint16_t RED = 0; // Last Red reflectance datapoint 00009 00010 //Wire read and write protocols 00011 int max30100::i2c_write (uint8_t i2c_addr, uint8_t register_addr, char* buffer, uint8_t Nbyte ) 00012 { 00013 int ret; 00014 char *tmp_buffer; 00015 00016 tmp_buffer = (char*)malloc(sizeof(char)*(Nbyte+1)); 00017 00018 /* First, send device address. Then, send data and STOP condition */ 00019 tmp_buffer[0] = register_addr; 00020 memcpy(tmp_buffer+1, buffer, Nbyte); 00021 00022 ret = i2c.write(i2c_addr, tmp_buffer, Nbyte+1, false); 00023 00024 return ret; 00025 } 00026 00027 int max30100::i2c_read (uint8_t i2c_addr, uint8_t register_addr, char* buffer, uint8_t Nbyte ) 00028 { 00029 int ret; 00030 00031 /* Send device address, with no STOP condition */ 00032 ret = i2c.write(i2c_addr, (const char*)®ister_addr, 1, true); 00033 if(!ret) { 00034 /* Read data, with STOP condition */ 00035 ret = i2c.read((i2c_addr|0x01), buffer, Nbyte, false); 00036 } 00037 00038 return ret; 00039 } 00040 // 00041 00042 void max30100::setLEDs(pulseWidth pw, ledCurrent red, ledCurrent ir){ 00043 char reg[1]; 00044 i2c_read(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG, ®[0], 1); 00045 reg[0] = reg[0] & 0xFC; // Set LED_PW to 00 00046 reg[0] = reg[0] | pw; 00047 i2c_write(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG, ®[0], 1); // Mask LED_PW 00048 reg[0] = (red<<4) | ir; 00049 i2c_write(MAX30100_ADDRESS, MAX30100_LED_CONFIG, ®[0], 1); // write LED configs 00050 } 00051 00052 void max30100::setSPO2(sampleRate sr){ 00053 char reg[1]; 00054 i2c_read(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG, ®[0], 1); 00055 reg[0] = reg[0] & 0xE3; // Set SPO2_SR to 000 00056 reg[0] = reg[0] | (sr<<2); 00057 i2c_write(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG, ®[0], 1); // Mask SPO2_SR 00058 i2c_read(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, ®[0], 1); 00059 reg[0] = reg[0] & 0xf8; // Set Mode to 000 00060 reg[0] = reg[0] | 0x03; 00061 i2c_write(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG, ®[0], 1); // Mask MODE 00062 } 00063 00064 int max30100::getNumSamp(void){ 00065 char wrPtr[1]; 00066 char rdPtr[1]; 00067 i2c_read(MAX30100_ADDRESS, MAX30100_FIFO_WR_PTR, &wrPtr[0], 1); 00068 i2c_read(MAX30100_ADDRESS, MAX30100_FIFO_RD_PTR, &rdPtr[0], 1); 00069 return (abs( 16 + wrPtr[0] - rdPtr[0] ) % 16); 00070 } 00071 00072 void max30100::readSensor(void){ 00073 char temp[4] = {0}; // Temporary buffer for read values 00074 i2c_read(MAX30100_ADDRESS, MAX30100_FIFO_DATA, &temp[0], 4); // Read four times from the FIFO 00075 IR = (temp[0]<<8) | temp[1]; // Combine values to get the actual number 00076 RED = (temp[2]<<8) | temp[3]; // Combine values to get the actual number 00077 } 00078 00079 void max30100::shutdown(void){ 00080 char reg[1]; 00081 i2c_read(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, ®[0], 1); // Get the current register 00082 reg[0] = reg[0] | 0x80; 00083 i2c_write(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, ®[0], 1); // mask the SHDN bit 00084 } 00085 00086 void max30100::reset(void){ 00087 char reg[1]; 00088 i2c_read(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, ®[0], 1); // Get the current register 00089 reg[0] = reg[0] | 0x40; 00090 i2c_write(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, ®[0], 1); // mask the RESET bit 00091 } 00092 00093 void max30100::startup(void){ 00094 char reg[1]; 00095 i2c_read(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, ®[0], 1); // Get the current register 00096 reg[0] = reg[0] & 0x7F; 00097 i2c_write(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, ®[0], 1); // mask the SHDN bit 00098 } 00099 00100 uint8_t max30100::getRevID(void){ 00101 char buffer[1]; 00102 i2c_read(MAX30100_ADDRESS, MAX30100_REV_ID, &buffer[0], 1); 00103 return buffer[0]; 00104 } 00105 00106 uint8_t max30100::getPartID(void){ 00107 char buffer[1]; 00108 i2c_read(MAX30100_ADDRESS, MAX30100_PART_ID, &buffer[0], 1); 00109 return buffer[0]; 00110 } 00111 00112 void max30100::begin(pulseWidth pw, ledCurrent ir, sampleRate sr){ 00113 char buffer[1]; 00114 buffer[0] = 0x02; 00115 i2c_write(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, &buffer[0], 1); // Heart rate only 00116 buffer[0] = ir; 00117 i2c_write(MAX30100_ADDRESS, MAX30100_LED_CONFIG, &buffer[0], 1); 00118 buffer[0] = (sr<<2)|pw; 00119 i2c_write(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG, &buffer[0], 1); 00120 } 00121 00122 void max30100::printRegisters(void){ 00123 char reg[1]; 00124 i2c_read(MAX30100_ADDRESS, MAX30100_INT_STATUS, ®[0], 1); 00125 pc.printf("MAX30100_INT_STATUS: %d\r\n",reg[0]); 00126 i2c_read(MAX30100_ADDRESS, MAX30100_INT_ENABLE, ®[0], 1); 00127 pc.printf("MAX30100_INT_ENABLE: %d\r\n",reg[0]); 00128 i2c_read(MAX30100_ADDRESS, MAX30100_FIFO_WR_PTR, ®[0], 1); 00129 pc.printf("MAX30100_FIFO_WR_PTR: %d\r\n",reg[0]); 00130 i2c_read(MAX30100_ADDRESS, MAX30100_OVRFLOW_CTR, ®[0], 1); 00131 pc.printf("MAX30100_OVRFLOW_CTR: %d\r\n",reg[0]); 00132 i2c_read(MAX30100_ADDRESS, MAX30100_FIFO_RD_PTR, ®[0], 1); 00133 pc.printf("MAX30100_FIFO_RD_PTR: %d\r\n",reg[0]); 00134 i2c_read(MAX30100_ADDRESS, MAX30100_FIFO_DATA, ®[0], 1); 00135 pc.printf("MAX30100_FIFO_DATA: %d\r\n",reg[0]); 00136 i2c_read(MAX30100_ADDRESS, MAX30100_MODE_CONFIG, ®[0], 1); 00137 pc.printf("MAX30100_MODE_CONFIG: %d\r\n",reg[0]); 00138 i2c_read(MAX30100_ADDRESS, MAX30100_SPO2_CONFIG, ®[0], 1); 00139 pc.printf("MAX30100_SPO2_CONFIG: %d\r\n",reg[0]); 00140 i2c_read(MAX30100_ADDRESS, MAX30100_LED_CONFIG, ®[0], 1); 00141 pc.printf("MAX30100_LED_CONFIG: %d\r\n",reg[0]); 00142 i2c_read(MAX30100_ADDRESS, MAX30100_TEMP_INTG, ®[0], 1); 00143 pc.printf("MAX30100_TEMP_INTG: %d\r\n",reg[0]); 00144 i2c_read(MAX30100_ADDRESS, MAX30100_TEMP_FRAC, ®[0], 1); 00145 pc.printf("MAX30100_TEMP_FRAC: %d\r\n",reg[0]); 00146 i2c_read(MAX30100_ADDRESS, MAX30100_REV_ID, ®[0], 1); 00147 pc.printf("MAX30100_REV_ID: %d\r\n",reg[0]); 00148 i2c_read(MAX30100_ADDRESS, MAX30100_PART_ID, ®[0], 1); 00149 pc.printf("MAX30100_PART_ID: %d\r\n",reg[0]); 00150 }
Generated on Wed Jul 20 2022 01:38:47 by
1.7.2