Hauptprogramm

Dependencies:   ILI9340_Driver_Lib PM2_Libary Lib_DFPlayerMini

main.cpp

Committer:
ackerden
Date:
2021-04-14
Revision:
12:dfa3591affef
Parent:
11:be62f37f3a98
Child:
13:096e5dc3ac23

File content as of revision 12:dfa3591affef:

/*Zeitinput am Anfang: Startzeit 8:00*/

#include <I2C.h>
#include "mbed.h"
#include "platform/mbed_thread.h"

#include "mbed_rtc_time.h"

/* PM2_Libary */
#include "EncoderCounter.h"
#include "Servo.h"
#include "SpeedController.h"
#include "FastPWM.h"

using namespace std::chrono;

InterruptIn user_button(USER_BUTTON);
DigitalOut  led(LED1);


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

/* declaration of custom button functions */
void button_fall();
void button_rise();


/* create analog input object */
/*AnalogIn analogIn(PC_2);
float    dist = 0.0f;
*/
/* create servo objects */
/*Servo servo_S1(PB_2);
Servo servo_S2(PC_8);
// Servo servo_S3(PC_6); // not needed in this example
int servoPeriod_mus = 20000;
int servoOutput_mus_S1 = 0;
int servoOutput_mus_S2 = 0;
int servo_counter = 0;*/
int loops_per_second = static_cast<int>(ceilf(1.0f/(0.001f*(float)Ts_ms)));

int main()
{
    set_time(1618332129);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
    user_button.fall(&button_fall);
    user_button.rise(&button_rise);
    loop_timer.start();


    /* enable servos, you can also disable them */
    //servo_S1.Enable(servoOutput_mus_S1, servoPeriod_mus);
    //servo_S2.Enable(servoOutput_mus_S2, servoPeriod_mus);

    while (true) {

        loop_timer.reset();
        
        

 

        if (executeMainTask) {
            time_t seconds = time(NULL);

        printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)seconds);

        printf("Time as a basic string = %s", ctime(&seconds));

        char buffer[32];
        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
        printf("Time as a custom formatted string = %s", buffer);

        ThisThread::sleep_for(50);

            /* read analog input */
            //dist = analogIn.read() * 3.3f;

            /* command servo position via output time, this needs to be calibrated */
            /*servo_S1.SetPosition(servoOutput_mus_S1);
            servo_S2.SetPosition(servoOutput_mus_S2);
            if (servoOutput_mus_S1 <= servoPeriod_mus & servo_counter%loops_per_second == 0 & servo_counter != 0) {
                servoOutput_mus_S1 += 100;
            }
            if (servoOutput_mus_S2 <= servoPeriod_mus & servo_counter%loops_per_second == 0 & servo_counter != 0) {
                servoOutput_mus_S2 += 100;
            }
            servo_counter++;*/

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

        } else {

            //dist = 0.0f;

            //servoOutput_mus_S1 = 0;
            //servoOutput_mus_S2 = 0;
            //servo_S1.SetPosition(servoOutput_mus_S1);
            //servo_S2.SetPosition(servoOutput_mus_S2);

            led = 0;
        }

        /* do only output via serial what's really necessary (this makes your code slow)*/
        //printf("%3.3f, %3d;\r\n",
               //dist,
               //servoOutput_mus_S1,
               //servoOutput_mus_S2);
            


        int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count();
        int dT_loop_ms = Ts_ms - T_loop_ms;
        thread_sleep_for(dT_loop_ms);
    }
}

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

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