ECE 2036 lab 7 IoT Clock starting template

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

Dependents:   CPP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 // Need include below to add the RTOS
00003 #include "rtos.h"
00004 //#include "EthernetInterface.h" //needed for Extra Credit
00005 //#include "NTPClient.h"
00006 #include "SDFileSystem.h"
00007 #include "uLCD_4DGL.h"
00008 #include "TMP36.h"
00009 #include "wave_player.h"
00010 // Setup four builtin leds for use by threads
00011 DigitalOut led1(LED1);
00012 DigitalOut led2(LED2);
00013 DigitalOut led3(LED3);
00014 DigitalOut led4(LED4);
00015 
00016 AnalogOut DACout(p18); // used to play sound on speaker
00017 
00018 //wave player plays a *.wav file to D/A and a PWM
00019 wave_player waver(&DACout);
00020 
00021 uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin;
00022 
00023 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup
00024 
00025 
00026 // Setup function code for three new threads to run.
00027 // Put in a while loop so that threads run forever.
00028 // Thread::wait will force at least a "x" millisecond
00029 // wait before the thread runs again. During this delay
00030 // the other threads will run
00031 // DO NOT use wait() with the RTOS!!!!!
00032 // wait just burns processor time and no other threads run
00033 void led2_thread(void const *argument)
00034 {
00035     led2 = 1;
00036     while (true) {
00037         led2 = !led2;
00038         Thread::wait(2000);
00039     }
00040 }
00041 void led3_thread(void const *argument)
00042 {
00043     led3 = 1;
00044     while (true) {
00045         led3 = !led3;
00046         Thread::wait(4000);
00047     }
00048 }
00049 void led4_thread(void const *argument)
00050 {
00051     TMP36 myTMP36(p20);
00052     led4 = 1;
00053     while (true) {
00054         led4 = !led4;
00055         Thread::wait(8000);
00056     }
00057 }
00058 
00059 int main()
00060 {
00061     led1 = 1;
00062 // code to set time in extra credit option goes here
00063 //
00064     uLCD.baudrate(3000000); //jack up baud rate to max for fast display
00065     uLCD.text_width(2); //2x size text
00066     uLCD.text_height(2);
00067 // Create 3 new thread objects thread1, thread2, and thread3
00068 // The RTOS will immediately start running them
00069     Thread thread1(led2_thread);
00070     Thread thread2(led3_thread);
00071     Thread thread3(led4_thread);
00072 // Main continues to run and is actually the first thread.
00073 // So a total of four threads are running now.
00074 // Each thread blinks an LED, but at a different rate
00075 // because of the different values used in Thread::wait().
00076 //
00077 // Set time in seconds since Jan 1 1970 (Unix style)
00078 // must set time to start Real Time clock running
00079     set_time(1286729737);
00080     char buffer[12];
00081     time_t seconds;
00082     while (true) {
00083 // reads time structure
00084         seconds = time(NULL);
00085 // converts time structure to a string
00086         strftime(buffer, 12, "%T", localtime(&seconds));
00087 // print time HH:MM:SS
00088         uLCD.locate(0,2);
00089         uLCD.printf("%s\n\r", buffer);
00090         led1 = !led1;
00091         Thread::wait(1000);
00092     }
00093 }