Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of FINAL_PROJECT_4180 by
Revision 12:5cb9ffad1ad7, committed 2016-05-06
- Comitter:
- nyengele
- Date:
- Fri May 06 13:10:20 2016 +0000
- Parent:
- 11:1d7021c0739d
- Child:
- 13:3d521aa61e6a
- Commit message:
- fixed magnetic card bug
Changed in this revision
--- a/4DGL-uLCD-SE.lib Mon Apr 25 02:38:50 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/cryst_lcd.cpp Fri May 06 13:10:20 2016 +0000
@@ -0,0 +1,247 @@
+/**
+ * Driver for LCD devices based on the HD44780 controller
+ */
+
+/* Description
+
+LCD SIZES SUPPORTED:
+1-line displays: 8x1, 16x1, 20x1, 24x1, 32x1, 40x1
+2-line displays: 16x2, 20x2, 24x2, 32x2, 40x2
+4-line displays: 16x4, 20x4, 40x4
+
+SUPPORTED PLATFORM: mbed
+*/
+
+
+#include "cryst_lcd.h"
+
+Cryst_LCD::Cryst_LCD(PinName rs, PinName en, PinName db4, PinName db5,
+ PinName db6, PinName db7, LCDSize size)
+ : _rs(rs), _en(en), _data(db4, db5, db6, db7)
+{
+ _size = size;
+ _row = 0;
+ _col = 0;
+ _displayStatus = 0x0C;
+ init();
+}
+
+
+void Cryst_LCD::cls()
+{
+ writeCommand(0x01, false);
+ wait_ms(2);
+ locate(0, 0);
+}
+
+void Cryst_LCD::locate(int row, int col)
+{
+ _row = 0;
+ _col = col;
+
+ if (row < getMaxRows()) {
+ _row = row;
+ }
+
+ if (_col >= getMaxCols()) {
+ _col = 0;
+ if (_row < getMaxRows() - 1)
+ _row++;
+ else _row = 0;
+ }
+
+ int addr = getAddress(_row, _col);
+ addr |= 0x80;
+ writeCommand(addr);
+}
+
+void Cryst_LCD::display_off() {
+ _displayStatus &= 0x0B;
+ writeCommand(_displayStatus);
+}
+
+void Cryst_LCD::display_on() {
+ _displayStatus |= 0x04;
+ writeCommand(_displayStatus);
+}
+
+void Cryst_LCD::cursor_on() {
+ _displayStatus |= 0x02;
+ writeCommand(_displayStatus);
+}
+
+void Cryst_LCD::cursor_off() {
+ _displayStatus &= 0x0D;
+ writeCommand(_displayStatus);
+}
+
+void Cryst_LCD::cursor_blink() {
+ _displayStatus |= 0x01;
+ writeCommand(_displayStatus);
+}
+
+void Cryst_LCD::cursor_no_blink() {
+ _displayStatus &= 0x0E;
+ writeCommand(_displayStatus);
+}
+
+void Cryst_LCD::clear_line() {
+ int lineWidth = getMaxCols();
+
+ for (int i = 0; i < lineWidth - 1; i++) {
+ locate(_row, i);
+ printf(" ");
+ }
+
+ locate(_row, 0);
+}
+
+void Cryst_LCD::reset()
+{
+ _rs = 0;
+ wait_ms(15); // wait approximately 15 ms
+ writeNibble(0x3); // write config data
+ wait_ms(5); // wait approximately 4.1 ms
+ writeNibble(0x3); // write config data again
+ wait_us(100); // wait approximately 100 us
+}
+
+void Cryst_LCD::init()
+{
+ reset();
+
+ // Function set
+ if (getMaxRows() <= 1) { // 1-line display
+ // (DF = 0, N = 0, F = 0)
+ // 4-bit mode, 1-line display, 5x8 font size
+ writeCommand(0x20);
+ } else { // Multi-line display
+ // (DF = 0, N = 1, F = 0)
+ // 4-bit mode, 2-line display, 5x8 font size
+ writeCommand(0x28);
+ }
+
+ // Entry Mode
+ // (I/D = 1, S = 0)
+ // DDRAM autoincrement, No display shifts
+ writeCommand(0x06);
+
+ // Display Control
+ // (D = 1, C = 0, B = 0)
+ // Display ON, cursor OFF, No cursor blinking
+ writeCommand(_displayStatus);
+
+ // Clear Display
+ cls();
+}
+
+int Cryst_LCD::getAddress(int row, int col)
+{
+ int rowStartAddr[4] = {0x00, 0x40, 0x14, 0x54};
+ int tempRow = 0, tempCol = 0;
+ if (row < getMaxRows()) tempRow = row;
+ if (col < getMaxCols()) tempCol = col;
+ return rowStartAddr[tempRow] + tempCol;
+}
+
+// Default wait time is 40 us
+void Cryst_LCD::writeData(int ch, bool use_default_timing)
+{
+ _rs = 1;
+ writeNibble((ch >> 4) & 0x0F);
+ writeNibble(ch & 0x0F);
+
+ if (use_default_timing)
+ wait_us(40);
+}
+
+// Default wait time is 40 us
+void Cryst_LCD::writeCommand(int cmd, bool use_default_timing)
+{
+ _rs = 0;
+ writeNibble((cmd >> 4) & 0x0F);
+ writeNibble(cmd & 0x0F);
+
+ if (use_default_timing)
+ wait_us(40);
+}
+
+void Cryst_LCD::writeNibble(int nib)
+{
+ _data = nib & 0xF;
+ wait_us(1);
+ _en = 1;
+ wait_us(1);
+ _en = 0;
+ wait_us(1);
+}
+
+void Cryst_LCD::getMaxDimensions(int* rowCount, int* colCount) {
+ switch(_size) {
+ // 1-line displays
+ case LCD8x1: *rowCount = 1; *colCount = 8; break;
+ case LCD16x1: *rowCount = 1; *colCount = 16; break;
+ case LCD20x1: *rowCount = 1; *colCount = 20; break;
+ case LCD24x1: *rowCount = 1; *colCount = 24; break;
+ case LCD32x1: *rowCount = 1; *colCount = 32; break;
+ case LCD40x1: *rowCount = 1; *colCount = 40; break;
+
+ // 2-line displays
+ case LCD16x2: *rowCount = 2; *colCount = 16; break;
+ case LCD20x2: *rowCount = 2; *colCount = 20; break;
+ case LCD24x2: *rowCount = 2; *colCount = 24; break;
+ case LCD32x2: *rowCount = 2; *colCount = 32; break;
+ case LCD40x2: *rowCount = 2; *colCount = 40; break;
+
+ // 4-line displays
+ case LCD16x4: *rowCount = 4; *colCount = 16; break;
+ case LCD20x4: *rowCount = 4; *colCount = 20; break;
+ case LCD40x4: *rowCount = 4; *colCount = 40; break;
+
+ default: *rowCount = 0; *colCount = 0; break;
+ }
+}
+
+int Cryst_LCD::getMaxRows()
+{
+ int rowCount = 0;
+ int colCount = 0;
+ getMaxDimensions(&rowCount, &colCount);
+ return rowCount;
+}
+
+int Cryst_LCD::getMaxCols()
+{
+ int rowCount = 0;
+ int colCount = 0;
+ getMaxDimensions(&rowCount, &colCount);
+ return colCount;
+}
+
+int Cryst_LCD::_putc(int ch)
+{
+ if (ch == '\n') {
+ locate(_row + 1, 0);
+ }
+
+ else if (ch == '\t') {
+ locate(_row, _col + 3);
+ }
+
+ else if (ch == '\r') {
+ locate(_row, 0);
+ }
+
+ else {
+ locate(_row, _col);
+ writeData(ch);
+ locate(_row, _col + 1);
+ }
+
+ return ch;
+}
+
+int Cryst_LCD::_getc()
+{
+ return -5;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/cryst_lcd.h Fri May 06 13:10:20 2016 +0000
@@ -0,0 +1,81 @@
+/**
+ * Driver for LCD devices based on the HD44780 controller
+ */
+
+/* Description
+
+LCD SIZES SUPPORTED:
+1-line displays: 8x1, 16x1, 20x1, 24x1, 32x1, 40x1
+2-line displays: 16x2, 20x2, 24x2, 32x2, 40x2
+4-line displays: 16x4, 20x4, 40x4
+
+SUPPORTED PLATFORM: mbed
+
+DISCLAIMER: The author does not warrant the functions contained in the
+ program will meet your requirements or that the operation
+ of the program will be uninterrupted or error-free.
+ In no event will the author (Gedeon Nyengele) be liable
+ for any damages, including any lost profit, lost savings,
+ lost patience or other incidental or consequential damage.
+*/
+
+#ifndef CRYST_LCD_H
+#define CRYST_LCD_H
+
+#include "mbed.h"
+
+class Cryst_LCD : public Stream
+{
+public:
+ // LCD Size
+ enum LCDSize {
+ LCD8x1, LCD16x1, LCD20x1, LCD24x1, LCD32x1, LCD40x1, // 1-line displays
+ LCD16x2, LCD20x2, LCD24x2, LCD32x2, LCD40x2, // 2-line displays
+ LCD16x4, LCD20x4, LCD40x4 // 4-line displays
+ };
+
+ Cryst_LCD(PinName rs, PinName en, PinName db4, PinName db5,
+ PinName db6, PinName db7, LCDSize size = LCD16x2);
+ void cls(); // Clear entire LCD screen
+ void locate(int row, int col); // Move cursor to speficic location
+ void display_off(); // Turn display off (data conserved)
+ void display_on(); // Turn display on
+ void cursor_on(); // Show cursor
+ void cursor_off(); // Hide cursor
+ void cursor_blink(); // Make cursor blink
+ void cursor_no_blink(); // Make cursor no to blink
+ void clear_line(); // Clear content on current line and put cursor at the
+ // beginning of the line
+ int cols() { return getMaxCols(); } // Get max number or columns for the LCD module used
+ int rows() { return getMaxRows(); } // Get max number or rows for the LCD module used
+ int getCursorRow() { return _row; } // Get row number where cursor is currently located
+ int getCursorCol() { return _col; } // Get column number where cursor is currently located
+
+protected:
+ virtual int _putc(int ch);
+ virtual int _getc();
+
+private:
+ void reset();
+ void init();
+ int getAddress(int row, int col);
+ void writeData(int ch, bool use_default_timing = true);
+ void writeCommand(int cmd, bool use_default_timing = true);
+ void writeNibble(int nib);
+ void getMaxDimensions(int* rowCount, int* colCount);
+ int getMaxRows();
+ int getMaxCols();
+
+private:
+ DigitalOut _rs;
+ DigitalOut _en;
+ BusOut _data;
+ int _row;
+ int _col;
+ LCDSize _size;
+ int _displayStatus;
+};
+
+
+
+#endif // CRYST_LCD_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/keypad.h Fri May 06 13:10:20 2016 +0000
@@ -0,0 +1,175 @@
+/**
+ * Driver for Keypad devices
+ * @author Gedeon Nyengele
+ * @version 1.0
+ * @date 5/7/2015
+ */
+
+#ifndef KEYPAD_H
+#define KEYPAD_H
+#include "mbed.h"
+
+template<unsigned char nRows, unsigned char nCols>
+class Keypad
+{
+public:
+ Keypad(char matrix[nRows][nCols], BusInOut* rows, BusInOut* cols);
+ char getKeyChar(); // Get the value of the pressed key
+ char getKeyChar(void (*wait_ms_fn)(int millis));
+ ~Keypad(){};
+ enum BTN_STATE { RELEASE, DEBOUNCE_PRESS, PRESSED, DEBOUNCE_RELEASE, DONE };
+
+private:
+ int getKey();
+
+protected:
+ char _matrix[nRows][nCols];
+ BusInOut* _cols;
+ BusInOut* _rows;
+};
+
+/** --------------------------------------------------------------------------
+ * Constructor
+ *----------------------------------------------------------------------------
+ */
+template<unsigned char nRows, unsigned char nCols>
+Keypad<nRows, nCols>::Keypad(char matrix[nRows][nCols], BusInOut* rows, BusInOut* cols)
+{
+
+ // Copy keypad matrix
+ for (int i = 0; i < nRows; i++) {
+ for (int j = 0; j < nCols; j++) {
+ _matrix[i][j] = matrix[i][j];
+ }
+ }
+
+ // Create data bus for row pins
+ _rows = rows;
+
+ // Create data bus for column pins
+ _cols = cols;
+}
+
+template<unsigned char nRows, unsigned char nCols>
+char Keypad<nRows, nCols>::getKeyChar(void (*wait_ms_fn)(int millis)) {
+ int key;
+ int temp_key = 0;
+ bool done = false;
+ BTN_STATE state = RELEASE;
+
+ while(!done) {
+ switch(state) {
+ case RELEASE:
+ key = getKey();
+ if (key < 0) {
+ state = RELEASE;
+ } else {
+ state = DEBOUNCE_PRESS;
+ temp_key = key;
+ }
+ break;
+
+ case DEBOUNCE_PRESS:
+ if (wait_ms_fn == 0) wait_ms(30);
+ else wait_ms_fn(30);
+ key = getKey();
+ if (key == temp_key) {
+ state = PRESSED;
+ } else {
+ state = RELEASE;
+ }
+ break;
+
+ case PRESSED:
+ key = getKey();
+ if (key == temp_key) {
+ state = PRESSED;
+ } else {
+ state = DEBOUNCE_RELEASE;
+ }
+ break;
+
+ case DEBOUNCE_RELEASE:
+ if (wait_ms_fn == 0) wait_ms(30);
+ else wait_ms_fn(30);
+ key = getKey();
+ if (key == temp_key) {
+ state = PRESSED;
+ } else {
+ state = RELEASE;
+ done = true;
+ }
+ }
+ }
+
+ return (char) temp_key;
+}
+
+/** --------------------------------------------------------------------------
+ * Get Key Character
+ *----------------------------------------------------------------------------
+ */
+template<unsigned char nRows, unsigned char nCols>
+char Keypad<nRows, nCols>::getKeyChar()
+{
+ return getKeyChar(0);
+}
+
+
+// HELPER FUNCTIONS
+
+template<unsigned char nRows, unsigned char nCols>
+int Keypad<nRows, nCols>::getKey()
+{
+ int rowVal = (1 << nRows) - 1;
+ int colVal = (1 << nCols) - 1;
+ int row = -1, col = -1;
+ unsigned int temp;
+
+ // Read rows
+ _cols->output();
+ _cols->write(0);
+ _rows->input();
+ _rows->mode(PullUp);
+ wait_us(5);
+ rowVal = _rows->read();
+
+ //Read colums
+ _rows->output();
+ _rows->write(0);
+ _cols->input();
+ _cols->mode(PullUp);
+ wait_us(5);
+ colVal = _cols->read();
+
+ // Signal no key press
+ if (rowVal == ((1 << nRows) - 1) || colVal == ((1 << nCols) - 1) )
+ return -1;
+
+ // Find row number
+ temp = rowVal ^ ((1 << nRows) - 1);
+ for (int i = 0; i < nRows; i++) {
+
+ if ((temp & (1 << i)) > 0) {
+ row = i;
+ break;
+ }
+ }
+
+ // Find column number
+ temp = colVal ^ ((1 << nCols) - 1);
+ for (int i = 0; i < nCols; i++) {
+ if ((temp & (1 << i)) > 0) {
+ col = i;
+ break;
+ }
+ }
+
+ if (row == -1 || col == -1)
+ return -1;
+ else {
+ return _matrix[row][col];
+ }
+}
+
+#endif // KEYPAD_H
\ No newline at end of file
--- a/lib.cpp Mon Apr 25 02:38:50 2016 +0000
+++ b/lib.cpp Fri May 06 13:10:20 2016 +0000
@@ -1,10 +1,12 @@
#include "mbed.h"
#include "lib.h"
+#include <cstring>
#define CMD_ENROLL 0x01
#define CMD_AUTH 0x02
#define FACE_FID 0x01
#define SPEECH_FID 0x02
+#define VOICE_FID 0x03
#define SUCCESS 0x55
#define FAILURE 0x8A
@@ -67,6 +69,7 @@
bool face_enroll(char *user_id, Serial *android)
{
char CMD[] = {
+ 0x0A,
CMD_ENROLL,
0x02,
0x04,
@@ -80,6 +83,7 @@
};
for (int i = 0; i < sizeof(CMD)/ sizeof(char); i++) {
+ while(!android->writeable());
android->putc(CMD[i]);
}
@@ -89,9 +93,10 @@
else return false;
}
-bool face_detect(char *user_id, Serial *android)
+bool face_auth(char *user_id, Serial *android)
{
char CMD[] = {
+ 0x0A,
CMD_AUTH,
0x02,
0x04,
@@ -105,6 +110,7 @@
};
for (int i = 0; i < sizeof(CMD)/ sizeof(char); i++) {
+ while(!android->writeable());
android->putc(CMD[i]);
}
@@ -117,6 +123,7 @@
bool speech_enroll(char *user_id, Serial *android)
{
char CMD[] = {
+ 0x0A,
CMD_ENROLL,
0x02,
0x04,
@@ -130,6 +137,7 @@
};
for (int i = 0; i < sizeof(CMD)/ sizeof(char); i++) {
+ while(!android->writeable());
android->putc(CMD[i]);
}
@@ -139,9 +147,10 @@
else return false;
}
-bool speech_detect(char *user_id, Serial *android)
+bool speech_auth(char *user_id, Serial *android)
{
char CMD[] = {
+ 0x0A,
CMD_AUTH,
0x02,
0x04,
@@ -155,6 +164,61 @@
};
for (int i = 0; i < sizeof(CMD)/ sizeof(char); i++) {
+ while(!android->writeable());
+ android->putc(CMD[i]);
+ }
+
+ while (!android->readable());
+ char temp = android->getc();
+ if (temp == SUCCESS) return true;
+ else return false;
+}
+
+bool voice_enroll(char *user_id, Serial *android)
+{
+ char CMD[] = {
+ 0x0A,
+ CMD_ENROLL,
+ 0x02,
+ 0x04,
+ 0x01,
+ user_id[0],
+ user_id[1],
+ user_id[2],
+ user_id[3],
+ VOICE_FID,
+ '\0'
+ };
+
+ for (int i = 0; i < sizeof(CMD)/ sizeof(char); i++) {
+ while(!android->writeable());
+ android->putc(CMD[i]);
+ }
+
+ while (!android->readable());
+ char temp = android->getc();
+ if (temp == SUCCESS) return true;
+ else return false;
+}
+
+bool voice_auth(char *user_id, Serial *android)
+{
+ char CMD[] = {
+ 0x0A,
+ CMD_AUTH,
+ 0x02,
+ 0x04,
+ 0x01,
+ user_id[0],
+ user_id[1],
+ user_id[2],
+ user_id[3],
+ VOICE_FID,
+ '\0'
+ };
+
+ for (int i = 0; i < sizeof(CMD)/ sizeof(char); i++) {
+ while(!android->writeable());
android->putc(CMD[i]);
}
@@ -172,72 +236,138 @@
*speaker = 0.0;
}
-void failure_display(uLCD_4DGL *lcd, char *text)
+void display_msg(Cryst_LCD *lcd, char *text)
{
- lcd->baudrate(3000000);
lcd->cls();
- lcd->textbackground_color(WHITE);
- lcd->text_mode(OPAQUE);
- lcd->text_width(4);
- lcd->text_height(4);
- lcd->text_string(text, 1, 4, FONT_7X8, RED);
+ lcd->locate(1, 0);
+ lcd->printf("%s", text);
}
-void success_display(uLCD_4DGL *lcd, char *text)
+void greeting(Cryst_LCD *lcd)
{
- lcd->baudrate(3000000);
lcd->cls();
- lcd->textbackground_color(WHITE);
- lcd->text_mode(OPAQUE);
- lcd->text_width(4);
- lcd->text_height(4);
- lcd->text_string(text, 1, 4, FONT_7X8, BLUE);
-}
-
-void greeting(uLCD_4DGL *lcd)
-{
- lcd->baudrate(3000000);
- lcd->cls();
- lcd->textbackground_color(WHITE);
- lcd->text_mode(OPAQUE);
- lcd->text_width(4);
- lcd->text_height(4);
- lcd->text_string("Enter 4-digit PIN: ", 1, 4, FONT_7X8, BLUE);
+ lcd->locate(0, 7);
+ lcd->printf("WELCOME");
+ lcd->locate(3, 0);
+ lcd->printf("*:AUTH");
+ lcd->locate(3, 12);
+ lcd->printf("#:ENROLL");
}
-int id_to_int(char *user_id, int size)
+bool prompt(const char* msg, Cryst_LCD *lcd, Keypad<4, 3>* pad)
{
- int val = 0;
- for (int i = 0; i < size; i++) {
- val += 10 * val + (user_id[i] - '0');
- }
- return val;
-}
-
-void add_to_table(HASH_RECORD *table, int ind, int user_id, int hash)
-{
- HASH_RECORD rec = {user_id, hash};
- table[ind] = rec;
+ bool res = false;
+ char temp;
+ lcd->cls();
+ lcd->locate(0,0);
+ lcd->printf("%s", msg);
+ lcd->locate(3, 0);
+ lcd->printf("*:NO");
+ lcd->locate(3, 15);
+ lcd->printf("#:YES");
+ while ((temp = pad->getKeyChar()) != '#' && temp != '*');
+ if (temp == '#') res = true;
+ return res;
}
-bool contain_hash(HASH_RECORD *table, int table_size, int user_id, int hash)
+bool is_valid_user(char *user_id)
{
- for (int i = 0; i < table_size; i++) {
- HASH_RECORD h = table[i];
- if (h.user_id == user_id && h.hash == hash) {
- return false;
- }
- }
- return false;
+ char full_path[30];
+ std::strcpy(full_path, "/local/");
+ char filename[10];
+ std::memcpy(filename, user_id, 4);
+ filename[4] = '\0';
+ std::strcat(filename, ".TXT");
+ std::strcat(full_path, filename);
+ FILE *fp = fopen(full_path, "r");
+ if (fp == NULL)return false;
+ else fclose(fp);
+ return true;
}
-int read_keypad()
+bool write_hash_to_file(char *user_id, int hash)
{
- return 0;
-}
-
-bool get_user_id(char *user_id)
-{
+ char full_path[30];
+ std::strcpy(full_path, "/local/");
+ char filename[10];
+ std::memcpy(filename, user_id, 4);
+ filename[4] = '\0';
+ std::strcat(filename, ".TXT");
+ std::strcat(full_path, filename);
+ FILE *fp = fopen(full_path, "w");
+ if (fp == NULL) return false;
+ fprintf(fp, "%d", hash);
+ fclose(fp);
return true;
}
+bool read_hash_from_file(char *user_id, int *hash)
+{
+ bool success_read = false;
+ char full_path[30];
+ std::strcpy(full_path, "/local/");
+ char filename[10];
+ std::memcpy(filename, user_id, 4);
+ filename[4] = '\0';
+ std::strcat(filename, ".TXT");
+ std::strcat(full_path, filename);
+ FILE *fp = fopen(full_path, "r");
+ if (fp) {
+ int t = fscanf(fp, "%d", hash);
+ if (t != 0 && t != EOF) success_read = true;
+ fclose(fp);
+ }
+ return success_read;
+}
+
+bool read_pin(char *buffer, int length, Cryst_LCD* lcd, Keypad<4, 3>* pad)
+{
+ bool res = false;
+ int cur = 0;
+ lcd->cls();
+ lcd->locate(3,0);
+ lcd->printf("*:CLEAR");
+ lcd->locate(3, 13);
+ lcd->printf("#:ENTER");
+ lcd->locate(0,0);
+ lcd->printf("Enter 4-Digit PIN:\n");
+ lcd->cursor_on();
+ lcd->cursor_blink();
+ bool done = false;
+ char temp_char;
+ while (!done) {
+ temp_char = pad->getKeyChar();
+ if (temp_char == '#') done = true;
+ else if (temp_char == '*') {
+ lcd->clear_line();
+ cur = 0;
+ } else {
+ if (cur < 19) lcd->printf("%C", '*');
+ if (cur < length) buffer[cur] = temp_char;
+ cur++;
+ }
+ }
+ if (cur == length) res = true;
+ lcd->cursor_off();
+ lcd->cursor_no_blink();
+ return res;
+}
+
+
+bool fp_clearAll(FPScanner *fp)
+{
+ bool status;
+ fp->SetLED(true);
+ status = fp->DeleteAll();
+ fp->SetLED(false);
+ return status;
+}
+
+char clear_serial_buffer(Serial *dev)
+{
+ char temp;
+ while (dev->readable()) {
+ temp = dev->getc();
+ }
+ return temp;
+}
--- a/lib.h Mon Apr 25 02:38:50 2016 +0000
+++ b/lib.h Fri May 06 13:10:20 2016 +0000
@@ -1,6 +1,7 @@
#include "mbed.h"
+#include "keypad.h"
+#include "cryst_lcd.h"
#include "FPScanner.h"
-#include "uLCD_4DGL.h"
typedef struct{
int user_id;
@@ -12,17 +13,20 @@
bool fp_enroll(FPScanner *fp);
bool fp_auth(FPScanner *fp);
bool face_enroll(char *user_id, Serial *android);
-bool face_detect(char *user_id, Serial *android);
-bool speech_detect(char *user_id, Serial *android);
+bool face_auth(char *user_id, Serial *android);
+bool speech_auth(char *user_id, Serial *android);
bool speech_enroll(char *user_id, Serial *android);
+bool voice_enroll(char *user_id, Serial *android);
+bool voice_auth(char *user_id, Serial *android);
void buzzer(PwmOut *speaker, int seconds);
-void failure_display(uLCD_4DGL *lcd, char *text);
-void success_display(uLCD_4DGL *lcd, char *text);
-int id_to_int(char *user_id, int size);
-void add_to_table(HASH_RECORD *table, int ind, int user_id, int hash);
-bool contain_hash(HASH_RECORD *table, int table_size, int user_id, int hash);
-void greeting(uLCD_4DGL *lcd);
+void display_msg(Cryst_LCD *lcd, char *text);
+bool read_pin(char *buffer, int length, Cryst_LCD* lcd, Keypad<4, 3>* pad);
+void greeting(Cryst_LCD *lcd);
+bool prompt(const char* msg, Cryst_LCD *lcd, Keypad<4, 3>* pad);
-// TO IMPLEMENT
-int read_keypad();
-bool get_user_id(char *user_id);
\ No newline at end of file
+// to implement
+bool is_valid_user(char *user_id);
+bool write_hash_to_file(char *user_id, int hash);
+bool read_hash_from_file(char *user_id, int *hash);
+bool fp_clearAll(FPScanner *fp);
+char clear_serial_buffer(Serial *dev);
\ No newline at end of file
--- a/main.cpp Mon Apr 25 02:38:50 2016 +0000
+++ b/main.cpp Fri May 06 13:10:20 2016 +0000
@@ -1,84 +1,229 @@
#include "mbed.h"
+#include "keypad.h"
+#include "cryst_lcd.h"
+#include "FPScanner.h"
#include "lib.h"
-
-Serial mag_card(p13, p14);
-Serial android(USBTX, USBRX);
-FPScanner fp(p9, p10);
-uLCD_4DGL lcd(p28, p27, p30);
-PwmOut speaker(p21);
+#include <cstring>
-// Flash Memory
-SPI spi(p5, p6, p7); // MOSI, MISO, CLK
-DigitalOut cs(p20); // Chip Select
+Serial mag_card(p9, p10);
+Serial android(USBTX, USBRX);
+FPScanner fp(p28, p27);
+Cryst_LCD lcd(p20, p19, p18, p17, p16, p15, Cryst_LCD::LCD20x4);
+PwmOut speaker(p21);
+//Create the keypad ky values
+char mat[][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'}};
+// Create the bus for use with the keypad's columns
+BusInOut colPins(p24, p22, p26); // increasing order (column 1 ....n)
+// Create the bus for use with the keypad's rows
+BusInOut rowPins(p23, p30, p29, p25); // increasing order (row 1 ....n)
+// Create the keypad object
+Keypad<4, 3> pad(mat, &rowPins, &colPins);
+LocalFileSystem local_fs("local");
+DigitalOut door_lock(p8);
+DigitalIn sys_reset(p14);
-// hash table
-const int MAX_ENTRIES = 100;
-HASH_RECORD records[MAX_ENTRIES];
-int record_index = 0;
-
-enum STATE {STANDBY, ENROLL, AUTH, FP_ENROLL, FP_AUTH, MC_ENROLL, MC_AUTH, FACE_ENROLL, FACE_AUTH, SPEECH_ENROLL,
- ENROLL_SUCCESS, AUTH_SUCCESS, ENROLL_FAIL, AUTH_FAIL, UNLOCK
+enum STATE {STANDBY, ENROLL, AUTH, FP_ENROLL, FP_AUTH, MC_ENROLL, MC_AUTH, FACE_ENROLL, FACE_AUTH, SPEECH_ENROLL, SPEECH_AUTH,
+ ENROLL_SUCCESS, AUTH_SUCCESS, VOICE_ENROLL, VOICE_AUTH
};
char user_id[4];
char mag_card_data[250];
+char keyboard_input[20];
int mc_bytes = 0;
-int temp;
-int temp1;
+char temp_char;
+int temp_int;
+bool flag;
int main()
{
+ sys_reset.mode(PullUp);
+ if (sys_reset == 0) fp_clearAll(&fp);
+ speaker.period(1.0/440);
+ door_lock = 0;
STATE current_state = STANDBY;
while (true) {
switch (current_state) {
- case STANDBY:
+ case STANDBY: {
+ clear_serial_buffer(&android);
greeting(&lcd);
- int input = read_keypad();
- while (input != 10 && input != 11) {
- wait(0.03);
- input = read_keypad();
+ temp_char = pad.getKeyChar();
+ while (temp_char != '#' && temp_char != '*') {
+ temp_char = pad.getKeyChar();
}
- if (input == 10) current_state = ENROLL;
- else current_state = AUTH;
+ if (temp_char == '*') current_state = AUTH;
+ else current_state = ENROLL;
break;
-
- case ENROLL:
- if (get_user_id(user_id)) current_state = FP_ENROLL;
- else current_state = ENROLL_FAIL;
+ }
+ case ENROLL: {
+ if (read_pin(user_id, 4, &lcd, &pad)) {
+ if (is_valid_user(user_id)) {
+ display_msg(&lcd, "USER ALREADY\nENROLLED.");
+ wait(3);
+ current_state = STANDBY;
+ } else current_state = FACE_ENROLL;
+ } else {
+ display_msg(&lcd, "INVALID PIN.");
+ wait(3);
+ current_state = STANDBY;
+ }
break;
-
- case FP_ENROLL:
+ }
+ case FP_ENROLL: {
+ display_msg(&lcd, "PLACE YOUR FINGER\nON SCANNER.");
if (fp_enroll(&fp)) {
current_state = MC_ENROLL;
} else {
- current_state = ENROLL_FAIL;
+ display_msg(&lcd, "FINGERPRINT FAILED.");
+ wait(3);
+ current_state = STANDBY;
+ }
+ break;
+ }
+ case MC_ENROLL: {
+ flag = false;
+ while (!flag) {
+ memset(mag_card_data, 0, 250);
+ display_msg(&lcd, "SWIPE CARD NOW");
+ read_mag_card(&mag_card, mag_card_data, &mc_bytes);
+ if (prompt("SUCCESSFUL SWIPE?", &lcd, &pad)) flag = true;
+ }
+ temp_int = hashcode(mag_card_data, mc_bytes);
+ if (write_hash_to_file(user_id, temp_int)) current_state = ENROLL_SUCCESS;
+ else {
+ display_msg(&lcd, "AN ERROR OCCURED.");
+ wait(3);
+ current_state = STANDBY;
+ }
+ break;
+ }
+ case FACE_ENROLL: {
+ display_msg(&lcd, "PROCEED TO\nFACE ENROLLMENT.");
+ wait(2);
+ if (face_enroll(user_id, &android)) {
+ current_state = SPEECH_ENROLL;
+ } else {
+ display_msg(&lcd, "FACE ENROLLMENT\nFAILED.");
+ wait(3);
+ current_state = STANDBY;
+ }
+ break;
+ }
+ case SPEECH_ENROLL: {
+ display_msg(&lcd, "PROCEED TO\nSPEECH ENROLLMENT.");
+ wait(2);
+ if (speech_enroll(user_id, &android)) {
+ current_state = FP_ENROLL;
+ } else {
+ display_msg(&lcd, "SPEECH ENROLLMENT\nFAILED.");
+ wait(3);
+ current_state = STANDBY;
+ }
+ break;
+ }
+ //case VOICE_ENROLL: {
+// display_msg(&lcd, "PROCEED TO\nVOICE ENROLLMENT.");
+// wait(3);
+// if (voice_enroll(user_id, &android)) {
+// current_state = FP_ENROLL;
+// } else {
+// display_msg(&lcd, "VOICE ENROLLMENT\nFAILED.");
+// wait(3);
+// current_state = STANDBY;
+// }
+// break;
+// }
+ case ENROLL_SUCCESS: {
+ display_msg(&lcd, "ENROLLMENT\nSUCCESSFUL.");
+ wait(3);
+ current_state = STANDBY;
+ break;
+ }
+ case AUTH: {
+ if (read_pin(user_id, 4, &lcd, &pad) && is_valid_user(user_id)) {
+ current_state = FP_AUTH;
+ } else {
+ display_msg(&lcd, "INVALID PIN.");
+ wait(3);
+ current_state = STANDBY;
}
break;
-
- case MC_ENROLL:
- read_mag_card(&mag_card, mag_card_data, &mc_bytes);
- temp = hashcode(mag_card_data, mc_bytes);
- temp1 = id_to_int(user_id, 4);
- add_to_table(records, record_index++, temp1, temp);
- current_state = FACE_ENROLL;
+ }
+ case FP_AUTH: {
+ display_msg(&lcd, "PLACE YOUR FINGER\nON SCANNER.");
+ if (fp_auth(&fp)) {
+ current_state = MC_AUTH;
+ } else {
+ display_msg(&lcd, "FINGERPRINT FAILED.");
+ wait(3);
+ current_state = STANDBY;
+ }
+ break;
+ }
+ case MC_AUTH: {
+ flag = false;
+ while (!flag) {
+ memset(mag_card_data, 0, 250);
+ display_msg(&lcd, "SWIPE CARD NOW");
+ read_mag_card(&mag_card, mag_card_data, &mc_bytes);
+ if (prompt("SUCCESSFUL SWIPE?", &lcd, &pad)) flag = true;
+ }
+ temp_int = hashcode(mag_card_data, mc_bytes);
+ int saved_hash = 0;
+ if (read_hash_from_file(user_id, &saved_hash) && temp_int == saved_hash) {
+ current_state = FACE_AUTH;
+ } else {
+ display_msg(&lcd, "AUTH FAILED.");
+ wait(3);
+ current_state = STANDBY;
+ }
break;
-
- case FACE_ENROLL:
- if (face_enroll(user_id, &android)) {
- current_state = SPEECH_ENROLL;
- } else current_state = ENROLL_FAIL;
+ }
+ case FACE_AUTH: {
+ display_msg(&lcd, "PROCEED TO FACE\nAUTHENTICATION.");
+ wait(2);
+ if (face_auth(user_id, &android)) {
+ current_state = SPEECH_AUTH;
+ } else {
+ display_msg(&lcd, "FACE AUTH FAILED.");
+ wait(3);
+ current_state = STANDBY;
+ }
+ break;
+ }
+ case SPEECH_AUTH: {
+ display_msg(&lcd, "PROCEED TO SPEECH\nAUTHENTICATION.");
+ wait(2);
+ if (speech_auth(user_id, &android)) {
+ current_state = AUTH_SUCCESS;
+ } else {
+ display_msg(&lcd, "SPEECH AUTH FAILED.");
+ wait(3);
+ current_state = STANDBY;
+ }
break;
-
- case SPEECH_ENROLL:
- if (speech_enroll(user_id, &android)) {
- current_state = ENROLL_SUCCESS;
- } else current_state = ENROLL_FAIL;
- break;
-
- case ENROLL_SUCCESS:
- success_display(&lcd, "ENROLLMENT SUCCESSFUL.");
+ }
+ //case VOICE_AUTH: {
+// display_msg(&lcd, "PROCEED TO VOICE\nAUTHENTICATION.");
+// wait(2);
+// if (voice_auth(user_id, &android)) {
+// current_state = AUTH_SUCCESS;
+// } else {
+// display_msg(&lcd, "VOICE AUTH FAILED.");
+// wait(3);
+// current_state = STANDBY;
+// }
+// break;
+// }
+ case AUTH_SUCCESS: {
+ speaker = 0.5;
+ door_lock = 1;
+ display_msg(&lcd, "ACCESS GRANTED!");
+ wait(3);
+ door_lock = 0;
+ speaker = 0;
current_state = STANDBY;
- break;
+ memset(user_id, 0, 4);
+ }
}
}
}
