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@10:7a939a54515e, 2020-04-24 (annotated)
- Committer:
- rmalik8
- Date:
- Fri Apr 24 03:00:36 2020 +0000
- Revision:
- 10:7a939a54515e
- Parent:
- 9:f4f03767acc0
- Child:
- 11:f5d0c8bf1849
added bluetooth thread
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mmarine3 | 2:1ec843c95914 | 1 | #include "mbed.h" |
rmalik8 | 4:7d02b3f3bee6 | 2 | #include "rtos.h" |
mmarine3 | 2:1ec843c95914 | 3 | #include "SDFileSystem.h" |
mmarine3 | 2:1ec843c95914 | 4 | #include "uLCD_4DGL.h" |
rmalik8 | 4:7d02b3f3bee6 | 5 | #include "ultrasonic.h" |
mmarine3 | 8:c0a6a3363e43 | 6 | #include "wave_player.h" |
rmalik8 | 4:7d02b3f3bee6 | 7 | |
rmalik8 | 3:b5cdd40e99e9 | 8 | //#include <stdio.h> |
mmarine3 | 2:1ec843c95914 | 9 | #include <string> |
mmarine3 | 2:1ec843c95914 | 10 | |
rmalik8 | 4:7d02b3f3bee6 | 11 | |
mmarine3 | 9:f4f03767acc0 | 12 | uLCD_4DGL uLCD(p13, p14, p12); // Initialize uLCD serial tx, serial rx, reset pin; |
rmalik8 | 4:7d02b3f3bee6 | 13 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
rmalik8 | 10:7a939a54515e | 14 | RawSerial blue(p9,p10); //Initialize Blutooth |
mmarine3 | 9:f4f03767acc0 | 15 | //Serial BT(p9,p10); |
mmarine3 | 9:f4f03767acc0 | 16 | AnalogOut speaker(p18); //Initialize speaker |
mmarine3 | 9:f4f03767acc0 | 17 | wave_player waver(&speaker); //Initialize Waveplayer |
rmalik8 | 10:7a939a54515e | 18 | Mutex serial_mutex; |
mmarine3 | 8:c0a6a3363e43 | 19 | Mutex dist; |
mmarine3 | 2:1ec843c95914 | 20 | DigitalOut MyLED(LED1); |
mmarine3 | 2:1ec843c95914 | 21 | |
rmalik8 | 10:7a939a54515e | 22 | volatile int musicChoice = 0; |
rmalik8 | 10:7a939a54515e | 23 | //musicChoice can be values 0 to 3 |
rmalik8 | 10:7a939a54515e | 24 | voltatile int sdist = 0; |
mmarine3 | 9:f4f03767acc0 | 25 | void sonar(int distance) |
rmalik8 | 4:7d02b3f3bee6 | 26 | { |
rmalik8 | 4:7d02b3f3bee6 | 27 | //put code here to execute when the sonar distance has changed |
mmarine3 | 9:f4f03767acc0 | 28 | dist.lock(); |
mmarine3 | 9:f4f03767acc0 | 29 | sdist = distance; |
mmarine3 | 9:f4f03767acc0 | 30 | dist.unlock(); |
mmarine3 | 9:f4f03767acc0 | 31 | //printf("Sonar Distance:\r\n %d", sdist); |
mmarine3 | 9:f4f03767acc0 | 32 | } |
rmalik8 | 4:7d02b3f3bee6 | 33 | |
mmarine3 | 9:f4f03767acc0 | 34 | ultrasonic mu(p30, p29, .1, 1, &sonar); //Set the trigger pin to p30 and the echo pin to p29 |
rmalik8 | 4:7d02b3f3bee6 | 35 | //have updates every .1 seconds and a timeout after 1 |
mmarine3 | 9:f4f03767acc0 | 36 | //second, and call sonar when the distance changes |
mmarine3 | 9:f4f03767acc0 | 37 | |
mmarine3 | 9:f4f03767acc0 | 38 | void Sonar(void const* arguments) |
mmarine3 | 9:f4f03767acc0 | 39 | { |
mmarine3 | 9:f4f03767acc0 | 40 | mu.startUpdates();//start measuring the distance |
mmarine3 | 9:f4f03767acc0 | 41 | while(1) |
mmarine3 | 9:f4f03767acc0 | 42 | { |
mmarine3 | 9:f4f03767acc0 | 43 | //Do something else here |
mmarine3 | 9:f4f03767acc0 | 44 | mu.checkDistance(); //call checkDistance() as much as possible, as this is where |
mmarine3 | 9:f4f03767acc0 | 45 | //the class checks if dist needs to be called. |
mmarine3 | 9:f4f03767acc0 | 46 | Thread::wait(10); |
rmalik8 | 4:7d02b3f3bee6 | 47 | } |
rmalik8 | 4:7d02b3f3bee6 | 48 | } |
mmarine3 | 2:1ec843c95914 | 49 | |
mmarine3 | 8:c0a6a3363e43 | 50 | //Thread to print the TOF and Sonar Values to the LCD |
mmarine3 | 8:c0a6a3363e43 | 51 | void LCD(void const *arguments) |
mmarine3 | 8:c0a6a3363e43 | 52 | { |
mmarine3 | 8:c0a6a3363e43 | 53 | Thread::wait(1000); //Wait for lidar and sonar setup |
mmarine3 | 8:c0a6a3363e43 | 54 | while(1) |
mmarine3 | 8:c0a6a3363e43 | 55 | { |
rmalik8 | 10:7a939a54515e | 56 | serial_mutex.lock(); |
mmarine3 | 8:c0a6a3363e43 | 57 | uLCD.cls(); |
mmarine3 | 8:c0a6a3363e43 | 58 | dist.lock(); |
mmarine3 | 9:f4f03767acc0 | 59 | uLCD.printf("Sonar Distance:\n %d", sdist); |
mmarine3 | 8:c0a6a3363e43 | 60 | dist.unlock(); |
rmalik8 | 10:7a939a54515e | 61 | serial_mutex.unlock(); |
mmarine3 | 8:c0a6a3363e43 | 62 | Thread::wait(1000); //Allow time to read value before reprint |
mmarine3 | 9:f4f03767acc0 | 63 | |
mmarine3 | 8:c0a6a3363e43 | 64 | } |
mmarine3 | 8:c0a6a3363e43 | 65 | } |
mmarine3 | 8:c0a6a3363e43 | 66 | |
rmalik8 | 10:7a939a54515e | 67 | void bluetooth(void const *arguments) |
rmalik8 | 10:7a939a54515e | 68 | { |
rmalik8 | 10:7a939a54515e | 69 | char bnum=0; |
rmalik8 | 10:7a939a54515e | 70 | char bhit=0; |
rmalik8 | 10:7a939a54515e | 71 | while(1) |
rmalik8 | 10:7a939a54515e | 72 | { |
rmalik8 | 10:7a939a54515e | 73 | serial_mutex.lock(); |
rmalik8 | 10:7a939a54515e | 74 | if(!blue.readable()){ |
rmalik8 | 10:7a939a54515e | 75 | Thread::yield(); |
rmalik8 | 10:7a939a54515e | 76 | } else { |
rmalik8 | 10:7a939a54515e | 77 | if (blue.getc()=='!') { |
rmalik8 | 10:7a939a54515e | 78 | if (blue.getc()=='B') { //button data packet |
rmalik8 | 10:7a939a54515e | 79 | bnum = blue.getc(); //button number |
rmalik8 | 10:7a939a54515e | 80 | bhit = blue.getc(); //1=hit, 0=release |
rmalik8 | 10:7a939a54515e | 81 | if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? |
rmalik8 | 10:7a939a54515e | 82 | |
rmalik8 | 10:7a939a54515e | 83 | switch (bnum) { |
rmalik8 | 10:7a939a54515e | 84 | case '1': //number button 1 |
rmalik8 | 10:7a939a54515e | 85 | if (bhit=='1') { |
rmalik8 | 10:7a939a54515e | 86 | musicChoice = 0; |
rmalik8 | 10:7a939a54515e | 87 | } else { |
rmalik8 | 10:7a939a54515e | 88 | //add release code here |
rmalik8 | 10:7a939a54515e | 89 | } |
rmalik8 | 10:7a939a54515e | 90 | break; |
rmalik8 | 10:7a939a54515e | 91 | case '2': //number button 2 |
rmalik8 | 10:7a939a54515e | 92 | if (bhit=='1') { |
rmalik8 | 10:7a939a54515e | 93 | musicChoice = 1; |
rmalik8 | 10:7a939a54515e | 94 | } else { |
rmalik8 | 10:7a939a54515e | 95 | //add release code here |
rmalik8 | 10:7a939a54515e | 96 | } |
rmalik8 | 10:7a939a54515e | 97 | break; |
rmalik8 | 10:7a939a54515e | 98 | case '3': //number button 3 |
rmalik8 | 10:7a939a54515e | 99 | if (bhit=='1') { |
rmalik8 | 10:7a939a54515e | 100 | musicChoice = 2; |
rmalik8 | 10:7a939a54515e | 101 | } else { |
rmalik8 | 10:7a939a54515e | 102 | //add release code here |
rmalik8 | 10:7a939a54515e | 103 | } |
rmalik8 | 10:7a939a54515e | 104 | break; |
rmalik8 | 10:7a939a54515e | 105 | case '4': //number button 4 |
rmalik8 | 10:7a939a54515e | 106 | if (bhit=='1') { |
rmalik8 | 10:7a939a54515e | 107 | //add hit code here |
rmalik8 | 10:7a939a54515e | 108 | } else { |
rmalik8 | 10:7a939a54515e | 109 | //add release code here |
rmalik8 | 10:7a939a54515e | 110 | } |
rmalik8 | 10:7a939a54515e | 111 | break; |
rmalik8 | 10:7a939a54515e | 112 | case '5': //button 5 up arrow |
rmalik8 | 10:7a939a54515e | 113 | if (bhit=='1') { |
rmalik8 | 10:7a939a54515e | 114 | musicChoice = 3; |
rmalik8 | 10:7a939a54515e | 115 | } else { |
rmalik8 | 10:7a939a54515e | 116 | //add release code here |
rmalik8 | 10:7a939a54515e | 117 | } |
rmalik8 | 10:7a939a54515e | 118 | break; |
rmalik8 | 10:7a939a54515e | 119 | case '6': //button 6 down arrow |
rmalik8 | 10:7a939a54515e | 120 | if (bhit=='1') { |
rmalik8 | 10:7a939a54515e | 121 | //add hit code here |
rmalik8 | 10:7a939a54515e | 122 | } else { |
rmalik8 | 10:7a939a54515e | 123 | //add release code here |
rmalik8 | 10:7a939a54515e | 124 | } |
rmalik8 | 10:7a939a54515e | 125 | break; |
rmalik8 | 10:7a939a54515e | 126 | case '7': //button 7 left arrow |
rmalik8 | 10:7a939a54515e | 127 | if (bhit=='1') { |
rmalik8 | 10:7a939a54515e | 128 | //add hit code here |
rmalik8 | 10:7a939a54515e | 129 | } else { |
rmalik8 | 10:7a939a54515e | 130 | //add release code here |
rmalik8 | 10:7a939a54515e | 131 | } |
rmalik8 | 10:7a939a54515e | 132 | break; |
rmalik8 | 10:7a939a54515e | 133 | case '8': //button 8 right arrow |
rmalik8 | 10:7a939a54515e | 134 | if (bhit=='1') { |
rmalik8 | 10:7a939a54515e | 135 | //add hit code here |
rmalik8 | 10:7a939a54515e | 136 | } else { |
rmalik8 | 10:7a939a54515e | 137 | //add release code here |
rmalik8 | 10:7a939a54515e | 138 | } |
rmalik8 | 10:7a939a54515e | 139 | break; |
rmalik8 | 10:7a939a54515e | 140 | default: |
rmalik8 | 10:7a939a54515e | 141 | break; |
rmalik8 | 10:7a939a54515e | 142 | } |
rmalik8 | 10:7a939a54515e | 143 | } |
rmalik8 | 10:7a939a54515e | 144 | } |
rmalik8 | 10:7a939a54515e | 145 | } |
rmalik8 | 10:7a939a54515e | 146 | } |
rmalik8 | 10:7a939a54515e | 147 | serial_mutex.unlock(); |
rmalik8 | 10:7a939a54515e | 148 | } |
rmalik8 | 10:7a939a54515e | 149 | } |
rmalik8 | 10:7a939a54515e | 150 | |
mmarine3 | 2:1ec843c95914 | 151 | int main() |
mmarine3 | 2:1ec843c95914 | 152 | { |
mmarine3 | 7:85d42006e380 | 153 | uLCD.cls(); |
mmarine3 | 7:85d42006e380 | 154 | uLCD.baudrate(BAUD_3000000); |
mmarine3 | 7:85d42006e380 | 155 | //wait(1.0); |
mmarine3 | 7:85d42006e380 | 156 | |
mmarine3 | 7:85d42006e380 | 157 | //blu.attach(&parse_message,Serial::RxIrq); |
mmarine3 | 8:c0a6a3363e43 | 158 | //Was used in lab 3 to interupt if reading in a blutooth command |
mmarine3 | 7:85d42006e380 | 159 | //Thread t#(name_of_thread_function); |
mmarine3 | 8:c0a6a3363e43 | 160 | Thread t1(LCD);//Initialize LCD thread |
mmarine3 | 9:f4f03767acc0 | 161 | Thread t2(Sonar);//Initialize Sonar thread |
rmalik8 | 4:7d02b3f3bee6 | 162 | |
mmarine3 | 9:f4f03767acc0 | 163 | |
mmarine3 | 8:c0a6a3363e43 | 164 | /* //Code to read and play a file |
mmarine3 | 7:85d42006e380 | 165 | FILE *wave_file; |
mmarine3 | 7:85d42006e380 | 166 | //printf("Hello World"); |
mmarine3 | 7:85d42006e380 | 167 | Thread::wait(1000); |
mmarine3 | 7:85d42006e380 | 168 | wave_file=fopen("/sd/test.wav","r"); |
mmarine3 | 7:85d42006e380 | 169 | //serial_mutex.lock(); |
mmarine3 | 7:85d42006e380 | 170 | if(wave_file==NULL) printf("file open error!\n\n\r"); |
mmarine3 | 7:85d42006e380 | 171 | //serial_mutex.unlock(); |
mmarine3 | 7:85d42006e380 | 172 | waver.play(wave_file); |
mmarine3 | 7:85d42006e380 | 173 | fclose(wave_file); |
mmarine3 | 8:c0a6a3363e43 | 174 | */ |
mmarine3 | 8:c0a6a3363e43 | 175 | |
mmarine3 | 8:c0a6a3363e43 | 176 | //Loop to validate the main loop is executing |
mmarine3 | 8:c0a6a3363e43 | 177 | while(1) |
mmarine3 | 8:c0a6a3363e43 | 178 | { |
mmarine3 | 9:f4f03767acc0 | 179 | //MyLED = !MyLED; |
mmarine3 | 9:f4f03767acc0 | 180 | //Thread::wait(100); |
mmarine3 | 8:c0a6a3363e43 | 181 | } |
mmarine3 | 2:1ec843c95914 | 182 | } |