update
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed wave_player
Fork of Lab3 by
Part2.cpp@2:d832b6c139d4, 2016-10-13 (annotated)
- Committer:
- aismail1997
- Date:
- Thu Oct 13 15:28:12 2016 +0000
- Revision:
- 2:d832b6c139d4
- Parent:
- 1:3af15e979565
- Child:
- 3:2adc8522c5e8
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aismail1997 | 0:8af0a4200bd0 | 1 | #include "mbed.h" |
aismail1997 | 0:8af0a4200bd0 | 2 | #include "rtos.h" |
aismail1997 | 0:8af0a4200bd0 | 3 | #include "uLCD_4DGL.h" |
aismail1997 | 0:8af0a4200bd0 | 4 | #include "SDFileSystem.h" |
aismail1997 | 0:8af0a4200bd0 | 5 | #include "wave_player.h" |
aismail1997 | 0:8af0a4200bd0 | 6 | #include <mpr121.h> |
aismail1997 | 0:8af0a4200bd0 | 7 | |
aismail1997 | 0:8af0a4200bd0 | 8 | // mutex to make the lcd lib thread safe |
aismail1997 | 0:8af0a4200bd0 | 9 | Mutex lcd_mutex; |
aismail1997 | 1:3af15e979565 | 10 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
aismail1997 | 1:3af15e979565 | 11 | AnalogOut speaker(p18); |
aismail1997 | 1:3af15e979565 | 12 | wave_player waver(&speaker); |
aismail1997 | 1:3af15e979565 | 13 | uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin |
aismail1997 | 1:3af15e979565 | 14 | Semaphore lcd_sem(1); |
aismail1997 | 0:8af0a4200bd0 | 15 | |
aismail1997 | 1:3af15e979565 | 16 | // Shiftbrite |
aismail1997 | 0:8af0a4200bd0 | 17 | DigitalOut latch(p15); |
aismail1997 | 0:8af0a4200bd0 | 18 | DigitalOut enable(p16); |
aismail1997 | 0:8af0a4200bd0 | 19 | |
aismail1997 | 0:8af0a4200bd0 | 20 | SPI spi(p11, p12, p13); |
aismail1997 | 0:8af0a4200bd0 | 21 | |
aismail1997 | 1:3af15e979565 | 22 | // Touchpad |
aismail1997 | 0:8af0a4200bd0 | 23 | // Create the interrupt receiver object on pin 26 |
aismail1997 | 0:8af0a4200bd0 | 24 | InterruptIn interrupt(p26); |
aismail1997 | 0:8af0a4200bd0 | 25 | // Setup the i2c bus on pins 9 and 10 |
aismail1997 | 0:8af0a4200bd0 | 26 | I2C i2c(p9, p10); |
aismail1997 | 0:8af0a4200bd0 | 27 | // Setup the Mpr121: |
aismail1997 | 0:8af0a4200bd0 | 28 | // constructor(i2c object, i2c address of the mpr121) |
aismail1997 | 0:8af0a4200bd0 | 29 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
aismail1997 | 0:8af0a4200bd0 | 30 | |
aismail1997 | 1:3af15e979565 | 31 | // Test leds |
aismail1997 | 0:8af0a4200bd0 | 32 | DigitalOut led1(LED1); |
aismail1997 | 0:8af0a4200bd0 | 33 | DigitalOut led2(LED2); |
aismail1997 | 0:8af0a4200bd0 | 34 | DigitalOut led3(LED3); |
aismail1997 | 0:8af0a4200bd0 | 35 | DigitalOut led4(LED4); |
aismail1997 | 0:8af0a4200bd0 | 36 | |
aismail1997 | 0:8af0a4200bd0 | 37 | // global variables |
aismail1997 | 0:8af0a4200bd0 | 38 | int light = 0; // to keep track of lighting |
aismail1997 | 2:d832b6c139d4 | 39 | int sound = 1; // to keep track of sound |
aismail1997 | 1:3af15e979565 | 40 | int sb_freq = 0.5; // rgb frequency |
aismail1997 | 1:3af15e979565 | 41 | int bright = 1; // time of the day |
aismail1997 | 0:8af0a4200bd0 | 42 | |
aismail1997 | 1:3af15e979565 | 43 | // Light sensor |
aismail1997 | 1:3af15e979565 | 44 | AnalogIn photocell(p17); |
aismail1997 | 1:3af15e979565 | 45 | //PwmOut myled(LED1); |
aismail1997 | 0:8af0a4200bd0 | 46 | |
aismail1997 | 1:3af15e979565 | 47 | |
aismail1997 | 1:3af15e979565 | 48 | // Microphone |
aismail1997 | 0:8af0a4200bd0 | 49 | class microphone |
aismail1997 | 0:8af0a4200bd0 | 50 | { |
aismail1997 | 0:8af0a4200bd0 | 51 | public : |
aismail1997 | 0:8af0a4200bd0 | 52 | microphone(PinName pin); |
aismail1997 | 0:8af0a4200bd0 | 53 | float read(); |
aismail1997 | 0:8af0a4200bd0 | 54 | operator float (); |
aismail1997 | 0:8af0a4200bd0 | 55 | private : |
aismail1997 | 0:8af0a4200bd0 | 56 | AnalogIn _pin; |
aismail1997 | 0:8af0a4200bd0 | 57 | }; |
aismail1997 | 1:3af15e979565 | 58 | |
aismail1997 | 0:8af0a4200bd0 | 59 | microphone::microphone (PinName pin): |
aismail1997 | 0:8af0a4200bd0 | 60 | _pin(pin) |
aismail1997 | 0:8af0a4200bd0 | 61 | { |
aismail1997 | 0:8af0a4200bd0 | 62 | } |
aismail1997 | 0:8af0a4200bd0 | 63 | |
aismail1997 | 0:8af0a4200bd0 | 64 | float microphone::read() |
aismail1997 | 0:8af0a4200bd0 | 65 | { |
aismail1997 | 0:8af0a4200bd0 | 66 | return _pin.read(); |
aismail1997 | 0:8af0a4200bd0 | 67 | } |
aismail1997 | 0:8af0a4200bd0 | 68 | |
aismail1997 | 0:8af0a4200bd0 | 69 | inline microphone::operator float () |
aismail1997 | 0:8af0a4200bd0 | 70 | { |
aismail1997 | 0:8af0a4200bd0 | 71 | return _pin.read(); |
aismail1997 | 0:8af0a4200bd0 | 72 | } |
aismail1997 | 0:8af0a4200bd0 | 73 | |
aismail1997 | 1:3af15e979565 | 74 | |
aismail1997 | 1:3af15e979565 | 75 | // joystick |
aismail1997 | 1:3af15e979565 | 76 | class Nav_Switch |
aismail1997 | 1:3af15e979565 | 77 | { |
aismail1997 | 1:3af15e979565 | 78 | public: |
aismail1997 | 1:3af15e979565 | 79 | Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); |
aismail1997 | 1:3af15e979565 | 80 | int read(); |
aismail1997 | 1:3af15e979565 | 81 | //boolean functions to test each switch |
aismail1997 | 1:3af15e979565 | 82 | bool up(); |
aismail1997 | 1:3af15e979565 | 83 | bool down(); |
aismail1997 | 1:3af15e979565 | 84 | bool left(); |
aismail1997 | 1:3af15e979565 | 85 | bool right(); |
aismail1997 | 1:3af15e979565 | 86 | bool fire(); |
aismail1997 | 1:3af15e979565 | 87 | //automatic read on RHS |
aismail1997 | 1:3af15e979565 | 88 | operator int (); |
aismail1997 | 1:3af15e979565 | 89 | //index to any switch array style |
aismail1997 | 1:3af15e979565 | 90 | bool operator[](int index) { |
aismail1997 | 1:3af15e979565 | 91 | return _pins[index]; |
aismail1997 | 1:3af15e979565 | 92 | }; |
aismail1997 | 1:3af15e979565 | 93 | private: |
aismail1997 | 1:3af15e979565 | 94 | BusIn _pins; |
aismail1997 | 0:8af0a4200bd0 | 95 | |
aismail1997 | 1:3af15e979565 | 96 | }; |
aismail1997 | 1:3af15e979565 | 97 | Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): |
aismail1997 | 1:3af15e979565 | 98 | _pins(up, down, left, right, fire) |
aismail1997 | 1:3af15e979565 | 99 | { |
aismail1997 | 1:3af15e979565 | 100 | _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise |
aismail1997 | 1:3af15e979565 | 101 | wait(0.001); //delays just a bit for pullups to pull inputs high |
aismail1997 | 1:3af15e979565 | 102 | } |
aismail1997 | 1:3af15e979565 | 103 | inline bool Nav_Switch::up() |
aismail1997 | 1:3af15e979565 | 104 | { |
aismail1997 | 1:3af15e979565 | 105 | return !(_pins[0]); |
aismail1997 | 1:3af15e979565 | 106 | } |
aismail1997 | 1:3af15e979565 | 107 | inline bool Nav_Switch::down() |
aismail1997 | 1:3af15e979565 | 108 | { |
aismail1997 | 1:3af15e979565 | 109 | return !(_pins[1]); |
aismail1997 | 1:3af15e979565 | 110 | } |
aismail1997 | 1:3af15e979565 | 111 | inline bool Nav_Switch::left() |
aismail1997 | 1:3af15e979565 | 112 | { |
aismail1997 | 1:3af15e979565 | 113 | return !(_pins[2]); |
aismail1997 | 1:3af15e979565 | 114 | } |
aismail1997 | 1:3af15e979565 | 115 | inline bool Nav_Switch::right() |
aismail1997 | 1:3af15e979565 | 116 | { |
aismail1997 | 1:3af15e979565 | 117 | return !(_pins[3]); |
aismail1997 | 1:3af15e979565 | 118 | } |
aismail1997 | 1:3af15e979565 | 119 | inline bool Nav_Switch::fire() |
aismail1997 | 1:3af15e979565 | 120 | { |
aismail1997 | 1:3af15e979565 | 121 | return !(_pins[4]); |
aismail1997 | 1:3af15e979565 | 122 | } |
aismail1997 | 1:3af15e979565 | 123 | inline int Nav_Switch::read() |
aismail1997 | 1:3af15e979565 | 124 | { |
aismail1997 | 1:3af15e979565 | 125 | return _pins.read(); |
aismail1997 | 1:3af15e979565 | 126 | } |
aismail1997 | 1:3af15e979565 | 127 | inline Nav_Switch::operator int () |
aismail1997 | 1:3af15e979565 | 128 | { |
aismail1997 | 1:3af15e979565 | 129 | return _pins.read(); |
aismail1997 | 1:3af15e979565 | 130 | } |
aismail1997 | 1:3af15e979565 | 131 | |
aismail1997 | 1:3af15e979565 | 132 | microphone mymicrophone(p19); // uncomment when using |
aismail1997 | 1:3af15e979565 | 133 | |
aismail1997 | 1:3af15e979565 | 134 | // joystick |
aismail1997 | 1:3af15e979565 | 135 | Nav_Switch myNav(p14, p25, p24, p10, p9); //pin order on Sparkfun breakout |
aismail1997 | 1:3af15e979565 | 136 | |
aismail1997 | 1:3af15e979565 | 137 | // Shiftbrite |
aismail1997 | 0:8af0a4200bd0 | 138 | void RGB_LED(int red, int green, int blue) |
aismail1997 | 0:8af0a4200bd0 | 139 | { |
aismail1997 | 0:8af0a4200bd0 | 140 | unsigned int low_color=0; |
aismail1997 | 0:8af0a4200bd0 | 141 | unsigned int high_color=0; |
aismail1997 | 0:8af0a4200bd0 | 142 | high_color=(blue<<4)|((red&0x3C0)>>6); |
aismail1997 | 0:8af0a4200bd0 | 143 | low_color=(((red&0x3F)<<10)|(green)); |
aismail1997 | 0:8af0a4200bd0 | 144 | spi.write(high_color); |
aismail1997 | 0:8af0a4200bd0 | 145 | spi.write(low_color); |
aismail1997 | 0:8af0a4200bd0 | 146 | latch=1; |
aismail1997 | 0:8af0a4200bd0 | 147 | latch=0; |
aismail1997 | 0:8af0a4200bd0 | 148 | } |
aismail1997 | 0:8af0a4200bd0 | 149 | |
aismail1997 | 0:8af0a4200bd0 | 150 | // create threads |
aismail1997 | 0:8af0a4200bd0 | 151 | // first thread dealing with LCD |
aismail1997 | 0:8af0a4200bd0 | 152 | void LCD_thread1(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 153 | { |
aismail1997 | 0:8af0a4200bd0 | 154 | while(true) { // thread loop |
aismail1997 | 0:8af0a4200bd0 | 155 | lcd_mutex.lock(); |
aismail1997 | 1:3af15e979565 | 156 | // lcd_sem.wait(); |
aismail1997 | 2:d832b6c139d4 | 157 | //if (light == 0 && sound == 1) { |
aismail1997 | 2:d832b6c139d4 | 158 | if (light == 0) { |
aismail1997 | 0:8af0a4200bd0 | 159 | // display red siren |
aismail1997 | 0:8af0a4200bd0 | 160 | uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, RED); |
aismail1997 | 0:8af0a4200bd0 | 161 | uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, RED); |
aismail1997 | 2:d832b6c139d4 | 162 | //} else if (light == 1 || sound == 0) { |
aismail1997 | 2:d832b6c139d4 | 163 | } else if (light == 1) { |
aismail1997 | 0:8af0a4200bd0 | 164 | // display white siren |
aismail1997 | 0:8af0a4200bd0 | 165 | uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, WHITE); |
aismail1997 | 0:8af0a4200bd0 | 166 | uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, WHITE); |
aismail1997 | 2:d832b6c139d4 | 167 | //} else if (light == 2 && sound == 1) { |
aismail1997 | 2:d832b6c139d4 | 168 | } else if (light == 2) { |
aismail1997 | 0:8af0a4200bd0 | 169 | // display blue siren |
aismail1997 | 0:8af0a4200bd0 | 170 | uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, BLUE); |
aismail1997 | 0:8af0a4200bd0 | 171 | uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, BLUE); |
aismail1997 | 0:8af0a4200bd0 | 172 | } |
aismail1997 | 0:8af0a4200bd0 | 173 | lcd_mutex.unlock(); |
aismail1997 | 1:3af15e979565 | 174 | // lcd_sem.release(); |
aismail1997 | 0:8af0a4200bd0 | 175 | Thread::wait(5); // wait till thread is done |
aismail1997 | 0:8af0a4200bd0 | 176 | } |
aismail1997 | 0:8af0a4200bd0 | 177 | } |
aismail1997 | 0:8af0a4200bd0 | 178 | |
aismail1997 | 0:8af0a4200bd0 | 179 | // second thread dealing with LCD |
aismail1997 | 0:8af0a4200bd0 | 180 | void LCD_thread2(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 181 | { |
aismail1997 | 0:8af0a4200bd0 | 182 | while(true) { // thread loop |
aismail1997 | 0:8af0a4200bd0 | 183 | lcd_mutex.lock(); |
aismail1997 | 1:3af15e979565 | 184 | // lcd_sem.wait(); |
aismail1997 | 0:8af0a4200bd0 | 185 | if (sound == 1) { |
aismail1997 | 0:8af0a4200bd0 | 186 | // some indication there is audio |
aismail1997 | 0:8af0a4200bd0 | 187 | // text saying "!ALERT!" |
aismail1997 | 0:8af0a4200bd0 | 188 | uLCD.color(0xFF0000); |
aismail1997 | 0:8af0a4200bd0 | 189 | uLCD.locate(6,1); |
aismail1997 | 0:8af0a4200bd0 | 190 | uLCD.set_font_size(7, 7); |
aismail1997 | 0:8af0a4200bd0 | 191 | uLCD.printf("!ALERT !"); |
aismail1997 | 0:8af0a4200bd0 | 192 | } else if (sound == 0) { |
aismail1997 | 1:3af15e979565 | 193 | //uLCD.color(0x000000); |
aismail1997 | 0:8af0a4200bd0 | 194 | uLCD.locate(6,1); |
aismail1997 | 0:8af0a4200bd0 | 195 | uLCD.set_font_size(7, 7); |
aismail1997 | 1:3af15e979565 | 196 | uLCD.textbackground_color(BLACK); |
aismail1997 | 1:3af15e979565 | 197 | uLCD.printf(" "); |
aismail1997 | 0:8af0a4200bd0 | 198 | // no indication |
aismail1997 | 0:8af0a4200bd0 | 199 | // text removed |
aismail1997 | 0:8af0a4200bd0 | 200 | } |
aismail1997 | 0:8af0a4200bd0 | 201 | lcd_mutex.unlock(); |
aismail1997 | 1:3af15e979565 | 202 | // lcd_sem.release(); |
aismail1997 | 0:8af0a4200bd0 | 203 | Thread::wait(5); // wait till thread is done |
aismail1997 | 0:8af0a4200bd0 | 204 | } |
aismail1997 | 0:8af0a4200bd0 | 205 | } |
aismail1997 | 0:8af0a4200bd0 | 206 | |
aismail1997 | 1:3af15e979565 | 207 | // thread playing video |
aismail1997 | 1:3af15e979565 | 208 | void video_thread(void const *args) |
aismail1997 | 1:3af15e979565 | 209 | { |
aismail1997 | 1:3af15e979565 | 210 | uLCD.media_init(); |
aismail1997 | 1:3af15e979565 | 211 | uLCD.set_sector_address(0x001D, 0x4C42); |
aismail1997 | 1:3af15e979565 | 212 | uLCD.display_video(0,0); |
aismail1997 | 1:3af15e979565 | 213 | } |
aismail1997 | 1:3af15e979565 | 214 | |
aismail1997 | 1:3af15e979565 | 215 | int play = 1; |
aismail1997 | 0:8af0a4200bd0 | 216 | // thread dealing with speaker |
aismail1997 | 0:8af0a4200bd0 | 217 | void speaker_thread(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 218 | { |
aismail1997 | 1:3af15e979565 | 219 | FILE *wave_file; |
aismail1997 | 0:8af0a4200bd0 | 220 | wave_file = fopen("/sd/police_siren.wav","r"); |
aismail1997 | 0:8af0a4200bd0 | 221 | // play siren |
aismail1997 | 0:8af0a4200bd0 | 222 | while(true) { // thread loop |
aismail1997 | 1:3af15e979565 | 223 | if (play == 1) { |
aismail1997 | 1:3af15e979565 | 224 | sound = 1; // comment when using mic |
aismail1997 | 1:3af15e979565 | 225 | waver.play(wave_file); |
aismail1997 | 1:3af15e979565 | 226 | play = 0; // comment when using joystick or touchpad |
aismail1997 | 1:3af15e979565 | 227 | } else if (play == 0) { |
aismail1997 | 1:3af15e979565 | 228 | // how can we stop play midway? - reduce volume? |
aismail1997 | 1:3af15e979565 | 229 | sound = 0; // comment when using mic |
aismail1997 | 1:3af15e979565 | 230 | wait(5); // comment when using joystick or touchpad |
aismail1997 | 1:3af15e979565 | 231 | play = 1; // comment when using joystick or touchpad |
aismail1997 | 1:3af15e979565 | 232 | } |
aismail1997 | 0:8af0a4200bd0 | 233 | Thread::wait(1000); // wait 1s |
aismail1997 | 0:8af0a4200bd0 | 234 | } |
aismail1997 | 1:3af15e979565 | 235 | fclose(wave_file); |
aismail1997 | 0:8af0a4200bd0 | 236 | } |
aismail1997 | 0:8af0a4200bd0 | 237 | |
aismail1997 | 0:8af0a4200bd0 | 238 | // thread reading from - ultrasonic sensor to do XYZ |
aismail1997 | 1:3af15e979565 | 239 | void sonar_thread(void const *args) |
aismail1997 | 1:3af15e979565 | 240 | { |
aismail1997 | 0:8af0a4200bd0 | 241 | |
aismail1997 | 1:3af15e979565 | 242 | } |
aismail1997 | 0:8af0a4200bd0 | 243 | |
aismail1997 | 0:8af0a4200bd0 | 244 | // thread reading from - tactile switch to control RGB/sound/lcd |
aismail1997 | 0:8af0a4200bd0 | 245 | void switchthread(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 246 | { |
aismail1997 | 1:3af15e979565 | 247 | while(true) { |
aismail1997 | 1:3af15e979565 | 248 | // control sound & RGB frequency |
aismail1997 | 1:3af15e979565 | 249 | //with pullups a button hit is a "0" - "~" inverts data to leds |
aismail1997 | 1:3af15e979565 | 250 | //mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs FOR TESTING ONLY |
aismail1997 | 1:3af15e979565 | 251 | if (myNav.up()) play = 1; |
aismail1997 | 1:3af15e979565 | 252 | if (myNav.down()) play = 0; |
aismail1997 | 1:3af15e979565 | 253 | if (sb_freq > 0.2) { |
aismail1997 | 1:3af15e979565 | 254 | if (myNav.left()) sb_freq -= 0.1; |
aismail1997 | 1:3af15e979565 | 255 | } |
aismail1997 | 1:3af15e979565 | 256 | if (sb_freq < 1) { |
aismail1997 | 1:3af15e979565 | 257 | if (myNav.right()) sb_freq += 0.1; |
aismail1997 | 1:3af15e979565 | 258 | } |
aismail1997 | 1:3af15e979565 | 259 | //if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button) |
aismail1997 | 1:3af15e979565 | 260 | //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this |
aismail1997 | 1:3af15e979565 | 261 | Thread::wait(200); |
aismail1997 | 0:8af0a4200bd0 | 262 | } |
aismail1997 | 0:8af0a4200bd0 | 263 | } |
aismail1997 | 0:8af0a4200bd0 | 264 | |
aismail1997 | 0:8af0a4200bd0 | 265 | // thread reading from - touch keypad same |
aismail1997 | 0:8af0a4200bd0 | 266 | void touchpad_thread(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 267 | { |
aismail1997 | 0:8af0a4200bd0 | 268 | while(true) { // thread loop |
aismail1997 | 0:8af0a4200bd0 | 269 | int key_code=0; |
aismail1997 | 0:8af0a4200bd0 | 270 | int i=0; |
aismail1997 | 0:8af0a4200bd0 | 271 | int value=mpr121.read(0x00); |
aismail1997 | 0:8af0a4200bd0 | 272 | value +=mpr121.read(0x01)<<8; |
aismail1997 | 0:8af0a4200bd0 | 273 | // LED demo mod |
aismail1997 | 0:8af0a4200bd0 | 274 | i=0; |
aismail1997 | 0:8af0a4200bd0 | 275 | // puts key number out to LEDs for demo |
aismail1997 | 0:8af0a4200bd0 | 276 | for (i=0; i<12; i++) { |
aismail1997 | 0:8af0a4200bd0 | 277 | if (((value>>i)&0x01)==1) key_code=i+1; |
aismail1997 | 0:8af0a4200bd0 | 278 | } |
aismail1997 | 1:3af15e979565 | 279 | if (key_code == 2) { |
aismail1997 | 1:3af15e979565 | 280 | play = 1; |
aismail1997 | 1:3af15e979565 | 281 | } else if (key_code == 8) { |
aismail1997 | 1:3af15e979565 | 282 | play = 0; |
aismail1997 | 1:3af15e979565 | 283 | } else if (key_code == 4) { |
aismail1997 | 1:3af15e979565 | 284 | sb_freq -= 0.1; |
aismail1997 | 1:3af15e979565 | 285 | } else if (key_code == 6) { |
aismail1997 | 1:3af15e979565 | 286 | sb_freq += 0.1; |
aismail1997 | 1:3af15e979565 | 287 | } |
aismail1997 | 0:8af0a4200bd0 | 288 | } |
aismail1997 | 0:8af0a4200bd0 | 289 | } |
aismail1997 | 0:8af0a4200bd0 | 290 | |
aismail1997 | 0:8af0a4200bd0 | 291 | // thread reading from - light sensor to do something |
aismail1997 | 0:8af0a4200bd0 | 292 | void lightsensor_thread(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 293 | { |
aismail1997 | 0:8af0a4200bd0 | 294 | while(true) { // thread loop |
aismail1997 | 1:3af15e979565 | 295 | if (photocell*3.3 < 1.34) { |
aismail1997 | 1:3af15e979565 | 296 | bright = 0; |
aismail1997 | 1:3af15e979565 | 297 | } else { |
aismail1997 | 1:3af15e979565 | 298 | bright = 1; |
aismail1997 | 1:3af15e979565 | 299 | } |
aismail1997 | 1:3af15e979565 | 300 | wait(0.1); |
aismail1997 | 1:3af15e979565 | 301 | Thread::wait(1000); // wait 0.25s |
aismail1997 | 0:8af0a4200bd0 | 302 | } |
aismail1997 | 0:8af0a4200bd0 | 303 | } |
aismail1997 | 0:8af0a4200bd0 | 304 | |
aismail1997 | 1:3af15e979565 | 305 | int mymic; |
aismail1997 | 0:8af0a4200bd0 | 306 | // thread reading from - microphone to control rgb |
aismail1997 | 0:8af0a4200bd0 | 307 | void mic_thread(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 308 | { |
aismail1997 | 1:3af15e979565 | 309 | while(true) { // thread loop |
aismail1997 | 1:3af15e979565 | 310 | //read in, subtract 0.67 DC bias, take absolute value, and scale up .1Vpp to 15 for builtin LED display |
aismail1997 | 1:3af15e979565 | 311 | mymic = int(abs((mymicrophone - (0.67/3.3)))*500.0); |
aismail1997 | 1:3af15e979565 | 312 | //Use an 8kHz audio sample rate (phone quality audio); |
aismail1997 | 1:3af15e979565 | 313 | if (mymic > 0.5) { |
aismail1997 | 1:3af15e979565 | 314 | sound = 1; |
aismail1997 | 1:3af15e979565 | 315 | //wait(7); // song is around 8 seconds long |
aismail1997 | 1:3af15e979565 | 316 | } else { |
aismail1997 | 1:3af15e979565 | 317 | sound = 0; |
aismail1997 | 1:3af15e979565 | 318 | } |
aismail1997 | 1:3af15e979565 | 319 | //wait(1.0/8000.0); |
aismail1997 | 1:3af15e979565 | 320 | Thread::wait(1000); // wait 0.25s |
aismail1997 | 1:3af15e979565 | 321 | } |
aismail1997 | 0:8af0a4200bd0 | 322 | } |
aismail1997 | 0:8af0a4200bd0 | 323 | |
aismail1997 | 0:8af0a4200bd0 | 324 | int main() |
aismail1997 | 0:8af0a4200bd0 | 325 | { |
aismail1997 | 1:3af15e979565 | 326 | uLCD.cls(); |
aismail1997 | 1:3af15e979565 | 327 | |
aismail1997 | 0:8af0a4200bd0 | 328 | // shiftbrite stuff |
aismail1997 | 0:8af0a4200bd0 | 329 | int red=0; |
aismail1997 | 0:8af0a4200bd0 | 330 | int green=0; |
aismail1997 | 0:8af0a4200bd0 | 331 | int blue=0; |
aismail1997 | 0:8af0a4200bd0 | 332 | spi.format(16,0); |
aismail1997 | 0:8af0a4200bd0 | 333 | spi.frequency(500000); |
aismail1997 | 0:8af0a4200bd0 | 334 | enable=0; |
aismail1997 | 0:8af0a4200bd0 | 335 | latch=0; |
aismail1997 | 0:8af0a4200bd0 | 336 | wait(2); |
aismail1997 | 1:3af15e979565 | 337 | |
aismail1997 | 1:3af15e979565 | 338 | // draw police car base? |
aismail1997 | 1:3af15e979565 | 339 | |
aismail1997 | 1:3af15e979565 | 340 | // t5 and t6 should not run at the same time |
aismail1997 | 0:8af0a4200bd0 | 341 | |
aismail1997 | 0:8af0a4200bd0 | 342 | // call threads here |
aismail1997 | 0:8af0a4200bd0 | 343 | Thread t1(LCD_thread1); //start thread1 |
aismail1997 | 0:8af0a4200bd0 | 344 | Thread t2(LCD_thread2); //start thread2 |
aismail1997 | 1:3af15e979565 | 345 | //Thread t3(speaker_thread); //start thread3 |
aismail1997 | 1:3af15e979565 | 346 | //Thread t4(sonar_thread); // start thread4 |
aismail1997 | 0:8af0a4200bd0 | 347 | //Thread t5(switchthread); //start thread5 |
aismail1997 | 0:8af0a4200bd0 | 348 | //Thread t6(touchpad_thread); //start thread6 |
aismail1997 | 0:8af0a4200bd0 | 349 | //Thread t7(lightsensor_thread); //start thread7 |
aismail1997 | 0:8af0a4200bd0 | 350 | //Thread t8(mic_thread); //start thread8 |
aismail1997 | 0:8af0a4200bd0 | 351 | |
aismail1997 | 1:3af15e979565 | 352 | // running shiftbrite |
aismail1997 | 1:3af15e979565 | 353 | int color = 255; |
aismail1997 | 0:8af0a4200bd0 | 354 | while(1) { |
aismail1997 | 0:8af0a4200bd0 | 355 | |
aismail1997 | 1:3af15e979565 | 356 | if (bright == 0) { |
aismail1997 | 1:3af15e979565 | 357 | color = 150; |
aismail1997 | 1:3af15e979565 | 358 | } else { |
aismail1997 | 1:3af15e979565 | 359 | color = 255; |
aismail1997 | 1:3af15e979565 | 360 | } |
aismail1997 | 0:8af0a4200bd0 | 361 | if (sound == 0) { |
aismail1997 | 0:8af0a4200bd0 | 362 | RGB_LED(0,0,0); |
aismail1997 | 1:3af15e979565 | 363 | Thread::wait(500); |
aismail1997 | 0:8af0a4200bd0 | 364 | } else { |
aismail1997 | 0:8af0a4200bd0 | 365 | light = 0; |
aismail1997 | 1:3af15e979565 | 366 | red = color; // flash red light |
aismail1997 | 0:8af0a4200bd0 | 367 | green = 0; |
aismail1997 | 0:8af0a4200bd0 | 368 | blue = 0; |
aismail1997 | 0:8af0a4200bd0 | 369 | RGB_LED(red, green, blue); |
aismail1997 | 1:3af15e979565 | 370 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 371 | |
aismail1997 | 0:8af0a4200bd0 | 372 | RGB_LED(0,0,0); |
aismail1997 | 1:3af15e979565 | 373 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 374 | |
aismail1997 | 0:8af0a4200bd0 | 375 | light = 1; |
aismail1997 | 1:3af15e979565 | 376 | red = color; // flash white light |
aismail1997 | 1:3af15e979565 | 377 | green = color; |
aismail1997 | 1:3af15e979565 | 378 | blue = color; |
aismail1997 | 0:8af0a4200bd0 | 379 | RGB_LED( red, green, blue); |
aismail1997 | 1:3af15e979565 | 380 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 381 | |
aismail1997 | 0:8af0a4200bd0 | 382 | RGB_LED(0,0,0); |
aismail1997 | 1:3af15e979565 | 383 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 384 | |
aismail1997 | 0:8af0a4200bd0 | 385 | light = 2; |
aismail1997 | 0:8af0a4200bd0 | 386 | red = 0; // flash blue light |
aismail1997 | 0:8af0a4200bd0 | 387 | green = 0; |
aismail1997 | 1:3af15e979565 | 388 | blue = color; |
aismail1997 | 0:8af0a4200bd0 | 389 | RGB_LED( red, green, blue); |
aismail1997 | 1:3af15e979565 | 390 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 391 | |
aismail1997 | 0:8af0a4200bd0 | 392 | RGB_LED(0,0,0); |
aismail1997 | 1:3af15e979565 | 393 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 394 | |
aismail1997 | 0:8af0a4200bd0 | 395 | light = 1; |
aismail1997 | 1:3af15e979565 | 396 | red = color; // flash white light |
aismail1997 | 1:3af15e979565 | 397 | green = color; |
aismail1997 | 1:3af15e979565 | 398 | blue = color; |
aismail1997 | 0:8af0a4200bd0 | 399 | RGB_LED( red, green, blue); |
aismail1997 | 1:3af15e979565 | 400 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 401 | |
aismail1997 | 0:8af0a4200bd0 | 402 | RGB_LED(0,0,0); |
aismail1997 | 1:3af15e979565 | 403 | Thread::wait(500); |
aismail1997 | 0:8af0a4200bd0 | 404 | } |
aismail1997 | 0:8af0a4200bd0 | 405 | } |
aismail1997 | 0:8af0a4200bd0 | 406 | } |