template

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

Fork of 2036lab7_template by jim hamblen

Files at this revision

API Documentation at this revision

Comitter:
ssong86
Date:
Mon Feb 01 06:41:04 2016 +0000
Parent:
0:df4d7c0a1594
Commit message:
template

Changed in this revision

Shiftbrite.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r df4d7c0a1594 -r 2a0dea19d2ba Shiftbrite.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Shiftbrite.h	Mon Feb 01 06:41:04 2016 +0000
@@ -0,0 +1,33 @@
+#include "mbed.h"
+
+//Setup a new class for a Shiftbrite RGB LED module
+class Shiftbrite
+{
+public:
+    Shiftbrite(PinName pin_e, PinName pin_l, PinName pin_do, PinName pin_di, PinName pin_clk);
+    void write(int red, int green, int blue);
+
+private:
+//class sets up the pins
+    DigitalOut _pin_e;
+    DigitalOut _pin_l;
+    SPI _spi;
+};
+
+Shiftbrite::Shiftbrite(PinName pin_e, PinName pin_l, PinName pin_do, PinName pin_di, PinName pin_clk)
+    : _pin_e(pin_e), _pin_l(pin_l), _spi(pin_do, pin_di, pin_clk)
+{
+ _spi.format(16,0);
+    _spi.frequency(500000);
+}
+
+void Shiftbrite::write(int red, int green, int blue)
+{
+ unsigned int low_color=0;
+    unsigned int high_color=0;
+    high_color=(blue<<4)|((red&0x3C0)>>6);
+    low_color=(((red&0x3F)<<10)|(green));
+    _spi.write(high_color);
+    _spi.write(low_color);
+    _pin_l = 1;
+}
diff -r df4d7c0a1594 -r 2a0dea19d2ba main.cpp
--- a/main.cpp	Mon Apr 06 12:28:01 2015 +0000
+++ b/main.cpp	Mon Feb 01 06:41:04 2016 +0000
@@ -7,12 +7,15 @@
 #include "uLCD_4DGL.h"
 #include "TMP36.h"
 #include "wave_player.h"
+#include "Shiftbrite.h"
 // Setup four builtin leds for use by threads
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
 
+Shiftbrite myShiftbrite(p9, p10, p11, p12, p13);// ei li di n/c ci
+
 AnalogOut DACout(p18); // used to play sound on speaker
 
 //wave player plays a *.wav file to D/A and a PWM
@@ -21,7 +24,7 @@
 uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin;
 
 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup
-
+Mutex myMutex;
 
 // Setup function code for three new threads to run.
 // Put in a while loop so that threads run forever.
@@ -30,40 +33,71 @@
 // the other threads will run
 // DO NOT use wait() with the RTOS!!!!!
 // wait just burns processor time and no other threads run
+inline float random_number()
+{
+    return (rand()/(float(RAND_MAX)));
+}
+
+
 void led2_thread(void const *argument)
 {
     led2 = 1;
+    //myShiftbrite.write(50,50,0);
     while (true) {
         led2 = !led2;
+        myShiftbrite.write(204,255,255);
         Thread::wait(2000);
     }
 }
 void led3_thread(void const *argument)
 {
     led3 = 1;
+     FILE *wave_file;
+     myShiftbrite.write(50,50,0);
     while (true) {
-        led3 = !led3;
-        Thread::wait(4000);
+    led3 = !led3;
+    myMutex.lock();
+    uLCD.printf("\n\n\nHello, wave world!\n");
+    myMutex.unlock();
+    
+    wave_file=fopen("/sd/fire.wav","r");
+    waver.play(wave_file);
+    fclose(wave_file);
+    Thread::wait(4000);
     }
 }
 void led4_thread(void const *argument)
 {
     TMP36 myTMP36(p20);
+    float tempC, tempF;
     led4 = 1;
+    myShiftbrite.write(50,50,0);
     while (true) {
         led4 = !led4;
-        Thread::wait(8000);
-    }
+        tempC = myTMP36.read();
+        tempF = (9.0*tempC)/5.0 + 32.0;
+        myMutex.lock();
+        uLCD.baudrate(3000000); //jack up baud rate to max for fast display
+        //uLCD.text_width(2); //2x size text
+        //uLCD.text_height(2);
+        uLCD.locate(2,4);
+        uLCD.printf("%5.2F F \n\r", tempF);
+        myMutex.unlock();
+        Thread::wait(4000);
+        }
 }
 
+
 int main()
 {
     led1 = 1;
 // code to set time in extra credit option goes here
 //
-    uLCD.baudrate(3000000); //jack up baud rate to max for fast display
-    uLCD.text_width(2); //2x size text
-    uLCD.text_height(2);
+    
+    //uLCD.baudrate(3000000); //jack up baud rate to max for fast display
+    //uLCD.text_width(2); //2x size text
+    //uLCD.text_height(2);
+    
 // Create 3 new thread objects thread1, thread2, and thread3
 // The RTOS will immediately start running them
     Thread thread1(led2_thread);
@@ -85,9 +119,20 @@
 // converts time structure to a string
         strftime(buffer, 12, "%T", localtime(&seconds));
 // print time HH:MM:SS
-        uLCD.locate(0,2);
+        myMutex.lock();
+        uLCD.locate(1,2);
         uLCD.printf("%s\n\r", buffer);
-        led1 = !led1;
-        Thread::wait(1000);
+        myMutex.unlock();
+   //     led1 = !led1;
+    //    Thread::wait(1000);
+    
+    while(1)
+    {
+        myShiftbrite.write(random_number()*255,random_number()*255,random_number()*255);
+        wait(0.4);
+    }
     }
 }
+
+
+