![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
4180 lab 5
Dependencies: mbed 4DGL-uLCD-SE RPCInterface
main.cpp@4:1ef667567942, 2020-02-27 (annotated)
- Committer:
- emilywilson
- Date:
- Thu Feb 27 12:57:44 2020 +0000
- Revision:
- 4:1ef667567942
- Parent:
- 3:94d9434576af
- Child:
- 5:b34ce166b697
add extra credit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:bdbd3d6fc5d5 | 1 | #include "mbed.h" |
emilywilson | 2:4845e2dae429 | 2 | #include "rtos.h" |
emilywilson | 3:94d9434576af | 3 | #include "uLCD_4DGL.h" |
emilywilson | 3:94d9434576af | 4 | #include "SDFileSystem.h" |
emilywilson | 3:94d9434576af | 5 | #include "wave_player.h" |
emilywilson | 4:1ef667567942 | 6 | #include "nav_switch.h" |
emilywilson | 3:94d9434576af | 7 | |
emilywilson | 2:4845e2dae429 | 8 | //#include "part2.h" |
emilywilson | 2:4845e2dae429 | 9 | //#include "part3.h" |
emilywilson | 2:4845e2dae429 | 10 | #include "part4_led.h" |
emilywilson | 2:4845e2dae429 | 11 | //#include "lidar_theremin.h" |
emilywilson | 2:4845e2dae429 | 12 | |
emilywilson | 2:4845e2dae429 | 13 | Serial pc(USBTX, USBRX); |
emilywilson | 4:1ef667567942 | 14 | //RawSerial dev(p9,p10); |
emilywilson | 2:4845e2dae429 | 15 | |
emilywilson | 2:4845e2dae429 | 16 | DigitalOut myled1(LED1); |
emilywilson | 3:94d9434576af | 17 | DigitalOut myled2(LED2); |
emilywilson | 3:94d9434576af | 18 | DigitalOut myled3(LED3); |
emilywilson | 3:94d9434576af | 19 | DigitalOut myled4(LED4); |
emilywilson | 2:4845e2dae429 | 20 | RGBLed led = RGBLed(p21, p22, p23); |
emilywilson | 4:1ef667567942 | 21 | |
emilywilson | 4:1ef667567942 | 22 | float p = 0.5; |
emilywilson | 4:1ef667567942 | 23 | Color colors[] = { _red(1.0), _pink(1.0), _orange(1.0), _yellow(1.0) }; |
emilywilson | 2:4845e2dae429 | 24 | |
emilywilson | 3:94d9434576af | 25 | uLCD_4DGL lcd(p28,p27,p30); |
emilywilson | 3:94d9434576af | 26 | |
emilywilson | 2:4845e2dae429 | 27 | Thread led_thread; |
emilywilson | 3:94d9434576af | 28 | Thread lcd_thread1; |
emilywilson | 3:94d9434576af | 29 | Thread lcd_thread2; |
emilywilson | 3:94d9434576af | 30 | Thread audio_thread; |
emilywilson | 2:4845e2dae429 | 31 | |
emilywilson | 4:1ef667567942 | 32 | //void changeColor() { |
emilywilson | 4:1ef667567942 | 33 | // while (dev.readable()) { |
emilywilson | 4:1ef667567942 | 34 | // char c = dev.getc(); |
emilywilson | 4:1ef667567942 | 35 | // |
emilywilson | 4:1ef667567942 | 36 | // if (c == 'r') { |
emilywilson | 4:1ef667567942 | 37 | // colors[0] = _red(p); |
emilywilson | 4:1ef667567942 | 38 | // colors[1] = _pink(p); |
emilywilson | 4:1ef667567942 | 39 | // colors[2] = _orange(p); |
emilywilson | 4:1ef667567942 | 40 | // colors[3] = _yellow(p); |
emilywilson | 4:1ef667567942 | 41 | // } else if (c == 'b') { |
emilywilson | 4:1ef667567942 | 42 | // colors[0] = _teal(p); |
emilywilson | 4:1ef667567942 | 43 | // colors[1] = _light_blue(p); |
emilywilson | 4:1ef667567942 | 44 | // colors[2] = _blue(p); |
emilywilson | 4:1ef667567942 | 45 | // colors[3] = _purple(p); |
emilywilson | 4:1ef667567942 | 46 | // } else if (c == 'g') { |
emilywilson | 4:1ef667567942 | 47 | // colors[0] = _yellow(p); |
emilywilson | 4:1ef667567942 | 48 | // colors[1] = _light_green(p); |
emilywilson | 4:1ef667567942 | 49 | // colors[2] = _green(p); |
emilywilson | 4:1ef667567942 | 50 | // colors[3] = _teal(p); |
emilywilson | 4:1ef667567942 | 51 | // } |
emilywilson | 4:1ef667567942 | 52 | // } |
emilywilson | 4:1ef667567942 | 53 | //} |
emilywilson | 2:4845e2dae429 | 54 | |
emilywilson | 2:4845e2dae429 | 55 | void led_handler() { |
emilywilson | 2:4845e2dae429 | 56 | Color c = colors[0]; |
emilywilson | 2:4845e2dae429 | 57 | int i = 0; |
emilywilson | 2:4845e2dae429 | 58 | while (1) { |
emilywilson | 4:1ef667567942 | 59 | led.write(c.red*p, c.green*p, c.blue*p); |
emilywilson | 2:4845e2dae429 | 60 | i = (i + 1) % 4; |
emilywilson | 2:4845e2dae429 | 61 | c = colors[i]; |
emilywilson | 2:4845e2dae429 | 62 | |
emilywilson | 2:4845e2dae429 | 63 | Thread::wait(500); |
emilywilson | 2:4845e2dae429 | 64 | } |
emilywilson | 2:4845e2dae429 | 65 | } |
emilywilson | 2:4845e2dae429 | 66 | |
emilywilson | 4:1ef667567942 | 67 | //Mutex lcd_mutex; |
emilywilson | 4:1ef667567942 | 68 | Semaphore sem(1); |
emilywilson | 3:94d9434576af | 69 | |
emilywilson | 3:94d9434576af | 70 | |
emilywilson | 3:94d9434576af | 71 | void lcd1_handler() { |
emilywilson | 3:94d9434576af | 72 | int i = 0; |
emilywilson | 3:94d9434576af | 73 | while (1) { |
emilywilson | 4:1ef667567942 | 74 | // lcd_mutex.lock(); |
emilywilson | 4:1ef667567942 | 75 | sem.wait(); |
emilywilson | 3:94d9434576af | 76 | lcd.locate(0, 0); |
emilywilson | 3:94d9434576af | 77 | lcd.printf("Counter: %d", i); |
emilywilson | 4:1ef667567942 | 78 | // lcd_mutex.unlock(); |
emilywilson | 4:1ef667567942 | 79 | sem.release(); |
emilywilson | 3:94d9434576af | 80 | |
emilywilson | 3:94d9434576af | 81 | i++; |
emilywilson | 3:94d9434576af | 82 | Thread::wait(1000); |
emilywilson | 3:94d9434576af | 83 | } |
emilywilson | 3:94d9434576af | 84 | } |
emilywilson | 3:94d9434576af | 85 | |
emilywilson | 3:94d9434576af | 86 | void lcd2_handler() { |
emilywilson | 3:94d9434576af | 87 | int i,k, offset; |
emilywilson | 3:94d9434576af | 88 | double s,a; |
emilywilson | 3:94d9434576af | 89 | k = 1; |
emilywilson | 3:94d9434576af | 90 | offset = 50; |
emilywilson | 4:1ef667567942 | 91 | // lcd_mutex.lock(); |
emilywilson | 4:1ef667567942 | 92 | sem.wait(); |
emilywilson | 3:94d9434576af | 93 | lcd.rectangle(89,0,127,17,1); |
emilywilson | 4:1ef667567942 | 94 | // lcd_mutex.unlock(); |
emilywilson | 4:1ef667567942 | 95 | sem.release(); |
emilywilson | 3:94d9434576af | 96 | |
emilywilson | 3:94d9434576af | 97 | while(1) { // thread loop |
emilywilson | 3:94d9434576af | 98 | // v = 1; |
emilywilson | 4:1ef667567942 | 99 | // lcd_mutex.lock(); |
emilywilson | 4:1ef667567942 | 100 | sem.wait(); |
emilywilson | 3:94d9434576af | 101 | for (i=0; i<127; i++) { |
emilywilson | 3:94d9434576af | 102 | s = 8 * sin((long double)(i+k) /5); // pixel to print |
emilywilson | 3:94d9434576af | 103 | a = 8 * sin((long double)(i+k-1) /5); // old pixel to erase |
emilywilson | 3:94d9434576af | 104 | lcd.pixel(i,offset + (int)a, BLACK); // erase pixel |
emilywilson | 3:94d9434576af | 105 | lcd.pixel(i,offset + (int)s, WHITE); // print pixel |
emilywilson | 3:94d9434576af | 106 | } |
emilywilson | 4:1ef667567942 | 107 | // lcd_mutex.unlock(); |
emilywilson | 4:1ef667567942 | 108 | sem.release(); |
emilywilson | 3:94d9434576af | 109 | k++; |
emilywilson | 3:94d9434576af | 110 | Thread::wait(100); // value of pot1 / 100 |
emilywilson | 3:94d9434576af | 111 | } |
emilywilson | 3:94d9434576af | 112 | } |
emilywilson | 3:94d9434576af | 113 | |
emilywilson | 3:94d9434576af | 114 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
emilywilson | 3:94d9434576af | 115 | AnalogOut DACout(p18); |
emilywilson | 3:94d9434576af | 116 | wave_player waver(&DACout); |
emilywilson | 4:1ef667567942 | 117 | bool play_audio = true; |
emilywilson | 3:94d9434576af | 118 | |
emilywilson | 3:94d9434576af | 119 | void audio_handler() { |
emilywilson | 3:94d9434576af | 120 | FILE *wave_file; |
emilywilson | 3:94d9434576af | 121 | myled2 = !myled2; |
emilywilson | 3:94d9434576af | 122 | // printf("\n\n\nHello, wave world!\n"); |
emilywilson | 3:94d9434576af | 123 | // myled4 = !myled4; |
emilywilson | 4:1ef667567942 | 124 | while (play_audio) { |
emilywilson | 3:94d9434576af | 125 | myled4 = !myled4; |
emilywilson | 3:94d9434576af | 126 | wave_file=fopen("/sd/sample.wav","r"); |
emilywilson | 3:94d9434576af | 127 | waver.play(wave_file); |
emilywilson | 3:94d9434576af | 128 | fclose(wave_file); |
emilywilson | 3:94d9434576af | 129 | Thread::wait(1000); |
emilywilson | 3:94d9434576af | 130 | } |
emilywilson | 3:94d9434576af | 131 | } |
emilywilson | 3:94d9434576af | 132 | |
emilywilson | 4:1ef667567942 | 133 | Nav_Switch nav(p15, p12, p16, p11, p17); |
emilywilson | 4:1ef667567942 | 134 | |
mbed_official | 0:bdbd3d6fc5d5 | 135 | int main() { |
emilywilson | 2:4845e2dae429 | 136 | // run_part2(); |
emilywilson | 2:4845e2dae429 | 137 | // run_part2_EC(); |
emilywilson | 2:4845e2dae429 | 138 | // run_lidar_theremin(); |
mbed_official | 0:bdbd3d6fc5d5 | 139 | |
emilywilson | 2:4845e2dae429 | 140 | // run_part3(); |
emilywilson | 3:94d9434576af | 141 | |
emilywilson | 3:94d9434576af | 142 | pc.baud(9600); |
emilywilson | 4:1ef667567942 | 143 | // dev.baud(9600); |
emilywilson | 2:4845e2dae429 | 144 | |
emilywilson | 2:4845e2dae429 | 145 | led_thread.start(led_handler); |
emilywilson | 3:94d9434576af | 146 | lcd.cls(); |
emilywilson | 3:94d9434576af | 147 | lcd_thread1.start(lcd1_handler); |
emilywilson | 3:94d9434576af | 148 | lcd_thread2.start(lcd2_handler); |
emilywilson | 3:94d9434576af | 149 | audio_thread.start(audio_handler); |
emilywilson | 3:94d9434576af | 150 | |
emilywilson | 4:1ef667567942 | 151 | // dev.attach(&changeColor, Serial::RxIrq); |
emilywilson | 2:4845e2dae429 | 152 | |
emilywilson | 2:4845e2dae429 | 153 | while (1) { |
emilywilson | 2:4845e2dae429 | 154 | myled1 = !myled1; |
emilywilson | 4:1ef667567942 | 155 | if (nav.fire()) { |
emilywilson | 4:1ef667567942 | 156 | play_audio = !play_audio; |
emilywilson | 4:1ef667567942 | 157 | } else if (nav.up() && p < 1.0f) { |
emilywilson | 4:1ef667567942 | 158 | p += 0.1f; |
emilywilson | 4:1ef667567942 | 159 | } else if (nav.down() && p > 0.0f) { |
emilywilson | 4:1ef667567942 | 160 | p -= 0.1f; |
emilywilson | 4:1ef667567942 | 161 | } else if (nav.left()) { |
emilywilson | 4:1ef667567942 | 162 | colors[0] = _red(1.0); |
emilywilson | 4:1ef667567942 | 163 | colors[1] = _pink(1.0); |
emilywilson | 4:1ef667567942 | 164 | colors[2] = _orange(1.0); |
emilywilson | 4:1ef667567942 | 165 | colors[3] = _yellow(1.0); |
emilywilson | 4:1ef667567942 | 166 | } else if (nav.right()) { |
emilywilson | 4:1ef667567942 | 167 | colors[0] = _teal(1.0); |
emilywilson | 4:1ef667567942 | 168 | colors[1] = _light_blue(1.0); |
emilywilson | 4:1ef667567942 | 169 | colors[2] = _blue(1.0); |
emilywilson | 4:1ef667567942 | 170 | colors[3] = _purple(1.0); |
emilywilson | 4:1ef667567942 | 171 | } |
emilywilson | 2:4845e2dae429 | 172 | |
emilywilson | 4:1ef667567942 | 173 | Thread::wait(100); |
mbed_official | 0:bdbd3d6fc5d5 | 174 | } |
emilywilson | 2:4845e2dae429 | 175 | } |