f
Dependencies: Speaker mbed wave_player mbed-rtos 4DGL-uLCD-SE LCD_fonts SDFileSystem_OldbutworkswithRTOS
main.cpp@5:5f393c6b02cb, 2019-02-21 (annotated)
- Committer:
- apaks180
- Date:
- Thu Feb 21 16:10:42 2019 +0000
- Revision:
- 5:5f393c6b02cb
- Parent:
- 4:79863d2ea5a0
- Child:
- 6:bb887c1a381b
f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 2:a69c8c5f5b03 | 1 | // example to test the mbed Lab Board lcd lib with the mbed rtos |
4180_1 | 4:79863d2ea5a0 | 2 | // Pot1 changes the contrast |
4180_1 | 4:79863d2ea5a0 | 3 | // Pot2 changes the speed of the sin wave |
dreschpe | 1:1c6a9eaf55b5 | 4 | |
dreschpe | 0:f6a57b843f79 | 5 | #include "mbed.h" |
dreschpe | 0:f6a57b843f79 | 6 | #include "rtos.h" |
apaks180 | 5:5f393c6b02cb | 7 | #include "uLCD_4DGL.h" |
dreschpe | 0:f6a57b843f79 | 8 | #include "stdio.h" |
apaks180 | 5:5f393c6b02cb | 9 | #include "Speaker.h" |
apaks180 | 5:5f393c6b02cb | 10 | #include "banner.h" |
apaks180 | 5:5f393c6b02cb | 11 | #include "SDFileSystem.h" |
apaks180 | 5:5f393c6b02cb | 12 | #include "wave_player.h" |
dreschpe | 0:f6a57b843f79 | 13 | |
apaks180 | 5:5f393c6b02cb | 14 | AnalogOut DACout(p18); |
apaks180 | 5:5f393c6b02cb | 15 | wave_player waver(&DACout); |
apaks180 | 5:5f393c6b02cb | 16 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
apaks180 | 5:5f393c6b02cb | 17 | uLCD_4DGL uLCD(p13,p14,p11); |
apaks180 | 5:5f393c6b02cb | 18 | //AnalogIn Pot1(p19); |
apaks180 | 5:5f393c6b02cb | 19 | //AnalogIn Pot2(p20); |
apaks180 | 5:5f393c6b02cb | 20 | //Speaker speaker(p26); |
4180_1 | 4:79863d2ea5a0 | 21 | PwmOut RGBLED_r(p23); |
4180_1 | 4:79863d2ea5a0 | 22 | PwmOut RGBLED_g(p24); |
4180_1 | 4:79863d2ea5a0 | 23 | PwmOut RGBLED_b(p25); |
apaks180 | 5:5f393c6b02cb | 24 | //DigitalIn joyfire(p14); |
apaks180 | 5:5f393c6b02cb | 25 | //BusIn joy(p15,p12,p13,p16); |
4180_1 | 4:79863d2ea5a0 | 26 | BusOut leds(LED1,LED2,LED3,LED4); |
dreschpe | 2:a69c8c5f5b03 | 27 | |
4180_1 | 4:79863d2ea5a0 | 28 | // mutex to make the lcd lib thread safe |
dreschpe | 1:1c6a9eaf55b5 | 29 | Mutex lcd_mutex; |
dreschpe | 0:f6a57b843f79 | 30 | |
apaks180 | 5:5f393c6b02cb | 31 | //// Thread 1 |
apaks180 | 5:5f393c6b02cb | 32 | //// print counter into first line and wait for 1 s |
dreschpe | 0:f6a57b843f79 | 33 | void thread1(void const *args) |
dreschpe | 0:f6a57b843f79 | 34 | { |
dreschpe | 0:f6a57b843f79 | 35 | int i; |
dreschpe | 0:f6a57b843f79 | 36 | while(true) { // thread loop |
dreschpe | 2:a69c8c5f5b03 | 37 | lcd_mutex.lock(); |
apaks180 | 5:5f393c6b02cb | 38 | uLCD.printf("Thread1 count: %d",i); |
dreschpe | 2:a69c8c5f5b03 | 39 | lcd_mutex.unlock(); |
dreschpe | 0:f6a57b843f79 | 40 | i++; |
dreschpe | 0:f6a57b843f79 | 41 | Thread::wait(1000); |
dreschpe | 0:f6a57b843f79 | 42 | } |
dreschpe | 0:f6a57b843f79 | 43 | } |
apaks180 | 5:5f393c6b02cb | 44 | // |
apaks180 | 5:5f393c6b02cb | 45 | //// Thread 2 |
apaks180 | 5:5f393c6b02cb | 46 | //// print counter into third line and wait for 0,5s |
apaks180 | 5:5f393c6b02cb | 47 | //void thread2(void const *args) |
apaks180 | 5:5f393c6b02cb | 48 | //{ |
apaks180 | 5:5f393c6b02cb | 49 | // int k; |
apaks180 | 5:5f393c6b02cb | 50 | // while(true) { // thread loop |
apaks180 | 5:5f393c6b02cb | 51 | // lcd_mutex.lock(); |
apaks180 | 5:5f393c6b02cb | 52 | // LCD.locate(0,20); |
apaks180 | 5:5f393c6b02cb | 53 | // LCD.set_font((unsigned char*) Arial_9); |
apaks180 | 5:5f393c6b02cb | 54 | // LCD.printf("Thread 2 count : %d",k); |
apaks180 | 5:5f393c6b02cb | 55 | // lcd_mutex.unlock(); |
apaks180 | 5:5f393c6b02cb | 56 | // k++; |
apaks180 | 5:5f393c6b02cb | 57 | // Thread::wait(500); // wait 0.5s |
apaks180 | 5:5f393c6b02cb | 58 | // } |
apaks180 | 5:5f393c6b02cb | 59 | //} |
apaks180 | 5:5f393c6b02cb | 60 | // |
apaks180 | 5:5f393c6b02cb | 61 | //// Thread 3 |
apaks180 | 5:5f393c6b02cb | 62 | //// print a sin function in a small window |
apaks180 | 5:5f393c6b02cb | 63 | //// the value of pot 1 changes the speed of the sine wave |
apaks180 | 5:5f393c6b02cb | 64 | //void thread3(void const *args) |
apaks180 | 5:5f393c6b02cb | 65 | //{ |
apaks180 | 5:5f393c6b02cb | 66 | // int i,k,v; |
apaks180 | 5:5f393c6b02cb | 67 | // double s,a; |
apaks180 | 5:5f393c6b02cb | 68 | // k = 1; |
apaks180 | 5:5f393c6b02cb | 69 | // lcd_mutex.lock(); |
apaks180 | 5:5f393c6b02cb | 70 | // LCD.rect(89,0,127,17,1); |
apaks180 | 5:5f393c6b02cb | 71 | // lcd_mutex.unlock(); |
apaks180 | 5:5f393c6b02cb | 72 | // while(true) { // thread loop |
apaks180 | 5:5f393c6b02cb | 73 | // v = Pot1.read_u16(); // get value of pot 1 |
apaks180 | 5:5f393c6b02cb | 74 | // lcd_mutex.lock(); |
apaks180 | 5:5f393c6b02cb | 75 | // for (i=90; i<127; i++) { |
apaks180 | 5:5f393c6b02cb | 76 | // s = 8 * sin((long double)(i+k) /5); // pixel to print |
apaks180 | 5:5f393c6b02cb | 77 | // a = 8 * sin((long double)(i+k-1) /5); // old pixel to erase |
apaks180 | 5:5f393c6b02cb | 78 | // LCD.pixel(i,9 + (int)a ,0); // erase pixel |
apaks180 | 5:5f393c6b02cb | 79 | // LCD.pixel(i,9 + (int)s ,1); // print pixel |
apaks180 | 5:5f393c6b02cb | 80 | // } |
apaks180 | 5:5f393c6b02cb | 81 | // LCD.copy_to_lcd(); // LCD.pixel does not update the lcd |
apaks180 | 5:5f393c6b02cb | 82 | // lcd_mutex.unlock(); |
apaks180 | 5:5f393c6b02cb | 83 | // k++; |
apaks180 | 5:5f393c6b02cb | 84 | // Thread::wait(v/100); // value of pot1 / 100 |
apaks180 | 5:5f393c6b02cb | 85 | // } |
apaks180 | 5:5f393c6b02cb | 86 | //} |
apaks180 | 5:5f393c6b02cb | 87 | // |
apaks180 | 5:5f393c6b02cb | 88 | //// Thread 4 |
apaks180 | 5:5f393c6b02cb | 89 | //// input pot 2 and change the contrast of LCD |
apaks180 | 5:5f393c6b02cb | 90 | //void thread4(void const *args) |
apaks180 | 5:5f393c6b02cb | 91 | //{ |
apaks180 | 5:5f393c6b02cb | 92 | // int k; |
apaks180 | 5:5f393c6b02cb | 93 | // while(true) { // thread loop |
apaks180 | 5:5f393c6b02cb | 94 | // k = Pot2.read_u16(); // get the value of poti 2 |
apaks180 | 5:5f393c6b02cb | 95 | // k = k >> 10; // need only 6 bits for contrast |
apaks180 | 5:5f393c6b02cb | 96 | // lcd_mutex.lock(); |
apaks180 | 5:5f393c6b02cb | 97 | // LCD.set_contrast(k); |
apaks180 | 5:5f393c6b02cb | 98 | // lcd_mutex.unlock(); |
apaks180 | 5:5f393c6b02cb | 99 | // Thread::wait(500); // wait 0.5s |
apaks180 | 5:5f393c6b02cb | 100 | // } |
apaks180 | 5:5f393c6b02cb | 101 | //} |
apaks180 | 5:5f393c6b02cb | 102 | //// Thread 5 |
apaks180 | 5:5f393c6b02cb | 103 | //// RGB LED |
apaks180 | 5:5f393c6b02cb | 104 | void thread5(void const *args) |
dreschpe | 0:f6a57b843f79 | 105 | { |
apaks180 | 5:5f393c6b02cb | 106 | int i = 0; |
apaks180 | 5:5f393c6b02cb | 107 | while(true) { |
apaks180 | 5:5f393c6b02cb | 108 | // thread loop |
apaks180 | 5:5f393c6b02cb | 109 | if(i==0){ |
apaks180 | 5:5f393c6b02cb | 110 | RGBLED_r = 1; |
apaks180 | 5:5f393c6b02cb | 111 | RGBLED_g = 0; |
apaks180 | 5:5f393c6b02cb | 112 | RGBLED_b = 0; |
apaks180 | 5:5f393c6b02cb | 113 | } |
apaks180 | 5:5f393c6b02cb | 114 | else if(i==1){ |
apaks180 | 5:5f393c6b02cb | 115 | RGBLED_r = 1; |
apaks180 | 5:5f393c6b02cb | 116 | RGBLED_g = 1; |
apaks180 | 5:5f393c6b02cb | 117 | RGBLED_b = 1; |
apaks180 | 5:5f393c6b02cb | 118 | } |
apaks180 | 5:5f393c6b02cb | 119 | else{ |
apaks180 | 5:5f393c6b02cb | 120 | RGBLED_r = 0; |
apaks180 | 5:5f393c6b02cb | 121 | RGBLED_g = 0; |
apaks180 | 5:5f393c6b02cb | 122 | RGBLED_b = 1; |
apaks180 | 5:5f393c6b02cb | 123 | } |
apaks180 | 5:5f393c6b02cb | 124 | i++; |
apaks180 | 5:5f393c6b02cb | 125 | i=i%3; |
apaks180 | 5:5f393c6b02cb | 126 | Thread::wait(673); // wait 1.5s |
dreschpe | 0:f6a57b843f79 | 127 | } |
dreschpe | 0:f6a57b843f79 | 128 | } |
apaks180 | 5:5f393c6b02cb | 129 | //// Thread 6 |
apaks180 | 5:5f393c6b02cb | 130 | //// Speaker |
apaks180 | 5:5f393c6b02cb | 131 | void thread6(void const *args) |
dreschpe | 1:1c6a9eaf55b5 | 132 | { |
apaks180 | 5:5f393c6b02cb | 133 | //while(true) { // thread loop |
apaks180 | 5:5f393c6b02cb | 134 | // speaker.PlayNote(311.1, 1, .1); |
apaks180 | 5:5f393c6b02cb | 135 | // Thread::wait(1000); // wait 1.0s |
apaks180 | 5:5f393c6b02cb | 136 | // speaker.PlayNote(0,1,0); |
apaks180 | 5:5f393c6b02cb | 137 | // Thread::wait(1000); |
apaks180 | 5:5f393c6b02cb | 138 | // } |
apaks180 | 5:5f393c6b02cb | 139 | FILE *wave_file; |
apaks180 | 5:5f393c6b02cb | 140 | printf("\n\n\nHello, wave world!\n"); |
apaks180 | 5:5f393c6b02cb | 141 | wave_file=fopen("/sd/banner2.wav","r"); |
apaks180 | 5:5f393c6b02cb | 142 | waver.play(wave_file); |
apaks180 | 5:5f393c6b02cb | 143 | fclose(wave_file); |
dreschpe | 1:1c6a9eaf55b5 | 144 | } |
dreschpe | 0:f6a57b843f79 | 145 | |
4180_1 | 4:79863d2ea5a0 | 146 | void thread7(void const *args) |
4180_1 | 4:79863d2ea5a0 | 147 | { |
apaks180 | 5:5f393c6b02cb | 148 | int i = 0; |
4180_1 | 4:79863d2ea5a0 | 149 | while(true) { // thread loop |
apaks180 | 5:5f393c6b02cb | 150 | i++; |
apaks180 | 5:5f393c6b02cb | 151 | printf("thread 1: %d\n", i); |
4180_1 | 4:79863d2ea5a0 | 152 | Thread::wait(200); // wait 0.25s |
4180_1 | 4:79863d2ea5a0 | 153 | } |
dreschpe | 1:1c6a9eaf55b5 | 154 | } |
dreschpe | 1:1c6a9eaf55b5 | 155 | |
dreschpe | 2:a69c8c5f5b03 | 156 | |
4180_1 | 4:79863d2ea5a0 | 157 | |
dreschpe | 0:f6a57b843f79 | 158 | int main() |
dreschpe | 0:f6a57b843f79 | 159 | { |
apaks180 | 5:5f393c6b02cb | 160 | // int j; |
apaks180 | 5:5f393c6b02cb | 161 | uLCD.cls(); |
4180_1 | 4:79863d2ea5a0 | 162 | |
dreschpe | 2:a69c8c5f5b03 | 163 | Thread t1(thread1); //start thread1 |
apaks180 | 5:5f393c6b02cb | 164 | //Thread t2(thread2); //start thread2 |
apaks180 | 5:5f393c6b02cb | 165 | //Thread t3(thread3); //start thread3 |
apaks180 | 5:5f393c6b02cb | 166 | //Thread t4(thread4); //start thread4 |
4180_1 | 4:79863d2ea5a0 | 167 | Thread t5(thread5); //start thread5 |
4180_1 | 4:79863d2ea5a0 | 168 | Thread t6(thread6); //start thread6 |
4180_1 | 4:79863d2ea5a0 | 169 | Thread t7(thread7); //start thread7 |
dreschpe | 0:f6a57b843f79 | 170 | |
dreschpe | 2:a69c8c5f5b03 | 171 | while(true) { // main is the next thread |
apaks180 | 5:5f393c6b02cb | 172 | // speaker.PlayNote(311.1, 1, .1); |
apaks180 | 5:5f393c6b02cb | 173 | //Thread::wait(1000); // wait 1.0s |
apaks180 | 5:5f393c6b02cb | 174 | // speaker.PlayNote(0,1,0); |
apaks180 | 5:5f393c6b02cb | 175 | //Thread::wait(1000); |
apaks180 | 5:5f393c6b02cb | 176 | // lcd_mutex.lock(); |
apaks180 | 5:5f393c6b02cb | 177 | // uLCD.cls(); |
apaks180 | 5:5f393c6b02cb | 178 | // uLCD.media_init(); |
apaks180 | 5:5f393c6b02cb | 179 | // uLCD.set_sector_address(0x001D, 0x4C42); |
apaks180 | 5:5f393c6b02cb | 180 | // uLCD.display_video(0,0); |
apaks180 | 5:5f393c6b02cb | 181 | // lcd_mutex.unlock(); |
apaks180 | 5:5f393c6b02cb | 182 | // Thread::wait(33); // wait 0.5s |
dreschpe | 0:f6a57b843f79 | 183 | } |
dreschpe | 0:f6a57b843f79 | 184 | } |