template
Dependencies: 4DGL-uLCD-SE EthernetInterface NTPClient SDFileSystem mbed-rtos mbed wave_player
Fork of 2036lab7_template by
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 #include "Shiftbrite.h" 00011 // Setup four builtin leds for use by threads 00012 DigitalOut led1(LED1); 00013 DigitalOut led2(LED2); 00014 DigitalOut led3(LED3); 00015 DigitalOut led4(LED4); 00016 00017 Shiftbrite myShiftbrite(p9, p10, p11, p12, p13);// ei li di n/c ci 00018 00019 AnalogOut DACout(p18); // used to play sound on speaker 00020 00021 //wave player plays a *.wav file to D/A and a PWM 00022 wave_player waver(&DACout); 00023 00024 uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin; 00025 00026 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup 00027 Mutex myMutex; 00028 00029 // Setup function code for three new threads to run. 00030 // Put in a while loop so that threads run forever. 00031 // Thread::wait will force at least a "x" millisecond 00032 // wait before the thread runs again. During this delay 00033 // the other threads will run 00034 // DO NOT use wait() with the RTOS!!!!! 00035 // wait just burns processor time and no other threads run 00036 inline float random_number() 00037 { 00038 return (rand()/(float(RAND_MAX))); 00039 } 00040 00041 00042 void led2_thread(void const *argument) 00043 { 00044 led2 = 1; 00045 //myShiftbrite.write(50,50,0); 00046 while (true) { 00047 led2 = !led2; 00048 myShiftbrite.write(204,255,255); 00049 Thread::wait(2000); 00050 } 00051 } 00052 void led3_thread(void const *argument) 00053 { 00054 led3 = 1; 00055 FILE *wave_file; 00056 myShiftbrite.write(50,50,0); 00057 while (true) { 00058 led3 = !led3; 00059 myMutex.lock(); 00060 uLCD.printf("\n\n\nHello, wave world!\n"); 00061 myMutex.unlock(); 00062 00063 wave_file=fopen("/sd/fire.wav","r"); 00064 waver.play(wave_file); 00065 fclose(wave_file); 00066 Thread::wait(4000); 00067 } 00068 } 00069 void led4_thread(void const *argument) 00070 { 00071 TMP36 myTMP36(p20); 00072 float tempC, tempF; 00073 led4 = 1; 00074 myShiftbrite.write(50,50,0); 00075 while (true) { 00076 led4 = !led4; 00077 tempC = myTMP36.read(); 00078 tempF = (9.0*tempC)/5.0 + 32.0; 00079 myMutex.lock(); 00080 uLCD.baudrate(3000000); //jack up baud rate to max for fast display 00081 //uLCD.text_width(2); //2x size text 00082 //uLCD.text_height(2); 00083 uLCD.locate(2,4); 00084 uLCD.printf("%5.2F F \n\r", tempF); 00085 myMutex.unlock(); 00086 Thread::wait(4000); 00087 } 00088 } 00089 00090 00091 int main() 00092 { 00093 led1 = 1; 00094 // code to set time in extra credit option goes here 00095 // 00096 00097 //uLCD.baudrate(3000000); //jack up baud rate to max for fast display 00098 //uLCD.text_width(2); //2x size text 00099 //uLCD.text_height(2); 00100 00101 // Create 3 new thread objects thread1, thread2, and thread3 00102 // The RTOS will immediately start running them 00103 Thread thread1(led2_thread); 00104 Thread thread2(led3_thread); 00105 Thread thread3(led4_thread); 00106 // Main continues to run and is actually the first thread. 00107 // So a total of four threads are running now. 00108 // Each thread blinks an LED, but at a different rate 00109 // because of the different values used in Thread::wait(). 00110 // 00111 // Set time in seconds since Jan 1 1970 (Unix style) 00112 // must set time to start Real Time clock running 00113 set_time(1286729737); 00114 char buffer[12]; 00115 time_t seconds; 00116 while (true) { 00117 // reads time structure 00118 seconds = time(NULL); 00119 // converts time structure to a string 00120 strftime(buffer, 12, "%T", localtime(&seconds)); 00121 // print time HH:MM:SS 00122 myMutex.lock(); 00123 uLCD.locate(1,2); 00124 uLCD.printf("%s\n\r", buffer); 00125 myMutex.unlock(); 00126 // led1 = !led1; 00127 // Thread::wait(1000); 00128 00129 while(1) 00130 { 00131 myShiftbrite.write(random_number()*255,random_number()*255,random_number()*255); 00132 wait(0.4); 00133 } 00134 } 00135 } 00136 00137 00138
Generated on Wed Jul 13 2022 02:00:57 by 1.7.2