fun test of fun things

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

Committer:
cheryldocherty
Date:
Sat Apr 11 13:03:41 2020 +0000
Revision:
0:97120df18654
Child:
1:40788af73d41
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cheryldocherty 0:97120df18654 1 #include "mbed.h"
cheryldocherty 0:97120df18654 2 #include "rtos.h"
cheryldocherty 0:97120df18654 3 #include "uLCD_4DGL.h"
cheryldocherty 0:97120df18654 4 #include "SDFileSystem.h"
cheryldocherty 0:97120df18654 5 #include "wave_player.h"
cheryldocherty 0:97120df18654 6
cheryldocherty 0:97120df18654 7 // Onboard LEDs (for testing)
cheryldocherty 0:97120df18654 8 DigitalOut led1(LED1);
cheryldocherty 0:97120df18654 9 DigitalOut led2(LED2);
cheryldocherty 0:97120df18654 10 DigitalOut led3(LED3);
cheryldocherty 0:97120df18654 11 DigitalOut led4(LED4);
cheryldocherty 0:97120df18654 12
cheryldocherty 0:97120df18654 13 // RBG LED
cheryldocherty 0:97120df18654 14 PwmOut RGBLED_r(p23);
cheryldocherty 0:97120df18654 15 PwmOut RGBLED_g(p24);
cheryldocherty 0:97120df18654 16 PwmOut RGBLED_b(p25);
cheryldocherty 0:97120df18654 17
cheryldocherty 0:97120df18654 18 // LCD Screen
cheryldocherty 0:97120df18654 19 uLCD_4DGL uLCD(p9,p10,p17); // serial tx, serial rx, reset pin;
cheryldocherty 0:97120df18654 20
cheryldocherty 0:97120df18654 21 Thread thread1;
cheryldocherty 0:97120df18654 22 Thread thread2;
cheryldocherty 0:97120df18654 23 Thread thread3;
cheryldocherty 0:97120df18654 24
cheryldocherty 0:97120df18654 25 // mutex to make the lcd lib thread safe
cheryldocherty 0:97120df18654 26 Mutex lcd_mutex;
cheryldocherty 0:97120df18654 27
cheryldocherty 0:97120df18654 28 //SD Card
cheryldocherty 0:97120df18654 29 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
cheryldocherty 0:97120df18654 30
cheryldocherty 0:97120df18654 31 AnalogOut DACout(p18);
cheryldocherty 0:97120df18654 32
cheryldocherty 0:97120df18654 33 wave_player waver(&DACout);
cheryldocherty 0:97120df18654 34
cheryldocherty 0:97120df18654 35 //Bouncing Ball Demo
cheryldocherty 0:97120df18654 36 float fx=60.0,fy=60.0,vx=1.0,vy=0.4;
cheryldocherty 0:97120df18654 37 int x=60,y=60,radius=4;
cheryldocherty 0:97120df18654 38 int i = 0;
cheryldocherty 0:97120df18654 39
cheryldocherty 0:97120df18654 40 void led2_thread() {
cheryldocherty 0:97120df18654 41 while (true) {
cheryldocherty 0:97120df18654 42 lcd_mutex.lock();
cheryldocherty 0:97120df18654 43 //erase old ball location
cheryldocherty 0:97120df18654 44 uLCD.filled_circle(x, y, radius, BLACK);
cheryldocherty 0:97120df18654 45 //move ball
cheryldocherty 0:97120df18654 46 fx=fx+vx;
cheryldocherty 0:97120df18654 47 fy=fy+vy;
cheryldocherty 0:97120df18654 48 x=(int)fx;
cheryldocherty 0:97120df18654 49 y=(int)fy;
cheryldocherty 0:97120df18654 50 //draw ball
cheryldocherty 0:97120df18654 51 uLCD.filled_circle(x, y, radius, RED);
cheryldocherty 0:97120df18654 52 lcd_mutex.unlock();
cheryldocherty 0:97120df18654 53 //bounce off edge walls and slow down a bit?
cheryldocherty 0:97120df18654 54 if ((x<=radius+1) || (x>=126-radius)) vx = -.90*vx;
cheryldocherty 0:97120df18654 55 if ((y<=radius+51) || (y>=126-radius)) vy = -.90*vy;
cheryldocherty 0:97120df18654 56 Thread::wait(40);
cheryldocherty 0:97120df18654 57 }
cheryldocherty 0:97120df18654 58 }
cheryldocherty 0:97120df18654 59
cheryldocherty 0:97120df18654 60 void led3_thread() {
cheryldocherty 0:97120df18654 61 while (true) {
cheryldocherty 0:97120df18654 62 lcd_mutex.lock();
cheryldocherty 0:97120df18654 63 uLCD.locate(1,3);
cheryldocherty 0:97120df18654 64 uLCD.printf("%2D",i);
cheryldocherty 0:97120df18654 65 lcd_mutex.unlock();
cheryldocherty 0:97120df18654 66 i++;
cheryldocherty 0:97120df18654 67 Thread::wait(1000);
cheryldocherty 0:97120df18654 68 }
cheryldocherty 0:97120df18654 69 }
cheryldocherty 0:97120df18654 70
cheryldocherty 0:97120df18654 71 void led4_thread() {
cheryldocherty 0:97120df18654 72 while(true) { // thread loop
cheryldocherty 0:97120df18654 73 RGBLED_r = 0.5 + (rand() % 11)/20.0;
cheryldocherty 0:97120df18654 74 RGBLED_g = 0.1 + (rand() % 11)/20.0;
cheryldocherty 0:97120df18654 75 RGBLED_b = 0.1 + (rand() % 11)/20.0;
cheryldocherty 0:97120df18654 76 Thread::wait(40);
cheryldocherty 0:97120df18654 77 }
cheryldocherty 0:97120df18654 78 }
cheryldocherty 0:97120df18654 79
cheryldocherty 0:97120df18654 80 int main() {
cheryldocherty 0:97120df18654 81 thread1.start(led2_thread);
cheryldocherty 0:97120df18654 82 thread2.start(led3_thread);
cheryldocherty 0:97120df18654 83 thread3.start(led4_thread);
cheryldocherty 0:97120df18654 84
cheryldocherty 0:97120df18654 85 lcd_mutex.lock();
cheryldocherty 0:97120df18654 86 //draw walls
cheryldocherty 0:97120df18654 87 uLCD.line(0, 50, 127, 50, WHITE);
cheryldocherty 0:97120df18654 88 uLCD.line(127, 50, 127, 127, WHITE);
cheryldocherty 0:97120df18654 89 uLCD.line(127, 127, 0, 127, WHITE);
cheryldocherty 0:97120df18654 90 uLCD.line(0, 127, 0, 50, WHITE);
cheryldocherty 0:97120df18654 91 uLCD.locate(0,1);
cheryldocherty 0:97120df18654 92 uLCD.printf("Seconds Since \nInitialisation: "); //Default Green on black text
cheryldocherty 0:97120df18654 93 lcd_mutex.unlock();
cheryldocherty 0:97120df18654 94
cheryldocherty 0:97120df18654 95 FILE *wave_file;
cheryldocherty 0:97120df18654 96 printf("\n\n\nHello, wave world!\n");
cheryldocherty 0:97120df18654 97 wave_file=fopen("/sd/OriginalSeriesMainTitle.wav","r");
cheryldocherty 0:97120df18654 98 waver.play(wave_file);
cheryldocherty 0:97120df18654 99 fclose(wave_file);
cheryldocherty 0:97120df18654 100 while (true) {
cheryldocherty 0:97120df18654 101 led1 = !led1;
cheryldocherty 0:97120df18654 102
cheryldocherty 0:97120df18654 103
cheryldocherty 0:97120df18654 104 Thread::wait(500);
cheryldocherty 0:97120df18654 105 }
cheryldocherty 0:97120df18654 106 }