People Counter / Mbed 2 deprecated person_counter

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

main.cpp

Committer:
rmalik8
Date:
2020-04-24
Revision:
12:bebfef8d229f
Parent:
11:f5d0c8bf1849
Child:
13:3ba367bc16bc

File content as of revision 12:bebfef8d229f:

#include "mbed.h"
#include "rtos.h"
//#include "SDFileSystem.h"
#include "uLCD_4DGL.h"
#include "ultrasonic.h"
//#include "wave_player.h"
#include "SongPlayer.h"

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


uLCD_4DGL uLCD(p13, p14, p12); // Initialize uLCD serial tx, serial rx, reset pin;
SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
RawSerial blue(p9,p10);           //Initialize Blutooth
//Serial BT(p9,p10);

//waiting to figure out if sd card works
//AnalogOut speaker(p18);         //Initialize speaker
//wave_player waver(&speaker);    //Initialize Waveplayer

Mutex serial_mutex;
Mutex dist;
DigitalOut MyLED(LED1);

volatile int musicChoice = 0;
//musicChoice can be values 0 to 3
volatile int sdist = 0;
volatile int counter = 0;

float note1[2]= {1568.0, 0.0
                };
float note2[2]= {1568.0, 0.0
                };
float note3[2]= {1568.0, 0.0
                };
float duration[2]= {1, 0.0
                   };


void sonar(int distance)
{
    //put code here to execute when the sonar distance has changed
    dist.lock();
    sdist = distance;
    dist.unlock();
    //printf("Sonar Distance:\r\n %d", sdist);
}

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 sonar when the distance changes

void Sonar(void const* arguments)
{
    mu.startUpdates();//start measuring the distance
    while(1) {
        //Do something else here
        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
        //the class checks if dist needs to be called.
        Thread::wait(10);
    }
}

//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
    serial_mutex.lock();
    uLCD.cls();
    uLCD.baudrate(BAUD_3000000);
    serial_mutex.unlock();
    while(1) {
        serial_mutex.lock();
        uLCD.cls();
        dist.lock();
        uLCD.printf("Sonar Distance:\n %d", sdist);
        dist.unlock();
        serial_mutex.unlock();
        Thread::wait(1000); //Allow time to read value before reprint

    }
}

void sounds(void const *arguments)
{
    int last_counter = 0;
    // setup instance of new SongPlayer class, mySpeaker using pin 26
// the pin must be a PWM output pin
    SongPlayer mySpeaker(p26);
// Start song and return once playing starts
    //mySpeaker.PlaySong(note1,duration);
    while(1) {
        if (counter > last_counter) {
            switch (musicChoice) {
                case '0':
                    //do nothing
                    break;
                case '1':
                    mySpeaker.PlaySong(note1,duration);
                    break;
                case '2':
                    mySpeaker.PlaySong(note2,duration);
                    break;
                case '3':
                    mySpeaker.PlaySong(note3,duration);
                    break;
                default:
                    break;
            }
        }
        last_counter = counter;
        Thread::wait(100);
    }
}

void bluetooth(void const *arguments)
{
    char bnum=0;
    char bhit=0;
    while(1) {
        serial_mutex.lock();
        if(!blue.readable()) {
            Thread::yield();
        } else {
            if (blue.getc()=='!') {
                if (blue.getc()=='B') { //button data packet
                    bnum = blue.getc(); //button number
                    bhit = blue.getc(); //1=hit, 0=release
                    if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?

                        switch (bnum) {
                            case '1': //number button 1
                                if (bhit=='1') {
                                    musicChoice = 0;
                                } else {
                                    //add release code here
                                }
                                break;
                            case '2': //number button 2
                                if (bhit=='1') {
                                    musicChoice = 1;
                                } else {
                                    //add release code here
                                }
                                break;
                            case '3': //number button 3
                                if (bhit=='1') {
                                    musicChoice = 2;
                                } else {
                                    //add release code here
                                }
                                break;
                            case '4': //number button 4
                                if (bhit=='1') {
                                    //add hit code here
                                } else {
                                    //add release code here
                                }
                                break;
                            case '5': //button 5 up arrow
                                if (bhit=='1') {
                                    musicChoice = 3;
                                } else {
                                    //add release code here
                                }
                                break;
                            case '6': //button 6 down arrow
                                if (bhit=='1') {
                                    //add hit code here
                                } else {
                                    //add release code here
                                }
                                break;
                            case '7': //button 7 left arrow
                                if (bhit=='1') {
                                    //add hit code here
                                } else {
                                    //add release code here
                                }
                                break;
                            case '8': //button 8 right arrow
                                if (bhit=='1') {
                                    //add hit code here
                                } else {
                                    //add release code here
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
        serial_mutex.unlock();
    }
}

int main()
{

    //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(Sonar);//Initialize Sonar 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);
    }
}