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:
Sat Dec 05 15:23:55 2015 +0000
Revision:
1:7f873efe5b11
Parent:
0:04dcbfb4388c
Child:
2:922d5b43bee3
ethernet and keypad

Who changed what in which revision?

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