Senso_3

main.cpp

Committer:
pmic
Date:
2021-03-13
Revision:
6:6c1c38d4faa4
Parent:
5:887081decd5c
Child:
7:dc463bf54be6

File content as of revision 6:6c1c38d4faa4:

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

using namespace std::chrono;

InterruptIn  user_button(USER_BUTTON);
DigitalOut   led(LED1);
BufferedSerial pc(USBTX, USBRX);

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

void         button_fall();
void         button_rise();

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

float        floatNumber = 0.003713f;
int          integerNumber = 777;

int main()
{
    user_button.fall(&button_fall);
    user_button.rise(&button_rise);
    loop_timer.start();

    while (true) {

        loop_timer.reset();

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

        if(executeMainTask) {

            dist = analogIn.read()*3.3f;
            
            // printf("Measval: %d\r\n", (static_cast<int>(dist * 1000)));

            string msg_str = to_string((static_cast<int>(dist * 1e6)));
            msg_str.append(";");
            msg_str.append(to_string((static_cast<int>(floatNumber * 1e6))));
            msg_str.append(";");
            msg_str.append(to_string(integerNumber));
            msg_str.append(";\r\n");
            char msg[msg_str.length() + 1];
            strcpy(msg, msg_str.c_str());
            pc.write(msg, sizeof(msg));

            /* visual feedback that the main task is executed */
            led = !led;

        } else {
            led = 0;
        }

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

        int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count();
        int dT_loop_ms = Ts_ms - T_loop_ms;
        printf("T_loop_ms: %d\r\n", dT_loop_ms);
        thread_sleep_for(500);
    }
}

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