Ahmad Alkaff / Mbed 2 deprecated CS3237

Dependencies:   mbed

main.cpp

Committer:
AhmadAlkaff
Date:
2019-10-09
Revision:
2:17d5a74fc2c4
Parent:
1:e4a32b4163f2
Child:
3:a7396ed925e6

File content as of revision 2:17d5a74fc2c4:

#include "mbed.h"
#include "BMP280.h"
#include "LIS2DH.h"
#include "MAX14720.h"
#include "MAX30001.h"
#include "MAX30101.h"
#include "MAX30205.h"
#include "System.h"
#include "USBSerial.h"
 
/************************************************************************************************************************************/
// define the HVOUT Boost Voltage default for the MAX14720 PMIC
#define HVOUT_VOLTAGE 4500 // set to 4500 mV
 
// define all I2C addresses
#define LIS2DH_I2C_SLAVE_ADDR          (0x32)
#define MAX14720_I2C_SLAVE_ADDR        (0x54)
#define MAX30101_I2C_SLAVE_ADDR        (0xAE)
#define MAX30205_I2C_SLAVE_ADDR_BOTTOM (0x90)
#define MAX30205_I2C_SLAVE_ADDR_TOP    (0x92)
#define BMP280_I2C_SLAVE_ADDR          (0xEC)
 
// Settings for ECG Initialization
#define En_ecg     0x01
#define Openp      0x00
#define Openn      0x00
#define Pol        0x00
#define Calp_sel   0x00
#define Caln_sel   0x00
#define E_fit      0x0F
#define Rate       0x02
#define Gain       0x00
#define Dhpf       0x01
#define Dlpf       0x01
 
// Settings for ECG RtoR
#define En_rtor    0x01
#define Wndw       0x03
#define Gain1      0x0f
#define Pavg       0x02
#define Ptsf       0x03
#define Hoff       0x20
#define Ravg       0x02
#define Rhsf       0x04
#define Clr_rrint  0x01

// Settings for Lead Detection
#define En_dcloff  0x01
#define Ipol       0x00
#define Imag       0x00
#define Vth        0x00

// Settings for the HR initialization
#define FIFO_WATERLEVEL_MARK 15
#define SAMPLE_AVG           2
#define SAMPLE_RATE          1
#define PULSE_WIDTH          2
#define RED_LED_CURRENT      0x1F
#define IR_LED_CURRENT       0X1F

// Define with Maxim VID and a Maxim assigned PID, set to version 0x0001 and non-blocking
USBSerial usbSerial(0x0b6a, 0x0100, 0x0001, false);
 
// I2C Masters
I2C i2c1(I2C1_SDA, I2C1_SCL); // used by MAX30205, BMP280
I2C i2c2(I2C2_SDA, I2C2_SCL); // used by MAX14720, MAX30101, LIS2DH
 
// SPI Master 0 with SPI0_SS for use with MAX30001
SPI spi(SPI0_MOSI, SPI0_MISO, SPI0_SCK, SPI0_SS); 
 
// PMIC
MAX14720 max14720(&i2c2, MAX14720_I2C_SLAVE_ADDR);
 
// Optical Oximeter
MAX30101 max30101(&i2c2, MAX30101_I2C_SLAVE_ADDR);
InterruptIn max30101_Interrupt(P4_0);
 
// Accelerometer
LIS2DH lis2dh(&i2c2, LIS2DH_I2C_SLAVE_ADDR);
InterruptIn lis2dh_Interrupt(P4_7);

// Barometric Pressure Sensor
BMP280 bmp280_pres(&i2c1, BMP280_I2C_SLAVE_ADDR);
 
// Top Temperature Sensor
MAX30205 MAX30205_top(&i2c1, MAX30205_I2C_SLAVE_ADDR_TOP);

// Botttom Temperature Sensor
MAX30205 MAX30205_bottom(&i2c1, MAX30205_I2C_SLAVE_ADDR_BOTTOM);

// ECG device
MAX30001 max30001(&spi);
InterruptIn max30001_InterruptB(P3_6);
InterruptIn max30001_Interrupt2B(P4_5);
 
// PWM used as fclk for the MAX30001
PwmOut pwmout(P1_7);
  
DigitalOut led(LED1); 

// Data of sensors
int32_t ECG_Raw, ECG_cumul, SpO_Raw;
double ECG_converted = 0;
uint32_t index = 0;

//Temperature sensor variables
uint16_t rawTemp_top, rawTemp_bottom;
float celsius_top, celsius_bottom, fahrenheit_top, fahrenheit_bottom;

// Barometric sensor variables
char bmp280_rawData = 0;
float Temp_degC, Press_Pa, Press_Bar;

// Accelerometer sensor variables
int16_t accel_value[3];

//write the power down command to PMIC
void turnOff() {
    max14720.shutdown();    
}
 
//@brief Creates a packet that will be streamed via USB Serial
//@brief the packet created will be inserted into a fifo to be streamed at a later time
//@param id Streaming ID
//@param buffer Pointer to a uint32 array that contains the data to include in the packet
//@param number Number of elements in the buffer
void StreamPacketUint32_ex(uint32_t id, uint32_t *buffer, uint32_t number) {
    int k;
    
    if(id == MAX30001_DATA_ECG) {
        for (k = 0; k < number ; k++) {
            ECG_Raw = (int32_t)(buffer[k] << 8);
            ECG_Raw = ECG_Raw >> 14;
            usbSerial.printf("ECG Raw: %d;%d\r\n", index, ECG_Raw);
            index++; 
        }
    }
       
    //if(id == MAX30101_OXIMETER_DATA + 2) {
    //    for (k = 0; k < number; k++)
    //        SpO_Raw = (int32_t)(buffer[k]);
    //    usbSerial.printf("SpO Raw: %d\r\n", SpO_Raw);
    //} 
}

//void MAX30101_OnInterrupt(void){
//    I2CM_Init_Reset(2, 1);
//}

int main() {
    // hold results for returning functions
    int result;
    
    // initialize HVOUT on the MAX14720 PMIC
    result = max14720.init();
    if (result == MAX14720_ERROR)
        printf("Error initializing MAX14720");
    max14720.boostEn = MAX14720::BOOST_ENABLED;
    max14720.boostSetVoltage(HVOUT_VOLTAGE);
    
    // This is the SpO2 mode (IR&Red LED)
    //max30101.SpO2mode_init(FIFO_WATERLEVEL_MARK, SAMPLE_AVG, SAMPLE_RATE, PULSE_WIDTH, RED_LED_CURRENT, IR_LED_CURRENT);
        
    // MAX30101 initialize interrupt  
    //max30101.onInterrupt(&MAX30101_OnInterrupt);
    //max30101.onDataAvailable(&StreamPacketUint32_ex);
    //max30101_Interrupt.fall(&MAX30101MidIntHandler);

    // LIS2DH initialize interrupt  
    //lis2dh.onInterrupt(&MAX30101_OnInterrupt);
    //lis2dh.onDataAvailable(&StreamPacketUint32_ex);
    //lis2dh_Interrupt.fall(&MAX30101MidIntHandler);
    
    // Initialise the BMP280
    //bmp280_pres.init(BMP280::OVERSAMPLING_X16_P, BMP280::OVERSAMPLING_X2_T, BMP280::FILT_4, BMP280::NORMAL_MODE, BMP280::T_62_5);

    // LIS2DH initialize interrupt  
    lis2dh.initStart(LIS2DH_DATARATE_10HZ, LIS2DH_FIFO_SIZE);

    // Interrupt priority    
    NVIC_SetPriority(GPIO_P0_IRQn, 5);
    NVIC_SetPriority(GPIO_P1_IRQn, 5);
    NVIC_SetPriority(GPIO_P2_IRQn, 5);
    NVIC_SetPriority(GPIO_P3_IRQn, 5);
    NVIC_SetPriority(GPIO_P4_IRQn, 5);
    NVIC_SetPriority(GPIO_P5_IRQn, 5);
    NVIC_SetPriority(GPIO_P6_IRQn, 5);
    // Used by the MAX30001
    NVIC_SetPriority(SPI1_IRQn, 0);
    
    /* ECG Initialize */
    max30001_InterruptB.disable_irq();
    max30001_Interrupt2B.disable_irq();
    max30001_InterruptB.mode(PullUp);
    max30001_InterruptB.fall(&MAX30001Mid_IntB_Handler);
    max30001_Interrupt2B.mode(PullUp);
    max30001_Interrupt2B.fall(&MAX30001Mid_Int2B_Handler);
    max30001_InterruptB.enable_irq();
    max30001_Interrupt2B.enable_irq();
    MAX30001_AllowInterrupts(1);    
    // Configuring the FCLK for the ECG, set to 32.768KHZ
    pwmout.period_us(31);
    pwmout.write(0.5);          // 0-1 is 0-100%, 0.5 = 50% duty cycle.
    max30001.max30001_sw_rst(); // Do a software reset of the MAX30001
    max30001.max30001_INT_assignment(
        MAX30001::MAX30001_INT_B,  // en_enint_loc
        MAX30001::MAX30001_NO_INT, // en_eovf_loc
        MAX30001::MAX30001_NO_INT, // en_fstint_loc

        MAX30001::MAX30001_INT_2B, // en_dcloffint_loc
        MAX30001::MAX30001_INT_B, // en_bint_loc
        MAX30001::MAX30001_NO_INT, // en_bovf_loc

        MAX30001::MAX30001_INT_2B, // en_bover_loc
        MAX30001::MAX30001_INT_2B, // en_bundr_loc
        MAX30001::MAX30001_NO_INT, // en_bcgmon_loc

        MAX30001::MAX30001_INT_B,  // en_pint_loc
        MAX30001::MAX30001_NO_INT, // en_povf_loc,
        MAX30001::MAX30001_NO_INT, // en_pedge_loc

        MAX30001::MAX30001_INT_2B, // en_lonint_loc
        MAX30001::MAX30001_INT_B,  // en_rrint_loc
        MAX30001::MAX30001_NO_INT, //  en_samp_loc

        MAX30001::MAX30001_INT_ODNR,  // intb_Type
        MAX30001::MAX30001_INT_ODNR); // int2b_Type
        
    // MAX30001 initialize interrupt
    max30001.max30001_ECG_InitStart(En_ecg, Openp, Openn, Pol, Calp_sel, Caln_sel, E_fit, Rate, Gain, Dhpf, Dlpf);
    max30001.max30001_synch();
    max30001.onDataAvailable(&StreamPacketUint32_ex);
    int a;
    
    while (1) {
        if (a == 65) {
            max30001.max30001_Stop_ECG();
            index = 0;
        }
       // if (a == 97)
        // Read ECG
        max30001.max30001_ECG_InitStart(En_ecg, Openp, Openn, Pol, Calp_sel, Caln_sel, E_fit, Rate, Gain, Dhpf, Dlpf);
       
        //Read Temperature
        //MAX30205_top.readTemperature(&rawTemp_top);
        //MAX30205_bottom.readTemperature(&rawTemp_bottom);

        // Convert to Celsius
        //celsius_top = MAX30205_top.toCelsius(rawTemp_top);
        //celsius_bottom = MAX30205_bottom.toCelsius(rawTemp_bottom);
        // Convert to Fahrenheit
        //fahrenheit_top = MAX30205_top.toFahrenheit(celsius_top);
        //fahrenheit_bottom = MAX30205_bottom.toFahrenheit(celsius_bottom);
         
        // Read BMP280 Temp. and Pressure
        //bmp280_pres.ReadCompDataRaw(&bmp280_rawData);
        //bmp280_pres.ToFloat(&bmp280_rawData, &Temp_degC, &Press_Pa);    
        //Press_Bar = Press_Pa / 100000; 

        /* Printing various sensor values */
        //usbSerial.printf("***** MAX30205 Temperature Sensor Reading *****\n\r");
        //usbSerial.printf("Top Temperature: %.2f\370 C\n\rBottom Temperature: %.2f\370 C \n\rTop Temperature in Farenheit: %.2f\370 F\n\rBottom Temperature in Farenheit %.2f\370 F\n\n\r", celsius_top, celsius_bottom, fahrenheit_top, fahrenheit_bottom);
        
        //usbSerial.printf("***** BMP280 Barometric Sensor Reading *****\n\r");
        //usbSerial.printf("Temp_degC : %.2f , Press_Bar is %.2f , Press_pa is %.2f\n\n\r",Temp_degC, Press_Bar, Press_Pa);

        lis2dh.get_motion_cached(&accel_value[0], &accel_value[1], &accel_value[2]);

        usbSerial.printf("***** LIS2DH Acclerometer Sensor Reading *****\n\r");
        usbSerial.printf("X-Axis: %d\n\rY-Axis: %d\n\rZ-Axis: %d", &accel_value[0], &accel_value[1], &accel_value[2]);
        usbSerial.printf("-------------------------------------------------\n\n\n\r");

        wait(1.5);
    }     
}