update

Dependencies:   4DGL-uLCD-SE mbed-rtos mbed wave_player

Fork of Lab3 by Azra Ismail

Committer:
aismail1997
Date:
Sun Oct 16 14:42:22 2016 +0000
Revision:
4:7ec6407f673f
Parent:
3:2adc8522c5e8
updated version;

Who changed what in which revision?

UserRevisionLine numberNew 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 4:7ec6407f673f 4 //#include "SDFileSystem.h"
aismail1997 4:7ec6407f673f 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 4:7ec6407f673f 9 Mutex lcd_mutex;
aismail1997 4:7ec6407f673f 10 //SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
aismail1997 4:7ec6407f673f 11 PwmOut speaker(p26);
aismail1997 4:7ec6407f673f 12 //wave_player waver(&speaker);
aismail1997 1:3af15e979565 13 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin
aismail1997 4:7ec6407f673f 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 4:7ec6407f673f 20 /*
aismail1997 1:3af15e979565 21 // Touchpad
aismail1997 0:8af0a4200bd0 22 // Create the interrupt receiver object on pin 26
aismail1997 4:7ec6407f673f 23 InterruptIn interrupt(p21);
aismail1997 0:8af0a4200bd0 24 // Setup the i2c bus on pins 9 and 10
aismail1997 4:7ec6407f673f 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 4:7ec6407f673f 28 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
aismail1997 4:7ec6407f673f 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 4:7ec6407f673f 36 // sonar
aismail1997 4:7ec6407f673f 37
aismail1997 4:7ec6407f673f 38 DigitalOut trigger(p20);
aismail1997 4:7ec6407f673f 39 //DigitalOut myled(LED1); //monitor trigger
aismail1997 4:7ec6407f673f 40 //DigitalOut myled2(LED2); //monitor echo
aismail1997 4:7ec6407f673f 41 DigitalIn echo(p19);
aismail1997 4:7ec6407f673f 42 int distance = 0;
aismail1997 4:7ec6407f673f 43 int correction = 0;
aismail1997 4:7ec6407f673f 44 Timer sonar;
aismail1997 4:7ec6407f673f 45
aismail1997 4:7ec6407f673f 46
aismail1997 0:8af0a4200bd0 47 // global variables
aismail1997 0:8af0a4200bd0 48 int light = 0; // to keep track of lighting
aismail1997 2:d832b6c139d4 49 int sound = 1; // to keep track of sound
aismail1997 4:7ec6407f673f 50 int sb_freq = 1; // rgb frequency
aismail1997 1:3af15e979565 51 int bright = 1; // time of the day
aismail1997 0:8af0a4200bd0 52
aismail1997 1:3af15e979565 53 // Light sensor
aismail1997 4:7ec6407f673f 54 AnalogIn photocell(p17);
aismail1997 1:3af15e979565 55 //PwmOut myled(LED1);
aismail1997 0:8af0a4200bd0 56
aismail1997 1:3af15e979565 57 // Microphone
aismail1997 0:8af0a4200bd0 58 class microphone
aismail1997 0:8af0a4200bd0 59 {
aismail1997 0:8af0a4200bd0 60 public :
aismail1997 0:8af0a4200bd0 61 microphone(PinName pin);
aismail1997 0:8af0a4200bd0 62 float read();
aismail1997 0:8af0a4200bd0 63 operator float ();
aismail1997 0:8af0a4200bd0 64 private :
aismail1997 0:8af0a4200bd0 65 AnalogIn _pin;
aismail1997 0:8af0a4200bd0 66 };
aismail1997 1:3af15e979565 67
aismail1997 0:8af0a4200bd0 68 microphone::microphone (PinName pin):
aismail1997 0:8af0a4200bd0 69 _pin(pin)
aismail1997 0:8af0a4200bd0 70 {
aismail1997 0:8af0a4200bd0 71 }
aismail1997 0:8af0a4200bd0 72
aismail1997 0:8af0a4200bd0 73 float microphone::read()
aismail1997 0:8af0a4200bd0 74 {
aismail1997 0:8af0a4200bd0 75 return _pin.read();
aismail1997 0:8af0a4200bd0 76 }
aismail1997 0:8af0a4200bd0 77
aismail1997 0:8af0a4200bd0 78 inline microphone::operator float ()
aismail1997 0:8af0a4200bd0 79 {
aismail1997 0:8af0a4200bd0 80 return _pin.read();
aismail1997 0:8af0a4200bd0 81 }
aismail1997 0:8af0a4200bd0 82
aismail1997 1:3af15e979565 83
aismail1997 1:3af15e979565 84 // joystick
aismail1997 1:3af15e979565 85 class Nav_Switch
aismail1997 1:3af15e979565 86 {
aismail1997 1:3af15e979565 87 public:
aismail1997 1:3af15e979565 88 Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
aismail1997 1:3af15e979565 89 int read();
aismail1997 1:3af15e979565 90 //boolean functions to test each switch
aismail1997 1:3af15e979565 91 bool up();
aismail1997 1:3af15e979565 92 bool down();
aismail1997 1:3af15e979565 93 bool left();
aismail1997 1:3af15e979565 94 bool right();
aismail1997 1:3af15e979565 95 bool fire();
aismail1997 1:3af15e979565 96 //automatic read on RHS
aismail1997 1:3af15e979565 97 operator int ();
aismail1997 1:3af15e979565 98 //index to any switch array style
aismail1997 1:3af15e979565 99 bool operator[](int index) {
aismail1997 1:3af15e979565 100 return _pins[index];
aismail1997 1:3af15e979565 101 };
aismail1997 1:3af15e979565 102 private:
aismail1997 1:3af15e979565 103 BusIn _pins;
aismail1997 0:8af0a4200bd0 104
aismail1997 1:3af15e979565 105 };
aismail1997 1:3af15e979565 106 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
aismail1997 1:3af15e979565 107 _pins(up, down, left, right, fire)
aismail1997 1:3af15e979565 108 {
aismail1997 1:3af15e979565 109 _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
aismail1997 1:3af15e979565 110 wait(0.001); //delays just a bit for pullups to pull inputs high
aismail1997 1:3af15e979565 111 }
aismail1997 1:3af15e979565 112 inline bool Nav_Switch::up()
aismail1997 1:3af15e979565 113 {
aismail1997 1:3af15e979565 114 return !(_pins[0]);
aismail1997 1:3af15e979565 115 }
aismail1997 1:3af15e979565 116 inline bool Nav_Switch::down()
aismail1997 1:3af15e979565 117 {
aismail1997 1:3af15e979565 118 return !(_pins[1]);
aismail1997 1:3af15e979565 119 }
aismail1997 1:3af15e979565 120 inline bool Nav_Switch::left()
aismail1997 1:3af15e979565 121 {
aismail1997 1:3af15e979565 122 return !(_pins[2]);
aismail1997 1:3af15e979565 123 }
aismail1997 1:3af15e979565 124 inline bool Nav_Switch::right()
aismail1997 1:3af15e979565 125 {
aismail1997 1:3af15e979565 126 return !(_pins[3]);
aismail1997 1:3af15e979565 127 }
aismail1997 1:3af15e979565 128 inline bool Nav_Switch::fire()
aismail1997 1:3af15e979565 129 {
aismail1997 1:3af15e979565 130 return !(_pins[4]);
aismail1997 1:3af15e979565 131 }
aismail1997 1:3af15e979565 132 inline int Nav_Switch::read()
aismail1997 1:3af15e979565 133 {
aismail1997 1:3af15e979565 134 return _pins.read();
aismail1997 1:3af15e979565 135 }
aismail1997 1:3af15e979565 136 inline Nav_Switch::operator int ()
aismail1997 1:3af15e979565 137 {
aismail1997 1:3af15e979565 138 return _pins.read();
aismail1997 1:3af15e979565 139 }
aismail1997 1:3af15e979565 140
aismail1997 3:2adc8522c5e8 141 //microphone mymicrophone(p19); // uncomment when using
aismail1997 1:3af15e979565 142
aismail1997 1:3af15e979565 143 // joystick
aismail1997 4:7ec6407f673f 144 //Nav_Switch myNav(p25, p22, p23, p21, p24); //pin order on Sparkfun breakout
aismail1997 1:3af15e979565 145
aismail1997 1:3af15e979565 146 // Shiftbrite
aismail1997 0:8af0a4200bd0 147 void RGB_LED(int red, int green, int blue)
aismail1997 0:8af0a4200bd0 148 {
aismail1997 0:8af0a4200bd0 149 unsigned int low_color=0;
aismail1997 0:8af0a4200bd0 150 unsigned int high_color=0;
aismail1997 0:8af0a4200bd0 151 high_color=(blue<<4)|((red&0x3C0)>>6);
aismail1997 0:8af0a4200bd0 152 low_color=(((red&0x3F)<<10)|(green));
aismail1997 0:8af0a4200bd0 153 spi.write(high_color);
aismail1997 0:8af0a4200bd0 154 spi.write(low_color);
aismail1997 0:8af0a4200bd0 155 latch=1;
aismail1997 0:8af0a4200bd0 156 latch=0;
aismail1997 0:8af0a4200bd0 157 }
aismail1997 0:8af0a4200bd0 158
aismail1997 0:8af0a4200bd0 159 // create threads
aismail1997 0:8af0a4200bd0 160 // first thread dealing with LCD
aismail1997 0:8af0a4200bd0 161 void LCD_thread1(void const *args)
aismail1997 0:8af0a4200bd0 162 {
aismail1997 0:8af0a4200bd0 163 while(true) { // thread loop
aismail1997 4:7ec6407f673f 164 lcd_mutex.lock();
aismail1997 4:7ec6407f673f 165 //lcd_sem.wait();
aismail1997 2:d832b6c139d4 166 //if (light == 0 && sound == 1) {
aismail1997 2:d832b6c139d4 167 if (light == 0) {
aismail1997 0:8af0a4200bd0 168 // display red siren
aismail1997 0:8af0a4200bd0 169 uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, RED);
aismail1997 0:8af0a4200bd0 170 uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, RED);
aismail1997 3:2adc8522c5e8 171 //} else if (light == 1 || sound == 0) {
aismail1997 3:2adc8522c5e8 172 } else if (light == 1) {
aismail1997 0:8af0a4200bd0 173 // display white siren
aismail1997 0:8af0a4200bd0 174 uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, WHITE);
aismail1997 0:8af0a4200bd0 175 uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, WHITE);
aismail1997 3:2adc8522c5e8 176 //} else if (light == 2 && sound == 1) {
aismail1997 3:2adc8522c5e8 177 } else if (light == 2) {
aismail1997 0:8af0a4200bd0 178 // display blue siren
aismail1997 0:8af0a4200bd0 179 uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, BLUE);
aismail1997 0:8af0a4200bd0 180 uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, BLUE);
aismail1997 0:8af0a4200bd0 181 }
aismail1997 4:7ec6407f673f 182 lcd_mutex.unlock();
aismail1997 4:7ec6407f673f 183 //lcd_sem.release();
aismail1997 0:8af0a4200bd0 184 Thread::wait(5); // wait till thread is done
aismail1997 0:8af0a4200bd0 185 }
aismail1997 0:8af0a4200bd0 186 }
aismail1997 0:8af0a4200bd0 187
aismail1997 0:8af0a4200bd0 188 // second thread dealing with LCD
aismail1997 0:8af0a4200bd0 189 void LCD_thread2(void const *args)
aismail1997 0:8af0a4200bd0 190 {
aismail1997 0:8af0a4200bd0 191 while(true) { // thread loop
aismail1997 4:7ec6407f673f 192 lcd_mutex.lock();
aismail1997 4:7ec6407f673f 193 //lcd_sem.wait();
aismail1997 0:8af0a4200bd0 194 if (sound == 1) {
aismail1997 0:8af0a4200bd0 195 // some indication there is audio
aismail1997 0:8af0a4200bd0 196 // text saying "!ALERT!"
aismail1997 0:8af0a4200bd0 197 uLCD.color(0xFF0000);
aismail1997 0:8af0a4200bd0 198 uLCD.locate(6,1);
aismail1997 0:8af0a4200bd0 199 uLCD.set_font_size(7, 7);
aismail1997 0:8af0a4200bd0 200 uLCD.printf("!ALERT !");
aismail1997 0:8af0a4200bd0 201 } else if (sound == 0) {
aismail1997 1:3af15e979565 202 //uLCD.color(0x000000);
aismail1997 0:8af0a4200bd0 203 uLCD.locate(6,1);
aismail1997 0:8af0a4200bd0 204 uLCD.set_font_size(7, 7);
aismail1997 1:3af15e979565 205 uLCD.textbackground_color(BLACK);
aismail1997 1:3af15e979565 206 uLCD.printf(" ");
aismail1997 0:8af0a4200bd0 207 // no indication
aismail1997 0:8af0a4200bd0 208 // text removed
aismail1997 0:8af0a4200bd0 209 }
aismail1997 4:7ec6407f673f 210 lcd_mutex.unlock();
aismail1997 4:7ec6407f673f 211 //lcd_sem.release();
aismail1997 0:8af0a4200bd0 212 Thread::wait(5); // wait till thread is done
aismail1997 0:8af0a4200bd0 213 }
aismail1997 0:8af0a4200bd0 214 }
aismail1997 0:8af0a4200bd0 215
aismail1997 1:3af15e979565 216 // thread playing video
aismail1997 1:3af15e979565 217 void video_thread(void const *args)
aismail1997 1:3af15e979565 218 {
aismail1997 1:3af15e979565 219 uLCD.media_init();
aismail1997 1:3af15e979565 220 uLCD.set_sector_address(0x001D, 0x4C42);
aismail1997 1:3af15e979565 221 uLCD.display_video(0,0);
aismail1997 1:3af15e979565 222 }
aismail1997 1:3af15e979565 223
aismail1997 1:3af15e979565 224 int play = 1;
aismail1997 3:2adc8522c5e8 225
aismail1997 0:8af0a4200bd0 226 // thread dealing with speaker
aismail1997 0:8af0a4200bd0 227 void speaker_thread(void const *args)
aismail1997 0:8af0a4200bd0 228 {
aismail1997 0:8af0a4200bd0 229 // play siren
aismail1997 0:8af0a4200bd0 230 while(true) { // thread loop
aismail1997 4:7ec6407f673f 231 /*FILE * wave_file;
aismail1997 3:2adc8522c5e8 232 wave_file = fopen("/sd/police-siren.wav","r");
aismail1997 4:7ec6407f673f 233 if (play == 1) {
aismail1997 3:2adc8522c5e8 234 sound = 1; // comment when using mic
aismail1997 3:2adc8522c5e8 235 waver.play(wave_file);
aismail1997 3:2adc8522c5e8 236 //play = 0; // comment when using joystick or touchpad
aismail1997 4:7ec6407f673f 237 } else if (play == 0) {
aismail1997 3:2adc8522c5e8 238 // how can we stop play midway? - reduce volume?
aismail1997 3:2adc8522c5e8 239 sound = 0; // comment when using mic
aismail1997 3:2adc8522c5e8 240 wait(5); // comment when using joystick or touchpad
aismail1997 3:2adc8522c5e8 241 play = 1; // comment when using joystick or touchpad
aismail1997 4:7ec6407f673f 242 }
aismail1997 4:7ec6407f673f 243 fclose(wave_file);*/
aismail1997 4:7ec6407f673f 244 //if (play == 1) {
aismail1997 4:7ec6407f673f 245 for (int i=0; i<1000; i=i+2) {
aismail1997 4:7ec6407f673f 246 speaker.period(1.0/969.0);
aismail1997 4:7ec6407f673f 247 speaker = float(i/50.0);
aismail1997 4:7ec6407f673f 248 wait(.5);
aismail1997 4:7ec6407f673f 249 speaker.period(1.0/800.0);
aismail1997 4:7ec6407f673f 250 wait(.5);
aismail1997 4:7ec6407f673f 251 sound = 1;
aismail1997 4:7ec6407f673f 252 if (play == 0) {
aismail1997 4:7ec6407f673f 253 sound = 0;
aismail1997 4:7ec6407f673f 254 //wait(1);
aismail1997 4:7ec6407f673f 255 break;
aismail1997 4:7ec6407f673f 256 }
aismail1997 4:7ec6407f673f 257 // }
aismail1997 4:7ec6407f673f 258 }
aismail1997 4:7ec6407f673f 259 ///wait(1);
aismail1997 0:8af0a4200bd0 260 Thread::wait(1000); // wait 1s
aismail1997 0:8af0a4200bd0 261 }
aismail1997 0:8af0a4200bd0 262 }
aismail1997 0:8af0a4200bd0 263
aismail1997 0:8af0a4200bd0 264 // thread reading from - ultrasonic sensor to do XYZ
aismail1997 1:3af15e979565 265 void sonar_thread(void const *args)
aismail1997 1:3af15e979565 266 {
aismail1997 4:7ec6407f673f 267 sonar.reset();
aismail1997 4:7ec6407f673f 268 sonar.start();
aismail1997 4:7ec6407f673f 269 while(echo==2) {}
aismail1997 0:8af0a4200bd0 270
aismail1997 4:7ec6407f673f 271 //while (echo==2) {};
aismail1997 4:7ec6407f673f 272 //led2 = 0;
aismail1997 4:7ec6407f673f 273 // stop timer
aismail1997 4:7ec6407f673f 274 sonar.stop();
aismail1997 4:7ec6407f673f 275 // read timer
aismail1997 4:7ec6407f673f 276 correction = sonar.read_us();
aismail1997 4:7ec6407f673f 277 // printf("Approximate software overhead timer delay is %d uS\n\r",correction);
aismail1997 4:7ec6407f673f 278
aismail1997 4:7ec6407f673f 279 //Loop to read Sonar distance values, scale, and print
aismail1997 4:7ec6407f673f 280 while(1) {
aismail1997 4:7ec6407f673f 281 // trigger sonar to send a ping
aismail1997 4:7ec6407f673f 282 trigger = 1;
aismail1997 4:7ec6407f673f 283 //led1 = 1;
aismail1997 4:7ec6407f673f 284 //led2 = 0;
aismail1997 4:7ec6407f673f 285 sonar.reset();
aismail1997 4:7ec6407f673f 286 wait_us(10.0);
aismail1997 4:7ec6407f673f 287 trigger = 0;
aismail1997 4:7ec6407f673f 288 //led1 = 0;
aismail1997 4:7ec6407f673f 289 //wait for echo high
aismail1997 4:7ec6407f673f 290 while (echo==0) {};
aismail1997 4:7ec6407f673f 291 //led2=echo;
aismail1997 4:7ec6407f673f 292 //echo high, so start timer
aismail1997 4:7ec6407f673f 293 sonar.start();
aismail1997 4:7ec6407f673f 294 //wait for echo low
aismail1997 4:7ec6407f673f 295 while (echo==1) {};
aismail1997 4:7ec6407f673f 296 //stop timer and read value
aismail1997 4:7ec6407f673f 297 sonar.stop();
aismail1997 4:7ec6407f673f 298 //subtract software overhead timer delay and scale to cm
aismail1997 4:7ec6407f673f 299 distance = (sonar.read_us()-correction)/58.0;
aismail1997 4:7ec6407f673f 300 //led2 = 0;
aismail1997 4:7ec6407f673f 301 // printf(" %d cm \n\r",distance);
aismail1997 4:7ec6407f673f 302 //wait so that any echo(s) return before sending another ping
aismail1997 4:7ec6407f673f 303 //wait(0.2);
aismail1997 4:7ec6407f673f 304 if (distance < 10) {
aismail1997 4:7ec6407f673f 305 play = 0;
aismail1997 4:7ec6407f673f 306 } else {
aismail1997 4:7ec6407f673f 307 play = 1;
aismail1997 4:7ec6407f673f 308 }
aismail1997 4:7ec6407f673f 309 Thread::wait(500);
aismail1997 4:7ec6407f673f 310 }
aismail1997 1:3af15e979565 311 }
aismail1997 0:8af0a4200bd0 312
aismail1997 4:7ec6407f673f 313
aismail1997 0:8af0a4200bd0 314 // thread reading from - tactile switch to control RGB/sound/lcd
aismail1997 3:2adc8522c5e8 315 /*
aismail1997 0:8af0a4200bd0 316 void switchthread(void const *args)
aismail1997 0:8af0a4200bd0 317 {
aismail1997 1:3af15e979565 318 while(true) {
aismail1997 1:3af15e979565 319 // control sound & RGB frequency
aismail1997 1:3af15e979565 320 //with pullups a button hit is a "0" - "~" inverts data to leds
aismail1997 1:3af15e979565 321 //mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs FOR TESTING ONLY
aismail1997 1:3af15e979565 322 if (myNav.up()) play = 1;
aismail1997 1:3af15e979565 323 if (myNav.down()) play = 0;
aismail1997 4:7ec6407f673f 324 if (sb_freq > 0.5) {
aismail1997 4:7ec6407f673f 325 if (myNav.left()) sb_freq -= 0.2;
aismail1997 1:3af15e979565 326 }
aismail1997 4:7ec6407f673f 327 if (sb_freq < 3) {
aismail1997 4:7ec6407f673f 328 if (myNav.right()) sb_freq += 0.2;
aismail1997 1:3af15e979565 329 }
aismail1997 1:3af15e979565 330 //if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button)
aismail1997 1:3af15e979565 331 //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this
aismail1997 1:3af15e979565 332 Thread::wait(200);
aismail1997 0:8af0a4200bd0 333 }
aismail1997 4:7ec6407f673f 334 }*/
aismail1997 4:7ec6407f673f 335 /*
aismail1997 0:8af0a4200bd0 336 // thread reading from - touch keypad same
aismail1997 0:8af0a4200bd0 337 void touchpad_thread(void const *args)
aismail1997 0:8af0a4200bd0 338 {
aismail1997 0:8af0a4200bd0 339 while(true) { // thread loop
aismail1997 4:7ec6407f673f 340 int key_code = 0;
aismail1997 0:8af0a4200bd0 341 int i=0;
aismail1997 0:8af0a4200bd0 342 int value=mpr121.read(0x00);
aismail1997 0:8af0a4200bd0 343 value +=mpr121.read(0x01)<<8;
aismail1997 0:8af0a4200bd0 344 // LED demo mod
aismail1997 0:8af0a4200bd0 345 i=0;
aismail1997 0:8af0a4200bd0 346 // puts key number out to LEDs for demo
aismail1997 0:8af0a4200bd0 347 for (i=0; i<12; i++) {
aismail1997 4:7ec6407f673f 348 if (((value>>i)&0x01)==1) key_code=i;
aismail1997 0:8af0a4200bd0 349 }
aismail1997 4:7ec6407f673f 350 if (key_code == 6) {
aismail1997 1:3af15e979565 351 play = 1;
aismail1997 4:7ec6407f673f 352 } else if (key_code == 4) {
aismail1997 1:3af15e979565 353 play = 0;
aismail1997 4:7ec6407f673f 354 } else if (key_code == 1) {
aismail1997 4:7ec6407f673f 355 if (sb_freq > 0.5) {
aismail1997 4:7ec6407f673f 356 sb_freq += -0.2;
aismail1997 4:7ec6407f673f 357 }
aismail1997 4:7ec6407f673f 358 } else if (key_code == 9) {
aismail1997 4:7ec6407f673f 359 if (sb_freq < 3) {
aismail1997 4:7ec6407f673f 360 sb_freq += 0.2;
aismail1997 4:7ec6407f673f 361 }
aismail1997 1:3af15e979565 362 }
aismail1997 0:8af0a4200bd0 363 }
aismail1997 0:8af0a4200bd0 364 }
aismail1997 3:2adc8522c5e8 365 */
aismail1997 0:8af0a4200bd0 366 // thread reading from - light sensor to do something
aismail1997 4:7ec6407f673f 367
aismail1997 0:8af0a4200bd0 368 void lightsensor_thread(void const *args)
aismail1997 0:8af0a4200bd0 369 {
aismail1997 0:8af0a4200bd0 370 while(true) { // thread loop
aismail1997 1:3af15e979565 371 if (photocell*3.3 < 1.34) {
aismail1997 1:3af15e979565 372 bright = 0;
aismail1997 1:3af15e979565 373 } else {
aismail1997 1:3af15e979565 374 bright = 1;
aismail1997 1:3af15e979565 375 }
aismail1997 4:7ec6407f673f 376 //wait(0.1);
aismail1997 4:7ec6407f673f 377 Thread::wait(500); // wait 0.25s
aismail1997 0:8af0a4200bd0 378 }
aismail1997 0:8af0a4200bd0 379 }
aismail1997 4:7ec6407f673f 380
aismail1997 3:2adc8522c5e8 381 //int mymic;
aismail1997 3:2adc8522c5e8 382 /*
aismail1997 0:8af0a4200bd0 383 // thread reading from - microphone to control rgb
aismail1997 0:8af0a4200bd0 384 void mic_thread(void const *args)
aismail1997 0:8af0a4200bd0 385 {
aismail1997 1:3af15e979565 386 while(true) { // thread loop
aismail1997 1:3af15e979565 387 //read in, subtract 0.67 DC bias, take absolute value, and scale up .1Vpp to 15 for builtin LED display
aismail1997 1:3af15e979565 388 mymic = int(abs((mymicrophone - (0.67/3.3)))*500.0);
aismail1997 1:3af15e979565 389 //Use an 8kHz audio sample rate (phone quality audio);
aismail1997 1:3af15e979565 390 if (mymic > 0.5) {
aismail1997 1:3af15e979565 391 sound = 1;
aismail1997 1:3af15e979565 392 //wait(7); // song is around 8 seconds long
aismail1997 1:3af15e979565 393 } else {
aismail1997 1:3af15e979565 394 sound = 0;
aismail1997 1:3af15e979565 395 }
aismail1997 1:3af15e979565 396 //wait(1.0/8000.0);
aismail1997 1:3af15e979565 397 Thread::wait(1000); // wait 0.25s
aismail1997 1:3af15e979565 398 }
aismail1997 0:8af0a4200bd0 399 }
aismail1997 3:2adc8522c5e8 400 */
aismail1997 0:8af0a4200bd0 401 int main()
aismail1997 0:8af0a4200bd0 402 {
aismail1997 4:7ec6407f673f 403 uLCD.cls();
aismail1997 4:7ec6407f673f 404 wait(1);
aismail1997 1:3af15e979565 405
aismail1997 0:8af0a4200bd0 406 // shiftbrite stuff
aismail1997 0:8af0a4200bd0 407 int red=0;
aismail1997 0:8af0a4200bd0 408 int green=0;
aismail1997 0:8af0a4200bd0 409 int blue=0;
aismail1997 0:8af0a4200bd0 410 spi.format(16,0);
aismail1997 0:8af0a4200bd0 411 spi.frequency(500000);
aismail1997 0:8af0a4200bd0 412 enable=0;
aismail1997 0:8af0a4200bd0 413 latch=0;
aismail1997 0:8af0a4200bd0 414 wait(2);
aismail1997 1:3af15e979565 415
aismail1997 1:3af15e979565 416 // t5 and t6 should not run at the same time
aismail1997 0:8af0a4200bd0 417
aismail1997 0:8af0a4200bd0 418 // call threads here
aismail1997 0:8af0a4200bd0 419 Thread t1(LCD_thread1); //start thread1
aismail1997 0:8af0a4200bd0 420 Thread t2(LCD_thread2); //start thread2
aismail1997 3:2adc8522c5e8 421 Thread t3(speaker_thread); //start thread3
aismail1997 1:3af15e979565 422 //Thread t4(sonar_thread); // start thread4
aismail1997 0:8af0a4200bd0 423 //Thread t5(switchthread); //start thread5
aismail1997 0:8af0a4200bd0 424 //Thread t6(touchpad_thread); //start thread6
aismail1997 4:7ec6407f673f 425 Thread t7(lightsensor_thread); //start thread7
aismail1997 0:8af0a4200bd0 426 //Thread t8(mic_thread); //start thread8
aismail1997 0:8af0a4200bd0 427
aismail1997 4:7ec6407f673f 428 //t1.set_priority(osPriorityHigh);
aismail1997 4:7ec6407f673f 429 //t2.set_priority(osPriorityHigh);
aismail1997 4:7ec6407f673f 430 //t3.set_priority(osPriorityNormal);
aismail1997 4:7ec6407f673f 431 //t5.set_priority(osPriorityHigh);
aismail1997 4:7ec6407f673f 432 //t1.yield();
aismail1997 4:7ec6407f673f 433 //t2.yield();
aismail1997 4:7ec6407f673f 434 //t3.yield();
aismail1997 4:7ec6407f673f 435 //t5.yield();
aismail1997 4:7ec6407f673f 436
aismail1997 1:3af15e979565 437 // running shiftbrite
aismail1997 1:3af15e979565 438 int color = 255;
aismail1997 3:2adc8522c5e8 439
aismail1997 0:8af0a4200bd0 440 while(1) {
aismail1997 0:8af0a4200bd0 441
aismail1997 4:7ec6407f673f 442 if (bright == 0) {
aismail1997 4:7ec6407f673f 443 color = 150;
aismail1997 4:7ec6407f673f 444 green = 100;
aismail1997 4:7ec6407f673f 445 } else if (bright == 1) {
aismail1997 4:7ec6407f673f 446 color = 255;
aismail1997 4:7ec6407f673f 447 green = 0;
aismail1997 4:7ec6407f673f 448 }
aismail1997 3:2adc8522c5e8 449
aismail1997 0:8af0a4200bd0 450 if (sound == 0) {
aismail1997 4:7ec6407f673f 451 RGB_LED(0,0,0);
aismail1997 4:7ec6407f673f 452 Thread::wait(200);
aismail1997 0:8af0a4200bd0 453 } else {
aismail1997 4:7ec6407f673f 454 light = 0;
aismail1997 4:7ec6407f673f 455 red = color; // flash red light
aismail1997 4:7ec6407f673f 456 //green = 0;
aismail1997 4:7ec6407f673f 457 blue = 0;
aismail1997 4:7ec6407f673f 458 RGB_LED(red, green, blue);
aismail1997 4:7ec6407f673f 459 wait(sb_freq);
aismail1997 4:7ec6407f673f 460 //wait(0.5);
aismail1997 0:8af0a4200bd0 461
aismail1997 4:7ec6407f673f 462 RGB_LED(0,0,0);
aismail1997 4:7ec6407f673f 463 wait(sb_freq);
aismail1997 4:7ec6407f673f 464 //wait(0.5);
aismail1997 0:8af0a4200bd0 465
aismail1997 4:7ec6407f673f 466 light = 1;
aismail1997 4:7ec6407f673f 467 red = color; // flash white light
aismail1997 4:7ec6407f673f 468 //green = color;
aismail1997 4:7ec6407f673f 469 blue = color;
aismail1997 4:7ec6407f673f 470 RGB_LED( red, green, blue);
aismail1997 4:7ec6407f673f 471 wait(sb_freq);
aismail1997 4:7ec6407f673f 472 //wait(0.5);
aismail1997 0:8af0a4200bd0 473
aismail1997 4:7ec6407f673f 474 RGB_LED(0,0,0);
aismail1997 4:7ec6407f673f 475 wait(sb_freq);
aismail1997 4:7ec6407f673f 476 //wait(0.5);
aismail1997 0:8af0a4200bd0 477
aismail1997 4:7ec6407f673f 478 light = 2;
aismail1997 4:7ec6407f673f 479 red = 0; // flash blue light
aismail1997 4:7ec6407f673f 480 //green = 0;
aismail1997 4:7ec6407f673f 481 blue = color;
aismail1997 4:7ec6407f673f 482 RGB_LED( red, green, blue);
aismail1997 4:7ec6407f673f 483 wait(sb_freq);
aismail1997 4:7ec6407f673f 484 //wait(0.5);
aismail1997 0:8af0a4200bd0 485
aismail1997 4:7ec6407f673f 486 RGB_LED(0,0,0);
aismail1997 4:7ec6407f673f 487 wait(sb_freq);
aismail1997 4:7ec6407f673f 488 //wait(0.5);
aismail1997 0:8af0a4200bd0 489
aismail1997 4:7ec6407f673f 490 light = 1;
aismail1997 4:7ec6407f673f 491 red = color; // flash white light
aismail1997 4:7ec6407f673f 492 //green = color;
aismail1997 4:7ec6407f673f 493 blue = color;
aismail1997 4:7ec6407f673f 494 RGB_LED( red, green, blue);
aismail1997 4:7ec6407f673f 495 wait(sb_freq);
aismail1997 4:7ec6407f673f 496 //wait(0.5);
aismail1997 0:8af0a4200bd0 497
aismail1997 4:7ec6407f673f 498 RGB_LED(0,0,0);
aismail1997 4:7ec6407f673f 499 wait(sb_freq);
aismail1997 4:7ec6407f673f 500 //wait(0.5);
aismail1997 4:7ec6407f673f 501 Thread::wait(500);
aismail1997 0:8af0a4200bd0 502 }
aismail1997 0:8af0a4200bd0 503 }
aismail1997 0:8af0a4200bd0 504 }