rongyu lou / Mbed OS CarPakingSystem_V13

Dependencies:   HCSR04new SSH1106-alan

main.cpp

Committer:
alanchip
Date:
2020-09-27
Revision:
0:69272b5e3faf
Child:
2:1baa0bd2fde0

File content as of revision 0:69272b5e3faf:

/* mbed Microcontroller Library
 * Copyright (c) 2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

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


// Blinking rate in milliseconds
#define BLINKING_RATE_MS          1000

//Usensor time params
struct Usensor
{
  int dist;
  int time_in_min;
  int time_in_sec;
  int time_out_min;
  int time_out_sec;
};

Serial pc(USBTX, USBRX); //uart init
DigitalOut led(LED1);    //LED init

// RTC par
struct tm st_tmp;    
struct tm *p_tm;
time_t seconds;  

    
//Usensor struct
Usensor Usensor0;
Usensor Usensor1;

/* 
HCSR04_0
    PING: 
        Trig :D5
        Echo :D4
*/
DigitalOut trig0(D5);
DigitalIn  echo0(D4);
Timer t ;
HCSR04 capteur0(&trig0, &echo0, &t); 
/* 
HCSR04_1
    PING: 
        Trig :D2
        Echo :D3
*/
DigitalOut trig1(D2);
DigitalIn  echo1(D3);
Timer t1 ;
HCSR04 capteur1(&trig1, &echo1, &t1);


Thread init_start_thread;
Thread sensor_thread0;
Thread sensor_thread1;
Thread RTC_thread;
Thread sensor_capture_thread;






void RTC_INIT()
{
     st_tmp.tm_year = 2020-1900;
     st_tmp.tm_mon  = 9;
     st_tmp.tm_mday = 27;
     st_tmp.tm_hour = 1;
     st_tmp.tm_min  = 53;
     st_tmp.tm_sec  = 30;
     
     seconds  = mktime(&st_tmp);
     set_time(seconds);  
}

void RTC_display ()//display time  printf
{
    seconds = time(NULL);
    p_tm = localtime(&seconds);
    //Display format year-month-day hour:min:sec   
    pc.printf("%04d-%02d-%04d  %02d:%02d:%02d \n\n", p_tm->tm_year + 1900,p_tm->tm_mon,p_tm->tm_mday, p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec);
}

void dist0_thread(void)
{
    while(true){
        //pc.printf("dist0 = %d \n\n", capteur0.measDist());
        Usensor0.dist = capteur0.measDist();
        thread_sleep_for(10);
        }
}

void dist1_thread(void)
{
    while(true){
        //pc.printf("dist1 = %d \n\n", capteur1.measDist()); 
        Usensor1.dist = capteur1.measDist();
        thread_sleep_for(10);
        }
}

void capture_sensor()
{    
    while(true){  
        //pc.printf("capture_sensor0 is working \n\n"); 
        if(Usensor0.dist < 10)
        {
            Usensor0.time_in_min =  p_tm->tm_min;
            Usensor0.time_in_sec =  p_tm->tm_sec;
            pc.printf("iiiiiiiiiiin-min-0 = %d \n\n", Usensor0.time_in_min);
            pc.printf("iiiiiiiiiiin-sec-0 = %d \n\n", Usensor0.time_in_sec);
            
            //pc.printf("dist0-------------------------- = %d \n\n", Usensor0.dist);
            
            while(Usensor0.dist < 10)
            {
                thread_sleep_for(1);  
            }   
            
            Usensor0.time_out_min =  p_tm->tm_min;
            Usensor0.time_out_sec =  p_tm->tm_sec;
            pc.printf("oooooooooout-min-0 = %d \n\n", Usensor0.time_out_min);
            pc.printf("oooooooooout-sec-0 = %d \n\n", Usensor0.time_out_sec);
         } 
         
          if(Usensor1.dist < 10)
           {
            Usensor1.time_in_min =  p_tm->tm_min;
            Usensor1.time_in_sec =  p_tm->tm_sec;
            pc.printf("iiiiiiiiiiin-min-11111 = %d \n\n", Usensor1.time_in_min);
            pc.printf("iiiiiiiiiiin-sec-11111 = %d \n\n", Usensor1.time_in_sec);
            
            //pc.printf("dist1-------------------------- = %d \n\n", Usensor1.dist);
            
            while(Usensor1.dist < 10)
            {
                thread_sleep_for(1);  
            }   
            
            Usensor1.time_out_min =  p_tm->tm_min;
            Usensor1.time_out_sec =  p_tm->tm_sec;
            pc.printf("oooooooooout-min-11111111 = %d \n\n", Usensor1.time_out_min);
            pc.printf("oooooooooout-sec-11111111 = %d \n\n", Usensor1.time_out_sec);
         }  
         thread_sleep_for(10);  
    }   
       
}


void RTC_display_thread(void)
{
    while(true){  
        RTC_display();
        thread_sleep_for(1000);
        }
}

void init_thread(void)
{
        while(true){
        RTC_INIT();
        init_start_thread.terminate();// thread gang up
        }
}


int main()
{   
    //init    
    init_start_thread.start(init_thread);
    
    //thread0 for Usensor0     
    sensor_thread0.start(dist0_thread);
    
    sensor_capture_thread.start(capture_sensor);
    
    //thread1 for Usensor1  
    sensor_thread1.start(dist1_thread);
   
    //thread for RTC  
    RTC_thread.start(RTC_display_thread);

    
    while (true) 
    {         
        led = !led;  
        thread_sleep_for(BLINKING_RATE_MS);
    }
}