Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE HC_SR04_Ultrasonic_Library
main.cpp
- Committer:
- rmalik8
- Date:
- 2020-04-24
- Revision:
- 11:f5d0c8bf1849
- Parent:
- 10:7a939a54515e
- Child:
- 12:bebfef8d229f
File content as of revision 11:f5d0c8bf1849:
#include "mbed.h"
#include "rtos.h"
#include "SDFileSystem.h"
#include "uLCD_4DGL.h"
#include "ultrasonic.h"
#include "wave_player.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);
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;
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
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 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()
{
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(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);
}
}