security Sys

Dependencies:   4DGL-uLCD-SE ECE4180_Touchpad_V2 IoTsecuritySys PinDetect mbed-rtos mbed

Fork of ECE4180_Touchpad_V2 by Jacob Smith

Committer:
landes
Date:
Mon Dec 07 20:08:40 2015 +0000
Revision:
6:33185f926189
Parent:
5:ebc70efa2e86
Child:
7:758d4e6fc145
latest

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jsmith352 0:04dcbfb4388c 1 #include <mbed.h>
landes 2:922d5b43bee3 2 #include <string>
landes 5:ebc70efa2e86 3 #include <iostream>
landes 1:7f873efe5b11 4 #include "rtos.h"
jsmith352 0:04dcbfb4388c 5 #include <mpr121.h>
jsmith352 0:04dcbfb4388c 6 #include <stdlib.h>
jsmith352 0:04dcbfb4388c 7 #include "PinDetect.h"
landes 1:7f873efe5b11 8 #include "uLCD_4DGL.h"
landes 1:7f873efe5b11 9 #include "SongPlayer.h"
landes 1:7f873efe5b11 10 #include "PinDetect.h"
landes 1:7f873efe5b11 11 #include "Speaker.h"
landes 1:7f873efe5b11 12 #include "EthernetInterface.h"
jsmith352 0:04dcbfb4388c 13 /* CODE_LENGTH needs to be double the amount of numbers you
jsmith352 0:04dcbfb4388c 14 want in your authenticator/passcode because of the way interrupt.fall(&fallInterrupt)
jsmith352 0:04dcbfb4388c 15 works. It is called twice every time an interrupt is detected.
jsmith352 0:04dcbfb4388c 16 The extra numbers in the array will just be filled with zeros and ignored in checking
jsmith352 0:04dcbfb4388c 17 code sequences, so they will not matter either way */
jsmith352 0:04dcbfb4388c 18 /* i.e, you want a code with 7 numbers, CODE_LENGTH needs to be 14 */
landes 1:7f873efe5b11 19 #define CODE_LENGTH 8
jsmith352 0:04dcbfb4388c 20
jsmith352 0:04dcbfb4388c 21 DigitalOut led1(LED1);
jsmith352 0:04dcbfb4388c 22 DigitalOut led2(LED2);
jsmith352 0:04dcbfb4388c 23 DigitalOut led3(LED3);
jsmith352 0:04dcbfb4388c 24 DigitalOut led4(LED4);
jsmith352 0:04dcbfb4388c 25
landes 1:7f873efe5b11 26 DigitalOut doorlock(p21);
landes 1:7f873efe5b11 27
landes 1:7f873efe5b11 28
landes 1:7f873efe5b11 29 //uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
landes 1:7f873efe5b11 30 uLCD_4DGL uLCD(p28, p27, p29);
jsmith352 0:04dcbfb4388c 31
jsmith352 0:04dcbfb4388c 32 // Create the interrupt receiver object on pin 26
jsmith352 0:04dcbfb4388c 33 InterruptIn interrupt(p30);
jsmith352 0:04dcbfb4388c 34
jsmith352 0:04dcbfb4388c 35 // Setup the i2c bus on pins 9 and 10
jsmith352 0:04dcbfb4388c 36 I2C i2c(p9, p10);
jsmith352 0:04dcbfb4388c 37
jsmith352 0:04dcbfb4388c 38 // Setup the Mpr121:
jsmith352 0:04dcbfb4388c 39 // constructor(i2c object, i2c address of the mpr121)
jsmith352 0:04dcbfb4388c 40 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
jsmith352 0:04dcbfb4388c 41
jsmith352 0:04dcbfb4388c 42 // pc serial communication for testing
jsmith352 0:04dcbfb4388c 43 Serial pc(USBTX, USBRX);
jsmith352 0:04dcbfb4388c 44
landes 1:7f873efe5b11 45 //Set up IR sensor
landes 1:7f873efe5b11 46 AnalogIn IrSensor(p20);
landes 1:7f873efe5b11 47
landes 1:7f873efe5b11 48 //Shiftbright
landes 1:7f873efe5b11 49 DigitalOut latch(p15);
landes 1:7f873efe5b11 50 DigitalOut enable(p16);
landes 1:7f873efe5b11 51 //AnalogOut DACout(p18);
landes 1:7f873efe5b11 52 //Cycles through different colors on RGB LED
landes 1:7f873efe5b11 53 SPI spi(p11, p12, p13);
landes 1:7f873efe5b11 54
landes 1:7f873efe5b11 55
landes 1:7f873efe5b11 56 SongPlayer mySpeaker(p26);
landes 1:7f873efe5b11 57 Speaker NotePlayer(p26);
landes 1:7f873efe5b11 58
landes 1:7f873efe5b11 59 // ethernet setup
landes 1:7f873efe5b11 60 EthernetInterface eth;
landes 1:7f873efe5b11 61
landes 1:7f873efe5b11 62 //Lock timeout
landes 4:d8de964b3d2c 63 Timeout flipper;
landes 1:7f873efe5b11 64
jsmith352 0:04dcbfb4388c 65 // ***** GLOBALS ***** //
jsmith352 0:04dcbfb4388c 66 // Timer is to seed rand
landes 4:d8de964b3d2c 67 string ID = "1";
landes 5:ebc70efa2e86 68 volatile int NumTry = 0;
jsmith352 0:04dcbfb4388c 69 Timer t1;
landes 6:33185f926189 70 Timer t2;
jsmith352 0:04dcbfb4388c 71 // code counter is the next position in inputCode array
landes 1:7f873efe5b11 72 volatile int codeCounter;
jsmith352 0:04dcbfb4388c 73 // inputCode array is the sequence of numbers the user will enter
landes 1:7f873efe5b11 74 volatile int inputCode[CODE_LENGTH];
landes 1:7f873efe5b11 75 volatile bool code_enabled;
landes 1:7f873efe5b11 76 volatile float IrVoltage = 0.0;
landes 5:ebc70efa2e86 77 int FindStrLocation(string sntsc, string word, string ptr);
landes 4:d8de964b3d2c 78 volatile enum Statetype { Armed = 0, IR_sensed = 1, Second_Step = 2, Cleared = 3, Alarm_ON = 4};
landes 1:7f873efe5b11 79 Statetype state = Armed;
landes 2:922d5b43bee3 80 char charCode[5];
landes 1:7f873efe5b11 81
landes 1:7f873efe5b11 82 float note[18]= {1568.0,1396.9};
landes 1:7f873efe5b11 83 float duration[18]= {0.48,0.24};
landes 1:7f873efe5b11 84
landes 1:7f873efe5b11 85 Mutex LCD_Access;
landes 5:ebc70efa2e86 86 Mutex PC_Access;
landes 1:7f873efe5b11 87 Semaphore Consul_Access(5);
landes 1:7f873efe5b11 88
landes 1:7f873efe5b11 89 void Shiftbright_thread(void const *args);
landes 1:7f873efe5b11 90 void IR_thread(void const *args);
landes 1:7f873efe5b11 91 void LCD_Code_Enter_Thread(void const *args);
landes 1:7f873efe5b11 92 void uLCD_thread(void const *args);
landes 1:7f873efe5b11 93 void RGB_LED(int red, int green, int blue);
landes 1:7f873efe5b11 94 void Speaker_thread(void const *args);
landes 1:7f873efe5b11 95 void Ethernet_thread(void const *args);
landes 2:922d5b43bee3 96 bool Ethernet_massage_Send(string args);
landes 1:7f873efe5b11 97 void Activate_Lock();
landes 1:7f873efe5b11 98 void init_LCD();
jsmith352 0:04dcbfb4388c 99 // Key hit/release interrupt routine
jsmith352 0:04dcbfb4388c 100 void fallInterrupt() {
jsmith352 0:04dcbfb4388c 101 int key_code=0;
jsmith352 0:04dcbfb4388c 102 int i=0;
jsmith352 0:04dcbfb4388c 103 int value=mpr121.read(0x00);
jsmith352 0:04dcbfb4388c 104 value +=mpr121.read(0x01)<<8;
jsmith352 0:04dcbfb4388c 105 // LED demo mod
jsmith352 0:04dcbfb4388c 106 i=0;
jsmith352 0:04dcbfb4388c 107 // puts key number out to LEDs for demo
jsmith352 0:04dcbfb4388c 108 for (i=0; i<12; i++) {
jsmith352 0:04dcbfb4388c 109 if (((value>>i)&0x01)==1) key_code=i+1;
jsmith352 0:04dcbfb4388c 110 }
jsmith352 0:04dcbfb4388c 111 led4=key_code & 0x01;
jsmith352 0:04dcbfb4388c 112 led3=(key_code>>1) & 0x01;
jsmith352 0:04dcbfb4388c 113 led2=(key_code>>2) & 0x01;
jsmith352 0:04dcbfb4388c 114 led1=(key_code>>3) & 0x01;
jsmith352 0:04dcbfb4388c 115
jsmith352 0:04dcbfb4388c 116 // save the keypress to inputCode array
landes 2:922d5b43bee3 117 switch (state) {
landes 2:922d5b43bee3 118 case Armed:
landes 2:922d5b43bee3 119 break;
landes 2:922d5b43bee3 120 case IR_sensed:
landes 2:922d5b43bee3 121 case Second_Step:
landes 2:922d5b43bee3 122 if(codeCounter < CODE_LENGTH){
landes 2:922d5b43bee3 123 // ignore odd numbers
landes 2:922d5b43bee3 124 if(codeCounter % 2 != 0){
landes 2:922d5b43bee3 125 inputCode[codeCounter] = 0;
landes 2:922d5b43bee3 126 }
landes 2:922d5b43bee3 127 // only save the even numbers (see lines 6-10)
landes 2:922d5b43bee3 128 else{
landes 2:922d5b43bee3 129 inputCode[codeCounter] = key_code - 1;
landes 2:922d5b43bee3 130 //pc.printf("codeCounter: %d -- code: %d\n\r", codeCounter, key_code - 1);
landes 2:922d5b43bee3 131 }
landes 2:922d5b43bee3 132 codeCounter++;
jsmith352 0:04dcbfb4388c 133 }
landes 2:922d5b43bee3 134 break;
landes 2:922d5b43bee3 135 case Alarm_ON:
landes 2:922d5b43bee3 136 break;
landes 2:922d5b43bee3 137 case Cleared:
landes 5:ebc70efa2e86 138 if(key_code == 12 ){
landes 2:922d5b43bee3 139 state = Armed;
landes 5:ebc70efa2e86 140 Ethernet_massage_Send("UpdateStatus");
landes 5:ebc70efa2e86 141 }
landes 5:ebc70efa2e86 142 else if (key_code == 11 ){
landes 5:ebc70efa2e86 143 doorlock = 1;
landes 5:ebc70efa2e86 144 flipper.attach(&Activate_Lock, 5.0);
landes 5:ebc70efa2e86 145 }
landes 2:922d5b43bee3 146 break;
landes 2:922d5b43bee3 147 }
landes 2:922d5b43bee3 148
jsmith352 0:04dcbfb4388c 149 }
jsmith352 0:04dcbfb4388c 150
jsmith352 0:04dcbfb4388c 151 // generate randomized code
jsmith352 0:04dcbfb4388c 152 void generate_random_code(int (&codeArray)[CODE_LENGTH]){
landes 1:7f873efe5b11 153 //int i = 0;
jsmith352 0:04dcbfb4388c 154 // only care about the even numbers (see lines 6-10)
landes 5:ebc70efa2e86 155 //////////PC_Access.lock();
landes 5:ebc70efa2e86 156 //pc.printf("NEW CODE: ");
landes 5:ebc70efa2e86 157 //PC_Access.unlock();
landes 1:7f873efe5b11 158 for(int i = 0; i < CODE_LENGTH; i+=2){
jsmith352 0:04dcbfb4388c 159 srand(t1.read_us());
landes 1:7f873efe5b11 160 codeArray[i] = (rand() % 9)+1; //nake code only 1-9
landes 5:ebc70efa2e86 161 //////////PC_Access.lock();
jsmith352 0:04dcbfb4388c 162 pc.printf("%d, ", codeArray[i]);
landes 5:ebc70efa2e86 163 //PC_Access.unlock();
jsmith352 0:04dcbfb4388c 164 }
landes 5:ebc70efa2e86 165
landes 2:922d5b43bee3 166 snprintf(charCode, sizeof(charCode), "%i%i%i%i ", codeArray[0], codeArray[2], codeArray[4], codeArray[6]);
landes 5:ebc70efa2e86 167 //////////PC_Access.lock();
landes 5:ebc70efa2e86 168 //pc.printf("\n\r");
landes 5:ebc70efa2e86 169 //pc.printf("%s\n\r",charCode,codeArray[6]);
landes 5:ebc70efa2e86 170 //PC_Access.unlock();
landes 5:ebc70efa2e86 171 //Ethernet_massage_Send("TextCode");
jsmith352 0:04dcbfb4388c 172 }
jsmith352 0:04dcbfb4388c 173
jsmith352 0:04dcbfb4388c 174 // check if the code entered is the correct code
jsmith352 0:04dcbfb4388c 175 bool check_code_sequence(int (&codeArray)[CODE_LENGTH]){
landes 5:ebc70efa2e86 176 //int i = 0;
jsmith352 0:04dcbfb4388c 177 int j = 0;
jsmith352 0:04dcbfb4388c 178 // only care about the even numbers (see lines 6-10)
landes 4:d8de964b3d2c 179
landes 5:ebc70efa2e86 180 if (NumTry < 3) {
landes 5:ebc70efa2e86 181 NumTry++;
landes 5:ebc70efa2e86 182 for(int i = 0; i < CODE_LENGTH; i+=2){
landes 5:ebc70efa2e86 183
landes 5:ebc70efa2e86 184 if(inputCode[i] == codeArray[i]){
jsmith352 0:04dcbfb4388c 185 j++; // count the number of right numbers
landes 5:ebc70efa2e86 186 }
landes 5:ebc70efa2e86 187
jsmith352 0:04dcbfb4388c 188 }
landes 5:ebc70efa2e86 189 if(j == CODE_LENGTH/2){
landes 5:ebc70efa2e86 190 for(int i = 0; i < CODE_LENGTH; i+=2){
landes 5:ebc70efa2e86 191 inputCode[i] =0;
landes 5:ebc70efa2e86 192 }
jsmith352 0:04dcbfb4388c 193 return(true);
landes 5:ebc70efa2e86 194 }
landes 2:922d5b43bee3 195 else if (Ethernet_massage_Send("GetTempCode")){
landes 5:ebc70efa2e86 196 for(int i = 0; i < CODE_LENGTH; i+=2){
landes 5:ebc70efa2e86 197 inputCode[i] =0;
landes 5:ebc70efa2e86 198 }
landes 5:ebc70efa2e86 199 //pc.printf("return true");
landes 2:922d5b43bee3 200 return(true);
landes 2:922d5b43bee3 201 }
landes 2:922d5b43bee3 202 else {
landes 5:ebc70efa2e86 203 for(int i = 0; i < CODE_LENGTH; i+=2){
landes 5:ebc70efa2e86 204 inputCode[i] =0;
landes 5:ebc70efa2e86 205 }
landes 2:922d5b43bee3 206 return(false);
landes 2:922d5b43bee3 207 }
landes 5:ebc70efa2e86 208 }
landes 5:ebc70efa2e86 209 else {
landes 5:ebc70efa2e86 210 pc.printf("3 times ");
landes 5:ebc70efa2e86 211 state = Alarm_ON;
landes 5:ebc70efa2e86 212 return(false);
landes 5:ebc70efa2e86 213 }
jsmith352 0:04dcbfb4388c 214 }
jsmith352 0:04dcbfb4388c 215
jsmith352 0:04dcbfb4388c 216 int main() {
jsmith352 0:04dcbfb4388c 217 interrupt.fall(&fallInterrupt);
jsmith352 0:04dcbfb4388c 218 interrupt.mode(PullUp);
landes 1:7f873efe5b11 219 pc.baud(921600);
landes 1:7f873efe5b11 220
jsmith352 0:04dcbfb4388c 221
jsmith352 0:04dcbfb4388c 222 // authenticator is the randomly generated sequence of numbers by the machine
jsmith352 0:04dcbfb4388c 223 // the user has to match this sequence to gain access, used for phase 2
jsmith352 0:04dcbfb4388c 224 int authenticator[CODE_LENGTH];
jsmith352 0:04dcbfb4388c 225 // passcode is the user's personal passcode, used for phase 1
landes 1:7f873efe5b11 226 int passcode[CODE_LENGTH] = {1,0,2,0,3,0,4,0};//,4,0,5,0,6,0};
jsmith352 0:04dcbfb4388c 227 codeCounter = 0;
jsmith352 0:04dcbfb4388c 228 bool pass = false;
jsmith352 0:04dcbfb4388c 229
jsmith352 0:04dcbfb4388c 230 // these 2 variables tell the machine when to generate a new random authentication code
jsmith352 0:04dcbfb4388c 231 int new_code_timer = 0;
jsmith352 0:04dcbfb4388c 232 int new_code_counter = 0;
jsmith352 0:04dcbfb4388c 233 // this tells the state machine with phase of authentication we are in
jsmith352 0:04dcbfb4388c 234 code_enabled = false;
jsmith352 0:04dcbfb4388c 235
jsmith352 0:04dcbfb4388c 236 for(int i = 0; i < CODE_LENGTH; i++){
jsmith352 0:04dcbfb4388c 237 authenticator[i] = 0;
jsmith352 0:04dcbfb4388c 238 inputCode[i] = 0;
jsmith352 0:04dcbfb4388c 239 }
jsmith352 0:04dcbfb4388c 240
jsmith352 0:04dcbfb4388c 241 // go ahead and start the timer so that when a random code is generated,
jsmith352 0:04dcbfb4388c 242 // the seed will always be random, unlike the predecessor version
jsmith352 0:04dcbfb4388c 243 t1.start();
landes 1:7f873efe5b11 244 init_LCD();
landes 1:7f873efe5b11 245 //start threads:
landes 5:ebc70efa2e86 246 //////////PC_Access.lock();
landes 1:7f873efe5b11 247 pc.printf("\n\n\nSetting up Ethernet\n\r");
landes 5:ebc70efa2e86 248 //PC_Access.unlock();
landes 2:922d5b43bee3 249 Thread Ethernetthread(Ethernet_thread);
landes 2:922d5b43bee3 250 wait(5); //Give the Ethernet connection some time to set up
landes 1:7f873efe5b11 251 Thread IRthread(IR_thread);
landes 1:7f873efe5b11 252 Thread Shiftbright(Shiftbright_thread);
landes 1:7f873efe5b11 253 Thread LCDthread(uLCD_thread);
landes 5:ebc70efa2e86 254 //Thread LCD_CodeEnterThread(LCD_Code_Enter_Thread);
landes 5:ebc70efa2e86 255 //Thread Speakerthread(Speaker_thread);
landes 5:ebc70efa2e86 256
jsmith352 0:04dcbfb4388c 257 // while loop constantly checks if the entered code sequence is right or wrong,
jsmith352 0:04dcbfb4388c 258 // given that the correct amount of numbers were entered
landes 5:ebc70efa2e86 259 ////////////PC_Access.lock();
landes 5:ebc70efa2e86 260 pc.printf("Ready\n\r");
landes 5:ebc70efa2e86 261 ////PC_Access.unlock();
landes 1:7f873efe5b11 262 doorlock = 0; // make sure locked
jsmith352 0:04dcbfb4388c 263 while (1){
landes 1:7f873efe5b11 264 switch(state){
landes 1:7f873efe5b11 265 case Cleared:
landes 1:7f873efe5b11 266 break;
landes 1:7f873efe5b11 267 case IR_sensed:
jsmith352 0:04dcbfb4388c 268 if(codeCounter >= CODE_LENGTH){
jsmith352 0:04dcbfb4388c 269 pass = check_code_sequence(passcode);
jsmith352 0:04dcbfb4388c 270 if(pass == true){
landes 5:ebc70efa2e86 271 ////////////////////////PC_Access.lock();
landes 5:ebc70efa2e86 272 //pc.printf("SENDING AUTHENTICATION CODE...\n\r");
landes 5:ebc70efa2e86 273 ////////PC_Access.unlock();
jsmith352 0:04dcbfb4388c 274 generate_random_code(authenticator);
jsmith352 0:04dcbfb4388c 275 t1.stop(); // reset the time
jsmith352 0:04dcbfb4388c 276 t1.reset(); // so that it is an even
jsmith352 0:04dcbfb4388c 277 t1.start(); // 30 seconds before 1st new code is generated
jsmith352 0:04dcbfb4388c 278 codeCounter = 0;
landes 1:7f873efe5b11 279 //code_enabled = true;
landes 1:7f873efe5b11 280 state = Second_Step;
landes 4:d8de964b3d2c 281 //Ethernet_massage_Send("UpdateStatus");
jsmith352 0:04dcbfb4388c 282 }
jsmith352 0:04dcbfb4388c 283 else{
landes 5:ebc70efa2e86 284 ////////////////////////PC_Access.lock();
landes 5:ebc70efa2e86 285 //pc.printf("WRONG passcode\n\r");
landes 5:ebc70efa2e86 286 ////PC_Access.unlock();
jsmith352 0:04dcbfb4388c 287 codeCounter = 0;
jsmith352 0:04dcbfb4388c 288 }
jsmith352 0:04dcbfb4388c 289 }
jsmith352 0:04dcbfb4388c 290 break;
landes 1:7f873efe5b11 291 case Second_Step:
jsmith352 0:04dcbfb4388c 292 if(codeCounter >= CODE_LENGTH){
landes 5:ebc70efa2e86 293 NumTry = 0;
jsmith352 0:04dcbfb4388c 294 pass = check_code_sequence(authenticator);
jsmith352 0:04dcbfb4388c 295 if(pass == true){
landes 5:ebc70efa2e86 296 ////////////////////////PC_Access.lock();
landes 5:ebc70efa2e86 297 //pc.printf("ACCESS GRANTED\n\r");
landes 5:ebc70efa2e86 298 ////PC_Access.unlock();
landes 1:7f873efe5b11 299 doorlock = 1;
landes 1:7f873efe5b11 300 flipper.attach(&Activate_Lock, 5.0);
landes 1:7f873efe5b11 301 //wait(5);
landes 1:7f873efe5b11 302 //doorlock = 0;*/
landes 5:ebc70efa2e86 303 //pc.printf("Resetting....\n\r");
landes 5:ebc70efa2e86 304 //pc.printf("\n\n\rPlease Enter Your Personal Security Code\n\r");
jsmith352 0:04dcbfb4388c 305 codeCounter = 0;
jsmith352 0:04dcbfb4388c 306 code_enabled = false;
landes 1:7f873efe5b11 307 state = Cleared;
landes 5:ebc70efa2e86 308 Ethernet_massage_Send("TextCode");
landes 5:ebc70efa2e86 309 //Ethernet_massage_Send("UpdateStatus");
jsmith352 0:04dcbfb4388c 310 }
jsmith352 0:04dcbfb4388c 311 else{
landes 5:ebc70efa2e86 312 ////////////////////////PC_Access.lock();
landes 5:ebc70efa2e86 313 //pc.printf("ACCESS DENIED\n\r");
landes 5:ebc70efa2e86 314 ////PC_Access.unlock();
jsmith352 0:04dcbfb4388c 315 codeCounter = 0;
jsmith352 0:04dcbfb4388c 316 }
jsmith352 0:04dcbfb4388c 317 }
jsmith352 0:04dcbfb4388c 318 // this code generates a new authentication code every 30 seconds (30000 ms)
landes 2:922d5b43bee3 319 new_code_timer = (int)(t1.read_ms()/300000000);
jsmith352 0:04dcbfb4388c 320 if(new_code_timer > new_code_counter){
jsmith352 0:04dcbfb4388c 321 new_code_counter++;
jsmith352 0:04dcbfb4388c 322 generate_random_code(authenticator);
jsmith352 0:04dcbfb4388c 323 codeCounter = 0;
jsmith352 0:04dcbfb4388c 324 }
jsmith352 0:04dcbfb4388c 325 break;
jsmith352 0:04dcbfb4388c 326 }
jsmith352 0:04dcbfb4388c 327 // reset the timer when the number gets too high, should last about 35 minutes
jsmith352 0:04dcbfb4388c 328 // We do this because int can only hold a number up to 2^32 - 1, preventing errors
jsmith352 0:04dcbfb4388c 329 if(t1.read_us() > 2147400000){
jsmith352 0:04dcbfb4388c 330 t1.stop();
jsmith352 0:04dcbfb4388c 331 t1.reset();
jsmith352 0:04dcbfb4388c 332 new_code_timer = 0;
jsmith352 0:04dcbfb4388c 333 new_code_counter = 0;
jsmith352 0:04dcbfb4388c 334 t1.start();
jsmith352 0:04dcbfb4388c 335 }
jsmith352 0:04dcbfb4388c 336 }
landes 1:7f873efe5b11 337 }
landes 1:7f873efe5b11 338
landes 1:7f873efe5b11 339
landes 1:7f873efe5b11 340 void Shiftbright_thread(void const *args){
landes 1:7f873efe5b11 341 spi.format(16,0);
landes 1:7f873efe5b11 342 spi.frequency(500000);
landes 1:7f873efe5b11 343 enable=0;
landes 1:7f873efe5b11 344 latch=0;
landes 1:7f873efe5b11 345
landes 1:7f873efe5b11 346 while(1) {
landes 1:7f873efe5b11 347 switch (state) {
landes 1:7f873efe5b11 348 case Armed:
landes 1:7f873efe5b11 349 for (int i = 0; i <= 50; i++) {
landes 1:7f873efe5b11 350 RGB_LED( i, 0, 0);
landes 1:7f873efe5b11 351 Thread::wait(10);
landes 1:7f873efe5b11 352 }
landes 1:7f873efe5b11 353 for (int i = 50; i >= 0; i--) {
landes 1:7f873efe5b11 354 RGB_LED( i, 0, 0);
landes 1:7f873efe5b11 355 Thread::wait(10);
landes 1:7f873efe5b11 356 }
landes 1:7f873efe5b11 357 break;
landes 1:7f873efe5b11 358 case IR_sensed:
landes 1:7f873efe5b11 359 RGB_LED( 100, 0, 0);
landes 1:7f873efe5b11 360 Thread::wait(500);
landes 1:7f873efe5b11 361 RGB_LED( 0, 0, 0);
landes 1:7f873efe5b11 362 break;
landes 1:7f873efe5b11 363 case Alarm_ON:
landes 1:7f873efe5b11 364 for (int i = 0; i <= 100; i++) {
landes 1:7f873efe5b11 365 RGB_LED( i, i/2, 0);
landes 1:7f873efe5b11 366 Thread::wait(10);
landes 1:7f873efe5b11 367 }
landes 1:7f873efe5b11 368 for (int i = 100; i >= 0; i--) {
landes 1:7f873efe5b11 369 RGB_LED( i, i/3, 0);
landes 1:7f873efe5b11 370 Thread::wait(10);
landes 1:7f873efe5b11 371 }
landes 1:7f873efe5b11 372 break;
landes 1:7f873efe5b11 373 case Cleared:
landes 1:7f873efe5b11 374 RGB_LED( 0, 100, 0);
landes 1:7f873efe5b11 375 break;
landes 1:7f873efe5b11 376 }
landes 1:7f873efe5b11 377 Thread::wait(1000);
landes 1:7f873efe5b11 378 }
landes 1:7f873efe5b11 379 }
landes 1:7f873efe5b11 380
landes 1:7f873efe5b11 381
landes 1:7f873efe5b11 382 void IR_thread(void const *args) {
landes 1:7f873efe5b11 383
landes 1:7f873efe5b11 384 Timer t;
landes 1:7f873efe5b11 385 t.start();
landes 1:7f873efe5b11 386
landes 1:7f873efe5b11 387 while(1) {
landes 1:7f873efe5b11 388
landes 1:7f873efe5b11 389 if (state == Armed) {
landes 1:7f873efe5b11 390 IrVoltage=IrSensor.read();
landes 1:7f873efe5b11 391 if (IrVoltage <= 0.1) { //if value just nois reset timer
landes 1:7f873efe5b11 392 t.reset();
landes 1:7f873efe5b11 393 state = Armed;
landes 4:d8de964b3d2c 394 //Ethernet_massage_Send("UpdateStatus");
landes 1:7f873efe5b11 395 }
landes 1:7f873efe5b11 396 if (t.read() >= 5) { //wait 5 seconds to make sure that sense someone
landes 1:7f873efe5b11 397 state = IR_sensed;
landes 4:d8de964b3d2c 398 //Ethernet_massage_Send("UpdateStatus");
landes 1:7f873efe5b11 399 }
landes 1:7f873efe5b11 400 Thread::wait(1000);
landes 1:7f873efe5b11 401 }
landes 1:7f873efe5b11 402 else {
landes 1:7f873efe5b11 403 //nothing to do for this thread make space for others
landes 1:7f873efe5b11 404 Thread::wait(1000);
landes 1:7f873efe5b11 405 }
landes 1:7f873efe5b11 406 }
landes 1:7f873efe5b11 407 }
landes 1:7f873efe5b11 408
landes 1:7f873efe5b11 409 void RGB_LED(int red, int green, int blue) {
landes 1:7f873efe5b11 410
landes 1:7f873efe5b11 411 unsigned int low_color=0;
landes 1:7f873efe5b11 412 unsigned int high_color=0;
landes 1:7f873efe5b11 413 high_color=(blue<<4)|((red&0x3C0)>>6);
landes 1:7f873efe5b11 414 low_color=(((red&0x3F)<<10)|(green));
landes 1:7f873efe5b11 415 spi.write(high_color);
landes 1:7f873efe5b11 416 spi.write(low_color);
landes 1:7f873efe5b11 417 latch=1;
landes 1:7f873efe5b11 418 latch=0;
landes 1:7f873efe5b11 419 }
landes 1:7f873efe5b11 420
landes 1:7f873efe5b11 421 void init_LCD() {
landes 1:7f873efe5b11 422 uLCD.baudrate(3000000);
landes 1:7f873efe5b11 423 uLCD.background_color(BLACK);
landes 1:7f873efe5b11 424
landes 1:7f873efe5b11 425 }
landes 1:7f873efe5b11 426
landes 1:7f873efe5b11 427
landes 1:7f873efe5b11 428 void uLCD_thread(void const *args) {
landes 1:7f873efe5b11 429 int Change = 99;
landes 2:922d5b43bee3 430 string temp;
landes 2:922d5b43bee3 431 temp = "abc";
landes 1:7f873efe5b11 432 while(1) {
landes 1:7f873efe5b11 433
landes 1:7f873efe5b11 434 if (Change != state) {
landes 1:7f873efe5b11 435 Change = state;
landes 1:7f873efe5b11 436 switch (state) {
landes 1:7f873efe5b11 437 case Armed:
landes 1:7f873efe5b11 438 LCD_Access.lock();
landes 1:7f873efe5b11 439 uLCD.cls();
landes 1:7f873efe5b11 440 uLCD.color(WHITE);
landes 1:7f873efe5b11 441 uLCD.text_width(2);
landes 1:7f873efe5b11 442 uLCD.text_height(2);
landes 1:7f873efe5b11 443 uLCD.printf(" ARMED\r\n");
landes 1:7f873efe5b11 444 uLCD.text_width(1);
landes 1:7f873efe5b11 445 uLCD.text_height(1);
landes 1:7f873efe5b11 446
landes 1:7f873efe5b11 447 if (eth.getIPAddress() == "\0") {
landes 1:7f873efe5b11 448 uLCD.printf("\n\n No Internet connection");
landes 1:7f873efe5b11 449 }
landes 1:7f873efe5b11 450 else {
landes 1:7f873efe5b11 451 uLCD.printf("\n\nConnected to the Internet\n");
landes 1:7f873efe5b11 452 uLCD.printf("IP Address: \n%s ", eth.getIPAddress());
landes 1:7f873efe5b11 453 }
landes 1:7f873efe5b11 454 LCD_Access.unlock();
landes 1:7f873efe5b11 455 break;
landes 1:7f873efe5b11 456 case IR_sensed:
landes 1:7f873efe5b11 457 LCD_Access.lock();
landes 1:7f873efe5b11 458 uLCD.cls();
landes 1:7f873efe5b11 459 uLCD.printf("\nSensor triggred \n");
landes 1:7f873efe5b11 460 uLCD.printf("\n Enter the code ...");
landes 1:7f873efe5b11 461 LCD_Access.unlock();
landes 1:7f873efe5b11 462 for (int i=30; i>=0; --i) {
landes 1:7f873efe5b11 463 if (state == IR_sensed) {
landes 1:7f873efe5b11 464 LCD_Access.lock();
landes 1:7f873efe5b11 465 uLCD.text_width(4);
landes 1:7f873efe5b11 466 uLCD.text_height(4);
landes 1:7f873efe5b11 467 uLCD.color(RED);
landes 1:7f873efe5b11 468 uLCD.locate(1,2);
landes 1:7f873efe5b11 469 uLCD.printf("%2D",i);
landes 1:7f873efe5b11 470 LCD_Access.unlock();
landes 1:7f873efe5b11 471 Thread::wait(1000);
landes 1:7f873efe5b11 472 }
landes 1:7f873efe5b11 473 }
landes 1:7f873efe5b11 474 if (state == IR_sensed) {
landes 1:7f873efe5b11 475 state = Alarm_ON;
landes 4:d8de964b3d2c 476 //Ethernet_massage_Send("UpdateStatus");
landes 2:922d5b43bee3 477 Ethernet_massage_Send("TextAlarm");
landes 1:7f873efe5b11 478 }
landes 1:7f873efe5b11 479
landes 1:7f873efe5b11 480 break;
landes 1:7f873efe5b11 481 case Second_Step:
landes 1:7f873efe5b11 482 LCD_Access.lock();
landes 1:7f873efe5b11 483 uLCD.cls();
landes 1:7f873efe5b11 484 uLCD.color(BLUE);
landes 1:7f873efe5b11 485 uLCD.printf("\nPleas enter code from text massage \n");
landes 1:7f873efe5b11 486 LCD_Access.unlock();
landes 1:7f873efe5b11 487 break;
landes 1:7f873efe5b11 488 case Alarm_ON:
landes 1:7f873efe5b11 489 LCD_Access.lock();
landes 1:7f873efe5b11 490 uLCD.cls();
landes 1:7f873efe5b11 491 uLCD.color(RED);
landes 1:7f873efe5b11 492 uLCD.text_width(1.5); //4X size text
landes 1:7f873efe5b11 493 uLCD.text_height(1.5);
landes 1:7f873efe5b11 494 uLCD.printf("\nALARM IS ON \nText message sent \n");
landes 1:7f873efe5b11 495 LCD_Access.unlock();
landes 1:7f873efe5b11 496 break;
landes 1:7f873efe5b11 497 case Cleared:
landes 1:7f873efe5b11 498 LCD_Access.lock();
landes 1:7f873efe5b11 499 uLCD.cls();
landes 1:7f873efe5b11 500 uLCD.color(GREEN);
landes 1:7f873efe5b11 501 uLCD.printf("\n\nAccess Granted. \n\n");
landes 1:7f873efe5b11 502 LCD_Access.unlock();
landes 1:7f873efe5b11 503 break;
landes 1:7f873efe5b11 504 }
landes 1:7f873efe5b11 505 }
landes 1:7f873efe5b11 506 Thread::wait(500);
landes 1:7f873efe5b11 507 }
landes 1:7f873efe5b11 508 }
landes 1:7f873efe5b11 509
landes 1:7f873efe5b11 510 void LCD_Code_Enter_Thread(void const *args) {
landes 1:7f873efe5b11 511 int LineHight = 120;
landes 1:7f873efe5b11 512 int LineWidth = 10;
landes 1:7f873efe5b11 513 int SpaceWidth = 5;
landes 1:7f873efe5b11 514 int MidPoint = 127/2;
landes 1:7f873efe5b11 515 while(1) {
landes 1:7f873efe5b11 516 switch (state) {
landes 1:7f873efe5b11 517 case Armed:
landes 1:7f873efe5b11 518 break;
landes 1:7f873efe5b11 519 case IR_sensed:
landes 1:7f873efe5b11 520 case Cleared:
landes 1:7f873efe5b11 521 case Second_Step:
landes 1:7f873efe5b11 522
landes 1:7f873efe5b11 523 Thread::wait(500);
landes 1:7f873efe5b11 524 while((state == IR_sensed)||(state == Cleared)||(state == Second_Step)) {
landes 1:7f873efe5b11 525 LCD_Access.lock();
landes 1:7f873efe5b11 526 //dusplay four lines
landes 1:7f873efe5b11 527 uLCD.line(MidPoint-2*(LineWidth+SpaceWidth), LineHight, MidPoint- 2*SpaceWidth-LineWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
landes 1:7f873efe5b11 528 uLCD.line(MidPoint-LineWidth-SpaceWidth, LineHight, MidPoint-SpaceWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
landes 1:7f873efe5b11 529 uLCD.line(MidPoint+SpaceWidth, LineHight, MidPoint+SpaceWidth+LineWidth, LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
landes 1:7f873efe5b11 530 uLCD.line(MidPoint+2*SpaceWidth+LineWidth, LineHight, MidPoint+2*(SpaceWidth+LineWidth), LineHight, WHITE); //line( int x1, int y1, int x2, int y2, int color)
landes 1:7f873efe5b11 531 uLCD.locate(5,14);
landes 1:7f873efe5b11 532 uLCD.text_width(1); //4X size text
landes 1:7f873efe5b11 533 uLCD.text_height(1);
landes 5:ebc70efa2e86 534
landes 1:7f873efe5b11 535 // add black numbers
landes 5:ebc70efa2e86 536 /*if (inputCode[0] == inputCode[2] ==inputCode[4] ==inputCode[6] == 0) {
landes 5:ebc70efa2e86 537 uLCD.color(BLACK);
landes 5:ebc70efa2e86 538 }
landes 5:ebc70efa2e86 539 else {*/
landes 5:ebc70efa2e86 540 //uLCD.color(WHITE);
landes 1:7f873efe5b11 541 uLCD.printf("%d %d %d %d",inputCode[0],inputCode[2],inputCode[4],inputCode[6]);
landes 5:ebc70efa2e86 542 //}
landes 1:7f873efe5b11 543 LCD_Access.unlock();
landes 1:7f873efe5b11 544 }
landes 1:7f873efe5b11 545
landes 1:7f873efe5b11 546 case Alarm_ON:
landes 1:7f873efe5b11 547 break;
landes 1:7f873efe5b11 548 }
landes 1:7f873efe5b11 549 }
landes 1:7f873efe5b11 550 }
landes 1:7f873efe5b11 551
landes 1:7f873efe5b11 552 void Speaker_thread(void const *args) {
landes 1:7f873efe5b11 553 while (1) {
landes 1:7f873efe5b11 554 if (state == Alarm_ON) {
landes 1:7f873efe5b11 555 mySpeaker.PlaySong(note,duration);
landes 1:7f873efe5b11 556 Thread::wait(1000);
landes 1:7f873efe5b11 557 }
landes 1:7f873efe5b11 558 }
landes 1:7f873efe5b11 559 }
landes 1:7f873efe5b11 560
landes 1:7f873efe5b11 561 void Ethernet_thread(void const *args) {
landes 4:d8de964b3d2c 562
landes 2:922d5b43bee3 563 char buffer[300];
landes 5:ebc70efa2e86 564
landes 5:ebc70efa2e86 565 int ret,found;
landes 1:7f873efe5b11 566 eth.init(); //Use DHCP
landes 1:7f873efe5b11 567 eth.connect();
landes 5:ebc70efa2e86 568 //pc.printf("IP Address is: %s\n\r", eth.getIPAddress());
landes 5:ebc70efa2e86 569 while (1) {
landes 6:33185f926189 570 Thread::wait(1000);
landes 6:33185f926189 571 int timeout;
landes 6:33185f926189 572
landes 6:33185f926189 573 //t2.start();
landes 6:33185f926189 574 TCPSocketConnection sock;
landes 6:33185f926189 575 sock.connect("dreamphysix.com", 80);
landes 6:33185f926189 576 char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/readstatus.php?mbedID=0 HTTP/1.0\n\n";
landes 5:ebc70efa2e86 577 ////////////PC_Access.lock();
landes 5:ebc70efa2e86 578 //pc.printf("%s",http_cmd);
landes 5:ebc70efa2e86 579 ////PC_Access.unlock();
landes 4:d8de964b3d2c 580 sock.send_all(http_cmd, sizeof(http_cmd)-1);
landes 1:7f873efe5b11 581
landes 4:d8de964b3d2c 582 while (true) {
landes 1:7f873efe5b11 583 ret = sock.receive(buffer, sizeof(buffer)-1);
landes 1:7f873efe5b11 584 if (ret <= 0)
landes 1:7f873efe5b11 585 break;
landes 1:7f873efe5b11 586 buffer[ret] = '\0';
landes 1:7f873efe5b11 587 Consul_Access.wait();
landes 5:ebc70efa2e86 588 ////////////////////////PC_Access.lock();
landes 6:33185f926189 589 pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
landes 5:ebc70efa2e86 590 ////PC_Access.unlock();
landes 1:7f873efe5b11 591 Consul_Access.release();
landes 1:7f873efe5b11 592 }
landes 1:7f873efe5b11 593 sock.close();
landes 6:33185f926189 594 //t2.stop();
landes 6:33185f926189 595 string str(buffer);
landes 6:33185f926189 596 found = str.find("Status=");
landes 5:ebc70efa2e86 597 //pc.printf("location: %d string: %s" , found, str);
landes 5:ebc70efa2e86 598 //pc.printf("\n\rhttp_cmd: %c\n\r",buffer[found+7]);
landes 5:ebc70efa2e86 599 //pc.printf("\n\state: %i\n\r",state);
landes 6:33185f926189 600 int dummy = (buffer[found+7])-48;
landes 5:ebc70efa2e86 601 //int dummy = ((int)buffer[found+7]);
landes 5:ebc70efa2e86 602 //dummy = atoi(dummy);
landes 6:33185f926189 603 state = (Statetype)dummy;
landes 5:ebc70efa2e86 604 //state = (Statetype)buffer[found+7];
landes 6:33185f926189 605 pc.printf("\n\state: %i\n\r",dummy);
landes 5:ebc70efa2e86 606 //Thread::wait(3000);
landes 6:33185f926189 607
landes 6:33185f926189 608
landes 5:ebc70efa2e86 609 }
landes 2:922d5b43bee3 610 }
landes 2:922d5b43bee3 611
landes 2:922d5b43bee3 612 bool Ethernet_massage_Send(string buff) {
landes 4:d8de964b3d2c 613
landes 2:922d5b43bee3 614 char buffer[300];
landes 2:922d5b43bee3 615 int ret;
landes 5:ebc70efa2e86 616 //pc.printf("IP Address is: %s\n\r", eth.getIPAddress());
landes 2:922d5b43bee3 617
landes 2:922d5b43bee3 618 TCPSocketConnection sock;
landes 2:922d5b43bee3 619 sock.connect("dreamphysix.com", 80);
landes 2:922d5b43bee3 620
landes 2:922d5b43bee3 621 if (buff == "TextCode") {
landes 2:922d5b43bee3 622 char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/sendcode.php?authcode=0e9cae34a0&randomcode=";
landes 2:922d5b43bee3 623 strcat(http_cmd, charCode);
landes 2:922d5b43bee3 624 strcat(http_cmd, " HTTP/1.0\n\n");
landes 5:ebc70efa2e86 625 //////////////////////////PC_Access.lock();
landes 2:922d5b43bee3 626 pc.printf("%s",http_cmd);
landes 5:ebc70efa2e86 627 ////PC_Access.unlock();
landes 2:922d5b43bee3 628 sock.send_all(http_cmd, sizeof(http_cmd)-1);
landes 2:922d5b43bee3 629 }
landes 2:922d5b43bee3 630 else if (buff == "TextAlarm") {
landes 2:922d5b43bee3 631 char http_cmd[] = "GET http://dreamphysix.com/alarm/sendalert.php?authcode=0e9cae34a0 HTTP/1.0\n\n";
landes 2:922d5b43bee3 632 sock.send_all(http_cmd, sizeof(http_cmd)-1);
landes 2:922d5b43bee3 633 }
landes 2:922d5b43bee3 634 else if (buff == "GetTempCode") {
landes 4:d8de964b3d2c 635 snprintf(charCode, sizeof(charCode), "%i%i%i%i ", inputCode[0], inputCode[2], inputCode[4], inputCode[6]);
landes 2:922d5b43bee3 636 char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/validatecode.php?code=";
landes 2:922d5b43bee3 637 strcat(http_cmd, charCode);
landes 2:922d5b43bee3 638 strcat(http_cmd, " HTTP/1.0\n\n");
landes 5:ebc70efa2e86 639 //////////////////////////PC_Access.lock();
landes 2:922d5b43bee3 640 pc.printf("%s",http_cmd);
landes 5:ebc70efa2e86 641 ////PC_Access.unlock();
landes 2:922d5b43bee3 642 sock.send_all(http_cmd, sizeof(http_cmd)-1);
landes 2:922d5b43bee3 643 }
landes 2:922d5b43bee3 644 else if (buff == "UpdateStatus") {
landes 4:d8de964b3d2c 645 char tempStatus[2];
landes 4:d8de964b3d2c 646 snprintf(tempStatus, sizeof(tempStatus), "%i", state);
landes 4:d8de964b3d2c 647 char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/updatestatus.php?mbedID=1";
landes 4:d8de964b3d2c 648 //strcat(http_cmd, ID);
landes 4:d8de964b3d2c 649 strcat(http_cmd, "&status=");
landes 4:d8de964b3d2c 650 strcat(http_cmd, tempStatus);
landes 4:d8de964b3d2c 651 strcat(http_cmd, " HTTP/1.0\n\n");
landes 5:ebc70efa2e86 652 //////////////////////////PC_Access.lock();
landes 4:d8de964b3d2c 653 pc.printf("%s",http_cmd);
landes 5:ebc70efa2e86 654 ////PC_Access.unlock();
landes 4:d8de964b3d2c 655 sock.send_all(http_cmd, sizeof(http_cmd)-1);
landes 2:922d5b43bee3 656 }
landes 2:922d5b43bee3 657 else {
landes 2:922d5b43bee3 658
landes 2:922d5b43bee3 659 }
landes 2:922d5b43bee3 660
landes 2:922d5b43bee3 661
landes 2:922d5b43bee3 662 while (true) {
landes 2:922d5b43bee3 663 ret = sock.receive(buffer, sizeof(buffer)-1);
landes 2:922d5b43bee3 664 if (ret <= 0)
landes 2:922d5b43bee3 665 break;
landes 2:922d5b43bee3 666 buffer[ret] = '\0';
landes 2:922d5b43bee3 667 Consul_Access.wait();
landes 5:ebc70efa2e86 668 ////////////////////////PC_Access.lock();
landes 2:922d5b43bee3 669 pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
landes 5:ebc70efa2e86 670 ////PC_Access.unlock();
landes 2:922d5b43bee3 671 Consul_Access.release();
landes 2:922d5b43bee3 672 }
landes 2:922d5b43bee3 673 sock.close();
landes 4:d8de964b3d2c 674
landes 5:ebc70efa2e86 675 snprintf(buffer, ret, "%c",buffer);
landes 5:ebc70efa2e86 676 //pc.printf("buffer: %s", buffer);
landes 4:d8de964b3d2c 677 if (strstr(buffer,"True") != NULL) {
landes 5:ebc70efa2e86 678 //pc.printf("true fron eth check");
landes 5:ebc70efa2e86 679 return true;
landes 5:ebc70efa2e86 680 }
landes 5:ebc70efa2e86 681 else if (strstr(buffer,"False") != NULL) {
landes 5:ebc70efa2e86 682 //pc.printf("false fron eth check");
landes 4:d8de964b3d2c 683 return false;
landes 2:922d5b43bee3 684 }
landes 5:ebc70efa2e86 685 else {
landes 5:ebc70efa2e86 686 //pc.printf("default ");
landes 5:ebc70efa2e86 687 return false;
landes 4:d8de964b3d2c 688 }
landes 1:7f873efe5b11 689 }
landes 1:7f873efe5b11 690
landes 1:7f873efe5b11 691 void Activate_Lock(){
landes 1:7f873efe5b11 692 doorlock =! doorlock;
landes 1:7f873efe5b11 693 }