Workshop 1

Dependencies:   PM2_Libary

main.cpp

Committer:
pmic
Date:
2021-03-13
Revision:
4:dcdcb25d1069
Parent:
3:aa1d854807fe
Child:
5:887081decd5c

File content as of revision 4:dcdcb25d1069:

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

#include "BufferedSerial.h"

#define   pi 3.14159265358979323846 

using namespace std::chrono;

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

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

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: %d\r\n", (static_cast<int>(dist * 1000)));

            Timer s;
            
            s.start();
            char msg[] = "Hello World - buffered\n";
            pc.write(msg, sizeof(msg));
            int buffered_time = duration_cast<milliseconds>(s.elapsed_time()).count();
            thread_sleep_for(100);; // give time for the buffer to empty
            
            s.reset();
            printf("Hello World - blocking\n");
            int polled_time = duration_cast<milliseconds>(s.elapsed_time()).count();
            s.stop();
            thread_sleep_for(100);; // give time for the buffer to empty
            
            printf("printf buffered took %d us\n", buffered_time);
            printf("printf blocking took %d us\n", polled_time);
            thread_sleep_for(100);; // give time for the buffer to empty


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