Cheryl Docherty / Mbed 2 deprecated Lab3_Part3

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 
00003 
00004 #include "mbed.h"
00005 #include "rtos.h"
00006 #include "uLCD_4DGL.h"
00007 #include "SDFileSystem.h"
00008 #include "wave_player.h"
00009  
00010 // Onboard LEDs (for testing)
00011 DigitalOut led1(LED1);
00012 DigitalOut led2(LED2);
00013 DigitalOut led3(LED3);
00014 DigitalOut led4(LED4);
00015 
00016 // RBG LED
00017 PwmOut RGBLED_r(p23);
00018 PwmOut RGBLED_g(p24);
00019 PwmOut RGBLED_b(p25);
00020 
00021 // LCD Screen
00022 uLCD_4DGL uLCD(p9,p10,p17); // serial tx, serial rx, reset pin;
00023 
00024 Thread thread1;
00025 Thread thread2;
00026 Thread thread3;
00027 
00028 // mutex to make the lcd lib thread safe
00029 Mutex lcd_mutex;
00030 
00031 //SD Card
00032 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
00033 
00034 AnalogOut DACout(p18);
00035 
00036 wave_player waver(&DACout);
00037 
00038 //Bouncing Ball Demo
00039 float fx=60.0,fy=60.0,vx=1.0,vy=0.4;
00040 int x=60,y=60,radius=4;
00041 int i = 0;
00042  
00043 void led2_thread() {
00044     while (true) {
00045         lcd_mutex.lock();
00046         //erase old ball location
00047         uLCD.filled_circle(x, y, radius, BLACK);
00048         //move ball
00049         fx=fx+vx;
00050         fy=fy+vy;
00051         x=(int)fx;
00052         y=(int)fy;
00053         //draw ball
00054         uLCD.filled_circle(x, y, radius, RED);
00055         lcd_mutex.unlock();
00056         //bounce off edge walls and slow down a bit?
00057         if ((x<=radius+1) || (x>=126-radius)) vx = -.90*vx;
00058         if ((y<=radius+51) || (y>=126-radius)) vy = -.90*vy;
00059         Thread::wait(40);
00060     }
00061 }
00062 
00063 void led3_thread() {
00064     while (true) {
00065         lcd_mutex.lock();
00066         uLCD.locate(1,3);
00067         uLCD.printf("%2D",i);
00068         lcd_mutex.unlock();
00069         i++;
00070         Thread::wait(1000);
00071     }
00072 }
00073 
00074 void led4_thread() {
00075     while(true) {         // thread loop
00076         RGBLED_r = 0.5 + (rand() % 11)/20.0;
00077         RGBLED_g = 0.1 + (rand() % 11)/20.0;
00078         RGBLED_b = 0.1 + (rand() % 11)/20.0;
00079         Thread::wait(40);    
00080     }
00081 }
00082  
00083 int main() {
00084     thread1.start(led2_thread);
00085     thread2.start(led3_thread);
00086     thread3.start(led4_thread);
00087     
00088     lcd_mutex.lock();
00089     //draw walls
00090     uLCD.line(0, 50, 127, 50, WHITE);
00091     uLCD.line(127, 50, 127, 127, WHITE);
00092     uLCD.line(127, 127, 0, 127, WHITE);
00093     uLCD.line(0, 127, 0, 50, WHITE);
00094     uLCD.locate(0,1);
00095     uLCD.printf("Seconds Since \nInitialisation: "); //Default Green on black text
00096     lcd_mutex.unlock();
00097     
00098     FILE *wave_file;
00099     printf("\n\n\nHello, wave world!\n");
00100     wave_file=fopen("/sd/OriginalSeriesMainTitle.wav","r");
00101     waver.play(wave_file);
00102     fclose(wave_file);
00103     while (true) {
00104         led1 = !led1;
00105 
00106         
00107         Thread::wait(500);
00108     }
00109 }