Wireless Communication Project, Bluetooth Door Lock System, Seneca College January 2017
Dependencies: Adafruit-GFX-Library TFT_ILI9163C mbed
main.cpp@0:67d120b65790, 2017-04-09 (annotated)
- Committer:
- Ozmos
- Date:
- Sun Apr 09 17:40:00 2017 +0000
- Revision:
- 0:67d120b65790
WCM_PROJECT;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ozmos | 0:67d120b65790 | 1 | #include "mbed.h" |
Ozmos | 0:67d120b65790 | 2 | #include <string> |
Ozmos | 0:67d120b65790 | 3 | |
Ozmos | 0:67d120b65790 | 4 | #include <Adafruit_GFX.h> |
Ozmos | 0:67d120b65790 | 5 | #include <TFT_ILI9163C.h> |
Ozmos | 0:67d120b65790 | 6 | #define __MOSI D11 |
Ozmos | 0:67d120b65790 | 7 | #define __MISO NC |
Ozmos | 0:67d120b65790 | 8 | #define __SCLK D13 |
Ozmos | 0:67d120b65790 | 9 | #define __CS D10 |
Ozmos | 0:67d120b65790 | 10 | #define __DC D15 |
Ozmos | 0:67d120b65790 | 11 | #define __RST D12 |
Ozmos | 0:67d120b65790 | 12 | |
Ozmos | 0:67d120b65790 | 13 | // Color definitions |
Ozmos | 0:67d120b65790 | 14 | #define BLACK 0x0000 |
Ozmos | 0:67d120b65790 | 15 | #define BLUE 0x001F |
Ozmos | 0:67d120b65790 | 16 | #define RED 0xF800 |
Ozmos | 0:67d120b65790 | 17 | #define GREEN 0x07E0 |
Ozmos | 0:67d120b65790 | 18 | #define CYAN 0x07FF |
Ozmos | 0:67d120b65790 | 19 | #define MAGENTA 0xF81F |
Ozmos | 0:67d120b65790 | 20 | #define YELLOW 0xFFE0 |
Ozmos | 0:67d120b65790 | 21 | #define WHITE 0xFFFF |
Ozmos | 0:67d120b65790 | 22 | #define TRANSPARENT -1 |
Ozmos | 0:67d120b65790 | 23 | /******************************************************state machine decleration**********/ |
Ozmos | 0:67d120b65790 | 24 | #define initial 1 |
Ozmos | 0:67d120b65790 | 25 | #define pair 2 |
Ozmos | 0:67d120b65790 | 26 | #define checkpin 3 |
Ozmos | 0:67d120b65790 | 27 | #define disconnected 4 |
Ozmos | 0:67d120b65790 | 28 | //******************************************************************************************// |
Ozmos | 0:67d120b65790 | 29 | #define SPI_BITRATE 50000000L |
Ozmos | 0:67d120b65790 | 30 | int color[8] = {0x001F, 0xF800,0x07E0,0x07FF,0xF81F,0xFFE0,0xFFFF,0x001F}; |
Ozmos | 0:67d120b65790 | 31 | TFT_ILI9163C tft(__MOSI, __MISO, __SCLK, __CS, __DC, __RST); |
Ozmos | 0:67d120b65790 | 32 | |
Ozmos | 0:67d120b65790 | 33 | Serial blue(D1,D0); |
Ozmos | 0:67d120b65790 | 34 | //*****************************************function declerations************************/ |
Ozmos | 0:67d120b65790 | 35 | void intial_screen(void); |
Ozmos | 0:67d120b65790 | 36 | void connectedBlue(void); |
Ozmos | 0:67d120b65790 | 37 | void disconnectedBlue(void); |
Ozmos | 0:67d120b65790 | 38 | bool passwordType(void); |
Ozmos | 0:67d120b65790 | 39 | void CorrectPassword(void); |
Ozmos | 0:67d120b65790 | 40 | void Incorrect(void); |
Ozmos | 0:67d120b65790 | 41 | /***************************************************************************************/ |
Ozmos | 0:67d120b65790 | 42 | char blueInput; |
Ozmos | 0:67d120b65790 | 43 | char passwordGlobal[3]; |
Ozmos | 0:67d120b65790 | 44 | unsigned char stage = initial; |
Ozmos | 0:67d120b65790 | 45 | int look =1; |
Ozmos | 0:67d120b65790 | 46 | int check =0; |
Ozmos | 0:67d120b65790 | 47 | int main() |
Ozmos | 0:67d120b65790 | 48 | { |
Ozmos | 0:67d120b65790 | 49 | tft.begin(); |
Ozmos | 0:67d120b65790 | 50 | tft.setBitrate(50000000); |
Ozmos | 0:67d120b65790 | 51 | tft.setRotation(0); |
Ozmos | 0:67d120b65790 | 52 | blueInput = '\0'; |
Ozmos | 0:67d120b65790 | 53 | blue.printf("*P* "); |
Ozmos | 0:67d120b65790 | 54 | while (true) { |
Ozmos | 0:67d120b65790 | 55 | |
Ozmos | 0:67d120b65790 | 56 | |
Ozmos | 0:67d120b65790 | 57 | |
Ozmos | 0:67d120b65790 | 58 | switch(stage){ |
Ozmos | 0:67d120b65790 | 59 | |
Ozmos | 0:67d120b65790 | 60 | case initial : if(look^check) // prints only ones a time in for loop by xOR |
Ozmos | 0:67d120b65790 | 61 | { |
Ozmos | 0:67d120b65790 | 62 | intial_screen(); |
Ozmos | 0:67d120b65790 | 63 | check = 1; |
Ozmos | 0:67d120b65790 | 64 | } |
Ozmos | 0:67d120b65790 | 65 | |
Ozmos | 0:67d120b65790 | 66 | |
Ozmos | 0:67d120b65790 | 67 | |
Ozmos | 0:67d120b65790 | 68 | if (blueInput=='F') |
Ozmos | 0:67d120b65790 | 69 | { |
Ozmos | 0:67d120b65790 | 70 | |
Ozmos | 0:67d120b65790 | 71 | stage = disconnected; |
Ozmos | 0:67d120b65790 | 72 | check = 0; |
Ozmos | 0:67d120b65790 | 73 | } |
Ozmos | 0:67d120b65790 | 74 | |
Ozmos | 0:67d120b65790 | 75 | if(blueInput == 'N'){ |
Ozmos | 0:67d120b65790 | 76 | |
Ozmos | 0:67d120b65790 | 77 | if(look^check){ |
Ozmos | 0:67d120b65790 | 78 | connectedBlue(); |
Ozmos | 0:67d120b65790 | 79 | check =1; |
Ozmos | 0:67d120b65790 | 80 | } |
Ozmos | 0:67d120b65790 | 81 | stage = pair; |
Ozmos | 0:67d120b65790 | 82 | check = 0; |
Ozmos | 0:67d120b65790 | 83 | } |
Ozmos | 0:67d120b65790 | 84 | blueInput = blue.getc(); // waits for input char |
Ozmos | 0:67d120b65790 | 85 | |
Ozmos | 0:67d120b65790 | 86 | |
Ozmos | 0:67d120b65790 | 87 | case pair : if(look^check){ |
Ozmos | 0:67d120b65790 | 88 | connectedBlue(); |
Ozmos | 0:67d120b65790 | 89 | check =1; |
Ozmos | 0:67d120b65790 | 90 | } |
Ozmos | 0:67d120b65790 | 91 | stage = checkpin; |
Ozmos | 0:67d120b65790 | 92 | check =0; |
Ozmos | 0:67d120b65790 | 93 | |
Ozmos | 0:67d120b65790 | 94 | |
Ozmos | 0:67d120b65790 | 95 | case checkpin: if(passwordType()) { |
Ozmos | 0:67d120b65790 | 96 | if(look^check){ |
Ozmos | 0:67d120b65790 | 97 | CorrectPassword(); |
Ozmos | 0:67d120b65790 | 98 | check=1; |
Ozmos | 0:67d120b65790 | 99 | } |
Ozmos | 0:67d120b65790 | 100 | stage = initial; |
Ozmos | 0:67d120b65790 | 101 | check =0; |
Ozmos | 0:67d120b65790 | 102 | } |
Ozmos | 0:67d120b65790 | 103 | else{ |
Ozmos | 0:67d120b65790 | 104 | if(look^check){ |
Ozmos | 0:67d120b65790 | 105 | Incorrect(); |
Ozmos | 0:67d120b65790 | 106 | check=1; |
Ozmos | 0:67d120b65790 | 107 | } |
Ozmos | 0:67d120b65790 | 108 | stage = initial; |
Ozmos | 0:67d120b65790 | 109 | check =0;} |
Ozmos | 0:67d120b65790 | 110 | |
Ozmos | 0:67d120b65790 | 111 | |
Ozmos | 0:67d120b65790 | 112 | case disconnected : if(look^check){ |
Ozmos | 0:67d120b65790 | 113 | disconnectedBlue(); |
Ozmos | 0:67d120b65790 | 114 | check = 1; |
Ozmos | 0:67d120b65790 | 115 | } |
Ozmos | 0:67d120b65790 | 116 | |
Ozmos | 0:67d120b65790 | 117 | stage = initial; |
Ozmos | 0:67d120b65790 | 118 | check = 0; |
Ozmos | 0:67d120b65790 | 119 | |
Ozmos | 0:67d120b65790 | 120 | }//switch |
Ozmos | 0:67d120b65790 | 121 | }//forloop |
Ozmos | 0:67d120b65790 | 122 | }//main |
Ozmos | 0:67d120b65790 | 123 | |
Ozmos | 0:67d120b65790 | 124 | void intial_screen(void){ |
Ozmos | 0:67d120b65790 | 125 | |
Ozmos | 0:67d120b65790 | 126 | |
Ozmos | 0:67d120b65790 | 127 | tft.clearScreen(); |
Ozmos | 0:67d120b65790 | 128 | tft.fillScreen(BLACK); |
Ozmos | 0:67d120b65790 | 129 | tft.setTextColor(GREEN); |
Ozmos | 0:67d120b65790 | 130 | tft.setTextSize(2); |
Ozmos | 0:67d120b65790 | 131 | tft.setCursor(1, 1); |
Ozmos | 0:67d120b65790 | 132 | tft.printf("Bluetooth"); |
Ozmos | 0:67d120b65790 | 133 | tft.setCursor(1,30); |
Ozmos | 0:67d120b65790 | 134 | tft.printf("Pairing"); |
Ozmos | 0:67d120b65790 | 135 | tft.setCursor(1,60); |
Ozmos | 0:67d120b65790 | 136 | tft.printf("Required"); |
Ozmos | 0:67d120b65790 | 137 | |
Ozmos | 0:67d120b65790 | 138 | |
Ozmos | 0:67d120b65790 | 139 | } |
Ozmos | 0:67d120b65790 | 140 | |
Ozmos | 0:67d120b65790 | 141 | void connectedBlue(void){ |
Ozmos | 0:67d120b65790 | 142 | tft.clearScreen(); |
Ozmos | 0:67d120b65790 | 143 | tft.fillScreen(BLACK); |
Ozmos | 0:67d120b65790 | 144 | tft.setTextColor(GREEN); |
Ozmos | 0:67d120b65790 | 145 | tft.setTextSize(2); |
Ozmos | 0:67d120b65790 | 146 | tft.setCursor(1, 1); |
Ozmos | 0:67d120b65790 | 147 | tft.printf("connected"); |
Ozmos | 0:67d120b65790 | 148 | tft.setCursor(1,30); |
Ozmos | 0:67d120b65790 | 149 | tft.printf("Type"); |
Ozmos | 0:67d120b65790 | 150 | tft.setCursor(1,60); |
Ozmos | 0:67d120b65790 | 151 | tft.printf("Password"); |
Ozmos | 0:67d120b65790 | 152 | |
Ozmos | 0:67d120b65790 | 153 | } |
Ozmos | 0:67d120b65790 | 154 | |
Ozmos | 0:67d120b65790 | 155 | void disconnectedBlue(void){ |
Ozmos | 0:67d120b65790 | 156 | tft.clearScreen(); |
Ozmos | 0:67d120b65790 | 157 | blue.printf("*P* "); |
Ozmos | 0:67d120b65790 | 158 | tft.fillScreen(BLACK); |
Ozmos | 0:67d120b65790 | 159 | tft.setTextColor(GREEN); |
Ozmos | 0:67d120b65790 | 160 | tft.setTextSize(2); |
Ozmos | 0:67d120b65790 | 161 | tft.setCursor(1,1); |
Ozmos | 0:67d120b65790 | 162 | tft.printf("Connection"); |
Ozmos | 0:67d120b65790 | 163 | tft.setCursor(1,30); |
Ozmos | 0:67d120b65790 | 164 | tft.printf("Lost"); |
Ozmos | 0:67d120b65790 | 165 | |
Ozmos | 0:67d120b65790 | 166 | wait_ms(2000); |
Ozmos | 0:67d120b65790 | 167 | |
Ozmos | 0:67d120b65790 | 168 | } |
Ozmos | 0:67d120b65790 | 169 | |
Ozmos | 0:67d120b65790 | 170 | bool passwordType(void){ |
Ozmos | 0:67d120b65790 | 171 | int i = 0; |
Ozmos | 0:67d120b65790 | 172 | char password[3]; |
Ozmos | 0:67d120b65790 | 173 | int b =32; |
Ozmos | 0:67d120b65790 | 174 | string c; |
Ozmos | 0:67d120b65790 | 175 | blue.printf("*P* "); |
Ozmos | 0:67d120b65790 | 176 | |
Ozmos | 0:67d120b65790 | 177 | |
Ozmos | 0:67d120b65790 | 178 | |
Ozmos | 0:67d120b65790 | 179 | |
Ozmos | 0:67d120b65790 | 180 | |
Ozmos | 0:67d120b65790 | 181 | for(i=0;i<3;i++){ |
Ozmos | 0:67d120b65790 | 182 | password[i] = blue.getc(); |
Ozmos | 0:67d120b65790 | 183 | if (blueInput=='F') |
Ozmos | 0:67d120b65790 | 184 | { |
Ozmos | 0:67d120b65790 | 185 | |
Ozmos | 0:67d120b65790 | 186 | stage = disconnected; |
Ozmos | 0:67d120b65790 | 187 | check = 0; |
Ozmos | 0:67d120b65790 | 188 | } |
Ozmos | 0:67d120b65790 | 189 | tft.setCursor(b,90); |
Ozmos | 0:67d120b65790 | 190 | tft.printf("*"); |
Ozmos | 0:67d120b65790 | 191 | c = password; |
Ozmos | 0:67d120b65790 | 192 | blue.printf("*P%s*",c); |
Ozmos | 0:67d120b65790 | 193 | b+=20; |
Ozmos | 0:67d120b65790 | 194 | } |
Ozmos | 0:67d120b65790 | 195 | |
Ozmos | 0:67d120b65790 | 196 | if(c == "123") return 1; |
Ozmos | 0:67d120b65790 | 197 | |
Ozmos | 0:67d120b65790 | 198 | else return 0; |
Ozmos | 0:67d120b65790 | 199 | |
Ozmos | 0:67d120b65790 | 200 | }//function |
Ozmos | 0:67d120b65790 | 201 | |
Ozmos | 0:67d120b65790 | 202 | |
Ozmos | 0:67d120b65790 | 203 | void CorrectPassword(void){ |
Ozmos | 0:67d120b65790 | 204 | tft.clearScreen(); |
Ozmos | 0:67d120b65790 | 205 | tft.setCursor(1, 1); |
Ozmos | 0:67d120b65790 | 206 | tft.printf("Correct"); |
Ozmos | 0:67d120b65790 | 207 | tft.setCursor(1,30); |
Ozmos | 0:67d120b65790 | 208 | tft.printf("Password"); |
Ozmos | 0:67d120b65790 | 209 | tft.setCursor(1,60); |
Ozmos | 0:67d120b65790 | 210 | tft.printf("Door"); |
Ozmos | 0:67d120b65790 | 211 | tft.setCursor(1,90); |
Ozmos | 0:67d120b65790 | 212 | tft.printf("Open 5 sec"); |
Ozmos | 0:67d120b65790 | 213 | wait_ms(5000); |
Ozmos | 0:67d120b65790 | 214 | |
Ozmos | 0:67d120b65790 | 215 | |
Ozmos | 0:67d120b65790 | 216 | } |
Ozmos | 0:67d120b65790 | 217 | |
Ozmos | 0:67d120b65790 | 218 | void Incorrect(void){ |
Ozmos | 0:67d120b65790 | 219 | tft.clearScreen(); |
Ozmos | 0:67d120b65790 | 220 | tft.setCursor(1, 1); |
Ozmos | 0:67d120b65790 | 221 | tft.printf("Wrong"); |
Ozmos | 0:67d120b65790 | 222 | tft.setCursor(1,30); |
Ozmos | 0:67d120b65790 | 223 | tft.printf("Password"); |
Ozmos | 0:67d120b65790 | 224 | tft.setCursor(1,60); |
Ozmos | 0:67d120b65790 | 225 | wait_ms(2000); |
Ozmos | 0:67d120b65790 | 226 | |
Ozmos | 0:67d120b65790 | 227 | } |