update

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

Committer:
aismail1997
Date:
Thu Oct 13 10:06:25 2016 +0000
Revision:
0:8af0a4200bd0
Child:
1:3af15e979565
updates

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 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 0:8af0a4200bd0 10 //SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
aismail1997 0:8af0a4200bd0 11 //AnalogOut speaker(p26);
aismail1997 0:8af0a4200bd0 12 //wave_player waver(&speaker);
aismail1997 0:8af0a4200bd0 13 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
aismail1997 0:8af0a4200bd0 14 //BusIn joy(p15,p12,p13,p16);
aismail1997 0:8af0a4200bd0 15 BusOut leds(LED1,LED2,LED3,LED4);
aismail1997 0:8af0a4200bd0 16 DigitalIn joyfire(p14);
aismail1997 0:8af0a4200bd0 17
aismail1997 0:8af0a4200bd0 18 // Shiftbrite stuff
aismail1997 0:8af0a4200bd0 19 DigitalOut latch(p15);
aismail1997 0:8af0a4200bd0 20 DigitalOut enable(p16);
aismail1997 0:8af0a4200bd0 21
aismail1997 0:8af0a4200bd0 22 SPI spi(p11, p12, p13);
aismail1997 0:8af0a4200bd0 23
aismail1997 0:8af0a4200bd0 24 // Create the interrupt receiver object on pin 26
aismail1997 0:8af0a4200bd0 25 InterruptIn interrupt(p26);
aismail1997 0:8af0a4200bd0 26 // Setup the i2c bus on pins 9 and 10
aismail1997 0:8af0a4200bd0 27 I2C i2c(p9, p10);
aismail1997 0:8af0a4200bd0 28 // Setup the Mpr121:
aismail1997 0:8af0a4200bd0 29 // constructor(i2c object, i2c address of the mpr121)
aismail1997 0:8af0a4200bd0 30 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
aismail1997 0:8af0a4200bd0 31
aismail1997 0:8af0a4200bd0 32
aismail1997 0:8af0a4200bd0 33 DigitalOut led1(LED1);
aismail1997 0:8af0a4200bd0 34 DigitalOut led2(LED2);
aismail1997 0:8af0a4200bd0 35 DigitalOut led3(LED3);
aismail1997 0:8af0a4200bd0 36 DigitalOut led4(LED4);
aismail1997 0:8af0a4200bd0 37
aismail1997 0:8af0a4200bd0 38 // global variables
aismail1997 0:8af0a4200bd0 39 int light = 0; // to keep track of lighting
aismail1997 0:8af0a4200bd0 40 int sound = 0; // to keep track of sound
aismail1997 0:8af0a4200bd0 41 // led lights
aismail1997 0:8af0a4200bd0 42 //int red;
aismail1997 0:8af0a4200bd0 43 //int green;
aismail1997 0:8af0a4200bd0 44 //int blue;
aismail1997 0:8af0a4200bd0 45
aismail1997 0:8af0a4200bd0 46 //AnalogIn photocell(p15);
aismail1997 0:8af0a4200bd0 47 PwmOut myled(LED1);
aismail1997 0:8af0a4200bd0 48
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 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 0:8af0a4200bd0 73 //microphone mymicrophone(p16);
aismail1997 0:8af0a4200bd0 74
aismail1997 0:8af0a4200bd0 75 void RGB_LED(int red, int green, int blue)
aismail1997 0:8af0a4200bd0 76 {
aismail1997 0:8af0a4200bd0 77 unsigned int low_color=0;
aismail1997 0:8af0a4200bd0 78 unsigned int high_color=0;
aismail1997 0:8af0a4200bd0 79 high_color=(blue<<4)|((red&0x3C0)>>6);
aismail1997 0:8af0a4200bd0 80 low_color=(((red&0x3F)<<10)|(green));
aismail1997 0:8af0a4200bd0 81 spi.write(high_color);
aismail1997 0:8af0a4200bd0 82 spi.write(low_color);
aismail1997 0:8af0a4200bd0 83 latch=1;
aismail1997 0:8af0a4200bd0 84 latch=0;
aismail1997 0:8af0a4200bd0 85 }
aismail1997 0:8af0a4200bd0 86
aismail1997 0:8af0a4200bd0 87 // create threads
aismail1997 0:8af0a4200bd0 88 // first thread dealing with LCD
aismail1997 0:8af0a4200bd0 89 void LCD_thread1(void const *args)
aismail1997 0:8af0a4200bd0 90 {
aismail1997 0:8af0a4200bd0 91 while(true) { // thread loop
aismail1997 0:8af0a4200bd0 92 lcd_mutex.lock();
aismail1997 0:8af0a4200bd0 93 if (light == 0 && sound == 1) {
aismail1997 0:8af0a4200bd0 94 // display red siren
aismail1997 0:8af0a4200bd0 95 uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, RED);
aismail1997 0:8af0a4200bd0 96 uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, RED);
aismail1997 0:8af0a4200bd0 97 } else if (light == 1 ) {
aismail1997 0:8af0a4200bd0 98 // display white siren
aismail1997 0:8af0a4200bd0 99 uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, WHITE);
aismail1997 0:8af0a4200bd0 100 uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, WHITE);
aismail1997 0:8af0a4200bd0 101 } else if (light == 2 && sound == 1) {
aismail1997 0:8af0a4200bd0 102 // display blue siren
aismail1997 0:8af0a4200bd0 103 uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, BLUE);
aismail1997 0:8af0a4200bd0 104 uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, BLUE);
aismail1997 0:8af0a4200bd0 105 }
aismail1997 0:8af0a4200bd0 106 lcd_mutex.unlock();
aismail1997 0:8af0a4200bd0 107 Thread::wait(5); // wait till thread is done
aismail1997 0:8af0a4200bd0 108 }
aismail1997 0:8af0a4200bd0 109 }
aismail1997 0:8af0a4200bd0 110
aismail1997 0:8af0a4200bd0 111 // second thread dealing with LCD
aismail1997 0:8af0a4200bd0 112 void LCD_thread2(void const *args)
aismail1997 0:8af0a4200bd0 113 {
aismail1997 0:8af0a4200bd0 114 while(true) { // thread loop
aismail1997 0:8af0a4200bd0 115 lcd_mutex.lock();
aismail1997 0:8af0a4200bd0 116 if (sound == 1) {
aismail1997 0:8af0a4200bd0 117 // some indication there is audio
aismail1997 0:8af0a4200bd0 118 // text saying "!ALERT!"
aismail1997 0:8af0a4200bd0 119 uLCD.color(0xFF0000);
aismail1997 0:8af0a4200bd0 120 uLCD.locate(6,1);
aismail1997 0:8af0a4200bd0 121 uLCD.set_font_size(7, 7);
aismail1997 0:8af0a4200bd0 122 uLCD.printf("!ALERT !");
aismail1997 0:8af0a4200bd0 123 } else if (sound == 0) {
aismail1997 0:8af0a4200bd0 124 uLCD.color(0x000000);
aismail1997 0:8af0a4200bd0 125 uLCD.locate(6,1);
aismail1997 0:8af0a4200bd0 126 uLCD.set_font_size(7, 7);
aismail1997 0:8af0a4200bd0 127 uLCD.printf("!ALERT !");
aismail1997 0:8af0a4200bd0 128 // no indication
aismail1997 0:8af0a4200bd0 129 // text removed
aismail1997 0:8af0a4200bd0 130
aismail1997 0:8af0a4200bd0 131 }
aismail1997 0:8af0a4200bd0 132 lcd_mutex.unlock();
aismail1997 0:8af0a4200bd0 133 Thread::wait(5); // wait till thread is done
aismail1997 0:8af0a4200bd0 134 }
aismail1997 0:8af0a4200bd0 135 }
aismail1997 0:8af0a4200bd0 136
aismail1997 0:8af0a4200bd0 137 // thread dealing with speaker
aismail1997 0:8af0a4200bd0 138
aismail1997 0:8af0a4200bd0 139 void speaker_thread(void const *args)
aismail1997 0:8af0a4200bd0 140 {
aismail1997 0:8af0a4200bd0 141
aismail1997 0:8af0a4200bd0 142 /* FILE *wave_file;
aismail1997 0:8af0a4200bd0 143 wave_file = fopen("/sd/police_siren.wav","r");
aismail1997 0:8af0a4200bd0 144 // play siren
aismail1997 0:8af0a4200bd0 145 while(true) { // thread loop
aismail1997 0:8af0a4200bd0 146 sound = 1;
aismail1997 0:8af0a4200bd0 147 //wait(5);
aismail1997 0:8af0a4200bd0 148 waver.play(wave_file);
aismail1997 0:8af0a4200bd0 149
aismail1997 0:8af0a4200bd0 150 // generate a 500Hz tone using PWM hardware output
aismail1997 0:8af0a4200bd0 151 //speaker.period(1.0/500.0); // 500hz period
aismail1997 0:8af0a4200bd0 152 //speaker =0.5; //50% duty cycle - max volume
aismail1997 0:8af0a4200bd0 153 //wait(3);
aismail1997 0:8af0a4200bd0 154 //speaker=0.0; // turn off audio
aismail1997 0:8af0a4200bd0 155 //wait(2);
aismail1997 0:8af0a4200bd0 156 speaker = 0;
aismail1997 0:8af0a4200bd0 157 wait(5);
aismail1997 0:8af0a4200bd0 158 Thread::wait(1000); // wait 1s
aismail1997 0:8af0a4200bd0 159 //speaker=0.0; // off
aismail1997 0:8af0a4200bd0 160 }
aismail1997 0:8af0a4200bd0 161 //Thread::wait(100); // wait 1s
aismail1997 0:8af0a4200bd0 162
aismail1997 0:8af0a4200bd0 163 fclose(wave_file);*/
aismail1997 0:8af0a4200bd0 164 }
aismail1997 0:8af0a4200bd0 165
aismail1997 0:8af0a4200bd0 166 // thread reading from - ultrasonic sensor to do XYZ
aismail1997 0:8af0a4200bd0 167
aismail1997 0:8af0a4200bd0 168
aismail1997 0:8af0a4200bd0 169 // thread reading from - tactile switch to control RGB/sound/lcd
aismail1997 0:8af0a4200bd0 170 void switchthread(void const *args)
aismail1997 0:8af0a4200bd0 171 {
aismail1997 0:8af0a4200bd0 172 while(true) { // thread loop
aismail1997 0:8af0a4200bd0 173 /* if (joyfire) {
aismail1997 0:8af0a4200bd0 174 leds = 0xf;
aismail1997 0:8af0a4200bd0 175 } else {
aismail1997 0:8af0a4200bd0 176 leds = joy;
aismail1997 0:8af0a4200bd0 177 }
aismail1997 0:8af0a4200bd0 178 Thread::wait(1000); // wait 0.25s*/
aismail1997 0:8af0a4200bd0 179 }
aismail1997 0:8af0a4200bd0 180 }
aismail1997 0:8af0a4200bd0 181
aismail1997 0:8af0a4200bd0 182 // thread reading from - touch keypad same
aismail1997 0:8af0a4200bd0 183 void touchpad_thread(void const *args)
aismail1997 0:8af0a4200bd0 184 {
aismail1997 0:8af0a4200bd0 185 while(true) { // thread loop
aismail1997 0:8af0a4200bd0 186 int key_code=0;
aismail1997 0:8af0a4200bd0 187 int i=0;
aismail1997 0:8af0a4200bd0 188 int value=mpr121.read(0x00);
aismail1997 0:8af0a4200bd0 189 value +=mpr121.read(0x01)<<8;
aismail1997 0:8af0a4200bd0 190 // LED demo mod
aismail1997 0:8af0a4200bd0 191 i=0;
aismail1997 0:8af0a4200bd0 192 // puts key number out to LEDs for demo
aismail1997 0:8af0a4200bd0 193 for (i=0; i<12; i++) {
aismail1997 0:8af0a4200bd0 194 if (((value>>i)&0x01)==1) key_code=i+1;
aismail1997 0:8af0a4200bd0 195 }
aismail1997 0:8af0a4200bd0 196 led4=key_code & 0x01;
aismail1997 0:8af0a4200bd0 197 led3=(key_code>>1) & 0x01;
aismail1997 0:8af0a4200bd0 198 led2=(key_code>>2) & 0x01;
aismail1997 0:8af0a4200bd0 199 led1=(key_code>>3) & 0x01;
aismail1997 0:8af0a4200bd0 200 Thread::wait(1000); // wait 0.25s
aismail1997 0:8af0a4200bd0 201 }
aismail1997 0:8af0a4200bd0 202 }
aismail1997 0:8af0a4200bd0 203
aismail1997 0:8af0a4200bd0 204 // thread reading from - light sensor to do something
aismail1997 0:8af0a4200bd0 205 void lightsensor_thread(void const *args)
aismail1997 0:8af0a4200bd0 206 {
aismail1997 0:8af0a4200bd0 207 while(true) { // thread loop
aismail1997 0:8af0a4200bd0 208 /* myled = photocell;
aismail1997 0:8af0a4200bd0 209 wait(0.1);
aismail1997 0:8af0a4200bd0 210 Thread::wait(1000); // wait 0.25s*/
aismail1997 0:8af0a4200bd0 211 }
aismail1997 0:8af0a4200bd0 212 }
aismail1997 0:8af0a4200bd0 213
aismail1997 0:8af0a4200bd0 214 // thread reading from - microphone to control rgb
aismail1997 0:8af0a4200bd0 215 void mic_thread(void const *args)
aismail1997 0:8af0a4200bd0 216 {
aismail1997 0:8af0a4200bd0 217 /* while(true) { // thread loop
aismail1997 0:8af0a4200bd0 218 //read in, subtract 0.67 DC bias, take absolute value, and scale up .1Vpp to 15 for builtin LED display
aismail1997 0:8af0a4200bd0 219 leds = int(abs((mymicrophone - (0.67/3.3)))*500.0);
aismail1997 0:8af0a4200bd0 220 //Use an 8kHz audio sample rate (phone quality audio);
aismail1997 0:8af0a4200bd0 221 wait(1.0/8000.0);
aismail1997 0:8af0a4200bd0 222 Thread::wait(1000); // wait 0.25s
aismail1997 0:8af0a4200bd0 223 }*/
aismail1997 0:8af0a4200bd0 224 }
aismail1997 0:8af0a4200bd0 225
aismail1997 0:8af0a4200bd0 226 int main()
aismail1997 0:8af0a4200bd0 227 {
aismail1997 0:8af0a4200bd0 228 //uLCD.cls();
aismail1997 0:8af0a4200bd0 229 wait(1);
aismail1997 0:8af0a4200bd0 230 // shiftbrite stuff
aismail1997 0:8af0a4200bd0 231
aismail1997 0:8af0a4200bd0 232 int red=0;
aismail1997 0:8af0a4200bd0 233 int green=0;
aismail1997 0:8af0a4200bd0 234 int blue=0;
aismail1997 0:8af0a4200bd0 235 spi.format(16,0);
aismail1997 0:8af0a4200bd0 236 spi.frequency(500000);
aismail1997 0:8af0a4200bd0 237 enable=0;
aismail1997 0:8af0a4200bd0 238 latch=0;
aismail1997 0:8af0a4200bd0 239 wait(2);
aismail1997 0:8af0a4200bd0 240 // draw police car base
aismail1997 0:8af0a4200bd0 241
aismail1997 0:8af0a4200bd0 242 // call threads here
aismail1997 0:8af0a4200bd0 243 Thread t1(LCD_thread1); //start thread1
aismail1997 0:8af0a4200bd0 244 Thread t2(LCD_thread2); //start thread2
aismail1997 0:8af0a4200bd0 245 //Thread t4(speaker_thread); //start thread4
aismail1997 0:8af0a4200bd0 246 //Thread t5(switchthread); //start thread5
aismail1997 0:8af0a4200bd0 247 //Thread t6(touchpad_thread); //start thread6
aismail1997 0:8af0a4200bd0 248 //Thread t7(lightsensor_thread); //start thread7
aismail1997 0:8af0a4200bd0 249 //Thread t8(mic_thread); //start thread8
aismail1997 0:8af0a4200bd0 250
aismail1997 0:8af0a4200bd0 251 while(1) {
aismail1997 0:8af0a4200bd0 252
aismail1997 0:8af0a4200bd0 253 if (sound == 0) {
aismail1997 0:8af0a4200bd0 254 RGB_LED(0,0,0);
aismail1997 0:8af0a4200bd0 255 //Thread::wait(500);
aismail1997 0:8af0a4200bd0 256 } else {
aismail1997 0:8af0a4200bd0 257 light = 0;
aismail1997 0:8af0a4200bd0 258 red = 200; // flash red light
aismail1997 0:8af0a4200bd0 259 green = 0;
aismail1997 0:8af0a4200bd0 260 blue = 0;
aismail1997 0:8af0a4200bd0 261 RGB_LED(red, green, blue);
aismail1997 0:8af0a4200bd0 262 wait(0.5);
aismail1997 0:8af0a4200bd0 263
aismail1997 0:8af0a4200bd0 264 RGB_LED(0,0,0);
aismail1997 0:8af0a4200bd0 265 wait(0.5);
aismail1997 0:8af0a4200bd0 266
aismail1997 0:8af0a4200bd0 267 light = 1;
aismail1997 0:8af0a4200bd0 268 red = 200; // flash white light
aismail1997 0:8af0a4200bd0 269 green = 200;
aismail1997 0:8af0a4200bd0 270 blue = 200;
aismail1997 0:8af0a4200bd0 271 RGB_LED( red, green, blue);
aismail1997 0:8af0a4200bd0 272 wait(0.5);
aismail1997 0:8af0a4200bd0 273
aismail1997 0:8af0a4200bd0 274 RGB_LED(0,0,0);
aismail1997 0:8af0a4200bd0 275 wait(0.5);
aismail1997 0:8af0a4200bd0 276
aismail1997 0:8af0a4200bd0 277 light = 2;
aismail1997 0:8af0a4200bd0 278 red = 0; // flash blue light
aismail1997 0:8af0a4200bd0 279 green = 0;
aismail1997 0:8af0a4200bd0 280 blue = 200;
aismail1997 0:8af0a4200bd0 281 RGB_LED( red, green, blue);
aismail1997 0:8af0a4200bd0 282 wait(0.5);
aismail1997 0:8af0a4200bd0 283
aismail1997 0:8af0a4200bd0 284 RGB_LED(0,0,0);
aismail1997 0:8af0a4200bd0 285 wait(0.5);
aismail1997 0:8af0a4200bd0 286
aismail1997 0:8af0a4200bd0 287 light = 1;
aismail1997 0:8af0a4200bd0 288 red = 200; // flash white light
aismail1997 0:8af0a4200bd0 289 green = 200;
aismail1997 0:8af0a4200bd0 290 blue = 200;
aismail1997 0:8af0a4200bd0 291 RGB_LED( red, green, blue);
aismail1997 0:8af0a4200bd0 292 wait(0.5);
aismail1997 0:8af0a4200bd0 293
aismail1997 0:8af0a4200bd0 294 RGB_LED(0,0,0);
aismail1997 0:8af0a4200bd0 295 }
aismail1997 0:8af0a4200bd0 296 Thread::wait(500);
aismail1997 0:8af0a4200bd0 297 }
aismail1997 0:8af0a4200bd0 298
aismail1997 0:8af0a4200bd0 299
aismail1997 0:8af0a4200bd0 300
aismail1997 0:8af0a4200bd0 301 }