The purpose of this project was to create a system that would allow users to monitor a locked device using a Bluetooth device. This Bluetooth device will show the last user that unlocked the device, and also allows the user to unlock the device using the Bluetooth device. This device can be physically unlocked using a capacitive touch keypad sensor.
Dependencies: mbed Motor Servo
Fork of SerialPassthrough_LPC1768 by
main.cpp@7:79d0b30fedb4, 2016-03-15 (annotated)
- Committer:
- ewilliams61
- Date:
- Tue Mar 15 19:14:45 2016 +0000
- Revision:
- 7:79d0b30fedb4
- Parent:
- 6:0398d0fcc8cc
- Child:
- 8:39172c01c0f1
Works :)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 0:59bec1fd956e | 1 | #include "mbed.h" |
ewilliams61 | 7:79d0b30fedb4 | 2 | #include <mpr121.h> |
ewilliams61 | 7:79d0b30fedb4 | 3 | #include <string> |
ewilliams61 | 7:79d0b30fedb4 | 4 | #include "Shiftbrite.h" |
sam_grove | 5:96cb82af9996 | 5 | RawSerial pc(USBTX, USBRX); |
ewilliams61 | 7:79d0b30fedb4 | 6 | RawSerial dev(p9,p10); |
sam_grove | 5:96cb82af9996 | 7 | DigitalOut led1(LED1); |
sam_grove | 5:96cb82af9996 | 8 | DigitalOut led4(LED4); |
ewilliams61 | 7:79d0b30fedb4 | 9 | InterruptIn interrupt(p26); |
ewilliams61 | 7:79d0b30fedb4 | 10 | I2C i2c(p28, p27); |
ewilliams61 | 7:79d0b30fedb4 | 11 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
ewilliams61 | 7:79d0b30fedb4 | 12 | Shiftbrite myShiftbrite(p16, p15, p11, p12, p13); |
ewilliams61 | 7:79d0b30fedb4 | 13 | DigitalOut led2(LED2); |
ewilliams61 | 7:79d0b30fedb4 | 14 | DigitalOut led3(LED3); |
ewilliams61 | 7:79d0b30fedb4 | 15 | |
ewilliams61 | 7:79d0b30fedb4 | 16 | int code1[] = {1,1,1,1}; |
ewilliams61 | 7:79d0b30fedb4 | 17 | int code2[] = {2,2,2,2}; |
ewilliams61 | 7:79d0b30fedb4 | 18 | int code3[] = {8,8,8,8}; |
ewilliams61 | 7:79d0b30fedb4 | 19 | |
ewilliams61 | 7:79d0b30fedb4 | 20 | volatile int input[4]; |
ewilliams61 | 7:79d0b30fedb4 | 21 | volatile int inNdx = 0; |
ewilliams61 | 7:79d0b30fedb4 | 22 | bool codeIn = false; |
ewilliams61 | 7:79d0b30fedb4 | 23 | |
ewilliams61 | 7:79d0b30fedb4 | 24 | void fallInterrupt() { |
ewilliams61 | 7:79d0b30fedb4 | 25 | int key_code=0; |
ewilliams61 | 7:79d0b30fedb4 | 26 | int i = 0; |
ewilliams61 | 7:79d0b30fedb4 | 27 | // myShiftbrite.write(0,0,0); |
ewilliams61 | 7:79d0b30fedb4 | 28 | // myShiftbrite.write(0,0,255); |
ewilliams61 | 7:79d0b30fedb4 | 29 | int value=mpr121.read(0x00); |
ewilliams61 | 7:79d0b30fedb4 | 30 | value +=mpr121.read(0x01)<<8; |
ewilliams61 | 7:79d0b30fedb4 | 31 | for (i=0; i<12; i++) { |
ewilliams61 | 7:79d0b30fedb4 | 32 | if (((value>>i)&0x01)==1) key_code=i+1; |
ewilliams61 | 7:79d0b30fedb4 | 33 | } |
ewilliams61 | 7:79d0b30fedb4 | 34 | led4=key_code & 0x01; |
ewilliams61 | 7:79d0b30fedb4 | 35 | led3=(key_code>>1) & 0x01; |
ewilliams61 | 7:79d0b30fedb4 | 36 | led2=(key_code>>2) & 0x01; |
ewilliams61 | 7:79d0b30fedb4 | 37 | led1=(key_code>>3) & 0x01; |
ewilliams61 | 7:79d0b30fedb4 | 38 | if (value > 0){ |
ewilliams61 | 7:79d0b30fedb4 | 39 | input[inNdx] = value; |
ewilliams61 | 7:79d0b30fedb4 | 40 | inNdx++; |
ewilliams61 | 7:79d0b30fedb4 | 41 | } |
ewilliams61 | 7:79d0b30fedb4 | 42 | |
ewilliams61 | 7:79d0b30fedb4 | 43 | |
ewilliams61 | 7:79d0b30fedb4 | 44 | |
ewilliams61 | 7:79d0b30fedb4 | 45 | } |
mbedAustin | 2:a8dcb07a1d00 | 46 | |
sam_grove | 5:96cb82af9996 | 47 | void dev_recv() |
mbedAustin | 2:a8dcb07a1d00 | 48 | { |
ewilliams61 | 7:79d0b30fedb4 | 49 | string temp =""; |
sam_grove | 5:96cb82af9996 | 50 | led1 = !led1; |
sam_grove | 5:96cb82af9996 | 51 | while(dev.readable()) { |
ewilliams61 | 7:79d0b30fedb4 | 52 | //pc.putc(dev.getc()); |
ewilliams61 | 7:79d0b30fedb4 | 53 | if(!codeIn){ |
ewilliams61 | 7:79d0b30fedb4 | 54 | if(dev.getc() == 'a'){ |
ewilliams61 | 7:79d0b30fedb4 | 55 | dev.printf("Send code 4 digit code.\n"); |
ewilliams61 | 7:79d0b30fedb4 | 56 | inNdx = 0; |
ewilliams61 | 7:79d0b30fedb4 | 57 | codeIn = true; |
ewilliams61 | 7:79d0b30fedb4 | 58 | // myShiftbrite.write(0,0,255); |
ewilliams61 | 7:79d0b30fedb4 | 59 | } |
ewilliams61 | 7:79d0b30fedb4 | 60 | else {dev.printf("Command not recognized");} |
ewilliams61 | 7:79d0b30fedb4 | 61 | } else { |
ewilliams61 | 7:79d0b30fedb4 | 62 | // pc.printf("BT entered: %c\n", dev.getc()); |
ewilliams61 | 7:79d0b30fedb4 | 63 | input[inNdx] = dev.getc() - '0'; |
ewilliams61 | 7:79d0b30fedb4 | 64 | if(input[inNdx] > -1){ |
ewilliams61 | 7:79d0b30fedb4 | 65 | dev.printf("Just entered: %d", input[inNdx]); |
ewilliams61 | 7:79d0b30fedb4 | 66 | inNdx++;} |
ewilliams61 | 7:79d0b30fedb4 | 67 | //break; |
ewilliams61 | 7:79d0b30fedb4 | 68 | } |
sam_grove | 5:96cb82af9996 | 69 | } |
ewilliams61 | 7:79d0b30fedb4 | 70 | |
sam_grove | 5:96cb82af9996 | 71 | } |
sam_grove | 5:96cb82af9996 | 72 | |
sam_grove | 5:96cb82af9996 | 73 | void pc_recv() |
sam_grove | 5:96cb82af9996 | 74 | { |
sam_grove | 5:96cb82af9996 | 75 | led4 = !led4; |
sam_grove | 5:96cb82af9996 | 76 | while(pc.readable()) { |
mbedAustin | 4:ba9100d52e48 | 77 | dev.putc(pc.getc()); |
mbedAustin | 0:59bec1fd956e | 78 | } |
mbedAustin | 0:59bec1fd956e | 79 | } |
mbedAustin | 4:ba9100d52e48 | 80 | |
mbedAustin | 4:ba9100d52e48 | 81 | int main() |
mbedAustin | 4:ba9100d52e48 | 82 | { |
ewilliams61 | 7:79d0b30fedb4 | 83 | myShiftbrite.write(0,0,0); |
mbedAustin | 4:ba9100d52e48 | 84 | pc.baud(9600); |
mbedAustin | 4:ba9100d52e48 | 85 | dev.baud(9600); |
ewilliams61 | 7:79d0b30fedb4 | 86 | interrupt.fall(&fallInterrupt); |
ewilliams61 | 7:79d0b30fedb4 | 87 | interrupt.mode(PullUp); |
mbedAustin | 4:ba9100d52e48 | 88 | |
sam_grove | 5:96cb82af9996 | 89 | pc.attach(&pc_recv, Serial::RxIrq); |
sam_grove | 5:96cb82af9996 | 90 | dev.attach(&dev_recv, Serial::RxIrq); |
ewilliams61 | 7:79d0b30fedb4 | 91 | dev.printf("Monitoring Locked Device\n"); |
ewilliams61 | 7:79d0b30fedb4 | 92 | dev.printf("To unlock the device remotely, send 'a'.\n"); |
sam_grove | 5:96cb82af9996 | 93 | |
mbedAustin | 4:ba9100d52e48 | 94 | while(1) { |
ewilliams61 | 7:79d0b30fedb4 | 95 | // sleep(); |
ewilliams61 | 7:79d0b30fedb4 | 96 | wait(5); |
ewilliams61 | 7:79d0b30fedb4 | 97 | pc.printf("ndx: %d\n", inNdx); |
ewilliams61 | 7:79d0b30fedb4 | 98 | if (inNdx > 3) { |
ewilliams61 | 7:79d0b30fedb4 | 99 | dev.printf("Attempting to Unlock\n"); |
ewilliams61 | 7:79d0b30fedb4 | 100 | codeIn = false; |
ewilliams61 | 7:79d0b30fedb4 | 101 | bool c1 = true; bool c2 = true; bool c3 = true; |
ewilliams61 | 7:79d0b30fedb4 | 102 | for(int x = 0; x <= 3; x++){ |
ewilliams61 | 7:79d0b30fedb4 | 103 | if(!(c1 | c2 | c3)) { |
ewilliams61 | 7:79d0b30fedb4 | 104 | break; |
ewilliams61 | 7:79d0b30fedb4 | 105 | } else { |
ewilliams61 | 7:79d0b30fedb4 | 106 | if (code1[x] != input[x]){c1 = false;} |
ewilliams61 | 7:79d0b30fedb4 | 107 | if (code2[x] != input[x]){c2 = false;} |
ewilliams61 | 7:79d0b30fedb4 | 108 | if (code3[x] != input[x]){c3 = false;} |
ewilliams61 | 7:79d0b30fedb4 | 109 | |
ewilliams61 | 7:79d0b30fedb4 | 110 | } |
ewilliams61 | 7:79d0b30fedb4 | 111 | } |
ewilliams61 | 7:79d0b30fedb4 | 112 | if(c1){ |
ewilliams61 | 7:79d0b30fedb4 | 113 | //update website to say user 1 entered last |
ewilliams61 | 7:79d0b30fedb4 | 114 | dev.printf("Welcome User 1\n"); |
ewilliams61 | 7:79d0b30fedb4 | 115 | // myShiftbrite.write(0,0,0); |
ewilliams61 | 7:79d0b30fedb4 | 116 | myShiftbrite.write(0,255,0); |
ewilliams61 | 7:79d0b30fedb4 | 117 | } else if(c2) { |
ewilliams61 | 7:79d0b30fedb4 | 118 | dev.printf("Welcome User 2\n"); |
ewilliams61 | 7:79d0b30fedb4 | 119 | // myShiftbrite.write(0,0,0); |
ewilliams61 | 7:79d0b30fedb4 | 120 | myShiftbrite.write(0,255,0); |
ewilliams61 | 7:79d0b30fedb4 | 121 | } else if(c3) { |
ewilliams61 | 7:79d0b30fedb4 | 122 | dev.printf("Welcome User 3\n"); |
ewilliams61 | 7:79d0b30fedb4 | 123 | // myShiftbrite.write(0,0,0); |
ewilliams61 | 7:79d0b30fedb4 | 124 | myShiftbrite.write(0,255,0); |
ewilliams61 | 7:79d0b30fedb4 | 125 | } else { |
ewilliams61 | 7:79d0b30fedb4 | 126 | dev.printf("Try Again\n"); |
ewilliams61 | 7:79d0b30fedb4 | 127 | // myShiftbrite.write(0,0,0); |
ewilliams61 | 7:79d0b30fedb4 | 128 | myShiftbrite.write(255,0,0); |
ewilliams61 | 7:79d0b30fedb4 | 129 | |
ewilliams61 | 7:79d0b30fedb4 | 130 | } |
ewilliams61 | 7:79d0b30fedb4 | 131 | inNdx = 0; |
ewilliams61 | 7:79d0b30fedb4 | 132 | |
ewilliams61 | 7:79d0b30fedb4 | 133 | } |
mbedAustin | 4:ba9100d52e48 | 134 | } |
ewilliams61 | 7:79d0b30fedb4 | 135 | |
mbedAustin | 4:ba9100d52e48 | 136 | } |