Ahmad Alkaff / Mbed 2 deprecated CS3237

Dependencies:   mbed

main.cpp

Committer:
socrj
Date:
2019-10-31
Revision:
7:188bf08cfb44
Parent:
3:a7396ed925e6
Child:
12:2ad10c36f820

File content as of revision 7:188bf08cfb44:

#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"
//#include "unistd.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       0x00
#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("%s ECG", datetime.datetime.utcnow().strftime("%H:%M:%S"));
            usbSerial.printf("ECG\r\n");
            usbSerial.printf("%d\r\n", ECG_Raw);
            //usbSerial.printf("k is %d and number is %d\r\n", k, number);
            index++; 
        }
    }
}

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);
    
    // 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

    lis2dh.initStart(LIS2DH_DATARATE_50HZ, LIS2DH_FIFO_SIZE);

    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);

    while (1){
        // MAX30001 initialize interrupt
        // ECG READING     

        // LIS2DH initialize interrupt  
        //lis2dh.init();
        //lis2dh_Interrupt.fall(&LIS2DHIntHandler);
        //lis2dh_Interrupt.mode(PullUp);

        int a;
        lis2dh.get_motion_fifo(&accel_value[0], &accel_value[1], &accel_value[2]);
        usbSerial.printf("ACC\r\n");

        usbSerial.printf("X %d\r\nY %d\r\nZ %d\r\n", accel_value[0], accel_value[1], accel_value[2]);

        /*
        int child_pid = fork();
        int status;
        

        if (child_pid == 0) {
            while (1){
                a = usbSerial._getc();
                if (a==97){
                    max30001.max30001_Stop_ECG();
                    lis2dh.stop();
                    return 0;
                }
            }
        }
        //LOOP-FOREVER READ ACCELEROTMETER
        else {
            pid_t result = waitpid(child_pid, &status, WNOHANG);
            if (result == 0) {
                while (1) {
        */

                    /*
                    wait(5);
                    a = usbSerial._getc();
                    usbSerial.printf("WAIT 5 LIAO\r\n");
                    
                    if (a == 98) {
                        max30001.max30001_Stop_ECG();
                        index = 0;
                    }
                    */
        /*
                    lis2dh.int_handler();

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

                    //usbSerial.printf("***** LIS2DH Acclerometer Sensor Reading *****\r\n");
                    usbSerial.printf("X %d\r\nY %d\r\nZ %d\r\n", accel_value[0], accel_value[1], accel_value[2]);
                    //usbSerial.printf("-------------------------------------------------\r\n");
                    wait(1);
                }
            } else {
                while(1){
                    usbSerial.printf("Press c to continue\r\n");
                    a = usbSerial._getc();
                    if (a==99){
                        break;
                    }
                }
            }
        }
        */  
    } 
}