Workshop 1

Dependencies:   PM2_Libary

main.cpp

Committer:
pmic
Date:
2021-03-13
Revision:
3:aa1d854807fe
Parent:
2:4ba1937ce284
Child:
4:dcdcb25d1069

File content as of revision 3:aa1d854807fe:

#include "mbed.h"
#include "platform/mbed_thread.h"

#define   pi 3.14159265358979323846 

using namespace std::chrono;

InterruptIn  user_button(USER_BUTTON);
DigitalOut   led(LED1);
// Serial       pc(SERIAL_TX, SERIAL_RX);

bool         executeMainTask = false;
Timer        user_button_timer, loop_timer;
int          Ts_ms = 50;

void         button_fall(); // stuff
void         button_rise(); // stuff

AnalogIn     analogIn(PA_0);
float dist = 0.0f;

int main()
{
    // pc.baud(115200);
    user_button.fall(&button_fall);
    user_button.rise(&button_rise);
    loop_timer.reset();

    while (true) {

        /* ------------- start hacking ------------- -------------*/

        if(executeMainTask) {
            dist = analogIn.read()*3.3f;
            // printf("measurement: %9.6f\r\n", dist);
        } else {

        }

        /* ------------- stop hacking ------------- -------------*/

        if(executeMainTask) {
            led = !led;
        }
        int dT_loop = Ts_ms - duration_cast<milliseconds>(loop_timer.elapsed_time()).count();
        thread_sleep_for(dT_loop);
    }
}

void button_fall()
{
    user_button_timer.reset();
    user_button_timer.start();
}

void button_rise()
{
    int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count();
    user_button_timer.stop();
    if(t_button > 200) executeMainTask = !executeMainTask;
}