![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
t
Dependencies: mbed wave_player mbed-rtos C12832_lcd 4DGL-uLCD-SE LCD_fonts SDFileSystem
main.cpp@0:4f77ae831ee7, 2019-12-03 (annotated)
- Committer:
- yqin70
- Date:
- Tue Dec 03 21:24:00 2019 +0000
- Revision:
- 0:4f77ae831ee7
- Child:
- 1:a1aa9a79070a
kk
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yqin70 | 0:4f77ae831ee7 | 1 | // example to test the mbed Lab Board lcd lib with the mbed rtos |
yqin70 | 0:4f77ae831ee7 | 2 | // Pot1 changes the contrast |
yqin70 | 0:4f77ae831ee7 | 3 | // Pot2 changes the speed of the sin wave |
yqin70 | 0:4f77ae831ee7 | 4 | #include "mbed.h" |
yqin70 | 0:4f77ae831ee7 | 5 | #include "SDFileSystem.h" |
yqin70 | 0:4f77ae831ee7 | 6 | #include "wave_player.h" |
yqin70 | 0:4f77ae831ee7 | 7 | #include "uLCD_4DGL.h" |
yqin70 | 0:4f77ae831ee7 | 8 | //setup some color objects in flash using const's |
yqin70 | 0:4f77ae831ee7 | 9 | |
yqin70 | 0:4f77ae831ee7 | 10 | #include "rtos.h" |
yqin70 | 0:4f77ae831ee7 | 11 | #include "Small_6.h" |
yqin70 | 0:4f77ae831ee7 | 12 | #include "Small_7.h" |
yqin70 | 0:4f77ae831ee7 | 13 | #include "Arial_9.h" |
yqin70 | 0:4f77ae831ee7 | 14 | #include "stdio.h" |
yqin70 | 0:4f77ae831ee7 | 15 | #include "C12832_lcd.h" |
yqin70 | 0:4f77ae831ee7 | 16 | |
yqin70 | 0:4f77ae831ee7 | 17 | //class for 3 PWM color values for RGBLED |
yqin70 | 0:4f77ae831ee7 | 18 | class LEDColor |
yqin70 | 0:4f77ae831ee7 | 19 | { |
yqin70 | 0:4f77ae831ee7 | 20 | public: |
yqin70 | 0:4f77ae831ee7 | 21 | LEDColor(float r, float g, float b); |
yqin70 | 0:4f77ae831ee7 | 22 | float red; |
yqin70 | 0:4f77ae831ee7 | 23 | float green; |
yqin70 | 0:4f77ae831ee7 | 24 | float blue; |
yqin70 | 0:4f77ae831ee7 | 25 | }; |
yqin70 | 0:4f77ae831ee7 | 26 | LEDColor:: LEDColor(float r, float g, float b) |
yqin70 | 0:4f77ae831ee7 | 27 | : red(r), green(g), blue(b) |
yqin70 | 0:4f77ae831ee7 | 28 | { |
yqin70 | 0:4f77ae831ee7 | 29 | } |
yqin70 | 0:4f77ae831ee7 | 30 | //Operator overload to adjust brightness with no color change |
yqin70 | 0:4f77ae831ee7 | 31 | LEDColor operator * (const LEDColor& x, const float& b) |
yqin70 | 0:4f77ae831ee7 | 32 | { |
yqin70 | 0:4f77ae831ee7 | 33 | return LEDColor(x.red*b,x.green*b,x.blue*b); |
yqin70 | 0:4f77ae831ee7 | 34 | } |
yqin70 | 0:4f77ae831ee7 | 35 | //Operator overload to add colors |
yqin70 | 0:4f77ae831ee7 | 36 | LEDColor operator + (const LEDColor& x, const LEDColor& y) |
yqin70 | 0:4f77ae831ee7 | 37 | { |
yqin70 | 0:4f77ae831ee7 | 38 | return LEDColor(x.red+y.red,x.green+y.green,x.blue+y.blue); |
yqin70 | 0:4f77ae831ee7 | 39 | } |
yqin70 | 0:4f77ae831ee7 | 40 | |
yqin70 | 0:4f77ae831ee7 | 41 | //Class to control an RGB LED using three PWM pins |
yqin70 | 0:4f77ae831ee7 | 42 | class RGBLed |
yqin70 | 0:4f77ae831ee7 | 43 | { |
yqin70 | 0:4f77ae831ee7 | 44 | public: |
yqin70 | 0:4f77ae831ee7 | 45 | RGBLed(PinName redpin, PinName greenpin, PinName bluepin); |
yqin70 | 0:4f77ae831ee7 | 46 | void write(float red,float green, float blue); |
yqin70 | 0:4f77ae831ee7 | 47 | void write(LEDColor c); |
yqin70 | 0:4f77ae831ee7 | 48 | RGBLed operator = (LEDColor c) { |
yqin70 | 0:4f77ae831ee7 | 49 | write(c); |
yqin70 | 0:4f77ae831ee7 | 50 | return *this; |
yqin70 | 0:4f77ae831ee7 | 51 | }; |
yqin70 | 0:4f77ae831ee7 | 52 | private: |
yqin70 | 0:4f77ae831ee7 | 53 | PwmOut _redpin; |
yqin70 | 0:4f77ae831ee7 | 54 | PwmOut _greenpin; |
yqin70 | 0:4f77ae831ee7 | 55 | PwmOut _bluepin; |
yqin70 | 0:4f77ae831ee7 | 56 | }; |
yqin70 | 0:4f77ae831ee7 | 57 | |
yqin70 | 0:4f77ae831ee7 | 58 | RGBLed::RGBLed (PinName redpin, PinName greenpin, PinName bluepin) |
yqin70 | 0:4f77ae831ee7 | 59 | : _redpin(redpin), _greenpin(greenpin), _bluepin(bluepin) |
yqin70 | 0:4f77ae831ee7 | 60 | { |
yqin70 | 0:4f77ae831ee7 | 61 | //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker) |
yqin70 | 0:4f77ae831ee7 | 62 | _redpin.period(0.0005); |
yqin70 | 0:4f77ae831ee7 | 63 | } |
yqin70 | 0:4f77ae831ee7 | 64 | |
yqin70 | 0:4f77ae831ee7 | 65 | void RGBLed::write(float red,float green, float blue) |
yqin70 | 0:4f77ae831ee7 | 66 | { |
yqin70 | 0:4f77ae831ee7 | 67 | _redpin = red; |
yqin70 | 0:4f77ae831ee7 | 68 | _greenpin = green; |
yqin70 | 0:4f77ae831ee7 | 69 | _bluepin = blue; |
yqin70 | 0:4f77ae831ee7 | 70 | } |
yqin70 | 0:4f77ae831ee7 | 71 | void RGBLed::write(LEDColor c) |
yqin70 | 0:4f77ae831ee7 | 72 | { |
yqin70 | 0:4f77ae831ee7 | 73 | _redpin = c.red; |
yqin70 | 0:4f77ae831ee7 | 74 | _greenpin = c.green; |
yqin70 | 0:4f77ae831ee7 | 75 | _bluepin = c.blue; |
yqin70 | 0:4f77ae831ee7 | 76 | } |
yqin70 | 0:4f77ae831ee7 | 77 | |
yqin70 | 0:4f77ae831ee7 | 78 | //classes could be moved to include file |
yqin70 | 0:4f77ae831ee7 | 79 | |
yqin70 | 0:4f77ae831ee7 | 80 | //Setup RGB led using PWM pins and class |
yqin70 | 0:4f77ae831ee7 | 81 | RGBLed myRGBled(p23,p22,p21); //RGB PWM pins |
yqin70 | 0:4f77ae831ee7 | 82 | const LEDColor red(1.0,0.0,0.0); |
yqin70 | 0:4f77ae831ee7 | 83 | const LEDColor green(0.0,0.2,0.0); |
yqin70 | 0:4f77ae831ee7 | 84 | //brighter green LED is scaled down to same as red and |
yqin70 | 0:4f77ae831ee7 | 85 | //blue LED outputs on Sparkfun RGBLED |
yqin70 | 0:4f77ae831ee7 | 86 | const LEDColor blue(0.0,0.0,1.0); |
yqin70 | 0:4f77ae831ee7 | 87 | const LEDColor yellow(1.0,0.2,0.0); |
yqin70 | 0:4f77ae831ee7 | 88 | const LEDColor white(1.0,0.2,1.0); |
yqin70 | 0:4f77ae831ee7 | 89 | const LEDColor black(0.0,0.0,0.0); |
yqin70 | 0:4f77ae831ee7 | 90 | char bred=0; |
yqin70 | 0:4f77ae831ee7 | 91 | char bgreen=0; |
yqin70 | 0:4f77ae831ee7 | 92 | char bblue=0; |
yqin70 | 0:4f77ae831ee7 | 93 | |
yqin70 | 0:4f77ae831ee7 | 94 | volatile bool songselect = false; |
yqin70 | 0:4f77ae831ee7 | 95 | volatile bool homescreen = true; |
yqin70 | 0:4f77ae831ee7 | 96 | uLCD_4DGL uLCD(p28,p27,p30); |
yqin70 | 0:4f77ae831ee7 | 97 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
yqin70 | 0:4f77ae831ee7 | 98 | DigitalOut myled(LED1); |
yqin70 | 0:4f77ae831ee7 | 99 | DigitalIn pb1(p20); |
yqin70 | 0:4f77ae831ee7 | 100 | DigitalIn pb2(p19); |
yqin70 | 0:4f77ae831ee7 | 101 | |
yqin70 | 0:4f77ae831ee7 | 102 | // mutex to make the lcd lib thread safe |
yqin70 | 0:4f77ae831ee7 | 103 | Mutex lcd_mutex; |
yqin70 | 0:4f77ae831ee7 | 104 | int songnum = 1; |
yqin70 | 0:4f77ae831ee7 | 105 | AnalogIn joy_pot(p16); |
yqin70 | 0:4f77ae831ee7 | 106 | // Thread 1 |
yqin70 | 0:4f77ae831ee7 | 107 | // print counter into first line and wait for 1 s |
yqin70 | 0:4f77ae831ee7 | 108 | void thread1(void const *args) |
yqin70 | 0:4f77ae831ee7 | 109 | { |
yqin70 | 0:4f77ae831ee7 | 110 | |
yqin70 | 0:4f77ae831ee7 | 111 | while(true) { // thread loop |
yqin70 | 0:4f77ae831ee7 | 112 | |
yqin70 | 0:4f77ae831ee7 | 113 | if (homescreen){ |
yqin70 | 0:4f77ae831ee7 | 114 | lcd_mutex.lock(); |
yqin70 | 0:4f77ae831ee7 | 115 | uLCD.cls(); |
yqin70 | 0:4f77ae831ee7 | 116 | uLCD.text_height(1.8); |
yqin70 | 0:4f77ae831ee7 | 117 | uLCD.text_width(1.8); |
yqin70 | 0:4f77ae831ee7 | 118 | uLCD.color(WHITE); |
yqin70 | 0:4f77ae831ee7 | 119 | uLCD.locate(0,0); |
yqin70 | 0:4f77ae831ee7 | 120 | uLCD.printf("Pick a song"); |
yqin70 | 0:4f77ae831ee7 | 121 | uLCD.text_height(2); |
yqin70 | 0:4f77ae831ee7 | 122 | uLCD.text_width(2); |
yqin70 | 0:4f77ae831ee7 | 123 | uLCD.locate(1,2); |
yqin70 | 0:4f77ae831ee7 | 124 | uLCD.printf("Song1"); |
yqin70 | 0:4f77ae831ee7 | 125 | uLCD.locate(1,4); |
yqin70 | 0:4f77ae831ee7 | 126 | uLCD.printf("Song2"); |
yqin70 | 0:4f77ae831ee7 | 127 | uLCD.locate(1,6); |
yqin70 | 0:4f77ae831ee7 | 128 | uLCD.printf("Song3"); |
yqin70 | 0:4f77ae831ee7 | 129 | uLCD.rectangle(5, songnum*30, 100, songnum*30+25 ,GREEN); |
yqin70 | 0:4f77ae831ee7 | 130 | lcd_mutex.unlock(); |
yqin70 | 0:4f77ae831ee7 | 131 | if (songselect){ |
yqin70 | 0:4f77ae831ee7 | 132 | myled = 0; |
yqin70 | 0:4f77ae831ee7 | 133 | homescreen = false; |
yqin70 | 0:4f77ae831ee7 | 134 | lcd_mutex.lock(); |
yqin70 | 0:4f77ae831ee7 | 135 | uLCD.cls(); |
yqin70 | 0:4f77ae831ee7 | 136 | uLCD.printf("You selected song %2d",songnum); |
yqin70 | 0:4f77ae831ee7 | 137 | lcd_mutex.unlock(); |
yqin70 | 0:4f77ae831ee7 | 138 | |
yqin70 | 0:4f77ae831ee7 | 139 | } |
yqin70 | 0:4f77ae831ee7 | 140 | //FILE *fp = fopen("/sd/sdtest.txt", "r"); |
yqin70 | 0:4f77ae831ee7 | 141 | //char c = fgetc(fp); |
yqin70 | 0:4f77ae831ee7 | 142 | //while(c){ |
yqin70 | 0:4f77ae831ee7 | 143 | // c = fgetc(fp); |
yqin70 | 0:4f77ae831ee7 | 144 | // } |
yqin70 | 0:4f77ae831ee7 | 145 | } |
yqin70 | 0:4f77ae831ee7 | 146 | Thread::wait(200); |
yqin70 | 0:4f77ae831ee7 | 147 | } |
yqin70 | 0:4f77ae831ee7 | 148 | } |
yqin70 | 0:4f77ae831ee7 | 149 | |
yqin70 | 0:4f77ae831ee7 | 150 | void thread2(void const *args) |
yqin70 | 0:4f77ae831ee7 | 151 | { |
yqin70 | 0:4f77ae831ee7 | 152 | |
yqin70 | 0:4f77ae831ee7 | 153 | while(1){ |
yqin70 | 0:4f77ae831ee7 | 154 | //if (homescreen){ |
yqin70 | 0:4f77ae831ee7 | 155 | if ((joy_pot <= (1.4/3.3)) && songnum>1) { |
yqin70 | 0:4f77ae831ee7 | 156 | songnum--; |
yqin70 | 0:4f77ae831ee7 | 157 | } |
yqin70 | 0:4f77ae831ee7 | 158 | else if ((joy_pot >= (1.8/3.3)) && songnum<3){ |
yqin70 | 0:4f77ae831ee7 | 159 | songnum++; |
yqin70 | 0:4f77ae831ee7 | 160 | } |
yqin70 | 0:4f77ae831ee7 | 161 | // } |
yqin70 | 0:4f77ae831ee7 | 162 | Thread::wait(250); |
yqin70 | 0:4f77ae831ee7 | 163 | |
yqin70 | 0:4f77ae831ee7 | 164 | } |
yqin70 | 0:4f77ae831ee7 | 165 | } |
yqin70 | 0:4f77ae831ee7 | 166 | |
yqin70 | 0:4f77ae831ee7 | 167 | // Thread 3 |
yqin70 | 0:4f77ae831ee7 | 168 | // print a sin function in a small window |
yqin70 | 0:4f77ae831ee7 | 169 | // the value of pot 1 changes the speed of the sine wave |
yqin70 | 0:4f77ae831ee7 | 170 | void thread3(void const *args) |
yqin70 | 0:4f77ae831ee7 | 171 | { |
yqin70 | 0:4f77ae831ee7 | 172 | pb1.mode(PullUp); |
yqin70 | 0:4f77ae831ee7 | 173 | pb2.mode(PullUp); |
yqin70 | 0:4f77ae831ee7 | 174 | while(true) { // thread loop |
yqin70 | 0:4f77ae831ee7 | 175 | if (!pb2) |
yqin70 | 0:4f77ae831ee7 | 176 | { |
yqin70 | 0:4f77ae831ee7 | 177 | homescreen = true; |
yqin70 | 0:4f77ae831ee7 | 178 | songselect = false; |
yqin70 | 0:4f77ae831ee7 | 179 | } |
yqin70 | 0:4f77ae831ee7 | 180 | if (!pb1) |
yqin70 | 0:4f77ae831ee7 | 181 | { |
yqin70 | 0:4f77ae831ee7 | 182 | songselect = true; |
yqin70 | 0:4f77ae831ee7 | 183 | } |
yqin70 | 0:4f77ae831ee7 | 184 | Thread::wait(100); // value of pot1 / 100 |
yqin70 | 0:4f77ae831ee7 | 185 | } |
yqin70 | 0:4f77ae831ee7 | 186 | } |
yqin70 | 0:4f77ae831ee7 | 187 | |
yqin70 | 0:4f77ae831ee7 | 188 | int main() |
yqin70 | 0:4f77ae831ee7 | 189 | { |
yqin70 | 0:4f77ae831ee7 | 190 | uLCD.media_init(); |
yqin70 | 0:4f77ae831ee7 | 191 | uLCD.set_sector_address(0x001D, 0x4C01); |
yqin70 | 0:4f77ae831ee7 | 192 | uLCD.display_image(0,0); |
yqin70 | 0:4f77ae831ee7 | 193 | // t1.start(thread1); |
yqin70 | 0:4f77ae831ee7 | 194 | // t2.start(thread2); |
yqin70 | 0:4f77ae831ee7 | 195 | // t3.start(thread3); |
yqin70 | 0:4f77ae831ee7 | 196 | Thread t1(thread1); |
yqin70 | 0:4f77ae831ee7 | 197 | Thread t2(thread2); |
yqin70 | 0:4f77ae831ee7 | 198 | myled = 1; |
yqin70 | 0:4f77ae831ee7 | 199 | Thread t3(thread3); |
yqin70 | 0:4f77ae831ee7 | 200 | while(1) |
yqin70 | 0:4f77ae831ee7 | 201 | { |
yqin70 | 0:4f77ae831ee7 | 202 | |
yqin70 | 0:4f77ae831ee7 | 203 | Thread::wait(100); |
yqin70 | 0:4f77ae831ee7 | 204 | } |
yqin70 | 0:4f77ae831ee7 | 205 | } |
yqin70 | 0:4f77ae831ee7 | 206 | |
yqin70 | 0:4f77ae831ee7 | 207 |