template
Dependencies: 4DGL-uLCD-SE EthernetInterface NTPClient SDFileSystem mbed-rtos mbed wave_player
Fork of 2036lab7_template by
main.cpp@1:2a0dea19d2ba, 2016-02-01 (annotated)
- Committer:
- ssong86
- Date:
- Mon Feb 01 06:41:04 2016 +0000
- Revision:
- 1:2a0dea19d2ba
- Parent:
- 0:df4d7c0a1594
template
Who changed what in which revision?
User | Revision | Line number | New 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" |
ssong86 | 1:2a0dea19d2ba | 10 | #include "Shiftbrite.h" |
4180_1 | 0:df4d7c0a1594 | 11 | // Setup four builtin leds for use by threads |
4180_1 | 0:df4d7c0a1594 | 12 | DigitalOut led1(LED1); |
4180_1 | 0:df4d7c0a1594 | 13 | DigitalOut led2(LED2); |
4180_1 | 0:df4d7c0a1594 | 14 | DigitalOut led3(LED3); |
4180_1 | 0:df4d7c0a1594 | 15 | DigitalOut led4(LED4); |
4180_1 | 0:df4d7c0a1594 | 16 | |
ssong86 | 1:2a0dea19d2ba | 17 | Shiftbrite myShiftbrite(p9, p10, p11, p12, p13);// ei li di n/c ci |
ssong86 | 1:2a0dea19d2ba | 18 | |
4180_1 | 0:df4d7c0a1594 | 19 | AnalogOut DACout(p18); // used to play sound on speaker |
4180_1 | 0:df4d7c0a1594 | 20 | |
4180_1 | 0:df4d7c0a1594 | 21 | //wave player plays a *.wav file to D/A and a PWM |
4180_1 | 0:df4d7c0a1594 | 22 | wave_player waver(&DACout); |
4180_1 | 0:df4d7c0a1594 | 23 | |
4180_1 | 0:df4d7c0a1594 | 24 | uLCD_4DGL uLCD(p28,p27,p29); // serial tx, serial rx, reset pin; |
4180_1 | 0:df4d7c0a1594 | 25 | |
4180_1 | 0:df4d7c0a1594 | 26 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup |
ssong86 | 1:2a0dea19d2ba | 27 | Mutex myMutex; |
4180_1 | 0:df4d7c0a1594 | 28 | |
4180_1 | 0:df4d7c0a1594 | 29 | // Setup function code for three new threads to run. |
4180_1 | 0:df4d7c0a1594 | 30 | // Put in a while loop so that threads run forever. |
4180_1 | 0:df4d7c0a1594 | 31 | // Thread::wait will force at least a "x" millisecond |
4180_1 | 0:df4d7c0a1594 | 32 | // wait before the thread runs again. During this delay |
4180_1 | 0:df4d7c0a1594 | 33 | // the other threads will run |
4180_1 | 0:df4d7c0a1594 | 34 | // DO NOT use wait() with the RTOS!!!!! |
4180_1 | 0:df4d7c0a1594 | 35 | // wait just burns processor time and no other threads run |
ssong86 | 1:2a0dea19d2ba | 36 | inline float random_number() |
ssong86 | 1:2a0dea19d2ba | 37 | { |
ssong86 | 1:2a0dea19d2ba | 38 | return (rand()/(float(RAND_MAX))); |
ssong86 | 1:2a0dea19d2ba | 39 | } |
ssong86 | 1:2a0dea19d2ba | 40 | |
ssong86 | 1:2a0dea19d2ba | 41 | |
4180_1 | 0:df4d7c0a1594 | 42 | void led2_thread(void const *argument) |
4180_1 | 0:df4d7c0a1594 | 43 | { |
4180_1 | 0:df4d7c0a1594 | 44 | led2 = 1; |
ssong86 | 1:2a0dea19d2ba | 45 | //myShiftbrite.write(50,50,0); |
4180_1 | 0:df4d7c0a1594 | 46 | while (true) { |
4180_1 | 0:df4d7c0a1594 | 47 | led2 = !led2; |
ssong86 | 1:2a0dea19d2ba | 48 | myShiftbrite.write(204,255,255); |
4180_1 | 0:df4d7c0a1594 | 49 | Thread::wait(2000); |
4180_1 | 0:df4d7c0a1594 | 50 | } |
4180_1 | 0:df4d7c0a1594 | 51 | } |
4180_1 | 0:df4d7c0a1594 | 52 | void led3_thread(void const *argument) |
4180_1 | 0:df4d7c0a1594 | 53 | { |
4180_1 | 0:df4d7c0a1594 | 54 | led3 = 1; |
ssong86 | 1:2a0dea19d2ba | 55 | FILE *wave_file; |
ssong86 | 1:2a0dea19d2ba | 56 | myShiftbrite.write(50,50,0); |
4180_1 | 0:df4d7c0a1594 | 57 | while (true) { |
ssong86 | 1:2a0dea19d2ba | 58 | led3 = !led3; |
ssong86 | 1:2a0dea19d2ba | 59 | myMutex.lock(); |
ssong86 | 1:2a0dea19d2ba | 60 | uLCD.printf("\n\n\nHello, wave world!\n"); |
ssong86 | 1:2a0dea19d2ba | 61 | myMutex.unlock(); |
ssong86 | 1:2a0dea19d2ba | 62 | |
ssong86 | 1:2a0dea19d2ba | 63 | wave_file=fopen("/sd/fire.wav","r"); |
ssong86 | 1:2a0dea19d2ba | 64 | waver.play(wave_file); |
ssong86 | 1:2a0dea19d2ba | 65 | fclose(wave_file); |
ssong86 | 1:2a0dea19d2ba | 66 | Thread::wait(4000); |
4180_1 | 0:df4d7c0a1594 | 67 | } |
4180_1 | 0:df4d7c0a1594 | 68 | } |
4180_1 | 0:df4d7c0a1594 | 69 | void led4_thread(void const *argument) |
4180_1 | 0:df4d7c0a1594 | 70 | { |
4180_1 | 0:df4d7c0a1594 | 71 | TMP36 myTMP36(p20); |
ssong86 | 1:2a0dea19d2ba | 72 | float tempC, tempF; |
4180_1 | 0:df4d7c0a1594 | 73 | led4 = 1; |
ssong86 | 1:2a0dea19d2ba | 74 | myShiftbrite.write(50,50,0); |
4180_1 | 0:df4d7c0a1594 | 75 | while (true) { |
4180_1 | 0:df4d7c0a1594 | 76 | led4 = !led4; |
ssong86 | 1:2a0dea19d2ba | 77 | tempC = myTMP36.read(); |
ssong86 | 1:2a0dea19d2ba | 78 | tempF = (9.0*tempC)/5.0 + 32.0; |
ssong86 | 1:2a0dea19d2ba | 79 | myMutex.lock(); |
ssong86 | 1:2a0dea19d2ba | 80 | uLCD.baudrate(3000000); //jack up baud rate to max for fast display |
ssong86 | 1:2a0dea19d2ba | 81 | //uLCD.text_width(2); //2x size text |
ssong86 | 1:2a0dea19d2ba | 82 | //uLCD.text_height(2); |
ssong86 | 1:2a0dea19d2ba | 83 | uLCD.locate(2,4); |
ssong86 | 1:2a0dea19d2ba | 84 | uLCD.printf("%5.2F F \n\r", tempF); |
ssong86 | 1:2a0dea19d2ba | 85 | myMutex.unlock(); |
ssong86 | 1:2a0dea19d2ba | 86 | Thread::wait(4000); |
ssong86 | 1:2a0dea19d2ba | 87 | } |
4180_1 | 0:df4d7c0a1594 | 88 | } |
4180_1 | 0:df4d7c0a1594 | 89 | |
ssong86 | 1:2a0dea19d2ba | 90 | |
4180_1 | 0:df4d7c0a1594 | 91 | int main() |
4180_1 | 0:df4d7c0a1594 | 92 | { |
4180_1 | 0:df4d7c0a1594 | 93 | led1 = 1; |
4180_1 | 0:df4d7c0a1594 | 94 | // code to set time in extra credit option goes here |
4180_1 | 0:df4d7c0a1594 | 95 | // |
ssong86 | 1:2a0dea19d2ba | 96 | |
ssong86 | 1:2a0dea19d2ba | 97 | //uLCD.baudrate(3000000); //jack up baud rate to max for fast display |
ssong86 | 1:2a0dea19d2ba | 98 | //uLCD.text_width(2); //2x size text |
ssong86 | 1:2a0dea19d2ba | 99 | //uLCD.text_height(2); |
ssong86 | 1:2a0dea19d2ba | 100 | |
4180_1 | 0:df4d7c0a1594 | 101 | // Create 3 new thread objects thread1, thread2, and thread3 |
4180_1 | 0:df4d7c0a1594 | 102 | // The RTOS will immediately start running them |
4180_1 | 0:df4d7c0a1594 | 103 | Thread thread1(led2_thread); |
4180_1 | 0:df4d7c0a1594 | 104 | Thread thread2(led3_thread); |
4180_1 | 0:df4d7c0a1594 | 105 | Thread thread3(led4_thread); |
4180_1 | 0:df4d7c0a1594 | 106 | // Main continues to run and is actually the first thread. |
4180_1 | 0:df4d7c0a1594 | 107 | // So a total of four threads are running now. |
4180_1 | 0:df4d7c0a1594 | 108 | // Each thread blinks an LED, but at a different rate |
4180_1 | 0:df4d7c0a1594 | 109 | // because of the different values used in Thread::wait(). |
4180_1 | 0:df4d7c0a1594 | 110 | // |
4180_1 | 0:df4d7c0a1594 | 111 | // Set time in seconds since Jan 1 1970 (Unix style) |
4180_1 | 0:df4d7c0a1594 | 112 | // must set time to start Real Time clock running |
4180_1 | 0:df4d7c0a1594 | 113 | set_time(1286729737); |
4180_1 | 0:df4d7c0a1594 | 114 | char buffer[12]; |
4180_1 | 0:df4d7c0a1594 | 115 | time_t seconds; |
4180_1 | 0:df4d7c0a1594 | 116 | while (true) { |
4180_1 | 0:df4d7c0a1594 | 117 | // reads time structure |
4180_1 | 0:df4d7c0a1594 | 118 | seconds = time(NULL); |
4180_1 | 0:df4d7c0a1594 | 119 | // converts time structure to a string |
4180_1 | 0:df4d7c0a1594 | 120 | strftime(buffer, 12, "%T", localtime(&seconds)); |
4180_1 | 0:df4d7c0a1594 | 121 | // print time HH:MM:SS |
ssong86 | 1:2a0dea19d2ba | 122 | myMutex.lock(); |
ssong86 | 1:2a0dea19d2ba | 123 | uLCD.locate(1,2); |
4180_1 | 0:df4d7c0a1594 | 124 | uLCD.printf("%s\n\r", buffer); |
ssong86 | 1:2a0dea19d2ba | 125 | myMutex.unlock(); |
ssong86 | 1:2a0dea19d2ba | 126 | // led1 = !led1; |
ssong86 | 1:2a0dea19d2ba | 127 | // Thread::wait(1000); |
ssong86 | 1:2a0dea19d2ba | 128 | |
ssong86 | 1:2a0dea19d2ba | 129 | while(1) |
ssong86 | 1:2a0dea19d2ba | 130 | { |
ssong86 | 1:2a0dea19d2ba | 131 | myShiftbrite.write(random_number()*255,random_number()*255,random_number()*255); |
ssong86 | 1:2a0dea19d2ba | 132 | wait(0.4); |
ssong86 | 1:2a0dea19d2ba | 133 | } |
4180_1 | 0:df4d7c0a1594 | 134 | } |
4180_1 | 0:df4d7c0a1594 | 135 | } |
ssong86 | 1:2a0dea19d2ba | 136 | |
ssong86 | 1:2a0dea19d2ba | 137 | |
ssong86 | 1:2a0dea19d2ba | 138 |