altb_pmic / Mbed 2 deprecated Grove-Barometric_Pressure_Sensor_Example

Dependencies:   HP206C mbed

main.cpp

Committer:
pmic
Date:
2019-06-25
Revision:
6:898a83da0661
Parent:
4:cdf9641ba817
Child:
7:3f4048c1cc81

File content as of revision 6:898a83da0661:

#include "mbed.h"
#include "HP206C.h"
#include "IIR_filter.h"

/*  Notes pmic 25.06.2019:
  - 
*/

HP206C barometer(D14, D15); // 20 Hz parametrization
float altitude = 0.0f;

Serial pc(SERIAL_TX, SERIAL_RX);  // serial connection via USB - programmer
InterruptIn Button(USER_BUTTON);  // User Button
Ticker  LoopTimer;                // interrupt for control loop
Timer t;                          // timer to analyse Button

int   k;
bool  doRun = false;
float Ts = 0.05f;                 // sample time of main loop, 20 Hz

IIR_filter pt1(0.2f, Ts, 1.0f);
float altitudef = 0.0f;

// user defined functions
void updateLoop(void);   // loop (via interrupt)
void pressed(void);      // user Button pressed
void released(void);     // user Button released

// main program and control loop
// -----------------------------------------------------------------------------
int main()
{
    pc.baud(2000000);                  // for serial comm.
    LoopTimer.attach(&updateLoop, Ts); // attach loop to timer interrupt
    Button.fall(&pressed);             // attach key pressed function
    Button.rise(&released);            // attach key pressed function
    k = 0;
    pt1.reset(0.0f);
    barometer.reset();
    wait_us(100);
}

// the updateLoop starts as soon as you pressed the blue botton
void updateLoop(void)
{
    altitude = barometer();
    altitudef = pt1(altitude);
    // pc.printf("%10i %10.6e\r\n", k, altitude);
    if(doRun) {
        /*
        // use this section to do dynamical measurements
        if(doRun && k++ < 4000) {
            pc.printf("%10i %10.9e\r\n", k, altitude);
        }
        */
        ///*
        // use this section to do static measurements
        if(doRun && k++%25 == 0) {
            pc.printf("%10i %10.6e\r\n", k, altitudef);
        }
        //*/
    }
}

// buttonhandling
// -----------------------------------------------------------------------------
// start timer as soon as button is pressed
void pressed()
{
    t.start();
}

// evaluating statemachine
void released()
{
    // toggle state over boolean
    if(doRun) {
        k = 0;
        pt1.reset(0.0f);
    }
    doRun = !doRun;
    t.stop();
    t.reset();
}