~

Dependencies:   mbed SimpleRGB mbed-rtos 4DGL-uLCD-SE SDFileSystem wave_player

Committer:
kswanson31
Date:
Tue Oct 11 03:42:42 2016 +0000
Revision:
9:d6343146d23e
Parent:
8:38419fb4e242
Completed sound thread (haven't tested), formatted other threads

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kswanson31 0:51d88b4e2b23 1 #include "mbed.h"
kswanson31 0:51d88b4e2b23 2 #include "rtos.h"
kswanson31 5:420cda7cbfc5 3 #include <string>
fkhan39 4:4792f0db029a 4 #include "uLCD_4DGL.h"
kswanson31 1:ff69d2d97a2c 5 #include "SimpleRGB.h"
kswanson31 9:d6343146d23e 6 #include "SDFileSystem.h"
kswanson31 9:d6343146d23e 7 #include "wave_player.h"
kswanson31 1:ff69d2d97a2c 8
kswanson31 1:ff69d2d97a2c 9 #define SECOND 1000 // used with threads
kswanson31 3:641770225c44 10 DigitalOut led(LED1); // test led
kswanson31 0:51d88b4e2b23 11
kswanson31 8:38419fb4e242 12
kswanson31 3:641770225c44 13 /* 1 init/declare
kswanson31 1:ff69d2d97a2c 14
kswanson31 0:51d88b4e2b23 15 Ticker first;
kswanson31 0:51d88b4e2b23 16 Ticker second;
kswanson31 0:51d88b4e2b23 17 Ticker third;
kswanson31 0:51d88b4e2b23 18 Ticker fourth;
kswanson31 0:51d88b4e2b23 19 DigitalOut led1(LED1);
kswanson31 0:51d88b4e2b23 20 DigitalOut led2(LED2);
kswanson31 0:51d88b4e2b23 21 DigitalOut led3(LED3);
kswanson31 0:51d88b4e2b23 22 DigitalOut led4(LED4);
kswanson31 0:51d88b4e2b23 23
kswanson31 0:51d88b4e2b23 24 void one() { // attached to ticker object. will be called after time given
kswanson31 0:51d88b4e2b23 25 led1 = !led1; // flip the value of the led
kswanson31 0:51d88b4e2b23 26 } // nothing returned
kswanson31 0:51d88b4e2b23 27
kswanson31 0:51d88b4e2b23 28 void two() {
kswanson31 0:51d88b4e2b23 29 led2 = !led2;
kswanson31 0:51d88b4e2b23 30 }
kswanson31 0:51d88b4e2b23 31
kswanson31 0:51d88b4e2b23 32 void three() {
kswanson31 0:51d88b4e2b23 33 led3 = !led3;
kswanson31 0:51d88b4e2b23 34 }
kswanson31 0:51d88b4e2b23 35
fkhan39 2:c64bfb83fe76 36 void four() {
kswanson31 0:51d88b4e2b23 37 led4 = !led4;
kswanson31 0:51d88b4e2b23 38 }
kswanson31 1:ff69d2d97a2c 39
kswanson31 3:641770225c44 40 */// - 1
kswanson31 0:51d88b4e2b23 41
kswanson31 1:ff69d2d97a2c 42
kswanson31 3:641770225c44 43 /* 2 init/declare */
kswanson31 1:ff69d2d97a2c 44
kswanson31 8:38419fb4e242 45 uLCD_4DGL lcd(p9, p10, p11); // lcd used by the following threads
kswanson31 8:38419fb4e242 46 Mutex stdio_mutex; // mutex lock, ensure safe use of lcd
kswanson31 8:38419fb4e242 47
kswanson31 1:ff69d2d97a2c 48 // lcd thread
kswanson31 0:51d88b4e2b23 49 void uLCD_reboot_thread(void const *args) {
kswanson31 0:51d88b4e2b23 50 // show reboot process, associated with clip of apple restart
kswanson31 1:ff69d2d97a2c 51 while (true) {
kswanson31 0:51d88b4e2b23 52 // lock, print 'Rebooting!' to uLCD with "progress_bar" underneath
fkhan39 6:648cd6a6fae2 53 stdio_mutex.lock();
fkhan39 4:4792f0db029a 54 lcd.text_string("Rebooting", 2, 5, FONT_7X8, WHITE);
kswanson31 0:51d88b4e2b23 55 // draw progress bar 8 sec
fkhan39 4:4792f0db029a 56 for (int i = 0; i < 8; i++) {
kswanson31 5:420cda7cbfc5 57 lcd.text_string("|", 2 + i, 6, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 58 // progress[i+1] += arr[i];
kswanson31 5:420cda7cbfc5 59 Thread::wait(SECOND);
fkhan39 4:4792f0db029a 60 }
kswanson31 0:51d88b4e2b23 61 // draw an ascii 'apple'
kswanson31 5:420cda7cbfc5 62 lcd.text_string(" .:' ", 2, 4, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 63 lcd.text_string(" :' ", 2, 5, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 64 lcd.text_string(" .'` `-' ``. ", 2, 6, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 65 lcd.text_string(": .-' ", 2, 7, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 66 lcd.text_string(": : ", 2, 8, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 67 lcd.text_string(" : `-; ", 2, 9, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 68 lcd.text_string(" `.__.-.__.' ", 2, 10, FONT_7X8, WHITE);
kswanson31 0:51d88b4e2b23 69 // unlock, wait for 52
fkhan39 6:648cd6a6fae2 70 stdio_mutex.unlock();
kswanson31 5:420cda7cbfc5 71 Thread::wait(52 * SECOND);
kswanson31 5:420cda7cbfc5 72 // clear screen
kswanson31 8:38419fb4e242 73 stdio_mutex.lock();
kswanson31 5:420cda7cbfc5 74 lcd.cls();
kswanson31 8:38419fb4e242 75 stdio_mutex.unlock();
kswanson31 0:51d88b4e2b23 76 }
kswanson31 0:51d88b4e2b23 77 }
kswanson31 0:51d88b4e2b23 78
kswanson31 1:ff69d2d97a2c 79 // lcd thread
kswanson31 0:51d88b4e2b23 80 void uLCD_status_thread(void const *args) {
kswanson31 0:51d88b4e2b23 81 // show time with smile, frown, sad face at top right
fkhan39 4:4792f0db029a 82 set_time(1476146921);
kswanson31 1:ff69d2d97a2c 83 while (true) {
kswanson31 5:420cda7cbfc5 84 char buffer[32];
fkhan39 4:4792f0db029a 85 time_t seconds = time(NULL);
fkhan39 4:4792f0db029a 86 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
fkhan39 6:648cd6a6fae2 87 // lock, update time at top right with smiley at top left
fkhan39 6:648cd6a6fae2 88 stdio_mutex.lock();
kswanson31 9:d6343146d23e 89 lcd.text_string(buffer, 8, 1, FONT_7X8, BLUE);
fkhan39 4:4792f0db029a 90 lcd.text_string(":-)", 1, 1, FONT_7X8, WHITE);
kswanson31 0:51d88b4e2b23 91 // unlock, wait for 20 sec
fkhan39 6:648cd6a6fae2 92 stdio_mutex.unlock();
kswanson31 5:420cda7cbfc5 93 Thread::wait(20 * SECOND);
fkhan39 4:4792f0db029a 94 // lock, change to frown
fkhan39 6:648cd6a6fae2 95 stdio_mutex.lock();
fkhan39 4:4792f0db029a 96 lcd.text_string(">:|", 1, 1, FONT_7X8, WHITE);
fkhan39 4:4792f0db029a 97 // unlock, wait for 20 sec
fkhan39 6:648cd6a6fae2 98 stdio_mutex.unlock();
kswanson31 5:420cda7cbfc5 99 Thread::wait(20 * SECOND);
kswanson31 0:51d88b4e2b23 100 // lock, change to sad
fkhan39 6:648cd6a6fae2 101 stdio_mutex.lock();
fkhan39 4:4792f0db029a 102 lcd.text_string(":-(", 1, 1, FONT_7X8, WHITE);
kswanson31 0:51d88b4e2b23 103 // unlock, wait for 20 sec
fkhan39 6:648cd6a6fae2 104 stdio_mutex.unlock();
kswanson31 8:38419fb4e242 105 Thread::wait(20 * SECOND);
kswanson31 0:51d88b4e2b23 106 }
kswanson31 0:51d88b4e2b23 107 }
kswanson31 0:51d88b4e2b23 108
kswanson31 8:38419fb4e242 109
kswanson31 8:38419fb4e242 110 /* extra credit - video */
fkhan39 7:f3eb2b2c295d 111 void uLCD_video_thread(void const *args) {
kswanson31 8:38419fb4e242 112 lcd.media_init();
kswanson31 8:38419fb4e242 113 lcd.set_sector_address(0x001D, 0x4C42);
kswanson31 8:38419fb4e242 114 lcd.display_video(0,0);
fkhan39 7:f3eb2b2c295d 115 }
kswanson31 8:38419fb4e242 116 // - extra
kswanson31 8:38419fb4e242 117
fkhan39 7:f3eb2b2c295d 118
kswanson31 1:ff69d2d97a2c 119 RGBLed rgbLED(p21, p22, p23); // object to use the RGB LED
kswanson31 8:38419fb4e242 120 const LightColor green(0.0, 1.0, 0.0);
kswanson31 8:38419fb4e242 121 const LightColor yellow(1.0, 1.0, 0.0);
kswanson31 8:38419fb4e242 122 const LightColor orange(1.0, 0.6, 0.0);
kswanson31 1:ff69d2d97a2c 123 const LightColor red(1.0,0.0,0.0);
kswanson31 1:ff69d2d97a2c 124 const LightColor violet(1.0, 0.0, 0.8);
kswanson31 1:ff69d2d97a2c 125 const LightColor blue(0.0, 0.0, 1.0);
kswanson31 1:ff69d2d97a2c 126
kswanson31 1:ff69d2d97a2c 127 // rgb led thread
kswanson31 0:51d88b4e2b23 128 void rainbow_led_thread(void const *args) {
kswanson31 1:ff69d2d97a2c 129 // reboot with white (6 sec), then cycle through apple colors (54 sec)
kswanson31 1:ff69d2d97a2c 130 while (true) {
kswanson31 1:ff69d2d97a2c 131 rgbLED.write(1.0, 1.0, 1.0); // bright white
kswanson31 1:ff69d2d97a2c 132 Thread::wait(2 * SECOND);
kswanson31 1:ff69d2d97a2c 133 // dim to nothing over 4 seconds
kswanson31 1:ff69d2d97a2c 134 for (float c = 1.0; c > 0.0; c = c - 0.01) {
kswanson31 1:ff69d2d97a2c 135 rgbLED.write(c, c, c);
kswanson31 1:ff69d2d97a2c 136 Thread::wait(40); // 40 * 100 = 4000 = 4 secs
kswanson31 1:ff69d2d97a2c 137 }
kswanson31 1:ff69d2d97a2c 138 // rotate through other colors over 54 seconds
kswanson31 1:ff69d2d97a2c 139 rgbLED = green;
kswanson31 1:ff69d2d97a2c 140 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 141 rgbLED = yellow;
kswanson31 1:ff69d2d97a2c 142 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 143 rgbLED = orange;
kswanson31 1:ff69d2d97a2c 144 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 145 rgbLED = red;
kswanson31 1:ff69d2d97a2c 146 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 147 rgbLED = violet;
kswanson31 1:ff69d2d97a2c 148 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 149 rgbLED = blue;
kswanson31 1:ff69d2d97a2c 150 Thread::wait(9 * SECOND);
kswanson31 0:51d88b4e2b23 151 }
kswanson31 0:51d88b4e2b23 152 }
kswanson31 9:d6343146d23e 153
kswanson31 9:d6343146d23e 154 SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card with .wav files
kswanson31 9:d6343146d23e 155 AnalogOut DACout(p18); // DAC for producing sound
kswanson31 9:d6343146d23e 156 wave_player waver(&DACout); // wavplayer object
kswanson31 3:641770225c44 157 // - 2
kswanson31 0:51d88b4e2b23 158
kswanson31 1:ff69d2d97a2c 159
kswanson31 0:51d88b4e2b23 160 int main() {
kswanson31 3:641770225c44 161 /* 1 calls
kswanson31 0:51d88b4e2b23 162 led1 = 1, led2 = 1, led3 = 1, led4 = 1;
kswanson31 0:51d88b4e2b23 163
kswanson31 0:51d88b4e2b23 164 first.attach(&one, 1.0); // attach the function and when to call it
kswanson31 0:51d88b4e2b23 165 second.attach(&two, 2.0);
kswanson31 0:51d88b4e2b23 166 third.attach(&three, 3.0);
kswanson31 0:51d88b4e2b23 167 fourth.attach(&four, 4.0);
kswanson31 3:641770225c44 168 */// - 1
kswanson31 0:51d88b4e2b23 169
kswanson31 1:ff69d2d97a2c 170
kswanson31 3:641770225c44 171 /* 2 calls */
kswanson31 5:420cda7cbfc5 172 Thread thread2(uLCD_reboot_thread);
kswanson31 8:38419fb4e242 173 Thread thread3(uLCD_status_thread);
kswanson31 9:d6343146d23e 174 Thread thread4(rainbow_led_thread);
fkhan39 7:f3eb2b2c295d 175 //Thread thread5(uLCD_video_thread);
kswanson31 9:d6343146d23e 176
kswanson31 9:d6343146d23e 177 FILE *wave_file;
kswanson31 9:d6343146d23e 178
kswanson31 3:641770225c44 179 // - 2
kswanson31 1:ff69d2d97a2c 180
kswanson31 1:ff69d2d97a2c 181
kswanson31 3:641770225c44 182 led = 1;
kswanson31 3:641770225c44 183
kswanson31 1:ff69d2d97a2c 184 while(true) {
kswanson31 9:d6343146d23e 185
kswanson31 9:d6343146d23e 186 /* 2 speaker 'thread' calls */
kswanson31 0:51d88b4e2b23 187 // speaker plays mac reboot sound, error sound occasionally
kswanson31 0:51d88b4e2b23 188 // play reboot sound
kswanson31 9:d6343146d23e 189 wave_file = fopen("/sd/reboot.wav","r");
kswanson31 9:d6343146d23e 190 waver.play(wave_file);
kswanson31 9:d6343146d23e 191 fclose(wave_file);
kswanson31 0:51d88b4e2b23 192 // wait 20
kswanson31 9:d6343146d23e 193 Thread::wait(20 * SECOND);
kswanson31 9:d6343146d23e 194 // error
kswanson31 9:d6343146d23e 195 wave_file = fopen("/sd/error.wav","r");
kswanson31 9:d6343146d23e 196 waver.play(wave_file);
kswanson31 9:d6343146d23e 197 fclose(wave_file);
kswanson31 9:d6343146d23e 198 // wait 20
kswanson31 9:d6343146d23e 199 Thread::wait(20 * SECOND);
kswanson31 9:d6343146d23e 200 // error
kswanson31 9:d6343146d23e 201 waver.play(wave_file);
kswanson31 9:d6343146d23e 202 fclose(wave_file);
kswanson31 9:d6343146d23e 203 // wait 20
kswanson31 9:d6343146d23e 204 Thread::wait(20 * SECOND);
kswanson31 9:d6343146d23e 205 // - 2
kswanson31 0:51d88b4e2b23 206 } // interupts will occur automatically
kswanson31 0:51d88b4e2b23 207 }