People Counter / Mbed 2 deprecated person_counter

Dependencies:   mbed mbed-rtos 4DGL-uLCD-SE HC_SR04_Ultrasonic_Library

main.cpp

Committer:
mmarine3
Date:
2020-04-22
Revision:
8:c0a6a3363e43
Parent:
7:85d42006e380
Child:
9:f4f03767acc0

File content as of revision 8:c0a6a3363e43:

#include "mbed.h"
#include "rtos.h"
#include "SDFileSystem.h"
#include "uLCD_4DGL.h"
#include "XNucleo53L0A1.h"
#include "ultrasonic.h"
#include "wave_player.h"

//#include <stdio.h>
#include <string>

//I2C sensor pins
#define VL53L0_I2C_SDA   p28
#define VL53L0_I2C_SCL   p27

uLCD_4DGL uLCD(p13, p14, p12); // serial tx, serial rx, reset pin;
SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
DigitalOut shdn(p26);
RawSerial BT(p9,p10);
//Serial BT(p9,p10);
AnalogOut speaker(p18); //NEW
wave_player waver(&speaker);//NEW
Mutex mutex;
Mutex dist;
DigitalOut MyLED(LED1);

//needed for TOF
static XNucleo53L0A1 *board=NULL;

/*void sonar(int distance)
{
    //put code here to execute when the sonar distance has changed
    
}*/

//ultrasonic mu(p30, p29, .1, 1, &sonar);    //Set the trigger pin to p30 and the echo pin to p29
                                        //have updates every .1 seconds and a timeout after 1
                                        //second, and call dist when the distance changes
uint32_t far;   //Global variable to hold lidar TOF distance
void TOF(void const *arguments) {
    //initialize the TOF
    int status;
    uint32_t distance;
    DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
    /* creates the 53L0A1 expansion board singleton obj */
    board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2);
    shdn = 0; //must reset sensor for an mbed reset to work
    wait(0.1);
    shdn = 1;
    wait(0.1);
    /* init the 53L0A1 board with default values */
    status = board->init_board();
    while (status) {
        //pc.printf("Failed to init board! \r\n");
        status = board->init_board();
    }
    
    
    
    
    //Loop that reads in the TOF value 100 times per second
    while(1){
    status = board->sensor_centre->get_distance(&distance); //How to get distance value
    dist.lock();
    far = distance;
    dist.unlock();
    Thread::wait(10);
    }
    /*
    How you would print TOF to pc serial
    if (status == VL53L0X_ERROR_NONE) {
        pc.printf("D=%ld mm\r\n", distance);
    }
        
    */
}

//Thread to print the TOF and Sonar Values to the LCD
void LCD(void const *arguments)
{
    Thread::wait(1000); //Wait for lidar and sonar setup
    while(1)
    {
        uLCD.cls();
        dist.lock();
        uLCD.printf("TOF Distance %i \n", far);     
        dist.unlock();
        Thread::wait(1000); //Allow time to read value before reprint
    }
}

int main()
{
    uLCD.cls();
    uLCD.baudrate(BAUD_3000000);
    //wait(1.0);

    //blu.attach(&parse_message,Serial::RxIrq);
        //Was used in lab 3 to interupt if reading in a blutooth command
    //Thread t#(name_of_thread_function);
    Thread t1(LCD);//Initialize LCD thread
    Thread t2(TOF);//Initialize TOF thread
    
    /* //Code to read and play a file
    FILE *wave_file;
    //printf("Hello World");
    Thread::wait(1000);
    wave_file=fopen("/sd/test.wav","r");
    //serial_mutex.lock();
    if(wave_file==NULL) printf("file open error!\n\n\r");
    //serial_mutex.unlock();
    waver.play(wave_file);
    fclose(wave_file);
    */
    
    //Loop to validate the main loop is executing
    while(1)
    {
        MyLED = !MyLED;
        Thread::wait(100);
    }
}