ECE4180 / Mbed 2 deprecated Lab4_Two_Factor2

Dependencies:   4DGL-uLCD-SE EthernetInterface PinDetect mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
landes
Date:
Wed Oct 21 21:56:10 2015 +0000
Child:
1:bfaa82809420
Commit message:
first commit

Changed in this revision

4DGL-uLCD-SE.lib Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
PinDetect.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
SongPlayer.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
wave_player.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/4DGL-uLCD-SE.lib	Wed Oct 21 21:56:10 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Wed Oct 21 21:56:10 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#0d467ed3aa6e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PinDetect.lib	Wed Oct 21 21:56:10 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Wed Oct 21 21:56:10 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SongPlayer.h	Wed Oct 21 21:56:10 2015 +0000
@@ -0,0 +1,41 @@
+#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
+class SongPlayer
+{
+public:
+    SongPlayer(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the constructor
+    }
+// class method to play a note based on PwmOut class
+    void PlaySong(float frequency[], float duration[], float volume=1.0) {
+        vol = volume;
+        notecount = 0;
+        _pin.period(1.0/frequency[notecount]);
+        _pin = volume/2.0;
+        noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]);
+        // setup timer to interrupt for next note to play
+        frequencyptr = frequency;
+        durationptr = duration;
+        //returns after first note starts to play
+    }
+    void nextnote();
+private:
+    Timeout noteduration;
+    PwmOut _pin;
+    int notecount;
+    float vol;
+    float * frequencyptr;
+    float * durationptr;
+};
+//Interrupt Routine to play next note
+void SongPlayer::nextnote()
+{
+    _pin = 0.0;
+    notecount++; //setup next note in song
+    if (durationptr[notecount]!=0.0) {
+        _pin.period(1.0/frequencyptr[notecount]);
+        noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
+        _pin = vol/2.0;
+    } else
+        _pin = 0.0; //turn off on last note
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Oct 21 21:56:10 2015 +0000
@@ -0,0 +1,521 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "uLCD_4DGL.h"
+//#include "SDFileSystem.h"
+//#include "wave_player.h"
+#include "SongPlayer.h"
+#include "PinDetect.h"
+#include "string.h"
+#include "EthernetInterface.h"
+
+//uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
+uLCD_4DGL uLCD(p28, p27, p29); 
+//SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
+DigitalOut latch(p15);
+DigitalOut enable(p16);
+//AnalogOut DACout(p18);
+//Cycles through different colors on RGB LED
+SPI spi(p11, p12, p13);
+//Set up IR sensor 
+AnalogIn IrSensor(p20);
+Serial serial(USBTX, USBRX);
+SongPlayer mySpeaker(p26);
+
+// ethernet setup    
+EthernetInterface eth;
+
+DigitalOut myLED1(LED1);
+DigitalOut myLED2(LED2);
+DigitalOut myLED3(LED3);
+DigitalOut myLED4(LED4);
+
+PinDetect pb1(p21);
+PinDetect pb2(p22);
+PinDetect pb3(p23);
+PinDetect pb4(p24);
+
+
+//void Thread_maintnence(char Thread); 
+void init_pbs(void);
+enum Statetype { Armed =0 ,IR_sensed = 1,Second_Step = 2,Cleared = 3, Alarm_ON = 4};
+Statetype state = Armed;
+
+char Code[4] = {0,0,0,0}; 
+int CodeCounter = 0; 
+
+void pb1_hit_callback (void) {
+    if (state == IR_sensed) {
+    myLED1 = !myLED1;
+    Code[CodeCounter] = 1;
+    //printf("\n  PB: %d\n\r",Code[CodeCounter]);
+    CodeCounter++;
+    wait(0.500);
+    }
+}
+void pb2_hit_callback (void) {
+    if (state == IR_sensed) {
+    myLED2 = !myLED2;
+    Code[CodeCounter] = 2;
+    //printf("\n  PB: %d\n\r",Code[CodeCounter]);
+    CodeCounter++;
+    wait(0.500);
+    }
+}
+void pb3_hit_callback (void) {
+    if (state == IR_sensed) {
+    myLED3 = !myLED3;
+    Code[CodeCounter] = 3;
+    //printf("\n  PB: %d\n\r",Code[CodeCounter]);
+    CodeCounter++;
+    wait(0.500);
+    }
+}
+void pb4_hit_callback (void) {
+   if (state == IR_sensed) {
+   myLED4 = !myLED4;
+   Code[CodeCounter] = 4;
+   //printf("\n  PB: %d\n\r",Code[CodeCounter]);
+   CodeCounter++;
+   wait(0.500);
+   }
+}
+
+
+
+
+
+//Mutex stdio_mutex; 
+Semaphore LCD_Access(2);
+Semaphore Consul_Access(5);
+float IrVoltage = 0.0;
+
+
+//wave_player waver(&DACout);
+
+
+float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
+                 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
+};
+float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
+                     0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
+};
+
+
+
+void init_LCD();
+void RGB_LED(int red, int green, int blue);
+//void LCD_Armed(void);
+
+
+    
+void Speaker_thread(void const *args) {
+    while (1) {
+    if (state == Alarm_ON) {
+        Consul_Access.wait();
+        serial.printf("\n  play sound\n\r");
+        Consul_Access.release();
+        mySpeaker.PlaySong(note,duration);
+        Thread::wait(5000); 
+        }
+    }
+}
+
+void uLCD_thread(void const *args) {
+    int Change = 99; 
+    Consul_Access.wait();
+    //serial.printf("state  %d  %d\n\r",Change,state);
+    Consul_Access.release();
+    while(1) {
+        
+        if (Change != state) {
+            Change = state;
+            switch (state) {
+                case Armed:
+                    LCD_Access.wait();
+                    uLCD.cls();
+                    uLCD.color(WHITE);
+                    uLCD.printf("     ARMED           \n               ");
+                    LCD_Access.release();
+                break;
+                case IR_sensed:
+                    LCD_Access.wait();
+                    uLCD.cls();
+                    uLCD.printf("\nSensor triggred \n"); //Default Green on black text
+                    uLCD.printf("\n  Enter the code ...");
+                    uLCD.text_width(4); //4X size text
+                    uLCD.text_height(4);
+                    uLCD.color(RED);
+                    LCD_Access.release();
+                    for (int i=30; i>=0; --i) {
+                        if (state == IR_sensed) {
+                            LCD_Access.wait();
+                            uLCD.text_width(4); //4X size text
+                            uLCD.text_height(4);
+                            uLCD.color(RED);
+                            uLCD.locate(1,2);
+                            uLCD.printf("%2D",i);
+                            LCD_Access.release();
+                            Consul_Access.wait();
+                            serial.printf("CC: %d%d%d%d\n\r",Code[0],Code[1],Code[2],Code[3]);
+                            Consul_Access.release();
+                            Thread::wait(100);
+                            }
+                        }
+                        if (state == IR_sensed) {
+                            state = Alarm_ON;
+                            }
+
+                break;
+                case Second_Step: 
+                    LCD_Access.wait();
+                    uLCD.cls();
+                    uLCD.color(BLUE);
+                    uLCD.printf("\nPleas enter code from text massage \n");
+                    LCD_Access.release();
+                    break;
+                case Alarm_ON:
+                    LCD_Access.wait();
+                    uLCD.cls();
+                    uLCD.color(RED);
+                    uLCD.printf("\nThis house is being broken into. \nThe athoroties have been informed \n"); 
+                    LCD_Access.release();
+                    break;
+                case Cleared: 
+                    LCD_Access.wait();
+                    uLCD.cls();
+                    uLCD.color(GREEN);
+                    uLCD.printf("\nAccess Granted. \n\n"); 
+                    LCD_Access.release();  
+                break; 
+                }
+            }
+        }
+}
+
+void IR_thread(void const *args) {
+    
+    Timer t;
+    t.start(); 
+    
+    while(1) {
+        
+        if (state == Armed) {
+            IrVoltage=IrSensor.read();
+            if (IrVoltage <= 0.1) { //if value just nois reset timer
+                t.reset();
+                state = Armed;
+                Consul_Access.wait();
+                serial.printf("Vtimer reset %f\r\n", IrVoltage);
+                serial.printf("tread:  %f\r\n", t.read());
+                Consul_Access.release();
+                }
+            if (t.read() >= 5) { //wait 5 seconds to make sure that sense someone 
+                state = IR_sensed; 
+                Consul_Access.wait();
+                serial.printf("alarm on %f\r\n", IrVoltage);
+                Consul_Access.release();
+                }
+            Thread::wait(1000);
+        }
+        else {
+            //nothing to do for this thread make space for others
+            Thread::wait(1000);
+        }
+    }
+}
+
+void Shiftbright_thread(void const *args){
+    //int red=0;
+    //int green=0;
+    //int blue=0;
+    spi.format(16,0);
+    spi.frequency(500000);
+    enable=0;
+    latch=0;
+    
+    while(1) {
+    switch (state) {
+        case Armed:
+            Consul_Access.wait();
+            serial.printf("armed \r\n");
+            Consul_Access.release();
+            for (int i = 0; i <= 100; i++) {
+                RGB_LED( 0, i, 0);
+                Thread::wait(10);
+            }
+            for (int i = 100; i >= 0; i--) {
+                RGB_LED( 0, i, 0);
+                Thread::wait(10);
+            }
+        break;
+        case IR_sensed:
+            Consul_Access.wait();
+            serial.printf("sensed \r\n");
+            Consul_Access.release();
+            RGB_LED( 0, 0, 100);
+        break;
+        case Alarm_ON:
+            Consul_Access.wait();
+            serial.printf("on \r\n");
+            Consul_Access.release();
+            RGB_LED( 100, 0, 0);
+        break; 
+        case Cleared:
+            RGB_LED( 0, 100, 0);
+            break;
+        }
+        Thread::wait(1000);
+   }
+}
+
+void CheckCode_thread(void const *args) {
+    while(1){
+        if (state == IR_sensed) {
+            //printf("CC from check: %d%d%d%d\n\r",Code[0],Code[1],Code[2],Code[3]);
+            if ((Code[0] == 1) &&  (Code[1] == 2) && (Code[2] == 3) && (Code[3] == 4)) {
+                Code[0] = Code[1] = Code[2] = Code[3] =0;
+                state = Second_Step; 
+                CodeCounter = 0;
+                break;
+                }
+            else if ((Code[0] != 0) &&  (Code[1] != 0) && (Code[2] != 0) && (Code[3] != 0)) {
+                Code[0] = Code[1] = Code[2] = Code[3] =0;
+                Consul_Access.wait();
+                serial.printf("\nAWrong passcode. \n\n");   
+                Consul_Access.release();
+                state = Alarm_ON; 
+            }
+        }
+        if (state == Second_Step) {
+            Consul_Access.wait();
+            serial.printf("CC from check: %d%d%d%d\n\r",Code[0],Code[1],Code[2],Code[3]);
+            Consul_Access.release();
+            if ((Code[0] == 3) &&  (Code[1] == 2) && (Code[2] == 2) && (Code[3] == 1)) {
+                Code[0] = Code[1] = Code[2] = Code[3] =0;
+                state = Cleared; 
+                CodeCounter = 0;
+                }
+            else if ((Code[0] != 0) &&  (Code[1] != 0) && (Code[2] != 0) && (Code[3] != 0)) {
+                Code[0] = Code[1] = Code[2] = Code[3] =0;
+                Consul_Access.wait();
+                serial.printf("\nAWrong passcode. \n\n"); 
+                Consul_Access.release();  
+                state = Alarm_ON; 
+            }
+        } 
+    Thread::wait(1000);
+    }
+}
+
+void Ethernet_thread(void const *args) { 
+    
+    int Change = 99; 
+    while(1) {
+        if (Change != state) {
+             
+            Change = state;
+            switch (state) {
+                case Armed:
+                    //add code that verifies connection every so often 
+                    Thread::wait(1000);
+                break;
+                case IR_sensed: 
+                    /*// send alert
+                    TCPSocketConnection sock;
+                    sock.connect("dreamphysix.com", 80);
+
+                    char http_cmd[] = "GET http://dreamphysix.com/alert.php?authcode=0e9cae34a0 HTTP/1.0\n\n";
+                    sock.send_all(http_cmd, sizeof(http_cmd)-1);
+    
+                    char buffer[300];
+                    int ret;
+                    while (true) {
+                        ret = sock.receive(buffer, sizeof(buffer)-1);
+                        if (ret <= 0)
+                            break;
+                        buffer[ret] = '\0';
+                        Consul_Access.wait();
+                       serial.printf("Received %d chars from server:\n%s\n", ret, buffer);
+                        Consul_Access.release();
+                        }
+                    sock.close();*/
+                break;
+                case Second_Step: 
+                    /*TCPSocketConnection sock;
+                    sock.connect("dreamphysix.com", 80);
+                    char buffer[300];
+                    int ret;
+                    //code second step
+                    // send random code
+                    srand(time(NULL));
+                    int randomNumber = 3221 ;//rand() % 4444 + 1000;
+                    char randomCode[sizeof(int)*4];
+                    snprintf(randomCode, sizeof(randomCode), "%i", randomNumber);
+
+                    sock.connect("dreamphysix.com", 80);
+    
+                    char http_cmd1[100] = "GET http://dreamphysix.com/sendcode.php?authcode=0e9cae34a0&randomcode=";
+                    strcat(http_cmd1, randomCode);
+                    strcat(http_cmd1, " HTTP/1.0\n\n");
+                    sock.send_all(http_cmd1, sizeof(http_cmd1)-1);
+    
+                    while (true) {
+                        ret = sock.receive(buffer, sizeof(buffer)-1);
+                        if (ret <= 0)
+                            break;
+                        buffer[ret] = '\0';
+                        Consul_Access.wait();
+                       serial.printf("Received %d chars from server:\n%s\n", ret, buffer);
+                        Consul_Access.release();
+                    }
+    
+                    sock.close(); 
+                    */
+                break;
+                case Cleared:
+                    //nothing to do for this thread make space for others 
+                    Thread::wait(1000);
+                break;
+                case Alarm_ON:
+                    //nothing to do for this thread make space for others
+                    Thread::wait(1000);
+                break;
+            }
+        }
+    }   
+}
+  
+void LCD_Code_Enter_Thread(void const *args) {
+    int LineHight = 120;
+    int LineWidth = 10; 
+    int SpaceWidth = 5;
+    int MidPoint = 127/2;
+    while(1) {
+        switch (state) {
+            //state: Armed =0 ,IR_sensed = 1,Second_Step = 2,Cleared = 3, Alarm_ON = 4};
+                case Armed:
+                    Thread::wait(1000); //nothing to do for this thread make space for others
+                break;
+                case IR_sensed:
+                case Second_Step:
+                    LCD_Access.wait();
+
+                    uLCD.line(MidPoint-2*(LineWidth+SpaceWidth), LineHight, MidPoint- 2*SpaceWidth-LineWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
+                    uLCD.line(MidPoint-LineWidth-SpaceWidth, LineHight, MidPoint-SpaceWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
+                    uLCD.line(MidPoint+SpaceWidth, LineHight, MidPoint+SpaceWidth+LineWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
+                    uLCD.line(MidPoint+2*SpaceWidth+LineWidth, LineHight, MidPoint+2*(SpaceWidth+LineWidth), LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
+                    
+                    uLCD.locate(20,20);
+                    uLCD.printf("%d %d %d %d ",Code[0],Code[1],Code[2],Code[3]);
+                    LCD_Access.release();
+                break; 
+                case Cleared:
+                    Thread::wait(1000); //nothing to do for this thread make space for others
+                break;
+                case Alarm_ON:
+                    Thread::wait(1000); //nothing to do for this thread make space for others
+                break;
+                }
+        }
+}
+    
+int main() {
+    
+    init_pbs();
+    
+   serial.printf("Setting up connection\r\n");
+    
+    eth.init(); //Use DHCP
+    eth.connect();
+    Thread Ethernetthread(Ethernet_thread);
+   serial.printf("IP Address is %s\n", eth.getIPAddress());
+    wait(5);
+    
+    Thread LCDthread(uLCD_thread);
+    Thread IRthread(IR_thread);
+    Thread Shiftbright(Shiftbright_thread);
+    Thread Speakerthread(Speaker_thread);
+    Thread CheckCode(CheckCode_thread);
+    Thread LCD_CodeEnterThread(LCD_Code_Enter_Thread); 
+    
+    while (1) {
+           /* switch (state) {
+            //state: Armed =0 ,IR_sensed = 1,Second_Step = 2,Cleared = 3, Alarm_ON = 4};
+                case Armed:
+
+                break;
+                case IR_sensed:
+                    
+                break;
+                case Second_Step:
+                    
+                break; 
+                case Cleared:
+
+                break;
+                case Alarm_ON:
+
+                break;
+                }*/
+        }
+        
+    
+    
+    
+    
+    
+
+
+    
+    //while (true) { }
+
+}
+
+void init_LCD() {
+    
+    
+    
+    uLCD.baudrate(3000000);
+    uLCD.background_color(BLACK);
+}
+    
+void RGB_LED(int red, int green, int blue) {
+    
+    unsigned int low_color=0;
+    unsigned int high_color=0;
+    high_color=(blue<<4)|((red&0x3C0)>>6);
+    low_color=(((red&0x3F)<<10)|(green));
+    spi.write(high_color);
+    spi.write(low_color);
+    latch=1;
+    latch=0;
+    
+    }
+void init_pbs(void) {
+    // Use internal pullups for the three pushbuttons
+    pb1.mode(PullUp);
+    pb2.mode(PullUp);
+    pb3.mode(PullUp);
+    pb4.mode(PullUp);
+    // Delay for initial pullup to take effect
+    wait(.01);
+    // Setup Interrupt callback functions for a pb hit
+    pb1.attach_deasserted(&pb1_hit_callback);
+    pb2.attach_deasserted(&pb2_hit_callback);
+    pb3.attach_deasserted(&pb3_hit_callback);
+    pb4.attach_deasserted(&pb4_hit_callback);
+    // Start sampling pb inputs using interrupts
+    pb1.setSampleFrequency();
+    pb2.setSampleFrequency();
+    pb3.setSampleFrequency();
+    pb4.setSampleFrequency();
+    // pushbuttons now setup and running
+
+    }
+
+/*void Thread_maintnence(char Thread) {
+    switch (Thread) {
+        case 'IRthread':
+            IRthread.terminate();
+        break;
+}*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Oct 21 21:56:10 2015 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed-rtos/#d7bd06319118
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Oct 21 21:56:10 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wave_player.lib	Wed Oct 21 21:56:10 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/sravet/code/wave_player/#acc3e18e77ad