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