4180 Final Project

Dependencies:   4DGL-uLCD-SE mbed Servo

Committer:
ecarrick
Date:
Mon Apr 18 17:00:15 2016 +0000
Revision:
5:a2fbe5bd2be0
Parent:
4:e6b198a22216
Child:
6:50a82641d07b
Adding ability to save im vector to local file system.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pshabbaki3 0:5d35ff3dc9a5 1 #include "mbed.h"
pshabbaki3 0:5d35ff3dc9a5 2 #include <string>
pshabbaki3 0:5d35ff3dc9a5 3 #include <vector>
pshabbaki3 0:5d35ff3dc9a5 4 #include <iostream>
pshabbaki3 0:5d35ff3dc9a5 5 #include <sstream>
pshabbaki3 0:5d35ff3dc9a5 6 #include <iterator>
pshabbaki3 0:5d35ff3dc9a5 7 #include <stdio.h>
pshabbaki3 0:5d35ff3dc9a5 8 #include <ctype.h>
pshabbaki3 0:5d35ff3dc9a5 9 #include "uLCD_4DGL.h"
pshabbaki3 0:5d35ff3dc9a5 10
ecarrick 1:dd048edb2716 11 LocalFileSystem local("local");
pshabbaki3 0:5d35ff3dc9a5 12
pshabbaki3 0:5d35ff3dc9a5 13 DigitalOut myled(LED1);
pshabbaki3 0:5d35ff3dc9a5 14 Serial pc(USBTX,USBRX);
pshabbaki3 0:5d35ff3dc9a5 15 uLCD_4DGL lcd(p28,p27,p30); // serial tx, serial rx, reset pin;
pshabbaki3 0:5d35ff3dc9a5 16
pshabbaki3 0:5d35ff3dc9a5 17
pshabbaki3 0:5d35ff3dc9a5 18 vector<char> im;
pshabbaki3 4:e6b198a22216 19 vector<char> pass;
pshabbaki3 0:5d35ff3dc9a5 20 vector<char> name;
pshabbaki3 0:5d35ff3dc9a5 21 vector<char> final;
pshabbaki3 0:5d35ff3dc9a5 22
ecarrick 5:a2fbe5bd2be0 23 void save_to_file(){
ecarrick 5:a2fbe5bd2be0 24 FILE * fp = fopen("/local/users.txt", "w");
ecarrick 5:a2fbe5bd2be0 25 if(fp != NULL){
ecarrick 5:a2fbe5bd2be0 26 for(unsigned i=0; i< im.size(); i++){
ecarrick 5:a2fbe5bd2be0 27 fprintf(fp,"%c",vec[i]);
ecarrick 5:a2fbe5bd2be0 28 }
ecarrick 5:a2fbe5bd2be0 29 }
ecarrick 5:a2fbe5bd2be0 30 fclose(fp);
ecarrick 5:a2fbe5bd2be0 31 }
ecarrick 5:a2fbe5bd2be0 32
ecarrick 5:a2fbe5bd2be0 33 void load_from_file(){
ecarrick 5:a2fbe5bd2be0 34 FILE * fp = fopen("/local/users.txt", "r");
ecarrick 5:a2fbe5bd2be0 35 if(fp != NULL){
ecarrick 5:a2fbe5bd2be0 36 im.clear();
ecarrick 5:a2fbe5bd2be0 37 do{
ecarrick 5:a2fbe5bd2be0 38 im.push_back(fp.getc());
ecarrick 5:a2fbe5bd2be0 39 }
ecarrick 5:a2fbe5bd2be0 40 while(im.at(im.end()) != '#');
ecarrick 5:a2fbe5bd2be0 41 }
ecarrick 5:a2fbe5bd2be0 42 fclose(fp);
ecarrick 5:a2fbe5bd2be0 43 }
pshabbaki3 0:5d35ff3dc9a5 44
pshabbaki3 0:5d35ff3dc9a5 45 int i = 0;
pshabbaki3 0:5d35ff3dc9a5 46
pshabbaki3 4:e6b198a22216 47 void checkResult(vector<char> pass, vector<char> name)
pshabbaki3 0:5d35ff3dc9a5 48 {
pshabbaki3 0:5d35ff3dc9a5 49 // This function will check the keypad values and check if the passcode exists or not
pshabbaki3 0:5d35ff3dc9a5 50
pshabbaki3 0:5d35ff3dc9a5 51 vector<char> ans; //This vector should be the values coming from keypad
pshabbaki3 0:5d35ff3dc9a5 52 ans.push_back('1');
pshabbaki3 0:5d35ff3dc9a5 53 ans.push_back('2');
pshabbaki3 0:5d35ff3dc9a5 54 ans.push_back('3');
pshabbaki3 0:5d35ff3dc9a5 55 ans.push_back('4');
pshabbaki3 0:5d35ff3dc9a5 56 ans.push_back('5');
pshabbaki3 0:5d35ff3dc9a5 57 ans.push_back('6');
pshabbaki3 4:e6b198a22216 58 if (pass.size()==6) {
pshabbaki3 0:5d35ff3dc9a5 59 i++; //use "i" to find which person logged in.
pshabbaki3 4:e6b198a22216 60 if (ans == pass) {
pshabbaki3 0:5d35ff3dc9a5 61 // pc.printf("Found");
pshabbaki3 0:5d35ff3dc9a5 62 final = name;
pshabbaki3 4:e6b198a22216 63 final.insert(name.end(),pass.begin(),pass.end());
pshabbaki3 0:5d35ff3dc9a5 64
pshabbaki3 0:5d35ff3dc9a5 65 } else {
pshabbaki3 0:5d35ff3dc9a5 66 // pc.printf("not found");
pshabbaki3 4:e6b198a22216 67 //lcd.printf("\nnot found\n");
pshabbaki3 0:5d35ff3dc9a5 68 final.clear();
pshabbaki3 0:5d35ff3dc9a5 69 }
pshabbaki3 0:5d35ff3dc9a5 70 }
pshabbaki3 0:5d35ff3dc9a5 71 }
pshabbaki3 0:5d35ff3dc9a5 72
pshabbaki3 0:5d35ff3dc9a5 73
pshabbaki3 2:693a3af5af49 74 void checkKeyboard(vector<char> im)
pshabbaki3 0:5d35ff3dc9a5 75 {
pshabbaki3 2:693a3af5af49 76 for (int i = 0; i<im.size(); i++) {
pshabbaki3 4:e6b198a22216 77 // find space
pshabbaki3 2:693a3af5af49 78 if (im[i] ==' ') {
pshabbaki3 4:e6b198a22216 79 // Check the result
pshabbaki3 4:e6b198a22216 80 checkResult(pass,name);
pshabbaki3 4:e6b198a22216 81 // clear pass, name and final
pshabbaki3 4:e6b198a22216 82 pass.clear();
pshabbaki3 2:693a3af5af49 83 name.clear();
pshabbaki3 0:5d35ff3dc9a5 84 // pc.printf("clear");
pshabbaki3 4:e6b198a22216 85 //lcd.printf("clear");
pshabbaki3 2:693a3af5af49 86 } else {
pshabbaki3 2:693a3af5af49 87 if (isdigit(im[i])) {
pshabbaki3 4:e6b198a22216 88 // write numbers (char) to vector pass
pshabbaki3 4:e6b198a22216 89 pass.push_back(im[i]);
pshabbaki3 2:693a3af5af49 90
pshabbaki3 2:693a3af5af49 91 } else if (isalpha(im[i])) {
pshabbaki3 2:693a3af5af49 92 // write letters (char) to vector name
pshabbaki3 2:693a3af5af49 93 name.push_back(im[i]);
pshabbaki3 2:693a3af5af49 94
pshabbaki3 2:693a3af5af49 95 }
pshabbaki3 0:5d35ff3dc9a5 96 }
pshabbaki3 0:5d35ff3dc9a5 97 }
pshabbaki3 0:5d35ff3dc9a5 98 }
pshabbaki3 0:5d35ff3dc9a5 99
pshabbaki3 0:5d35ff3dc9a5 100
pshabbaki3 0:5d35ff3dc9a5 101
pshabbaki3 0:5d35ff3dc9a5 102 int main()
pshabbaki3 0:5d35ff3dc9a5 103 {
pshabbaki3 2:693a3af5af49 104 //load_from_file();
pshabbaki3 2:693a3af5af49 105
pshabbaki3 0:5d35ff3dc9a5 106 //interupt driven read from serial port
pshabbaki3 0:5d35ff3dc9a5 107 // calls save_to_file();
pshabbaki3 0:5d35ff3dc9a5 108 while(1) {
pshabbaki3 0:5d35ff3dc9a5 109 char c = pc.getc();
pshabbaki3 0:5d35ff3dc9a5 110 if (c!='#') {
pshabbaki3 0:5d35ff3dc9a5 111 im.push_back(c); //writing all the characters into vector
pshabbaki3 2:693a3af5af49 112 checkKeyboard(im);
pshabbaki3 0:5d35ff3dc9a5 113 } else {
pshabbaki3 0:5d35ff3dc9a5 114 for (int cnt = 0; cnt<im.size(); cnt++) {
pshabbaki3 0:5d35ff3dc9a5 115 pc.printf("%c",im[cnt]);
pshabbaki3 4:e6b198a22216 116 //lcd.printf("%c",im[cnt]);
pshabbaki3 0:5d35ff3dc9a5 117 }
pshabbaki3 0:5d35ff3dc9a5 118 }
pshabbaki3 0:5d35ff3dc9a5 119 }
pshabbaki3 0:5d35ff3dc9a5 120
pshabbaki3 0:5d35ff3dc9a5 121 }
pshabbaki3 0:5d35ff3dc9a5 122
pshabbaki3 0:5d35ff3dc9a5 123