ece 4180 lab 3

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

Files at this revision

API Documentation at this revision

Comitter:
emilywilson
Date:
Thu Feb 27 12:57:44 2020 +0000
Parent:
3:94d9434576af
Commit message:
add extra credit

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
nav_switch.h Show annotated file Show diff for this revision Revisions of this file
part4_led.h Show annotated file Show diff for this revision Revisions of this file
diff -r 94d9434576af -r 1ef667567942 main.cpp
--- a/main.cpp	Mon Feb 24 17:01:46 2020 +0000
+++ b/main.cpp	Thu Feb 27 12:57:44 2020 +0000
@@ -3,6 +3,7 @@
 #include "uLCD_4DGL.h"
 #include "SDFileSystem.h"
 #include "wave_player.h"
+#include "nav_switch.h"
 
 //#include "part2.h"
 //#include "part3.h"
@@ -10,14 +11,16 @@
 //#include "lidar_theremin.h"
 
 Serial pc(USBTX, USBRX);
-RawSerial dev(p9,p10);
+//RawSerial dev(p9,p10);
 
 DigitalOut myled1(LED1);
 DigitalOut myled2(LED2);
 DigitalOut myled3(LED3);
 DigitalOut myled4(LED4);
 RGBLed led = RGBLed(p21, p22, p23);
-Color colors[] = { _red, _pink, _orange, _yellow };
+
+float p = 0.5;
+Color colors[] = { _red(1.0), _pink(1.0), _orange(1.0), _yellow(1.0) };
 
 uLCD_4DGL lcd(p28,p27,p30);
 
@@ -26,34 +29,34 @@
 Thread lcd_thread2;
 Thread audio_thread;
 
-void changeColor() {
-    while (dev.readable()) {
-        char c = dev.getc();
-        
-        if (c == 'r') {
-            colors[0] = _red;
-            colors[1] = _pink;
-            colors[2] = _orange;
-            colors[3] = _yellow;
-        } else if (c == 'b') {
-            colors[0] = _teal;
-            colors[1] = _light_blue;
-            colors[2] = _blue;
-            colors[3] = _purple;
-        } else if (c == 'g') {
-            colors[0] = _yellow;
-            colors[1] = _light_green;
-            colors[2] = _green;
-            colors[3] = _teal;
-        }
-    }
-}
+//void changeColor() {
+//    while (dev.readable()) {
+//        char c = dev.getc();
+//        
+//        if (c == 'r') {
+//            colors[0] = _red(p);
+//            colors[1] = _pink(p);
+//            colors[2] = _orange(p);
+//            colors[3] = _yellow(p);
+//        } else if (c == 'b') {
+//            colors[0] = _teal(p);
+//            colors[1] = _light_blue(p);
+//            colors[2] = _blue(p);
+//            colors[3] = _purple(p);
+//        } else if (c == 'g') {
+//            colors[0] = _yellow(p);
+//            colors[1] = _light_green(p);
+//            colors[2] = _green(p);
+//            colors[3] = _teal(p);
+//        }
+//    }
+//}
 
 void led_handler() {
     Color c = colors[0];
     int i = 0;
     while (1) {
-        led.write(c.red, c.green, c.blue);
+        led.write(c.red*p, c.green*p, c.blue*p);
         i = (i + 1) % 4;
         c = colors[i];
         
@@ -61,19 +64,19 @@
     }
 }
 
-Mutex lcd_mutex;
-//Semaphore sem(1);
+//Mutex lcd_mutex;
+Semaphore sem(1);
 
 
 void lcd1_handler() {
     int i = 0;
     while (1) {
-        lcd_mutex.lock();
-//        sem.wait();
+//        lcd_mutex.lock();
+        sem.wait();
         lcd.locate(0, 0);
         lcd.printf("Counter: %d", i);
-        lcd_mutex.unlock();
-//        sem.release();
+//        lcd_mutex.unlock();
+        sem.release();
         
         i++;
         Thread::wait(1000);
@@ -85,24 +88,24 @@
     double s,a;
     k = 1;
     offset = 50;
-    lcd_mutex.lock();
-//    sem.wait();
+//    lcd_mutex.lock();
+    sem.wait();
     lcd.rectangle(89,0,127,17,1);
-    lcd_mutex.unlock();
-//    sem.release();
+//    lcd_mutex.unlock();
+    sem.release();
     
     while(1) {       // thread loop
 //        v = 1; 
-        lcd_mutex.lock();
-//        sem.wait();
+//        lcd_mutex.lock();
+        sem.wait();
         for (i=0; i<127; i++) {
             s = 8 * sin((long double)(i+k) /5);   // pixel to print
             a = 8 * sin((long double)(i+k-1) /5); // old pixel to erase
             lcd.pixel(i,offset + (int)a, BLACK);           // erase pixel
             lcd.pixel(i,offset + (int)s, WHITE);           // print pixel
         }
-        lcd_mutex.unlock();
-//        sem.release();
+//        lcd_mutex.unlock();
+        sem.release();
         k++;
         Thread::wait(100);   // value of pot1 / 100   
     }
@@ -111,13 +114,14 @@
 SDFileSystem sd(p5, p6, p7, p8, "sd");
 AnalogOut DACout(p18);
 wave_player waver(&DACout);
+bool play_audio = true;
 
 void audio_handler() {
     FILE *wave_file;
     myled2 = !myled2;
 //    printf("\n\n\nHello, wave world!\n");
 //    myled4 = !myled4;
-    while (1) {
+    while (play_audio) {
         myled4 = !myled4;
         wave_file=fopen("/sd/sample.wav","r");
         waver.play(wave_file);
@@ -126,6 +130,8 @@
     }
 }
 
+Nav_Switch nav(p15, p12, p16, p11, p17);
+
 int main() {
 //    run_part2();
 //    run_part2_EC();
@@ -134,21 +140,36 @@
 //    run_part3();
 
     pc.baud(9600);
-    dev.baud(9600);
+//    dev.baud(9600);
     
     led_thread.start(led_handler);
     lcd.cls();
     lcd_thread1.start(lcd1_handler);
     lcd_thread2.start(lcd2_handler);
-    myled2 = !myled2;
     audio_thread.start(audio_handler);
-    myled3 = !myled3;
     
-    dev.attach(&changeColor, Serial::RxIrq);
+//    dev.attach(&changeColor, Serial::RxIrq);
 
     while (1) {
         myled1 = !myled1;
+        if (nav.fire()) {
+            play_audio = !play_audio;
+        } else if (nav.up() && p < 1.0f) {
+            p += 0.1f;
+        } else if (nav.down() && p > 0.0f) {
+            p -= 0.1f;
+        } else if (nav.left()) {
+            colors[0] = _red(1.0);
+            colors[1] = _pink(1.0);
+            colors[2] = _orange(1.0);
+            colors[3] = _yellow(1.0);
+        } else if (nav.right()) {
+            colors[0] = _teal(1.0);
+            colors[1] = _light_blue(1.0);
+            colors[2] = _blue(1.0);
+            colors[3] = _purple(1.0);
+        }
 
-        Thread::wait(500);
+        Thread::wait(100);
     }
 }
\ No newline at end of file
diff -r 94d9434576af -r 1ef667567942 nav_switch.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nav_switch.h	Thu Feb 27 12:57:44 2020 +0000
@@ -0,0 +1,57 @@
+#include "mbed.h"
+
+class Nav_Switch
+{
+public:
+    Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
+    int read();
+//boolean functions to test each switch
+    bool up();
+    bool down();
+    bool left();
+    bool right();
+    bool fire();
+//automatic read on RHS
+    operator int ();
+//index to any switch array style
+    bool operator[](int index) {
+        return _pins[index];
+    };
+private:
+    BusIn _pins;
+ 
+};
+Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
+    _pins(up, down, left, right, fire)
+{
+    _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
+    wait(0.001); //delays just a bit for pullups to pull inputs high
+}
+inline bool Nav_Switch::up()
+{
+    return !(_pins[0]);
+}
+inline bool Nav_Switch::down()
+{
+    return !(_pins[1]);
+}
+inline bool Nav_Switch::left()
+{
+    return !(_pins[2]);
+}
+inline bool Nav_Switch::right()
+{
+    return !(_pins[3]);
+}
+inline bool Nav_Switch::fire()
+{
+    return !(_pins[4]);
+}
+inline int Nav_Switch::read()
+{
+    return _pins.read();
+}
+inline Nav_Switch::operator int ()
+{
+    return _pins.read();
+}
\ No newline at end of file
diff -r 94d9434576af -r 1ef667567942 part4_led.h
--- a/part4_led.h	Mon Feb 24 17:01:46 2020 +0000
+++ b/part4_led.h	Thu Feb 27 12:57:44 2020 +0000
@@ -1,13 +1,13 @@
 #include "RGBLed.h"
 #include "Color.h"
 
-#define _pink Color(255.0/255.0, 61.0/255.0, 152.0/255.0)
-#define _red Color(245.0/255.0, 66.0/255.0, 66.0/255.0)
-#define _orange Color(255.0/255.0, 190.0/255.0, 61.0/255.0)
-#define _yellow Color(255.0/255.0, 239.0/255.0, 61.0/255.0)
-#define _light_green Color(187.0/255.0, 255.0/255.0, 0.0)
-#define _green Color(17.0/255.0, 201.0/255.0, 0.0)
-#define _teal Color(0.0, 255.0/255.0, 170.0/255.0)
-#define _light_blue Color(0.0, 255.0/255.0, 247.0/255.0)
-#define _blue Color(0.0, 128.0/255.0, 255.0/255.0)
-#define _purple Color(106.0/255.0, 0.0, 255.0/255.0)
+#define _pink(p) Color(255.0*p/255.0, 61.0*p/255.0, 152.0*p/255.0)
+#define _red(p) Color(245.0*p/255.0, 66.0*p/255.0, 66.0*p/255.0)
+#define _orange(p) Color(255.0*p/255.0, 190.0*p/255.0, 61.0*p/255.0)
+#define _yellow(p) Color(255.0*p/255.0, 239.0*p/255.0, 61.0*p/255.0)
+#define _light_green(p) Color(187.0*p/255.0, 255.0*p/255.0, 0.0)
+#define _green(p) Color(17.0*p/255.0, 201.0*p/255.0, 0.0)
+#define _teal(p) Color(0.0, 255.0*p/255.0, 170.0*p/255.0)
+#define _light_blue(p) Color(0.0, 255.0*p/255.0, 247.0*p/255.0)
+#define _blue(p) Color(0.0, 128.0*p/255.0, 255.0*p/255.0)
+#define _purple(p) Color(106.0*p/255.0, 0.0, 255.0*p/255.0)