Azra Ismail / Mbed 2 deprecated Lab3-works

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

Fork of Lab3 by Azra Ismail

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Part2.cpp Source File

Part2.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "uLCD_4DGL.h"
00004 //#include "SDFileSystem.h"
00005 //#include "wave_player.h"
00006 #include <mpr121.h>
00007 
00008 // mutex to make the lcd lib thread safe
00009 Mutex lcd_mutex;
00010 //SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
00011 PwmOut speaker(p26);
00012 //wave_player waver(&speaker);
00013 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin
00014 //Semaphore lcd_sem(1);
00015 
00016 // Shiftbrite
00017 DigitalOut latch(p15);
00018 DigitalOut enable(p16);
00019 SPI spi(p11, p12, p13);
00020 /*
00021 // Touchpad
00022 // Create the interrupt receiver object on pin 26
00023 InterruptIn interrupt(p21);
00024 // Setup the i2c bus on pins 9 and 10
00025 I2C i2c(p9, p10);
00026 // Setup the Mpr121:
00027 // constructor(i2c object, i2c address of the mpr121)
00028 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00029 */
00030 // Test leds
00031 DigitalOut led1(LED1);
00032 DigitalOut led2(LED2);
00033 DigitalOut led3(LED3);
00034 DigitalOut led4(LED4);
00035 
00036 // sonar
00037 
00038 DigitalOut trigger(p20);
00039 //DigitalOut myled(LED1); //monitor trigger
00040 //DigitalOut myled2(LED2); //monitor echo
00041 DigitalIn echo(p19);
00042 int distance = 0;
00043 int correction = 0;
00044 Timer sonar;
00045 
00046 
00047 // global variables
00048 int light = 0; // to keep track of lighting
00049 int sound = 1; // to keep track of sound
00050 int sb_freq = 1; // rgb frequency
00051 int bright = 1; // time of the day
00052 
00053 // Light sensor
00054 AnalogIn photocell(p17);
00055 //PwmOut myled(LED1);
00056 
00057 // Microphone
00058 class microphone
00059 {
00060 public :
00061     microphone(PinName pin);
00062     float read();
00063     operator float ();
00064 private :
00065     AnalogIn _pin;
00066 };
00067 
00068 microphone::microphone (PinName pin):
00069     _pin(pin)
00070 {
00071 }
00072 
00073 float microphone::read()
00074 {
00075     return _pin.read();
00076 }
00077 
00078 inline microphone::operator float ()
00079 {
00080     return _pin.read();
00081 }
00082 
00083 
00084 // joystick
00085 class Nav_Switch
00086 {
00087 public:
00088     Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
00089     int read();
00090 //boolean functions to test each switch
00091     bool up();
00092     bool down();
00093     bool left();
00094     bool right();
00095     bool fire();
00096 //automatic read on RHS
00097     operator int ();
00098 //index to any switch array style
00099     bool operator[](int index) {
00100         return _pins[index];
00101     };
00102 private:
00103     BusIn _pins;
00104 
00105 };
00106 Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
00107     _pins(up, down, left, right, fire)
00108 {
00109     _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
00110     wait(0.001); //delays just a bit for pullups to pull inputs high
00111 }
00112 inline bool Nav_Switch::up()
00113 {
00114     return !(_pins[0]);
00115 }
00116 inline bool Nav_Switch::down()
00117 {
00118     return !(_pins[1]);
00119 }
00120 inline bool Nav_Switch::left()
00121 {
00122     return !(_pins[2]);
00123 }
00124 inline bool Nav_Switch::right()
00125 {
00126     return !(_pins[3]);
00127 }
00128 inline bool Nav_Switch::fire()
00129 {
00130     return !(_pins[4]);
00131 }
00132 inline int Nav_Switch::read()
00133 {
00134     return _pins.read();
00135 }
00136 inline Nav_Switch::operator int ()
00137 {
00138     return _pins.read();
00139 }
00140 
00141 //microphone mymicrophone(p19); // uncomment when using
00142 
00143 // joystick
00144 //Nav_Switch myNav(p25, p22, p23, p21, p24); //pin order on Sparkfun breakout
00145 
00146 // Shiftbrite
00147 void RGB_LED(int red, int green, int blue)
00148 {
00149     unsigned int low_color=0;
00150     unsigned int high_color=0;
00151     high_color=(blue<<4)|((red&0x3C0)>>6);
00152     low_color=(((red&0x3F)<<10)|(green));
00153     spi.write(high_color);
00154     spi.write(low_color);
00155     latch=1;
00156     latch=0;
00157 }
00158 
00159 // create threads
00160 // first thread dealing with LCD
00161 void LCD_thread1(void const *args)
00162 {
00163     while(true) {       // thread loop
00164         lcd_mutex.lock();
00165         //lcd_sem.wait();
00166         //if (light == 0 && sound == 1) {
00167         if (light == 0) {
00168             // display red siren
00169             uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, RED);
00170             uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, RED);
00171             //}        else if (light == 1 || sound == 0) {
00172         } else if (light == 1) {
00173             // display white siren
00174             uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, WHITE);
00175             uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, WHITE);
00176             //}        else if (light == 2 && sound == 1) {
00177         } else if (light == 2) {
00178             // display  blue siren
00179             uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, BLUE);
00180             uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, BLUE);
00181         }
00182         lcd_mutex.unlock();
00183         //lcd_sem.release();
00184         Thread::wait(5); // wait till thread is done
00185     }
00186 }
00187 
00188 // second thread dealing with LCD
00189 void LCD_thread2(void const *args)
00190 {
00191     while(true) {       // thread loop
00192         lcd_mutex.lock();
00193         //lcd_sem.wait();
00194         if (sound == 1) {
00195             // some indication there is audio
00196             // text saying "!ALERT!"
00197             uLCD.color(0xFF0000);
00198             uLCD.locate(6,1);
00199             uLCD.set_font_size(7, 7);
00200             uLCD.printf("!ALERT !");
00201         } else if (sound == 0) {
00202             //uLCD.color(0x000000);
00203             uLCD.locate(6,1);
00204             uLCD.set_font_size(7, 7);
00205             uLCD.textbackground_color(BLACK);
00206             uLCD.printf("        ");
00207             // no indication
00208             // text removed
00209         }
00210         lcd_mutex.unlock();
00211         //lcd_sem.release();
00212         Thread::wait(5); // wait till thread is done
00213     }
00214 }
00215 
00216 // thread playing video
00217 void video_thread(void const *args)
00218 {
00219     uLCD.media_init();
00220     uLCD.set_sector_address(0x001D, 0x4C42);
00221     uLCD.display_video(0,0);
00222 }
00223 
00224 int play = 1;
00225 
00226 // thread dealing with speaker
00227 void speaker_thread(void const *args)
00228 {
00229     // play siren
00230     while(true) {         // thread loop
00231         /*FILE * wave_file;
00232         wave_file = fopen("/sd/police-siren.wav","r");
00233         if (play == 1) {
00234         sound = 1; // comment when using mic
00235         waver.play(wave_file);
00236         //play = 0; // comment when using joystick or touchpad
00237         } else if (play == 0) {
00238         // how can we stop play midway? - reduce volume?
00239         sound = 0; // comment when using mic
00240         wait(5); // comment when using joystick or touchpad
00241         play = 1; // comment when using joystick or touchpad
00242         }
00243         fclose(wave_file);*/
00244         //if (play == 1) {
00245         for (int i=0; i<1000; i=i+2) {
00246             speaker.period(1.0/969.0);
00247             speaker = float(i/50.0);
00248             wait(.5);
00249             speaker.period(1.0/800.0);
00250             wait(.5);
00251             sound = 1;
00252             if (play == 0) {
00253                 sound = 0;
00254                 //wait(1);
00255                 break;
00256             }
00257             //  }
00258         }
00259         ///wait(1);
00260         Thread::wait(1000);    // wait 1s
00261     }
00262 }
00263 
00264 // thread reading from - ultrasonic sensor to do XYZ
00265 void sonar_thread(void const *args)
00266 {
00267     sonar.reset();
00268     sonar.start();
00269     while(echo==2) {}
00270 
00271     //while (echo==2) {};
00272     //led2 = 0;
00273 // stop timer
00274     sonar.stop();
00275 // read timer
00276     correction = sonar.read_us();
00277 //    printf("Approximate software overhead timer delay is %d uS\n\r",correction);
00278 
00279 //Loop to read Sonar distance values, scale, and print
00280     while(1) {
00281 // trigger sonar to send a ping
00282         trigger = 1;
00283         //led1 = 1;
00284         //led2 = 0;
00285         sonar.reset();
00286         wait_us(10.0);
00287         trigger = 0;
00288         //led1 = 0;
00289 //wait for echo high
00290         while (echo==0) {};
00291         //led2=echo;
00292 //echo high, so start timer
00293         sonar.start();
00294 //wait for echo low
00295         while (echo==1) {};
00296 //stop timer and read value
00297         sonar.stop();
00298 //subtract software overhead timer delay and scale to cm
00299         distance = (sonar.read_us()-correction)/58.0;
00300         //led2 = 0;
00301 //        printf(" %d cm \n\r",distance);
00302 //wait so that any echo(s) return before sending another ping
00303         //wait(0.2);
00304         if (distance < 10) {
00305             play = 0;
00306         } else {
00307             play = 1;
00308         }
00309         Thread::wait(500);
00310     }
00311 }
00312 
00313 
00314 // thread reading from - tactile switch to control RGB/sound/lcd
00315 /*
00316 void switchthread(void const *args)
00317 {
00318     while(true) {
00319         // control sound & RGB frequency
00320         //with pullups a button hit is a "0" - "~" inverts data to leds
00321         //mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs FOR TESTING ONLY
00322         if (myNav.up()) play = 1;
00323         if (myNav.down()) play = 0;
00324         if (sb_freq > 0.5) {
00325             if (myNav.left()) sb_freq -= 0.2;
00326         }
00327         if (sb_freq < 3) {
00328             if (myNav.right()) sb_freq += 0.2;
00329         }
00330         //if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button)
00331         //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this
00332         Thread::wait(200);
00333     }
00334 }*/
00335 /*
00336 // thread reading from - touch keypad same
00337 void touchpad_thread(void const *args)
00338 {
00339     while(true) {         // thread loop
00340         int key_code = 0;
00341         int i=0;
00342         int value=mpr121.read(0x00);
00343         value +=mpr121.read(0x01)<<8;
00344         // LED demo mod
00345         i=0;
00346         // puts key number out to LEDs for demo
00347         for (i=0; i<12; i++) {
00348             if (((value>>i)&0x01)==1) key_code=i;
00349         }
00350         if (key_code == 6) {
00351             play = 1;
00352         } else if (key_code == 4) {
00353             play = 0;
00354         } else if (key_code == 1) {
00355             if (sb_freq > 0.5) {
00356                 sb_freq += -0.2;
00357             }
00358         } else if (key_code == 9) {
00359             if (sb_freq < 3) {
00360                 sb_freq += 0.2;
00361             }
00362         }
00363     }
00364 }
00365 */
00366 // thread reading from - light sensor to do something
00367 
00368 void lightsensor_thread(void const *args)
00369 {
00370     while(true) {         // thread loop
00371         if (photocell*3.3 < 1.34) {
00372             bright = 0;
00373         } else {
00374             bright = 1;
00375         }
00376         //wait(0.1);
00377         Thread::wait(500);    // wait 0.25s
00378     }
00379 }
00380 
00381 //int mymic;
00382 /*
00383 // thread reading from - microphone to control rgb
00384 void mic_thread(void const *args)
00385 {
00386     while(true) {         // thread loop
00387         //read in, subtract 0.67 DC bias, take absolute value, and scale up .1Vpp to 15 for builtin LED display
00388         mymic = int(abs((mymicrophone - (0.67/3.3)))*500.0);
00389         //Use an 8kHz audio sample rate (phone quality audio);
00390         if (mymic > 0.5) {
00391             sound = 1;
00392             //wait(7); // song is around 8 seconds long
00393         } else {
00394             sound = 0;
00395         }
00396         //wait(1.0/8000.0);
00397         Thread::wait(1000);    // wait 0.25s
00398     }
00399 }
00400 */
00401 int main()
00402 {
00403     uLCD.cls();
00404     wait(1);
00405 
00406     // shiftbrite stuff
00407     int red=0;
00408     int green=0;
00409     int blue=0;
00410     spi.format(16,0);
00411     spi.frequency(500000);
00412     enable=0;
00413     latch=0;
00414     wait(2);
00415 
00416     // t5 and t6 should not run at the same time
00417 
00418     // call threads here
00419     Thread t1(LCD_thread1); //start thread1
00420     Thread t2(LCD_thread2); //start thread2
00421     Thread t3(speaker_thread); //start thread3
00422     //Thread t4(sonar_thread); // start thread4
00423     //Thread t5(switchthread); //start thread5
00424     //Thread t6(touchpad_thread); //start thread6
00425     Thread t7(lightsensor_thread); //start thread7
00426     //Thread t8(mic_thread); //start thread8
00427 
00428     //t1.set_priority(osPriorityHigh);
00429     //t2.set_priority(osPriorityHigh);
00430     //t3.set_priority(osPriorityNormal);
00431     //t5.set_priority(osPriorityHigh);
00432     //t1.yield();
00433     //t2.yield();
00434     //t3.yield();
00435     //t5.yield();
00436 
00437     // running shiftbrite
00438     int color = 255;
00439 
00440     while(1) {
00441 
00442          if (bright == 0) {
00443              color = 150;
00444              green = 100;
00445          } else if (bright == 1) {
00446              color = 255;
00447              green = 0;
00448          }
00449 
00450         if (sound == 0) {
00451             RGB_LED(0,0,0);
00452             Thread::wait(200);
00453         } else {
00454             light = 0;
00455             red = color;     // flash red light
00456             //green = 0;
00457             blue = 0;
00458             RGB_LED(red, green, blue);
00459             wait(sb_freq);
00460             //wait(0.5);
00461 
00462             RGB_LED(0,0,0);
00463             wait(sb_freq);
00464             //wait(0.5);
00465 
00466             light = 1;
00467             red = color;     // flash white light
00468             //green = color;
00469             blue = color;
00470             RGB_LED( red, green, blue);
00471             wait(sb_freq);
00472             //wait(0.5);
00473 
00474             RGB_LED(0,0,0);
00475             wait(sb_freq);
00476             //wait(0.5);
00477 
00478             light = 2;
00479             red = 0;     // flash blue light
00480             //green = 0;
00481             blue = color;
00482             RGB_LED( red, green, blue);
00483             wait(sb_freq);
00484             //wait(0.5);
00485 
00486             RGB_LED(0,0,0);
00487             wait(sb_freq);
00488             //wait(0.5);
00489 
00490             light = 1;
00491             red = color;     // flash white light
00492             //green = color;
00493             blue = color;
00494             RGB_LED( red, green, blue);
00495             wait(sb_freq);
00496             //wait(0.5);
00497 
00498             RGB_LED(0,0,0);
00499             wait(sb_freq);
00500             //wait(0.5);
00501             Thread::wait(500);
00502         }
00503     }
00504 }