Aosen Ba / Mbed 2 deprecated 2036lab6

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:
baaosen
Date:
Thu Oct 25 14:46:07 2018 +0000
Parent:
0:df4d7c0a1594
Commit message:
ECE 2036 project

Changed in this revision

RGB.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 a5f43fb83aca RGB.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RGB.h	Thu Oct 25 14:46:07 2018 +0000
@@ -0,0 +1,30 @@
+#include "mbed.h"
+//Class to control an RGB LED using three PWM pins
+class RGBLed
+{
+public:
+    RGBLed(PinName redpin, PinName greenpin, PinName bluepin);
+    void write(float red,float green, float blue);
+private:
+    PwmOut _redpin;
+    PwmOut _greenpin;
+    PwmOut _bluepin;
+};
+ 
+RGBLed::RGBLed (PinName redpin, PinName greenpin, PinName bluepin)
+    : _redpin(redpin), _greenpin(greenpin), _bluepin(bluepin)
+{
+    //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker)
+    _redpin.period(0.0005);
+}
+ 
+void RGBLed::write(float red,float green, float blue)
+{
+    _redpin = red;
+    _greenpin = green;
+    _bluepin = blue;
+}
+//class could be moved to include file
+ 
+ 
+//Setup RGB led using PWM pins and class
diff -r df4d7c0a1594 -r a5f43fb83aca main.cpp
--- a/main.cpp	Mon Apr 06 12:28:01 2015 +0000
+++ b/main.cpp	Thu Oct 25 14:46:07 2018 +0000
@@ -1,12 +1,14 @@
 #include "mbed.h"
 // Need include below to add the RTOS
 #include "rtos.h"
-//#include "EthernetInterface.h" //needed for Extra Credit
-//#include "NTPClient.h"
+#include "EthernetInterface.h" //needed for Extra Credit
+#include "NTPClient.h"
 #include "SDFileSystem.h"
 #include "uLCD_4DGL.h"
 #include "TMP36.h"
 #include "wave_player.h"
+#include "RGB.h"
+#include "time.h"
 // Setup four builtin leds for use by threads
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
@@ -18,10 +20,15 @@
 //wave player plays a *.wav file to D/A and a PWM
 wave_player waver(&DACout);
 
-uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin;
+uLCD_4DGL uLCD(p9, p10, p11); // serial tx, serial rx, reset pin;
+Mutex lcd_mutex;
 
 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup
-
+RGBLed myled(p24, p23, p22);
+inline float random_number()
+{
+    return (rand()/(float(RAND_MAX)));
+}
 
 // Setup function code for three new threads to run.
 // Put in a while loop so that threads run forever.
@@ -34,6 +41,10 @@
 {
     led2 = 1;
     while (true) {
+        // set up a timer to be used for sample rate interrupts
+        myled.write(random_number(),0.0,0.0);
+        wait(0.5);
+        myled.write(0.0,0.0,1.0-random_number());
         led2 = !led2;
         Thread::wait(2000);
     }
@@ -42,15 +53,28 @@
 {
     led3 = 1;
     while (true) {
+    // precompute 128 sample points on one sine wave cycle 
+    // used for continuous sine wave output later
+    FILE *p=fopen("/sd/fire.wav","r");;
+    waver.play(p);
+    fclose(p);
         led3 = !led3;
         Thread::wait(4000);
     }
 }
 void led4_thread(void const *argument)
 {
-    TMP36 myTMP36(p20);
+    TMP36 myTMP36(p15);
     led4 = 1;
     while (true) {
+        float tempC = myTMP36.read();
+        //convert to degrees F
+        float tempF = (9.0*tempC)/5.0 + 32.0;
+        //print current temp
+        lcd_mutex.lock();
+        uLCD.locate(1,4);  
+        uLCD.printf("%5.2F C  %5.2F F \n\r", tempC, tempF);
+        lcd_mutex.unlock();
         led4 = !led4;
         Thread::wait(8000);
     }
@@ -60,7 +84,31 @@
 {
     led1 = 1;
 // code to set time in extra credit option goes here
-//
+    {
+        EthernetInterface eth;
+        NTPClient ntp;
+        eth.init(); //Use DHCP
+        wait(2);
+        uLCD.cls();
+        uLCD.printf("\nGetting IP Address\r\n");
+        if(eth.connect(60000)!=0) {
+            uLCD.printf("DHCP error - No IP\n");
+            set_time(1360000000); //starts RTC
+            wait(10);
+        } 
+        else {
+            uLCD.printf("IP is %s\n", eth.getIPAddress());
+            wait(2);
+        }
+        uLCD.printf("\nTrying to update time via NTP...\r\n");
+        if (ntp.setTime("0.pool.ntp.org") == 0)
+        {
+            ntp.setTime("0.pool.ntp.org");
+            uLCD.printf("Set time successfully\r\n");
+        }
+        wait(2);
+        uLCD.cls();
+    }
     uLCD.baudrate(3000000); //jack up baud rate to max for fast display
     uLCD.text_width(2); //2x size text
     uLCD.text_height(2);
@@ -76,18 +124,18 @@
 //
 // Set time in seconds since Jan 1 1970 (Unix style)
 // must set time to start Real Time clock running
-    set_time(1286729737);
     char buffer[12];
     time_t seconds;
-    while (true) {
+    while (1) {
 // reads time structure
         seconds = time(NULL);
 // converts time structure to a string
         strftime(buffer, 12, "%T", localtime(&seconds));
 // print time HH:MM:SS
+        lcd_mutex.lock();
         uLCD.locate(0,2);
         uLCD.printf("%s\n\r", buffer);
-        led1 = !led1;
+        lcd_mutex.unlock();
         Thread::wait(1000);
     }
 }