fun fork test of fun fork things

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

Committer:
cheryldocherty
Date:
Sat Apr 11 13:07:47 2020 +0000
Revision:
2:7828936faad6
Parent:
1:40788af73d41
Child:
3:48862145678b
second space added

Who changed what in which revision?

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