update
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed wave_player
Fork of Lab3 by
Part2.cpp@1:3af15e979565, 2016-10-13 (annotated)
- Committer:
- aismail1997
- Date:
- Thu Oct 13 13:22:29 2016 +0000
- Revision:
- 1:3af15e979565
- Parent:
- 0:8af0a4200bd0
- Child:
- 2:d832b6c139d4
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 | 0:8af0a4200bd0 | 39 | int sound = 0; // 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 | 0:8af0a4200bd0 | 157 | if (light == 0 && sound == 1) { |
aismail1997 | 0:8af0a4200bd0 | 158 | // display red siren |
aismail1997 | 0:8af0a4200bd0 | 159 | uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, RED); |
aismail1997 | 0:8af0a4200bd0 | 160 | uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, RED); |
aismail1997 | 1:3af15e979565 | 161 | } else if (light == 1 || sound == 0) { |
aismail1997 | 0:8af0a4200bd0 | 162 | // display white siren |
aismail1997 | 0:8af0a4200bd0 | 163 | uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, WHITE); |
aismail1997 | 0:8af0a4200bd0 | 164 | uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, WHITE); |
aismail1997 | 0:8af0a4200bd0 | 165 | } else if (light == 2 && sound == 1) { |
aismail1997 | 0:8af0a4200bd0 | 166 | // display blue siren |
aismail1997 | 0:8af0a4200bd0 | 167 | uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, BLUE); |
aismail1997 | 0:8af0a4200bd0 | 168 | uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, BLUE); |
aismail1997 | 0:8af0a4200bd0 | 169 | } |
aismail1997 | 0:8af0a4200bd0 | 170 | lcd_mutex.unlock(); |
aismail1997 | 1:3af15e979565 | 171 | // lcd_sem.release(); |
aismail1997 | 0:8af0a4200bd0 | 172 | Thread::wait(5); // wait till thread is done |
aismail1997 | 0:8af0a4200bd0 | 173 | } |
aismail1997 | 0:8af0a4200bd0 | 174 | } |
aismail1997 | 0:8af0a4200bd0 | 175 | |
aismail1997 | 0:8af0a4200bd0 | 176 | // second thread dealing with LCD |
aismail1997 | 0:8af0a4200bd0 | 177 | void LCD_thread2(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 178 | { |
aismail1997 | 0:8af0a4200bd0 | 179 | while(true) { // thread loop |
aismail1997 | 0:8af0a4200bd0 | 180 | lcd_mutex.lock(); |
aismail1997 | 1:3af15e979565 | 181 | // lcd_sem.wait(); |
aismail1997 | 0:8af0a4200bd0 | 182 | if (sound == 1) { |
aismail1997 | 0:8af0a4200bd0 | 183 | // some indication there is audio |
aismail1997 | 0:8af0a4200bd0 | 184 | // text saying "!ALERT!" |
aismail1997 | 0:8af0a4200bd0 | 185 | uLCD.color(0xFF0000); |
aismail1997 | 0:8af0a4200bd0 | 186 | uLCD.locate(6,1); |
aismail1997 | 0:8af0a4200bd0 | 187 | uLCD.set_font_size(7, 7); |
aismail1997 | 0:8af0a4200bd0 | 188 | uLCD.printf("!ALERT !"); |
aismail1997 | 0:8af0a4200bd0 | 189 | } else if (sound == 0) { |
aismail1997 | 1:3af15e979565 | 190 | //uLCD.color(0x000000); |
aismail1997 | 0:8af0a4200bd0 | 191 | uLCD.locate(6,1); |
aismail1997 | 0:8af0a4200bd0 | 192 | uLCD.set_font_size(7, 7); |
aismail1997 | 1:3af15e979565 | 193 | uLCD.textbackground_color(BLACK); |
aismail1997 | 1:3af15e979565 | 194 | uLCD.printf(" "); |
aismail1997 | 0:8af0a4200bd0 | 195 | // no indication |
aismail1997 | 0:8af0a4200bd0 | 196 | // text removed |
aismail1997 | 0:8af0a4200bd0 | 197 | } |
aismail1997 | 0:8af0a4200bd0 | 198 | lcd_mutex.unlock(); |
aismail1997 | 1:3af15e979565 | 199 | // lcd_sem.release(); |
aismail1997 | 0:8af0a4200bd0 | 200 | Thread::wait(5); // wait till thread is done |
aismail1997 | 0:8af0a4200bd0 | 201 | } |
aismail1997 | 0:8af0a4200bd0 | 202 | } |
aismail1997 | 0:8af0a4200bd0 | 203 | |
aismail1997 | 1:3af15e979565 | 204 | // thread playing video |
aismail1997 | 1:3af15e979565 | 205 | void video_thread(void const *args) |
aismail1997 | 1:3af15e979565 | 206 | { |
aismail1997 | 1:3af15e979565 | 207 | uLCD.media_init(); |
aismail1997 | 1:3af15e979565 | 208 | uLCD.set_sector_address(0x001D, 0x4C42); |
aismail1997 | 1:3af15e979565 | 209 | uLCD.display_video(0,0); |
aismail1997 | 1:3af15e979565 | 210 | } |
aismail1997 | 1:3af15e979565 | 211 | |
aismail1997 | 1:3af15e979565 | 212 | int play = 1; |
aismail1997 | 0:8af0a4200bd0 | 213 | // thread dealing with speaker |
aismail1997 | 0:8af0a4200bd0 | 214 | void speaker_thread(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 215 | { |
aismail1997 | 1:3af15e979565 | 216 | FILE *wave_file; |
aismail1997 | 0:8af0a4200bd0 | 217 | wave_file = fopen("/sd/police_siren.wav","r"); |
aismail1997 | 0:8af0a4200bd0 | 218 | // play siren |
aismail1997 | 0:8af0a4200bd0 | 219 | while(true) { // thread loop |
aismail1997 | 1:3af15e979565 | 220 | if (play == 1) { |
aismail1997 | 1:3af15e979565 | 221 | sound = 1; // comment when using mic |
aismail1997 | 1:3af15e979565 | 222 | waver.play(wave_file); |
aismail1997 | 1:3af15e979565 | 223 | play = 0; // comment when using joystick or touchpad |
aismail1997 | 1:3af15e979565 | 224 | } else if (play == 0) { |
aismail1997 | 1:3af15e979565 | 225 | // how can we stop play midway? - reduce volume? |
aismail1997 | 1:3af15e979565 | 226 | sound = 0; // comment when using mic |
aismail1997 | 1:3af15e979565 | 227 | wait(5); // comment when using joystick or touchpad |
aismail1997 | 1:3af15e979565 | 228 | play = 1; // comment when using joystick or touchpad |
aismail1997 | 1:3af15e979565 | 229 | } |
aismail1997 | 0:8af0a4200bd0 | 230 | Thread::wait(1000); // wait 1s |
aismail1997 | 0:8af0a4200bd0 | 231 | } |
aismail1997 | 1:3af15e979565 | 232 | fclose(wave_file); |
aismail1997 | 0:8af0a4200bd0 | 233 | } |
aismail1997 | 0:8af0a4200bd0 | 234 | |
aismail1997 | 0:8af0a4200bd0 | 235 | // thread reading from - ultrasonic sensor to do XYZ |
aismail1997 | 1:3af15e979565 | 236 | void sonar_thread(void const *args) |
aismail1997 | 1:3af15e979565 | 237 | { |
aismail1997 | 0:8af0a4200bd0 | 238 | |
aismail1997 | 1:3af15e979565 | 239 | } |
aismail1997 | 0:8af0a4200bd0 | 240 | |
aismail1997 | 0:8af0a4200bd0 | 241 | // thread reading from - tactile switch to control RGB/sound/lcd |
aismail1997 | 0:8af0a4200bd0 | 242 | void switchthread(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 243 | { |
aismail1997 | 1:3af15e979565 | 244 | while(true) { |
aismail1997 | 1:3af15e979565 | 245 | // control sound & RGB frequency |
aismail1997 | 1:3af15e979565 | 246 | //with pullups a button hit is a "0" - "~" inverts data to leds |
aismail1997 | 1:3af15e979565 | 247 | //mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs FOR TESTING ONLY |
aismail1997 | 1:3af15e979565 | 248 | if (myNav.up()) play = 1; |
aismail1997 | 1:3af15e979565 | 249 | if (myNav.down()) play = 0; |
aismail1997 | 1:3af15e979565 | 250 | if (sb_freq > 0.2) { |
aismail1997 | 1:3af15e979565 | 251 | if (myNav.left()) sb_freq -= 0.1; |
aismail1997 | 1:3af15e979565 | 252 | } |
aismail1997 | 1:3af15e979565 | 253 | if (sb_freq < 1) { |
aismail1997 | 1:3af15e979565 | 254 | if (myNav.right()) sb_freq += 0.1; |
aismail1997 | 1:3af15e979565 | 255 | } |
aismail1997 | 1:3af15e979565 | 256 | //if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button) |
aismail1997 | 1:3af15e979565 | 257 | //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this |
aismail1997 | 1:3af15e979565 | 258 | Thread::wait(200); |
aismail1997 | 0:8af0a4200bd0 | 259 | } |
aismail1997 | 0:8af0a4200bd0 | 260 | } |
aismail1997 | 0:8af0a4200bd0 | 261 | |
aismail1997 | 0:8af0a4200bd0 | 262 | // thread reading from - touch keypad same |
aismail1997 | 0:8af0a4200bd0 | 263 | void touchpad_thread(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 264 | { |
aismail1997 | 0:8af0a4200bd0 | 265 | while(true) { // thread loop |
aismail1997 | 0:8af0a4200bd0 | 266 | int key_code=0; |
aismail1997 | 0:8af0a4200bd0 | 267 | int i=0; |
aismail1997 | 0:8af0a4200bd0 | 268 | int value=mpr121.read(0x00); |
aismail1997 | 0:8af0a4200bd0 | 269 | value +=mpr121.read(0x01)<<8; |
aismail1997 | 0:8af0a4200bd0 | 270 | // LED demo mod |
aismail1997 | 0:8af0a4200bd0 | 271 | i=0; |
aismail1997 | 0:8af0a4200bd0 | 272 | // puts key number out to LEDs for demo |
aismail1997 | 0:8af0a4200bd0 | 273 | for (i=0; i<12; i++) { |
aismail1997 | 0:8af0a4200bd0 | 274 | if (((value>>i)&0x01)==1) key_code=i+1; |
aismail1997 | 0:8af0a4200bd0 | 275 | } |
aismail1997 | 1:3af15e979565 | 276 | if (key_code == 2) { |
aismail1997 | 1:3af15e979565 | 277 | play = 1; |
aismail1997 | 1:3af15e979565 | 278 | } else if (key_code == 8) { |
aismail1997 | 1:3af15e979565 | 279 | play = 0; |
aismail1997 | 1:3af15e979565 | 280 | } else if (key_code == 4) { |
aismail1997 | 1:3af15e979565 | 281 | sb_freq -= 0.1; |
aismail1997 | 1:3af15e979565 | 282 | } else if (key_code == 6) { |
aismail1997 | 1:3af15e979565 | 283 | sb_freq += 0.1; |
aismail1997 | 1:3af15e979565 | 284 | } |
aismail1997 | 0:8af0a4200bd0 | 285 | } |
aismail1997 | 0:8af0a4200bd0 | 286 | } |
aismail1997 | 0:8af0a4200bd0 | 287 | |
aismail1997 | 0:8af0a4200bd0 | 288 | // thread reading from - light sensor to do something |
aismail1997 | 0:8af0a4200bd0 | 289 | void lightsensor_thread(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 290 | { |
aismail1997 | 0:8af0a4200bd0 | 291 | while(true) { // thread loop |
aismail1997 | 1:3af15e979565 | 292 | if (photocell*3.3 < 1.34) { |
aismail1997 | 1:3af15e979565 | 293 | bright = 0; |
aismail1997 | 1:3af15e979565 | 294 | } else { |
aismail1997 | 1:3af15e979565 | 295 | bright = 1; |
aismail1997 | 1:3af15e979565 | 296 | } |
aismail1997 | 1:3af15e979565 | 297 | wait(0.1); |
aismail1997 | 1:3af15e979565 | 298 | Thread::wait(1000); // wait 0.25s |
aismail1997 | 0:8af0a4200bd0 | 299 | } |
aismail1997 | 0:8af0a4200bd0 | 300 | } |
aismail1997 | 0:8af0a4200bd0 | 301 | |
aismail1997 | 1:3af15e979565 | 302 | int mymic; |
aismail1997 | 0:8af0a4200bd0 | 303 | // thread reading from - microphone to control rgb |
aismail1997 | 0:8af0a4200bd0 | 304 | void mic_thread(void const *args) |
aismail1997 | 0:8af0a4200bd0 | 305 | { |
aismail1997 | 1:3af15e979565 | 306 | while(true) { // thread loop |
aismail1997 | 1:3af15e979565 | 307 | //read in, subtract 0.67 DC bias, take absolute value, and scale up .1Vpp to 15 for builtin LED display |
aismail1997 | 1:3af15e979565 | 308 | mymic = int(abs((mymicrophone - (0.67/3.3)))*500.0); |
aismail1997 | 1:3af15e979565 | 309 | //Use an 8kHz audio sample rate (phone quality audio); |
aismail1997 | 1:3af15e979565 | 310 | if (mymic > 0.5) { |
aismail1997 | 1:3af15e979565 | 311 | sound = 1; |
aismail1997 | 1:3af15e979565 | 312 | //wait(7); // song is around 8 seconds long |
aismail1997 | 1:3af15e979565 | 313 | } else { |
aismail1997 | 1:3af15e979565 | 314 | sound = 0; |
aismail1997 | 1:3af15e979565 | 315 | } |
aismail1997 | 1:3af15e979565 | 316 | //wait(1.0/8000.0); |
aismail1997 | 1:3af15e979565 | 317 | Thread::wait(1000); // wait 0.25s |
aismail1997 | 1:3af15e979565 | 318 | } |
aismail1997 | 0:8af0a4200bd0 | 319 | } |
aismail1997 | 0:8af0a4200bd0 | 320 | |
aismail1997 | 0:8af0a4200bd0 | 321 | int main() |
aismail1997 | 0:8af0a4200bd0 | 322 | { |
aismail1997 | 1:3af15e979565 | 323 | uLCD.cls(); |
aismail1997 | 1:3af15e979565 | 324 | |
aismail1997 | 0:8af0a4200bd0 | 325 | // shiftbrite stuff |
aismail1997 | 0:8af0a4200bd0 | 326 | int red=0; |
aismail1997 | 0:8af0a4200bd0 | 327 | int green=0; |
aismail1997 | 0:8af0a4200bd0 | 328 | int blue=0; |
aismail1997 | 0:8af0a4200bd0 | 329 | spi.format(16,0); |
aismail1997 | 0:8af0a4200bd0 | 330 | spi.frequency(500000); |
aismail1997 | 0:8af0a4200bd0 | 331 | enable=0; |
aismail1997 | 0:8af0a4200bd0 | 332 | latch=0; |
aismail1997 | 0:8af0a4200bd0 | 333 | wait(2); |
aismail1997 | 1:3af15e979565 | 334 | |
aismail1997 | 1:3af15e979565 | 335 | // draw police car base? |
aismail1997 | 1:3af15e979565 | 336 | |
aismail1997 | 1:3af15e979565 | 337 | // t5 and t6 should not run at the same time |
aismail1997 | 0:8af0a4200bd0 | 338 | |
aismail1997 | 0:8af0a4200bd0 | 339 | // call threads here |
aismail1997 | 0:8af0a4200bd0 | 340 | Thread t1(LCD_thread1); //start thread1 |
aismail1997 | 0:8af0a4200bd0 | 341 | Thread t2(LCD_thread2); //start thread2 |
aismail1997 | 1:3af15e979565 | 342 | //Thread t3(speaker_thread); //start thread3 |
aismail1997 | 1:3af15e979565 | 343 | //Thread t4(sonar_thread); // start thread4 |
aismail1997 | 0:8af0a4200bd0 | 344 | //Thread t5(switchthread); //start thread5 |
aismail1997 | 0:8af0a4200bd0 | 345 | //Thread t6(touchpad_thread); //start thread6 |
aismail1997 | 0:8af0a4200bd0 | 346 | //Thread t7(lightsensor_thread); //start thread7 |
aismail1997 | 0:8af0a4200bd0 | 347 | //Thread t8(mic_thread); //start thread8 |
aismail1997 | 0:8af0a4200bd0 | 348 | |
aismail1997 | 1:3af15e979565 | 349 | // running shiftbrite |
aismail1997 | 1:3af15e979565 | 350 | int color = 255; |
aismail1997 | 0:8af0a4200bd0 | 351 | while(1) { |
aismail1997 | 0:8af0a4200bd0 | 352 | |
aismail1997 | 1:3af15e979565 | 353 | if (bright == 0) { |
aismail1997 | 1:3af15e979565 | 354 | color = 150; |
aismail1997 | 1:3af15e979565 | 355 | } else { |
aismail1997 | 1:3af15e979565 | 356 | color = 255; |
aismail1997 | 1:3af15e979565 | 357 | } |
aismail1997 | 0:8af0a4200bd0 | 358 | if (sound == 0) { |
aismail1997 | 0:8af0a4200bd0 | 359 | RGB_LED(0,0,0); |
aismail1997 | 1:3af15e979565 | 360 | Thread::wait(500); |
aismail1997 | 0:8af0a4200bd0 | 361 | } else { |
aismail1997 | 0:8af0a4200bd0 | 362 | light = 0; |
aismail1997 | 1:3af15e979565 | 363 | red = color; // flash red light |
aismail1997 | 0:8af0a4200bd0 | 364 | green = 0; |
aismail1997 | 0:8af0a4200bd0 | 365 | blue = 0; |
aismail1997 | 0:8af0a4200bd0 | 366 | RGB_LED(red, green, blue); |
aismail1997 | 1:3af15e979565 | 367 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 368 | |
aismail1997 | 0:8af0a4200bd0 | 369 | RGB_LED(0,0,0); |
aismail1997 | 1:3af15e979565 | 370 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 371 | |
aismail1997 | 0:8af0a4200bd0 | 372 | light = 1; |
aismail1997 | 1:3af15e979565 | 373 | red = color; // flash white light |
aismail1997 | 1:3af15e979565 | 374 | green = color; |
aismail1997 | 1:3af15e979565 | 375 | blue = color; |
aismail1997 | 0:8af0a4200bd0 | 376 | RGB_LED( red, green, blue); |
aismail1997 | 1:3af15e979565 | 377 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 378 | |
aismail1997 | 0:8af0a4200bd0 | 379 | RGB_LED(0,0,0); |
aismail1997 | 1:3af15e979565 | 380 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 381 | |
aismail1997 | 0:8af0a4200bd0 | 382 | light = 2; |
aismail1997 | 0:8af0a4200bd0 | 383 | red = 0; // flash blue light |
aismail1997 | 0:8af0a4200bd0 | 384 | green = 0; |
aismail1997 | 1:3af15e979565 | 385 | blue = color; |
aismail1997 | 0:8af0a4200bd0 | 386 | RGB_LED( red, green, blue); |
aismail1997 | 1:3af15e979565 | 387 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 388 | |
aismail1997 | 0:8af0a4200bd0 | 389 | RGB_LED(0,0,0); |
aismail1997 | 1:3af15e979565 | 390 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 391 | |
aismail1997 | 0:8af0a4200bd0 | 392 | light = 1; |
aismail1997 | 1:3af15e979565 | 393 | red = color; // flash white light |
aismail1997 | 1:3af15e979565 | 394 | green = color; |
aismail1997 | 1:3af15e979565 | 395 | blue = color; |
aismail1997 | 0:8af0a4200bd0 | 396 | RGB_LED( red, green, blue); |
aismail1997 | 1:3af15e979565 | 397 | wait(sb_freq); |
aismail1997 | 0:8af0a4200bd0 | 398 | |
aismail1997 | 0:8af0a4200bd0 | 399 | RGB_LED(0,0,0); |
aismail1997 | 1:3af15e979565 | 400 | Thread::wait(500); |
aismail1997 | 0:8af0a4200bd0 | 401 | } |
aismail1997 | 0:8af0a4200bd0 | 402 | } |
aismail1997 | 0:8af0a4200bd0 | 403 | } |