template

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

Fork of 2036lab7_template by jim hamblen

Committer:
4180_1
Date:
Mon Apr 06 12:28:01 2015 +0000
Revision:
0:df4d7c0a1594
Child:
1:2a0dea19d2ba
ver 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:df4d7c0a1594 1 #include "mbed.h"
4180_1 0:df4d7c0a1594 2 // Need include below to add the RTOS
4180_1 0:df4d7c0a1594 3 #include "rtos.h"
4180_1 0:df4d7c0a1594 4 //#include "EthernetInterface.h" //needed for Extra Credit
4180_1 0:df4d7c0a1594 5 //#include "NTPClient.h"
4180_1 0:df4d7c0a1594 6 #include "SDFileSystem.h"
4180_1 0:df4d7c0a1594 7 #include "uLCD_4DGL.h"
4180_1 0:df4d7c0a1594 8 #include "TMP36.h"
4180_1 0:df4d7c0a1594 9 #include "wave_player.h"
4180_1 0:df4d7c0a1594 10 // Setup four builtin leds for use by threads
4180_1 0:df4d7c0a1594 11 DigitalOut led1(LED1);
4180_1 0:df4d7c0a1594 12 DigitalOut led2(LED2);
4180_1 0:df4d7c0a1594 13 DigitalOut led3(LED3);
4180_1 0:df4d7c0a1594 14 DigitalOut led4(LED4);
4180_1 0:df4d7c0a1594 15
4180_1 0:df4d7c0a1594 16 AnalogOut DACout(p18); // used to play sound on speaker
4180_1 0:df4d7c0a1594 17
4180_1 0:df4d7c0a1594 18 //wave player plays a *.wav file to D/A and a PWM
4180_1 0:df4d7c0a1594 19 wave_player waver(&DACout);
4180_1 0:df4d7c0a1594 20
4180_1 0:df4d7c0a1594 21 uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin;
4180_1 0:df4d7c0a1594 22
4180_1 0:df4d7c0a1594 23 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup
4180_1 0:df4d7c0a1594 24
4180_1 0:df4d7c0a1594 25
4180_1 0:df4d7c0a1594 26 // Setup function code for three new threads to run.
4180_1 0:df4d7c0a1594 27 // Put in a while loop so that threads run forever.
4180_1 0:df4d7c0a1594 28 // Thread::wait will force at least a "x" millisecond
4180_1 0:df4d7c0a1594 29 // wait before the thread runs again. During this delay
4180_1 0:df4d7c0a1594 30 // the other threads will run
4180_1 0:df4d7c0a1594 31 // DO NOT use wait() with the RTOS!!!!!
4180_1 0:df4d7c0a1594 32 // wait just burns processor time and no other threads run
4180_1 0:df4d7c0a1594 33 void led2_thread(void const *argument)
4180_1 0:df4d7c0a1594 34 {
4180_1 0:df4d7c0a1594 35 led2 = 1;
4180_1 0:df4d7c0a1594 36 while (true) {
4180_1 0:df4d7c0a1594 37 led2 = !led2;
4180_1 0:df4d7c0a1594 38 Thread::wait(2000);
4180_1 0:df4d7c0a1594 39 }
4180_1 0:df4d7c0a1594 40 }
4180_1 0:df4d7c0a1594 41 void led3_thread(void const *argument)
4180_1 0:df4d7c0a1594 42 {
4180_1 0:df4d7c0a1594 43 led3 = 1;
4180_1 0:df4d7c0a1594 44 while (true) {
4180_1 0:df4d7c0a1594 45 led3 = !led3;
4180_1 0:df4d7c0a1594 46 Thread::wait(4000);
4180_1 0:df4d7c0a1594 47 }
4180_1 0:df4d7c0a1594 48 }
4180_1 0:df4d7c0a1594 49 void led4_thread(void const *argument)
4180_1 0:df4d7c0a1594 50 {
4180_1 0:df4d7c0a1594 51 TMP36 myTMP36(p20);
4180_1 0:df4d7c0a1594 52 led4 = 1;
4180_1 0:df4d7c0a1594 53 while (true) {
4180_1 0:df4d7c0a1594 54 led4 = !led4;
4180_1 0:df4d7c0a1594 55 Thread::wait(8000);
4180_1 0:df4d7c0a1594 56 }
4180_1 0:df4d7c0a1594 57 }
4180_1 0:df4d7c0a1594 58
4180_1 0:df4d7c0a1594 59 int main()
4180_1 0:df4d7c0a1594 60 {
4180_1 0:df4d7c0a1594 61 led1 = 1;
4180_1 0:df4d7c0a1594 62 // code to set time in extra credit option goes here
4180_1 0:df4d7c0a1594 63 //
4180_1 0:df4d7c0a1594 64 uLCD.baudrate(3000000); //jack up baud rate to max for fast display
4180_1 0:df4d7c0a1594 65 uLCD.text_width(2); //2x size text
4180_1 0:df4d7c0a1594 66 uLCD.text_height(2);
4180_1 0:df4d7c0a1594 67 // Create 3 new thread objects thread1, thread2, and thread3
4180_1 0:df4d7c0a1594 68 // The RTOS will immediately start running them
4180_1 0:df4d7c0a1594 69 Thread thread1(led2_thread);
4180_1 0:df4d7c0a1594 70 Thread thread2(led3_thread);
4180_1 0:df4d7c0a1594 71 Thread thread3(led4_thread);
4180_1 0:df4d7c0a1594 72 // Main continues to run and is actually the first thread.
4180_1 0:df4d7c0a1594 73 // So a total of four threads are running now.
4180_1 0:df4d7c0a1594 74 // Each thread blinks an LED, but at a different rate
4180_1 0:df4d7c0a1594 75 // because of the different values used in Thread::wait().
4180_1 0:df4d7c0a1594 76 //
4180_1 0:df4d7c0a1594 77 // Set time in seconds since Jan 1 1970 (Unix style)
4180_1 0:df4d7c0a1594 78 // must set time to start Real Time clock running
4180_1 0:df4d7c0a1594 79 set_time(1286729737);
4180_1 0:df4d7c0a1594 80 char buffer[12];
4180_1 0:df4d7c0a1594 81 time_t seconds;
4180_1 0:df4d7c0a1594 82 while (true) {
4180_1 0:df4d7c0a1594 83 // reads time structure
4180_1 0:df4d7c0a1594 84 seconds = time(NULL);
4180_1 0:df4d7c0a1594 85 // converts time structure to a string
4180_1 0:df4d7c0a1594 86 strftime(buffer, 12, "%T", localtime(&seconds));
4180_1 0:df4d7c0a1594 87 // print time HH:MM:SS
4180_1 0:df4d7c0a1594 88 uLCD.locate(0,2);
4180_1 0:df4d7c0a1594 89 uLCD.printf("%s\n\r", buffer);
4180_1 0:df4d7c0a1594 90 led1 = !led1;
4180_1 0:df4d7c0a1594 91 Thread::wait(1000);
4180_1 0:df4d7c0a1594 92 }
4180_1 0:df4d7c0a1594 93 }