zinnet yazıcı / Mbed OS max30105Example
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX30105.h Source File

MAX30105.h

00001 
00002 #ifndef _MAX30105_H_
00003 #define _MAX30105_H_
00004 
00005 #include "mbed.h"
00006 
00007 
00008 #define MAX30105_ADDRESS          0x57 //7-bit I2C Address //87 //1010111
00009 
00010 //#define MAX30105_I2C_ADDR  0xAE       << 1  //8-BİT //174//10101110 //
00011 
00012 //Note that MAX30102 has the same I2C address and Part ID
00013 
00014 
00015 #define MAX30105_NO_ERROR   0
00016 #define MAX30105_ERROR      -1
00017 #define MAX30105_TEMP_ERROR  -999.0
00018 
00019 //#define I2C_SPEED_STANDARD        100000
00020 //#define I2C_SPEED_FAST            400000
00021 
00022 #define I2C_BUFFER_LENGTH 32
00023 
00024 
00025 
00026 class MAX30105 {
00027  public:
00028   
00029     
00030   MAX30105(I2C &i2c);
00031   MAX30105(void);
00032 
00033   //boolean begin(TwoWire &wirePort = Wire, uint32_t i2cSpeed = I2C_SPEED_STANDARD, uint8_t i2caddr = MAX30105_ADDRESS);
00034 
00035   uint32_t getRed(void); //Returns immediate red value
00036   uint32_t getIR(void); //Returns immediate IR value
00037   uint32_t getGreen(void); //Returns immediate green value
00038   bool safeCheck(uint8_t maxTimeToCheck); //Given a max amount of time, check for new data
00039 
00040   // Configuration
00041   void softReset();
00042   void shutDown(); 
00043   void wakeUp(); 
00044 
00045   void setLEDMode(uint8_t mode);
00046 
00047   void setADCRange(uint8_t adcRange);
00048   void setSampleRate(uint8_t sampleRate);
00049   void setPulseWidth(uint8_t pulseWidth);
00050 
00051   void setPulseAmplitudeRed(uint8_t value);
00052   void setPulseAmplitudeIR(uint8_t value);
00053   void setPulseAmplitudeGreen(uint8_t value);
00054   void setPulseAmplitudeProximity(uint8_t value);
00055 
00056   void setProximityThreshold(uint8_t threshMSB);
00057 
00058   //Multi-led configuration mode (page 22)
00059   void enableSlot(uint8_t slotNumber, uint8_t device); //Given slot number, assign a device to slot
00060   void disableSlots(void);
00061   
00062   // Data Collection
00063 
00064   //Interrupts (page 13, 14)
00065   uint8_t getINT1(void); //Returns the main interrupt group
00066   uint8_t getINT2(void); //Returns the temp ready interrupt
00067   void enableAFULL(void); //Enable/disable individual interrupts
00068   void disableAFULL(void);
00069   void enableDATARDY(void);
00070   void disableDATARDY(void);
00071   void enableALCOVF(void);
00072   void disableALCOVF(void);
00073   void enablePROXINT(void);
00074   void disablePROXINT(void);
00075   void enableDIETEMPRDY(void);
00076   void disableDIETEMPRDY(void);
00077 
00078   //FIFO Configuration (page 18)
00079   void setFIFOAverage(uint8_t samples);
00080   void enableFIFORollover();
00081   void disableFIFORollover();
00082   void setFIFOAlmostFull(uint8_t samples);
00083   
00084   //FIFO Reading
00085   uint16_t check(void); //Checks for new data and fills FIFO
00086   uint8_t available(void); //Tells caller how many new samples are available (head - tail)
00087   void nextSample(void); //Advances the tail of the sense array
00088   uint32_t getFIFORed(void); //Returns the FIFO sample pointed to by tail
00089   uint32_t getFIFOIR(void); //Returns the FIFO sample pointed to by tail
00090   uint32_t getFIFOGreen(void); //Returns the FIFO sample pointed to by tail
00091 
00092   uint8_t getWritePointer(void);
00093   uint8_t getReadPointer(void);
00094   void clearFIFO(void); //Sets the read/write pointers to zero
00095 
00096   //Proximity Mode Interrupt Threshold
00097   void setPROXINTTHRESH(uint8_t val);
00098 
00099   // Die Temperature
00100   float readTemperature();
00101   float readTemperatureF();
00102 
00103   // Detecting ID/Revision
00104   uint8_t getRevisionID();
00105   uint8_t readPartID();  
00106 
00107   // Setup the IC with user selectable settings
00108   void setup(uint8_t powerLevel = 0x1F, uint8_t sampleAverage = 4, uint8_t ledMode = 3, int sampleRate = 400, int pulseWidth = 411, int adcRange = 4096);
00109 
00110   // Low-level I2C communication
00111   uint8_t readRegister8(uint8_t address, uint8_t reg);
00112   void writeRegister8(uint8_t address, uint8_t reg, uint8_t value);
00113   int writeRegValue(uint8_t reg,  char value);
00114   int writeReg(uint8_t reg);
00115   int readReg(uint8_t reg, char *value);
00116 
00117  private:
00118   I2C _i2c; //The generic connection to user's chosen I2C hardware
00119   uint8_t _i2caddr;
00120 
00121   //activeLEDs is the number of channels turned on, and can be 1 to 3. 2 is common for Red+IR.
00122   uint8_t activeLEDs; //Gets set during setup. Allows check() to calculate how many uint8_ts to read from FIFO
00123   
00124   uint8_t revisionID; 
00125 
00126   void readRevisionID();
00127 
00128   void bitMask(uint8_t reg, uint8_t mask, uint8_t thing);
00129  
00130    #define STORAGE_SIZE 4 //Each long is 4 uint8_ts so limit this to fit on your micro
00131   typedef struct Record
00132   {
00133     uint32_t red[STORAGE_SIZE];
00134     uint32_t IR[STORAGE_SIZE];
00135     uint32_t green[STORAGE_SIZE];
00136     uint8_t head;
00137     uint8_t tail;
00138   } sense_struct; //This is our circular buffer of readings from the sensor
00139 
00140   sense_struct sense;
00141   
00142   
00143   
00144   // Status Registers
00145     static const uint8_t MAX30105_INTSTAT1 =        0x00;
00146     static const uint8_t MAX30105_INTSTAT2 =        0x01;
00147     static const uint8_t MAX30105_INTENABLE1 =      0x02;
00148     static const uint8_t MAX30105_INTENABLE2 =      0x03;
00149     
00150     // FIFO Registers
00151     static const uint8_t MAX30105_FIFOWRITEPTR =    0x04;
00152     static const uint8_t MAX30105_FIFOOVERFLOW =    0x05;
00153     static const uint8_t MAX30105_FIFOREADPTR =     0x06;
00154     static const uint8_t MAX30105_FIFODATA =        0x07;
00155     
00156     // Configuration Registers
00157     static const uint8_t MAX30105_FIFOCONFIG =      0x08;
00158     static const uint8_t MAX30105_MODECONFIG =      0x09;
00159     static const uint8_t MAX30105_PARTICLECONFIG =  0x0A;    // Note, sometimes listed as "SPO2" config in datasheet (pg. 11)
00160     static const uint8_t MAX30105_LED1_PULSEAMP =   0x0C;
00161     static const uint8_t MAX30105_LED2_PULSEAMP =   0x0D;
00162     static const uint8_t MAX30105_LED3_PULSEAMP =   0x0E;
00163     static const uint8_t MAX30105_LED_PROX_AMP =    0x10;
00164     static const uint8_t MAX30105_MULTILEDCONFIG1 = 0x11;
00165     static const uint8_t MAX30105_MULTILEDCONFIG2 = 0x12;
00166     
00167     // Die Temperature Registers
00168     static const uint8_t MAX30105_DIETEMPINT =      0x1F;
00169     static const uint8_t MAX30105_DIETEMPFRAC =     0x20;
00170     static const uint8_t MAX30105_DIETEMPCONFIG =   0x21;
00171     
00172     // Proximity Function Registers
00173     static const uint8_t MAX30105_PROXINTTHRESH =   0x30;
00174     
00175     // Part ID Registers
00176     static const uint8_t MAX30105_REVISIONID =      0xFE;
00177     static const uint8_t MAX30105_PARTID =          0xFF;    // Should always be 0x15. Identical to MAX30102.
00178     
00179     // MAX30105 Commands
00180     // Interrupt configuration (pg 13, 14)
00181     static const uint8_t MAX30105_INT_A_FULL_MASK =     (uint8_t)~0b10000000;
00182     static const uint8_t MAX30105_INT_A_FULL_ENABLE =   0x80;
00183     static const uint8_t MAX30105_INT_A_FULL_DISABLE =  0x00;
00184     
00185     static const uint8_t MAX30105_INT_DATA_RDY_MASK = (uint8_t)~0b01000000;
00186     static const uint8_t MAX30105_INT_DATA_RDY_ENABLE = 0x40;
00187     static const uint8_t MAX30105_INT_DATA_RDY_DISABLE = 0x00;
00188     
00189     static const uint8_t MAX30105_INT_ALC_OVF_MASK = (uint8_t)~0b00100000;
00190     static const uint8_t MAX30105_INT_ALC_OVF_ENABLE =  0x20;
00191     static const uint8_t MAX30105_INT_ALC_OVF_DISABLE = 0x00;
00192     
00193     static const uint8_t MAX30105_INT_PROX_INT_MASK = (uint8_t)~0b00010000;
00194     static const uint8_t MAX30105_INT_PROX_INT_ENABLE = 0x10;
00195     static const uint8_t MAX30105_INT_PROX_INT_DISABLE = 0x00;
00196     
00197     static const uint8_t MAX30105_INT_DIE_TEMP_RDY_MASK = (uint8_t)~0b00000010;
00198     static const uint8_t MAX30105_INT_DIE_TEMP_RDY_ENABLE = 0x02;
00199     static const uint8_t MAX30105_INT_DIE_TEMP_RDY_DISABLE = 0x00;
00200     
00201     static const uint8_t MAX30105_SAMPLEAVG_MASK =  (uint8_t)~0b11100000;
00202     static const uint8_t MAX30105_SAMPLEAVG_1 =     0x00;
00203     static const uint8_t MAX30105_SAMPLEAVG_2 =     0x20;
00204     static const uint8_t MAX30105_SAMPLEAVG_4 =     0x40;
00205     static const uint8_t MAX30105_SAMPLEAVG_8 =     0x60;
00206     static const uint8_t MAX30105_SAMPLEAVG_16 =    0x80;
00207     static const uint8_t MAX30105_SAMPLEAVG_32 =    0xA0;
00208     
00209     static const uint8_t MAX30105_ROLLOVER_MASK =   0xEF;
00210     static const uint8_t MAX30105_ROLLOVER_ENABLE = 0x10;
00211     static const uint8_t MAX30105_ROLLOVER_DISABLE = 0x00;
00212     
00213     static const uint8_t MAX30105_A_FULL_MASK =     0xF0;
00214     
00215     // Mode configuration commands (page 19)
00216     static const uint8_t MAX30105_SHUTDOWN_MASK =   0x7F;
00217     static const uint8_t MAX30105_SHUTDOWN =        0x80;
00218     static const uint8_t MAX30105_WAKEUP =          0x00;
00219     
00220     static const uint8_t MAX30105_RESET_MASK =      0xBF;
00221     static const uint8_t MAX30105_RESET =           0x40;
00222     
00223     static const uint8_t MAX30105_MODE_MASK =       0xF8;
00224     static const uint8_t MAX30105_MODE_REDONLY =    0x02;
00225     static const uint8_t MAX30105_MODE_REDIRONLY =  0x03;
00226     static const uint8_t MAX30105_MODE_MULTILED =   0x07;
00227     
00228     // Particle sensing configuration commands (pgs 19-20)
00229     static const uint8_t MAX30105_ADCRANGE_MASK =   0x9F;
00230     static const uint8_t MAX30105_ADCRANGE_2048 =   0x00;
00231     static const uint8_t MAX30105_ADCRANGE_4096 =   0x20;
00232     static const uint8_t MAX30105_ADCRANGE_8192 =   0x40;
00233     static const uint8_t MAX30105_ADCRANGE_16384 =  0x60;
00234     
00235     static const uint8_t MAX30105_SAMPLERATE_MASK = 0xE3;
00236     static const uint8_t MAX30105_SAMPLERATE_50 =   0x00;
00237     static const uint8_t MAX30105_SAMPLERATE_100 =  0x04;
00238     static const uint8_t MAX30105_SAMPLERATE_200 =  0x08;
00239     static const uint8_t MAX30105_SAMPLERATE_400 =  0x0C;
00240     static const uint8_t MAX30105_SAMPLERATE_800 =  0x10;
00241     static const uint8_t MAX30105_SAMPLERATE_1000 = 0x14;
00242     static const uint8_t MAX30105_SAMPLERATE_1600 = 0x18;
00243     static const uint8_t MAX30105_SAMPLERATE_3200 = 0x1C;
00244     
00245     static const uint8_t MAX30105_PULSEWIDTH_MASK = 0xFC;
00246     static const uint8_t MAX30105_PULSEWIDTH_69 =   0x00;
00247     static const uint8_t MAX30105_PULSEWIDTH_118 =  0x01;
00248     static const uint8_t MAX30105_PULSEWIDTH_215 =  0x02;
00249     static const uint8_t MAX30105_PULSEWIDTH_411 =  0x03;
00250     
00251     //Multi-LED Mode configuration (pg 22)
00252     static const uint8_t MAX30105_SLOT1_MASK =      0xF8;
00253     static const uint8_t MAX30105_SLOT2_MASK =      0x8F;
00254     static const uint8_t MAX30105_SLOT3_MASK =      0xF8;
00255     static const uint8_t MAX30105_SLOT4_MASK =      0x8F;
00256     
00257     static const uint8_t SLOT_NONE =                0x00;
00258     static const uint8_t SLOT_RED_LED =             0x01;
00259     static const uint8_t SLOT_IR_LED =              0x02;
00260     static const uint8_t SLOT_GREEN_LED =           0x03;
00261     static const uint8_t SLOT_NONE_PILOT =          0x04;
00262     static const uint8_t SLOT_RED_PILOT =           0x05;
00263     static const uint8_t SLOT_IR_PILOT =            0x06;
00264     static const uint8_t SLOT_GREEN_PILOT =         0x07;
00265     
00266     static const uint8_t MAX_30105_EXPECTEDPARTID = 0x15;
00267 
00268 };
00269 #endif