IDT Optical Sensors / Mbed 2 deprecated OB1203_IDT

Dependencies:   mbed OB1203_example_driver

main.cpp

Committer:
laserdad
Date:
2018-04-25
Revision:
0:e9ae48b7f6f6
Child:
1:fb6b2b4d5d8a

File content as of revision 0:e9ae48b7f6f6:

#include "mbed.h"
#include "OB1203.h"
#include "math.h"


#define intb_pin D3

I2C i2c(I2C_SDA,I2C_SCL);
InterruptIn intb(intb_pin);
DigitalOut sda_pullup(D11,1);
DigitalOut scl_pullup(D12,1);
DigitalOut intb_pullup(D13,1);

OB1203 ob1203(&i2c);
Serial pc(USBTX, USBRX,115200);
Timer t;

bool mode = 0; //0 for PS_LS, 1 for SPO2
bool intFlagged =0;

void defaultConfig()
{
    ob1203.ls_res = LS_RATE(2); //100ms
    ob1203.ls_res = LS_RES(2); //18bit 100ms
    ob1203.ls_gain = LS_GAIN(1); //gain 3 default
    ob1203.ls_thres_hi = 0x0FFFFF;
    ob1203.ls_thres_lo = 0;
    ob1203.ls_sai = LS_SAI_OFF;
    ob1203.ls_mode = RGB_MODE;
    ob1203.ls_en = LS_ON;
    
    ob1203.ps_sai_en = PS_SAI_OFF;
    ob1203.ppg_ps_mode = PS_MODE;
    ob1203.ps_pulses = PS_PULSES(8);
    ob1203.ps_pwidth = PS_PWIDTH(1);
    ob1203.ps_rate = PS_RATE(5);
    ob1203.ps_avg_en = PS_AVG_OFF;
    ob1203.ps_can_ana = PS_CAN_ANA_0;
    ob1203.ps_digital_can = 0;
    ob1203.ps_hys_level = 0;
    ob1203.ps_current = 0x3FF;
    ob1203.ps_thres_hi = 0xFF;
    ob1203.ps_thres_lo = 0x00;
    
    //interrupts
    ob1203.ls_int_sel = LS_INT_SEL_W;
    ob1203.ls_var_mode = LS_THRES_INT_MODE;
    ob1203.ls_int_en = LS_INT_ON;
    ob1203.ppg_ps_en = PPG_PS_ON;
    ob1203.afull_int_en = AFULL_INT_ON;
    ob1203.ppg_int_en = PPG_INT_OFF;
    ob1203.ps_logic_mode = PS_INT_READ_CLEARS;
    ob1203.ps_int_en = PS_INT_ON;
    ob1203.ls_persist = LS_PERSIST(2);
    ob1203.ps_persist = PS_PERSIST(2);

    //PPG
    ob1203.ir_current = 0x1FF;
    ob1203.r_current = 0x1FF;
    ob1203.ppg_ps_gain = PPG_PS_GAIN_2;
    ob1203.ppg_pow_save = PPG_POW_SAVE_OFF;
    ob1203.led_flip = LED_FLIP_OFF;
    ob1203.ch1_can_ana = PPG_CH1_CAN(0);
    ob1203.ch2_can_ana = PPG_CH2_CAN(0);
    ob1203.ppg_avg = PPG_AVG(0);
    ob1203.ppg_pwidth = PPG_PWIDTH(4);
    ob1203.ppg_freq = PPG_FREQ_60HZ;
    ob1203.ppg_rate = PPG_RATE(2);
    ob1203.fifo_rollover_en = FIFO_ROLL_ON;
    ob1203.fifo_afull_samples_left = AFULL_SAMPLES_LEFT(8);

    if(mode)
    {
        ob1203.init_spo2();
    }
    else    
    {
        ob1203.init_ps_rgb();
    }
}


void intEvent(void)
{
    intFlagged = 1;
}

int main() 
{
    i2c.frequency( 100000 );
    char valid;
    uint32_t ps_ls_data[6];
    char samples2Read = 4;
    char fifoBuffer[samples2Read];
    uint32_t ppgData[samples2Read];  

      
    defaultConfig(); //rgb ps
        
    intb.fall(&intEvent);
    
    while(1)
    if(intFlagged)
    {
        if( mode )
        {
            ob1203.getFifoSamples(samples2Read,fifoBuffer);
            ob1203.parseFifoSamples(samples2Read,fifoBuffer,ppgData);
            for (int n=0;n<samples2Read/2;n++)
            {
                pc.printf("%d %d/r/n",ppgData[2*n],ppgData[2*n+1]);
            }
            intFlagged = 0;   
        }
        else
        {
            valid = ob1203.get_ps_ls_data(ps_ls_data);
            pc.printf("%d %d %d %d %d %d/r/n",ps_ls_data[0],ps_ls_data[1],ps_ls_data[2],ps_ls_data[3],ps_ls_data[4],ps_ls_data[5]);
        }
        intFlagged =0;
    }
}