ECE 4180 final project. Sound and Bluetooth activated coffee maker
Dependencies: mbed wave_player mbed-rtos C12832_lcd 4DGL-uLCD-SE LCD_fonts SDFileSystem
main.cpp@5:18b2796e4847, 2019-10-03 (annotated)
- Committer:
- nhummel3
- Date:
- Thu Oct 03 21:28:13 2019 +0000
- Revision:
- 5:18b2796e4847
- Parent:
- 4:79863d2ea5a0
- Child:
- 6:e4aba042979b
rtos attempt;
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" |
dreschpe | 0:f6a57b843f79 | 7 | #include "Small_6.h" |
dreschpe | 0:f6a57b843f79 | 8 | #include "Small_7.h" |
dreschpe | 0:f6a57b843f79 | 9 | #include "Arial_9.h" |
dreschpe | 0:f6a57b843f79 | 10 | #include "stdio.h" |
dreschpe | 1:1c6a9eaf55b5 | 11 | #include "C12832_lcd.h" |
nhummel3 | 5:18b2796e4847 | 12 | #include "sound.h" |
dreschpe | 0:f6a57b843f79 | 13 | |
nhummel3 | 5:18b2796e4847 | 14 | #define sample_freq 11025.0 |
nhummel3 | 5:18b2796e4847 | 15 | //get and set the frequency from wav conversion tool GUI |
nhummel3 | 5:18b2796e4847 | 16 | int i=0; |
4180_1 | 4:79863d2ea5a0 | 17 | |
nhummel3 | 5:18b2796e4847 | 18 | Ticker sampletick; |
dreschpe | 3:3ec443c0842a | 19 | C12832_LCD LCD; |
dreschpe | 1:1c6a9eaf55b5 | 20 | AnalogIn Pot1(p19); |
dreschpe | 1:1c6a9eaf55b5 | 21 | AnalogIn Pot2(p20); |
4180_1 | 4:79863d2ea5a0 | 22 | PwmOut Speaker(p26); |
4180_1 | 4:79863d2ea5a0 | 23 | PwmOut RGBLED_r(p23); |
4180_1 | 4:79863d2ea5a0 | 24 | PwmOut RGBLED_g(p24); |
4180_1 | 4:79863d2ea5a0 | 25 | PwmOut RGBLED_b(p25); |
4180_1 | 4:79863d2ea5a0 | 26 | DigitalIn joyfire(p14); |
4180_1 | 4:79863d2ea5a0 | 27 | BusIn joy(p15,p12,p13,p16); |
4180_1 | 4:79863d2ea5a0 | 28 | BusOut leds(LED1,LED2,LED3,LED4); |
dreschpe | 2:a69c8c5f5b03 | 29 | |
4180_1 | 4:79863d2ea5a0 | 30 | // mutex to make the lcd lib thread safe |
dreschpe | 1:1c6a9eaf55b5 | 31 | Mutex lcd_mutex; |
dreschpe | 0:f6a57b843f79 | 32 | |
nhummel3 | 5:18b2796e4847 | 33 | void audio_sample () |
dreschpe | 0:f6a57b843f79 | 34 | { |
dreschpe | 0:f6a57b843f79 | 35 | |
nhummel3 | 5:18b2796e4847 | 36 | Speaker = sound_data[i]/255.0;//scale to 0.0 to 1.0 for PWM |
nhummel3 | 5:18b2796e4847 | 37 | i++; |
nhummel3 | 5:18b2796e4847 | 38 | if (i>= NUM_ELEMENTS) { |
nhummel3 | 5:18b2796e4847 | 39 | i = 0; |
nhummel3 | 5:18b2796e4847 | 40 | sampletick.detach(); |
dreschpe | 0:f6a57b843f79 | 41 | } |
dreschpe | 0:f6a57b843f79 | 42 | } |
dreschpe | 0:f6a57b843f79 | 43 | |
nhummel3 | 5:18b2796e4847 | 44 | // |
nhummel3 | 5:18b2796e4847 | 45 | //// Thread 1 |
nhummel3 | 5:18b2796e4847 | 46 | //// print counter into first line and wait for 1 s |
nhummel3 | 5:18b2796e4847 | 47 | //void thread1(void const *args) |
nhummel3 | 5:18b2796e4847 | 48 | //{ |
nhummel3 | 5:18b2796e4847 | 49 | // int i; |
nhummel3 | 5:18b2796e4847 | 50 | // while(true) { // thread loop |
nhummel3 | 5:18b2796e4847 | 51 | // lcd_mutex.lock(); |
nhummel3 | 5:18b2796e4847 | 52 | // LCD.locate(0,0); |
nhummel3 | 5:18b2796e4847 | 53 | // LCD.set_font((unsigned char*) Small_6); |
nhummel3 | 5:18b2796e4847 | 54 | // LCD.printf("Thread1 count: %d",i); |
nhummel3 | 5:18b2796e4847 | 55 | // lcd_mutex.unlock(); |
nhummel3 | 5:18b2796e4847 | 56 | // i++; |
nhummel3 | 5:18b2796e4847 | 57 | // Thread::wait(1000); |
nhummel3 | 5:18b2796e4847 | 58 | // } |
nhummel3 | 5:18b2796e4847 | 59 | //} |
nhummel3 | 5:18b2796e4847 | 60 | // |
nhummel3 | 5:18b2796e4847 | 61 | //// Thread 2 |
nhummel3 | 5:18b2796e4847 | 62 | //// print counter into third line and wait for 0,5s |
nhummel3 | 5:18b2796e4847 | 63 | //void thread2(void const *args) |
nhummel3 | 5:18b2796e4847 | 64 | //{ |
nhummel3 | 5:18b2796e4847 | 65 | // int k; |
nhummel3 | 5:18b2796e4847 | 66 | // while(true) { // thread loop |
nhummel3 | 5:18b2796e4847 | 67 | // lcd_mutex.lock(); |
nhummel3 | 5:18b2796e4847 | 68 | // LCD.locate(0,20); |
nhummel3 | 5:18b2796e4847 | 69 | // LCD.set_font((unsigned char*) Arial_9); |
nhummel3 | 5:18b2796e4847 | 70 | // LCD.printf("Thread 2 count : %d",k); |
nhummel3 | 5:18b2796e4847 | 71 | // lcd_mutex.unlock(); |
nhummel3 | 5:18b2796e4847 | 72 | // k++; |
nhummel3 | 5:18b2796e4847 | 73 | // Thread::wait(500); // wait 0.5s |
nhummel3 | 5:18b2796e4847 | 74 | // } |
nhummel3 | 5:18b2796e4847 | 75 | //} |
nhummel3 | 5:18b2796e4847 | 76 | // |
nhummel3 | 5:18b2796e4847 | 77 | //// Thread 3 |
nhummel3 | 5:18b2796e4847 | 78 | //// print a sin function in a small window |
nhummel3 | 5:18b2796e4847 | 79 | //// the value of pot 1 changes the speed of the sine wave |
nhummel3 | 5:18b2796e4847 | 80 | //void thread3(void const *args) |
nhummel3 | 5:18b2796e4847 | 81 | //{ |
nhummel3 | 5:18b2796e4847 | 82 | // int i,k,v; |
nhummel3 | 5:18b2796e4847 | 83 | // double s,a; |
nhummel3 | 5:18b2796e4847 | 84 | // k = 1; |
nhummel3 | 5:18b2796e4847 | 85 | // lcd_mutex.lock(); |
nhummel3 | 5:18b2796e4847 | 86 | // LCD.rect(89,0,127,17,1); |
nhummel3 | 5:18b2796e4847 | 87 | // lcd_mutex.unlock(); |
nhummel3 | 5:18b2796e4847 | 88 | // while(true) { // thread loop |
nhummel3 | 5:18b2796e4847 | 89 | // v = Pot1.read_u16(); // get value of pot 1 |
nhummel3 | 5:18b2796e4847 | 90 | // lcd_mutex.lock(); |
nhummel3 | 5:18b2796e4847 | 91 | // for (i=90; i<127; i++) { |
nhummel3 | 5:18b2796e4847 | 92 | // s = 8 * sin((long double)(i+k) /5); // pixel to print |
nhummel3 | 5:18b2796e4847 | 93 | // a = 8 * sin((long double)(i+k-1) /5); // old pixel to erase |
nhummel3 | 5:18b2796e4847 | 94 | // LCD.pixel(i,9 + (int)a ,0); // erase pixel |
nhummel3 | 5:18b2796e4847 | 95 | // LCD.pixel(i,9 + (int)s ,1); // print pixel |
nhummel3 | 5:18b2796e4847 | 96 | // } |
nhummel3 | 5:18b2796e4847 | 97 | // LCD.copy_to_lcd(); // LCD.pixel does not update the lcd |
nhummel3 | 5:18b2796e4847 | 98 | // lcd_mutex.unlock(); |
nhummel3 | 5:18b2796e4847 | 99 | // k++; |
nhummel3 | 5:18b2796e4847 | 100 | // Thread::wait(v/100); // value of pot1 / 100 |
nhummel3 | 5:18b2796e4847 | 101 | // } |
nhummel3 | 5:18b2796e4847 | 102 | //} |
nhummel3 | 5:18b2796e4847 | 103 | // |
nhummel3 | 5:18b2796e4847 | 104 | //// Thread 4 |
nhummel3 | 5:18b2796e4847 | 105 | //// input pot 2 and change the contrast of LCD |
nhummel3 | 5:18b2796e4847 | 106 | //void thread4(void const *args) |
nhummel3 | 5:18b2796e4847 | 107 | //{ |
nhummel3 | 5:18b2796e4847 | 108 | // int k; |
nhummel3 | 5:18b2796e4847 | 109 | // while(true) { // thread loop |
nhummel3 | 5:18b2796e4847 | 110 | // k = Pot2.read_u16(); // get the value of poti 2 |
nhummel3 | 5:18b2796e4847 | 111 | // k = k >> 10; // need only 6 bits for contrast |
nhummel3 | 5:18b2796e4847 | 112 | // lcd_mutex.lock(); |
nhummel3 | 5:18b2796e4847 | 113 | // LCD.set_contrast(k); |
nhummel3 | 5:18b2796e4847 | 114 | // lcd_mutex.unlock(); |
nhummel3 | 5:18b2796e4847 | 115 | // Thread::wait(500); // wait 0.5s |
nhummel3 | 5:18b2796e4847 | 116 | // } |
nhummel3 | 5:18b2796e4847 | 117 | //} |
nhummel3 | 5:18b2796e4847 | 118 | //// Thread 5 |
nhummel3 | 5:18b2796e4847 | 119 | //// RGB LED |
nhummel3 | 5:18b2796e4847 | 120 | //void thread5(void const *args) |
nhummel3 | 5:18b2796e4847 | 121 | //{ |
nhummel3 | 5:18b2796e4847 | 122 | // while(true) { // thread loop |
nhummel3 | 5:18b2796e4847 | 123 | // RGBLED_r = 0.5 + (rand() % 11)/20.0; |
nhummel3 | 5:18b2796e4847 | 124 | // RGBLED_g = 0.5 + (rand() % 11)/20.0; |
nhummel3 | 5:18b2796e4847 | 125 | // RGBLED_b = 0.5 + (rand() % 11)/20.0; |
nhummel3 | 5:18b2796e4847 | 126 | // Thread::wait(1667); // wait 1.5s |
nhummel3 | 5:18b2796e4847 | 127 | // } |
nhummel3 | 5:18b2796e4847 | 128 | //} |
nhummel3 | 5:18b2796e4847 | 129 | //// Thread 6 |
nhummel3 | 5:18b2796e4847 | 130 | //// Speaker |
nhummel3 | 5:18b2796e4847 | 131 | //void thread6(void const *args) |
nhummel3 | 5:18b2796e4847 | 132 | //{ |
nhummel3 | 5:18b2796e4847 | 133 | // while(true) { |
nhummel3 | 5:18b2796e4847 | 134 | // //sampletick.attach(&audio_sample, 1.0 / sample_freq); |
nhummel3 | 5:18b2796e4847 | 135 | // /* |
nhummel3 | 5:18b2796e4847 | 136 | // // thread loop |
nhummel3 | 5:18b2796e4847 | 137 | // Speaker.period(1.0/800.0); |
nhummel3 | 5:18b2796e4847 | 138 | // Speaker = 0.01; |
nhummel3 | 5:18b2796e4847 | 139 | // Thread::wait(1000); // wait 1.0s |
nhummel3 | 5:18b2796e4847 | 140 | // Speaker.period(1.0/969.0); |
nhummel3 | 5:18b2796e4847 | 141 | // Speaker = 0.01; |
nhummel3 | 5:18b2796e4847 | 142 | // Thread::wait(1000); // wait 1.0s |
nhummel3 | 5:18b2796e4847 | 143 | // */ |
nhummel3 | 5:18b2796e4847 | 144 | // } |
nhummel3 | 5:18b2796e4847 | 145 | //} |
nhummel3 | 5:18b2796e4847 | 146 | // |
nhummel3 | 5:18b2796e4847 | 147 | //// Thread 7 |
nhummel3 | 5:18b2796e4847 | 148 | //// Joystick controls onboard mbed LEDs |
nhummel3 | 5:18b2796e4847 | 149 | //void thread7(void const *args) |
nhummel3 | 5:18b2796e4847 | 150 | //{ |
nhummel3 | 5:18b2796e4847 | 151 | // while(true) { // thread loop |
nhummel3 | 5:18b2796e4847 | 152 | // if (joyfire) { |
nhummel3 | 5:18b2796e4847 | 153 | // leds = 0xf; |
nhummel3 | 5:18b2796e4847 | 154 | // } else { |
nhummel3 | 5:18b2796e4847 | 155 | // leds = joy; |
nhummel3 | 5:18b2796e4847 | 156 | // } |
nhummel3 | 5:18b2796e4847 | 157 | // Thread::wait(200); // wait 0.25s |
nhummel3 | 5:18b2796e4847 | 158 | // } |
nhummel3 | 5:18b2796e4847 | 159 | //} |
dreschpe | 1:1c6a9eaf55b5 | 160 | |
dreschpe | 2:a69c8c5f5b03 | 161 | |
4180_1 | 4:79863d2ea5a0 | 162 | |
dreschpe | 0:f6a57b843f79 | 163 | int main() |
dreschpe | 0:f6a57b843f79 | 164 | { |
nhummel3 | 5:18b2796e4847 | 165 | //int j; |
nhummel3 | 5:18b2796e4847 | 166 | //LCD.cls(); |
nhummel3 | 5:18b2796e4847 | 167 | Speaker.period(1.0/250000.0); |
nhummel3 | 5:18b2796e4847 | 168 | //Thread t1(thread1); //start thread1 |
nhummel3 | 5:18b2796e4847 | 169 | //Thread t2(thread2); //start thread2 |
nhummel3 | 5:18b2796e4847 | 170 | //Thread t3(thread3); //start thread3 |
nhummel3 | 5:18b2796e4847 | 171 | //Thread t4(thread4); //start thread4 |
nhummel3 | 5:18b2796e4847 | 172 | //Thread t5(thread5); //start thread5 |
nhummel3 | 5:18b2796e4847 | 173 | //Thread t6(thread6); //start thread6 |
nhummel3 | 5:18b2796e4847 | 174 | //Thread t7(thread7); //start thread7 |
dreschpe | 0:f6a57b843f79 | 175 | |
dreschpe | 2:a69c8c5f5b03 | 176 | while(true) { // main is the next thread |
nhummel3 | 5:18b2796e4847 | 177 | //lcd_mutex.lock(); |
nhummel3 | 5:18b2796e4847 | 178 | |
nhummel3 | 5:18b2796e4847 | 179 | |
nhummel3 | 5:18b2796e4847 | 180 | |
nhummel3 | 5:18b2796e4847 | 181 | |
nhummel3 | 5:18b2796e4847 | 182 | sampletick.attach(&audio_sample, 1.0 / sample_freq); |
nhummel3 | 5:18b2796e4847 | 183 | |
nhummel3 | 5:18b2796e4847 | 184 | //LCD.locate(0,9); |
nhummel3 | 5:18b2796e4847 | 185 | //LCD.set_font((unsigned char*) Small_7); |
nhummel3 | 5:18b2796e4847 | 186 | //j = LCD.get_contrast(); // read the actual contrast |
nhummel3 | 5:18b2796e4847 | 187 | //LCD.printf("contrast : %d",j); |
nhummel3 | 5:18b2796e4847 | 188 | //lcd_mutex.unlock(); |
nhummel3 | 5:18b2796e4847 | 189 | // wait(1.0 / sample_freq); // wait 0.5s |
dreschpe | 0:f6a57b843f79 | 190 | } |
nhummel3 | 5:18b2796e4847 | 191 | } |