It is a door opener with mbed and Felica(RFID).
Dependencies: mbed Servo SDFileSystem
Revision 6:9fe8caff6142, committed 2012-05-15
- Comitter:
- ryought
- Date:
- Tue May 15 07:47:19 2012 +0000
- Parent:
- 5:4242d287f7f4
- Commit message:
- FINAL
Changed in this revision
diff -r 4242d287f7f4 -r 9fe8caff6142 RCS620S.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RCS620S.cpp Tue May 15 07:47:19 2012 +0000 @@ -0,0 +1,363 @@ +/* + * RC-S620/S sample library for Arduino + * + * Copyright 2010 Sony Corporation + * + * Rewrite for mbed + * + * modified by SWITCHSCIENCE + * + */ + +#include <stdio.h> +#include <string.h> +#include <stdint.h> +#include "RCS620S.h" +#include "mbed.h" + +extern Serial serial; + +/* -------------------------------- + * Constant + * -------------------------------- */ + +#define RCS620S_DEFAULT_TIMEOUT 1000 + +/* -------------------------------- + * Variable + * -------------------------------- */ + +/* -------------------------------- + * Prototype Declaration + * -------------------------------- */ + +/* -------------------------------- + * Macro + * -------------------------------- */ + +/* -------------------------------- + * Function + * -------------------------------- */ + +/* ------------------------ + * public + * ------------------------ */ + +RCS620S::RCS620S() +{ + this->timeout = RCS620S_DEFAULT_TIMEOUT; + +} + +int RCS620S::initDevice(void) +{ + int ret; + uint8_t response[RCS620S_MAX_RW_RESPONSE_LEN]; + uint16_t responseLen; + + /* RFConfiguration (various timings) */ + ret = rwCommand((const uint8_t*)"\xd4\x32\x02\x00\x00\x00", 6, + response, &responseLen); + if (!ret || (responseLen != 2) || + (memcmp(response, "\xd5\x33", 2) != 0)) { + return 0; + } + + /* RFConfiguration (max retries) */ + ret = rwCommand((const uint8_t*)"\xd4\x32\x05\x00\x00\x00", 6, + response, &responseLen); + if (!ret || (responseLen != 2) || + (memcmp(response, "\xd5\x33", 2) != 0)) { + return 0; + } + + /* RFConfiguration (additional wait time = 24ms) */ + ret = rwCommand((const uint8_t*)"\xd4\x32\x81\xb7", 4, + response, &responseLen); + if (!ret || (responseLen != 2) || + (memcmp(response, "\xd5\x33", 2) != 0)) { + return 0; + } + + return 1; +} + +int RCS620S::polling(uint16_t systemCode) +{ + int ret; + uint8_t buf[9]; + uint8_t response[RCS620S_MAX_RW_RESPONSE_LEN]; + uint16_t responseLen; + + /* InListPassiveTarget */ + memcpy(buf, "\xd4\x4a\x01\x01\x00\xff\xff\x00\x00", 9); + buf[5] = (uint8_t)((systemCode >> 8) & 0xff); + buf[6] = (uint8_t)((systemCode >> 0) & 0xff); + + ret = rwCommand(buf, 9, response, &responseLen); + if (!ret || (responseLen != 22) || + (memcmp(response, "\xd5\x4b\x01\x01\x12\x01", 6) != 0)) { + return 0; + } + + memcpy(this->idm, response + 6, 8); + memcpy(this->pmm, response + 14, 8); + + return 1; +} + +int RCS620S::cardCommand( + const uint8_t* command, + uint8_t commandLen, + uint8_t response[RCS620S_MAX_CARD_RESPONSE_LEN], + uint8_t* responseLen) +{ + int ret; + uint16_t commandTimeout; + uint8_t buf[RCS620S_MAX_RW_RESPONSE_LEN]; + uint16_t len; + + if (this->timeout >= (0x10000 / 2)) { + commandTimeout = 0xffff; + } else { + commandTimeout = (uint16_t)(this->timeout * 2); + } + + /* CommunicateThruEX */ + buf[0] = 0xd4; + buf[1] = 0xa0; + buf[2] = (uint8_t)((commandTimeout >> 0) & 0xff); + buf[3] = (uint8_t)((commandTimeout >> 8) & 0xff); + buf[4] = (uint8_t)(commandLen + 1); + memcpy(buf + 5, command, commandLen); + + ret = rwCommand(buf, 5 + commandLen, buf, &len); + if (!ret || (len < 4) || + (buf[0] != 0xd5) || (buf[1] != 0xa1) || (buf[2] != 0x00) || + (len != (3 + buf[3]))) { + return 0; + } + + *responseLen = (uint8_t)(buf[3] - 1); + memcpy(response, buf + 4, *responseLen); + + return 1; +} + +int RCS620S::rfOff(void) +{ + int ret; + uint8_t response[RCS620S_MAX_RW_RESPONSE_LEN]; + uint16_t responseLen; + + /* RFConfiguration (RF field) */ + ret = rwCommand((const uint8_t*)"\xd4\x32\x01\x00", 4, + response, &responseLen); + if (!ret || (responseLen != 2) || + (memcmp(response, "\xd5\x33", 2) != 0)) { + return 0; + } + + return 1; +} + +int RCS620S::push( + const uint8_t* data, + uint8_t dataLen) +{ + int ret; + uint8_t buf[RCS620S_MAX_CARD_RESPONSE_LEN]; + uint8_t responseLen; + + if (dataLen > 224) { + return 0; + } + + /* Push */ + buf[0] = 0xb0; + memcpy(buf + 1, this->idm, 8); + buf[9] = dataLen; + memcpy(buf + 10, data, dataLen); + + ret = cardCommand(buf, 10 + dataLen, buf, &responseLen); + if (!ret || (responseLen != 10) || (buf[0] != 0xb1) || + (memcmp(buf + 1, this->idm, 8) != 0) || (buf[9] != dataLen)) { + return 0; + } + + buf[0] = 0xa4; + memcpy(buf + 1, this->idm, 8); + buf[9] = 0x00; + + ret = cardCommand(buf, 10, buf, &responseLen); + if (!ret || (responseLen != 10) || (buf[0] != 0xa5) || + (memcmp(buf + 1, this->idm, 8) != 0) || (buf[9] != 0x00)) { + return 0; + } + + wait(1); + return 1; +} + +/* ------------------------ + * private + * ------------------------ */ + +int RCS620S::rwCommand( + const uint8_t* command, + uint16_t commandLen, + uint8_t response[RCS620S_MAX_RW_RESPONSE_LEN], + uint16_t* responseLen) +{ + int ret; + uint8_t buf[9]; + + flushSerial(); + + uint8_t dcs = calcDCS(command, commandLen); + + /* transmit the command */ + buf[0] = 0x00; + buf[1] = 0x00; + buf[2] = 0xff; + if (commandLen <= 255) { + /* normal frame */ + buf[3] = commandLen; + buf[4] = (uint8_t)-buf[3]; + writeSerial(buf, 5); + } else { + /* extended frame */ + buf[3] = 0xff; + buf[4] = 0xff; + buf[5] = (uint8_t)((commandLen >> 8) & 0xff); + buf[6] = (uint8_t)((commandLen >> 0) & 0xff); + buf[7] = (uint8_t)-(buf[5] + buf[6]); + writeSerial(buf, 8); + } + writeSerial(command, commandLen); + buf[0] = dcs; + buf[1] = 0x00; + writeSerial(buf, 2); + + /* receive an ACK */ + ret = readSerial(buf, 6); + if (!ret || (memcmp(buf, "\x00\x00\xff\x00\xff\x00", 6) != 0)) { + cancel(); + return 0; + } + + /* receive a response */ + ret = readSerial(buf, 5); + if (!ret) { + cancel(); + return 0; + } else if (memcmp(buf, "\x00\x00\xff", 3) != 0) { + return 0; + } + if ((buf[3] == 0xff) && (buf[4] == 0xff)) { + ret = readSerial(buf + 5, 3); + if (!ret || (((buf[5] + buf[6] + buf[7]) & 0xff) != 0)) { + return 0; + } + *responseLen = (((uint16_t)buf[5] << 8) | + ((uint16_t)buf[6] << 0)); + } else { + if (((buf[3] + buf[4]) & 0xff) != 0) { + return 0; + } + *responseLen = buf[3]; + } + if (*responseLen > RCS620S_MAX_RW_RESPONSE_LEN) { + return 0; + } + + ret = readSerial(response, *responseLen); + if (!ret) { + cancel(); + return 0; + } + + dcs = calcDCS(response, *responseLen); + + ret = readSerial(buf, 2); + if (!ret || (buf[0] != dcs) || (buf[1] != 0x00)) { + cancel(); + return 0; + } + + return 1; +} + +void RCS620S::cancel(void) +{ + /* transmit an ACK */ + writeSerial((const uint8_t*)"\x00\x00\xff\x00\xff\x00", 6); + wait(1); + flushSerial(); +} + +uint8_t RCS620S::calcDCS( + const uint8_t* data, + uint16_t len) +{ + uint8_t sum = 0; + + for (uint16_t i = 0; i < len; i++) { + sum += data[i]; + } + + return (uint8_t)-(sum & 0xff); +} + +void RCS620S::writeSerial( + const uint8_t* data, + uint16_t len) +{ +// Serial.write(data, len); + uint16_t nwrite = 0; + + while (nwrite < len) { + serial.putc( *(data + nwrite) ); + nwrite++; + } +} + + +int RCS620S::readSerial( + uint8_t* data, + uint16_t len) +{ + uint16_t nread = 0; + time_t t0 = time(NULL); + + while (nread < len) { + if ((checkTimeout(t0))) { + return 0; + } + + if (serial.readable() > 0) { + data[nread] = serial.getc(); + nread++; + } + } + + return 1; +} + + +void RCS620S::flushSerial(void) +{ + while( serial.readable() ); +} + + +int RCS620S::checkTimeout(time_t t0) +{ + time_t t = time(NULL); + + if ((t - t0) >= this->timeout) { + return 1; + } + + return 0; +} \ No newline at end of file
diff -r 4242d287f7f4 -r 9fe8caff6142 RCS620S.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RCS620S.h Tue May 15 07:47:19 2012 +0000 @@ -0,0 +1,74 @@ +/* + * RC-S620/S sample library for Arduino + * + * Copyright 2010 Sony Corporation + * + * Rewrite for mbed + * + * modified by SWITCHSCIENCE + * + */ + +#include "mbed.h" +#include <inttypes.h> + +#ifndef RCS620S_H_ +#define RCS620S_H_ + +/* -------------------------------- + * Constant + * -------------------------------- */ + +#define RCS620S_MAX_CARD_RESPONSE_LEN 254 +#define RCS620S_MAX_RW_RESPONSE_LEN 265 + +/* -------------------------------- + * Class Declaration + * -------------------------------- */ + +class RCS620S +{ +public: + RCS620S(); + + int initDevice(void); + int polling(uint16_t systemCode = 0xffff); + int cardCommand( + const uint8_t* command, + uint8_t commandLen, + uint8_t response[RCS620S_MAX_CARD_RESPONSE_LEN], + uint8_t* responseLen); + int rfOff(void); + + int push( + const uint8_t* data, + uint8_t dataLen); + +private: + int rwCommand( + const uint8_t* command, + uint16_t commandLen, + uint8_t response[RCS620S_MAX_RW_RESPONSE_LEN], + uint16_t* responseLen); + void cancel(void); + uint8_t calcDCS( + const uint8_t* data, + uint16_t len); + + void writeSerial( + const uint8_t* data, + uint16_t len); + int readSerial( + uint8_t* data, + uint16_t len); + void flushSerial(void); + + int checkTimeout(time_t t0); + +public: + time_t timeout; + uint8_t idm[8]; + uint8_t pmm[8]; +}; + +#endif /* !RCS620S_H_ */ \ No newline at end of file
diff -r 4242d287f7f4 -r 9fe8caff6142 TextLCD.lib --- a/TextLCD.lib Sun Dec 25 11:33:15 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/simon/code/TextLCD/#44f34c09bd37
diff -r 4242d287f7f4 -r 9fe8caff6142 TextLCD/TextLCD.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD/TextLCD.cpp Tue May 15 07:47:19 2012 +0000 @@ -0,0 +1,159 @@ +/* mbed TextLCD Library, for a 4-bit LCD based on HD44780 + * Copyright (c) 2007-2010, sford, http://mbed.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "TextLCD.h" +#include "mbed.h" + +TextLCD::TextLCD(PinName rs, PinName e, PinName d4, PinName d5, + PinName d6, PinName d7, LCDType type) : _rs(rs), + _e(e), _d(d4, d5, d6, d7), + _type(type) { + + _e = 1; + _rs = 0; // command mode + + wait(0.015); // Wait 15ms to ensure powered up + + // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) + for (int i=0; i<3; i++) { + writeByte(0x3); + wait(0.00164); // this command takes 1.64ms, so wait for it + } + writeByte(0x2); // 4-bit mode + wait(0.000040f); // most instructions take 40us + + writeCommand(0x28); // Function set 001 BW N F - - + writeCommand(0x0C); + writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes + cls(); +} + +void TextLCD::character(int column, int row, int c) { + int a = address(column, row); + writeCommand(a); + writeData(c); +} + +void TextLCD::cls() { + writeCommand(0x01); // cls, and set cursor to 0 + wait(0.00164f); // This command takes 1.64 ms + locate(0, 0); +} + +void TextLCD::locate(int column, int row) { + _column = column; + _row = row; +} + +int TextLCD::_putc(int value) { + if (value == '\n') { + _column = 0; + _row++; + if (_row >= rows()) { + _row = 0; + } + } else { + character(_column, _row, value); + _column++; + if (_column >= columns()) { + _column = 0; + _row++; + if (_row >= rows()) { + _row = 0; + } + } + } + return value; +} + +int TextLCD::_getc() { + return -1; +} + +void TextLCD::writeByte(int value) { + _d = value >> 4; + wait(0.000040f); // most instructions take 40us + _e = 0; + wait(0.000040f); + _e = 1; + _d = value >> 0; + wait(0.000040f); + _e = 0; + wait(0.000040f); // most instructions take 40us + _e = 1; +} + +void TextLCD::writeCommand(int command) { + _rs = 0; + writeByte(command); +} + +void TextLCD::writeData(int data) { + _rs = 1; + writeByte(data); +} + +int TextLCD::address(int column, int row) { + switch (_type) { + case LCD20x4: + switch (row) { + case 0: + return 0x80 + column; + case 1: + return 0xc0 + column; + case 2: + return 0x94 + column; + case 3: + return 0xd4 + column; + } + case LCD16x2B: + return 0x80 + (row * 40) + column; + case LCD16x2: + case LCD20x2: + default: + return 0x80 + (row * 0x40) + column; + } +} + +int TextLCD::columns() { + switch (_type) { + case LCD20x4: + case LCD20x2: + return 20; + case LCD16x2: + case LCD16x2B: + default: + return 16; + } +} + +int TextLCD::rows() { + switch (_type) { + case LCD20x4: + return 4; + case LCD16x2: + case LCD16x2B: + case LCD20x2: + default: + return 2; + } +}
diff -r 4242d287f7f4 -r 9fe8caff6142 TextLCD/TextLCD.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD/TextLCD.h Tue May 15 07:47:19 2012 +0000 @@ -0,0 +1,111 @@ +/* mbed TextLCD Library, for a 4-bit LCD based on HD44780 + * Copyright (c) 2007-2010, sford, http://mbed.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MBED_TEXTLCD_H +#define MBED_TEXTLCD_H + +#include "mbed.h" + +/** A TextLCD interface for driving 4-bit HD44780-based LCDs + * + * Currently supports 16x2, 20x2 and 20x4 panels + * + * @code + * #include "mbed.h" + * #include "TextLCD.h" + * + * TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d4-d7 + * + * int main() { + * lcd.printf("Hello World!\n"); + * } + * @endcode + */ +class TextLCD : public Stream { +public: + + /** LCD panel format */ + enum LCDType { + LCD16x2 /**< 16x2 LCD panel (default) */ + , LCD16x2B /**< 16x2 LCD panel alternate addressing */ + , LCD20x2 /**< 20x2 LCD panel */ + , LCD20x4 /**< 20x4 LCD panel */ + }; + + /** Create a TextLCD interface + * + * @param rs Instruction/data control line + * @param e Enable line (clock) + * @param d4-d7 Data lines for using as a 4-bit interface + * @param type Sets the panel size/addressing mode (default = LCD16x2) + */ + TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2); + +#if DOXYGEN_ONLY + /** Write a character to the LCD + * + * @param c The character to write to the display + */ + int putc(int c); + + /** Write a formated string to the LCD + * + * @param format A printf-style format string, followed by the + * variables to use in formating the string. + */ + int printf(const char* format, ...); +#endif + + /** Locate to a screen column and row + * + * @param column The horizontal position from the left, indexed from 0 + * @param row The vertical position from the top, indexed from 0 + */ + void locate(int column, int row); + + /** Clear the screen and locate to 0,0 */ + void cls(); + + int rows(); + int columns(); + +protected: + + // Stream implementation functions + virtual int _putc(int value); + virtual int _getc(); + + int address(int column, int row); + void character(int column, int row, int c); + void writeByte(int value); + void writeCommand(int command); + void writeData(int data); + + DigitalOut _rs, _e; + BusOut _d; + LCDType _type; + + int _column; + int _row; +}; + +#endif
diff -r 4242d287f7f4 -r 9fe8caff6142 main.cpp --- a/main.cpp Sun Dec 25 11:33:15 2011 +0000 +++ b/main.cpp Tue May 15 07:47:19 2012 +0000 @@ -2,23 +2,48 @@ #include "Servo.h" #include "TextLCD.h" #include "SDFileSystem.h" +#include "RCS620S.h" //library for felica RCS620S(switch science) +#include <inttypes.h> +#include <string.h> + +#define MAX_USER_NUMBER 40 TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7 Serial pc(USBTX, USBRX); -SDFileSystem sd(p5, p6, p7, p8, "sd"); +SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card reader module Servo myServo(p21); //port-21 = doorlock servo -//DigitalIn button1(p22); -InterruptIn int_p22(p22); + +InterruptIn int_p23(p23); //reg_button +InterruptIn int_doorbutton(p24); //door open/close analog control button +Serial serial(p9, p10); //Felica read and write module + +static char HEXSTRING[17] = "0123456789ABCDEF"; +char *itoh(uint8_t val, char *ptr); + +char userid[MAX_USER_NUMBER][18]; //userid read from SD +char regid[18]; //register id + +char *format = "%s"; +bool reg_button = false; +bool door_status; + +int already_count = 0; +//int try_count = 0; + +char *p_items; //used in loading userid from SDcard + +DigitalOut serialready(LED1); +DigitalOut felicarwready(LED2); +DigitalOut door_status_led(LED3); DigitalOut int_led(LED4); -FILE *fp = fopen("/sd/rdoor/userlist.txt", "r"); //user list(in sd) -FILE *fp2 = fopen("/sd/rdoor/log.txt", "a"); //logfile(in sd) +DigitalOut felicaready(p25); +DigitalOut auth_ok(p26); +DigitalOut auth_ng(p27); +DigitalOut regready(p28); +DigitalOut error_led(p29); -bool exit_button = false; - -//char servoStatus; - -class Watchdog { +class Watchdog { //watchdog timer public: void kick(float s) { LPC_WDT->WDCLKSEL = 0x1; @@ -37,115 +62,288 @@ Watchdog w; -void int_rise() { - int_led = !int_led; - exit_button = !exit_button; - /* - lcd.locate(15, 1); - lcd.printf("E"); */ + +//interrupt pin setup +void int_reg() { //registering button's interrupt + int_led = 1; + reg_button = 1; + regready = 1; +} + + +void logSetup(){ //log and userid setup and read again + FILE *fp = fopen("/sd/rdoor/userlist.txt", "r"); //user list(in sd) + FILE *fp2 = fopen("/sd/rdoor/log.txt", "a"); //logfile(in sd) + if (fp2 == NULL) error("could not read LOGFILES\n"); + if (fp == NULL) error("could not read USERLIST\n"); + + //load all userid saved in SDcard + for (int ii=0; ii < MAX_USER_NUMBER; ii++) { + p_items = fgets(userid[ii], 18, fp); //load userid(max.18 character) from fp + strtok(userid[ii], "\n\0"); // change \n to \0 method + if(p_items == NULL) break; + } + + fclose(fp); + fclose(fp2); + pc.printf("File OK\n"); } -void logSetup(){ - if (fp2 == NULL) { - lcd.cls(); - lcd.printf("couldnt read LOGFILE check SD!"); - error("could not read LOGFILES\n"); - } else { - fprintf(fp2, "[start] Power souce is turned on now.\n"); - } - - if (fp == NULL) { - lcd.cls(); - lcd.printf("could not read USERLIST!"); - fprintf(fp2, "[error] could not read USERLIST.TXT!!\n"); //errorlog out - error("could not read USERLIST\n"); - } else { - lcd.cls(); - lcd.printf("files was completely opened"); - fprintf(fp2, "[ok] USERLIST.TXT was loaded.\n"); - wait(1); - } - fprintf(fp2, "[ok] motor&SD is ready.\n"); - fprintf(fp2, "[ok] system is ready.\n"); - -} - - -void openDoor(){ +void openDoor(){ //opening door(move motor, refresh lcd, change door_status, led) lcd.cls(); lcd.printf("-MOTOR DRIVING-"); - fprintf(fp2, "[ok] motor driving started opening.\n"); - lcd.locate(0,1); //yoko,retsu - - for(float p=0; p<=1.0; p += 0.1) { - fprintf(fp2, "[ok] motor status: %f\n",p); - lcd.printf("*"); - myServo = p; - wait(0.2); - } + myServo = 0; lcd.cls(); lcd.printf("-DOOR:OPENED-"); - fprintf(fp2, "[ok] Door:Opened.\n"); + door_status = true; + door_status_led = 1; +} +void closeDoor(){ //closing door + lcd.cls(); + lcd.printf("-MOTOR DRIVING"); + myServo = 1.0; + lcd.cls(); + lcd.printf("-DOOR:CLOSED-"); + door_status = false; + door_status_led = 0; +} + +//interrupt pin setup +void int_doorctrl() { //doorcontrol button's interrupt + if (door_status == true) { + closeDoor(); + door_status = true; + + } else if (door_status == false) { + openDoor(); + door_status = false; + + } + door_status = !door_status; + wait(2); } - -void closeDoor(){ - lcd.cls(); - lcd.printf("-MOTOR DRIVING"); - fprintf(fp2, "[ok] motor driving started closeing.\n"); - lcd.locate(0,1); //yoko,retsu - lcd.printf("**********"); - lcd.locate(0,1); +int main() { + lcd.printf("Hello! \n RFID_doorlock\n"); + + + //first setup + logSetup(); //load all userid from SD(must : connect SD card) + closeDoor(); //at beginning, door_status is set up tp close(false) + + int_p23.rise(&int_reg); + int_doorbutton.rise(&int_doorctrl); //interrupt pin setup + - for(float p=1.0; p>=0; p -= 0.1) { - fprintf(fp2, "[ok] motor status: %f\n",p); - lcd.printf(" "); - myServo = p; - wait(0.2); - } - lcd.cls(); - lcd.printf("-DOOR:CLOSED-"); - fprintf(fp2, "[ok] Door:Closed.\n"); + //felica setup + int ret; + int loop; + char idbuffer[17]; + char *hexid; + + serial.baud(115200); + serialready = 1; -} + ret = 0; + RCS620S felica; + while (ret == 0) { + wait(1); + ret = felica.initDevice(); + } + felicarwready = 1; + pc.printf("Reader OK \n"); //Felica Reader loading finished - - -int main() { - //w.kick(10); - lcd.printf("Hello World\n RFID_doorlock"); - printf("hello world"); //for debug - wait(1); + + //print all loaded USERID first. + for (int x = 0; x <= MAX_USER_NUMBER; x++) { + pc.printf("-%s \n", userid[x]); + } + - logSetup(); - - //kokono aida ni felica - int_p22.rise(&int_rise); - + //main routine while(1) { - openDoor(); - wait(3); + pc.printf("Please FeliCa Touch\n"); + felicaready = 1; + + //reading id of touched Felica card + hexid = idbuffer + sizeof(idbuffer); // hexid = buffer[ last of elements ]; + *--hexid = '\0'; + while (felica.polling(0xffff) == 0); + felicarwready = 0; + pc.printf(">idm: "); + felicaready = 0; + for (loop = 7; loop >= 0; loop--){ + hexid = itoh(felica.idm[loop], hexid); + } + pc.printf("%s \n", hexid); //print touched id + + + /*cf. + hexid : touched Felica ID + regid : special ID used by registering method + userid[] : registered ID read from SDcard + */ + - closeDoor(); - wait(3); - if (exit_button == true) { - break; + //authentication + for (int i = 0; i <= MAX_USER_NUMBER; i++) { + if (strcmp(hexid, userid[i]) == 0) { //hexid = userid[i] + pc.printf(">Auth Succeeded\n"); + auth_ok = 1; + if (door_status == true) { //if door is opened now... close door + closeDoor(); + pc.printf(">close door by %s to %s(No.%d)\n", hexid, userid[i], i+1); + wait(1); + auth_ok = 0; + break; + } + if (door_status == false) { //if door is closed now... open door + openDoor(); + pc.printf(">open door by %s to %s(No.%d)\n", hexid, userid[i], i+1); + wait(1); + auth_ok = 0; + break; + } + } else { //hexid �� userid[i] + pc.printf(">Auth failed by %s to %s(No.%d)\n", hexid, userid[i], i+1); + + if (i == MAX_USER_NUMBER) { + pc.printf(">Auth Failed\n"); + auth_ng = 1; + wait(1); + auth_ng = 0; + } + } } + wait(1); //to avoid repeating auth of same card + + + //register new user's card + if (reg_button == true) { + pc.printf("--register mode--\n"); + + //read again USERLIST from SD + FILE *fp = fopen("/sd/rdoor/userlist.txt", "a"); + if (fp == NULL) error("could not read USERLIST\n"); + + //first, read and auth MASTER_ID + pc.printf("Please Master Felica Card Touch\n"); + felicaready = 1; + hexid = idbuffer + sizeof(idbuffer); + *--hexid = '\0'; + while (felica.polling(0xffff) == 0); + felicarwready = 0; + pc.printf(">idm: "); + felicaready = 0; + for (loop = 7; loop >= 0; loop--){ + hexid = itoh(felica.idm[loop], hexid); + } + pc.printf("%s (MASTER)\n", hexid); + wait(3); + + + //first id = master id... + if (strcmp(hexid, userid[0]) == 0) { + pc.printf(">Auth Succeeded\n"); + auth_ok = 1; + auth_ng = 0; + //next, read and register NEW_USER's_ID + pc.printf("Please Felica of New_User Touch\n"); + felicaready = 1; + + //Felica auth(hexid = NEW_USER's_ID) + hexid = idbuffer + sizeof(idbuffer); + *--hexid = '\0'; + while (felica.polling(0xffff) == 0); + felicarwready = 0; + pc.printf(">idm: "); + felicaready = 0; + for (loop = 7; loop >= 0; loop--){ + hexid = itoh(felica.idm[loop], hexid); + } + pc.printf("%s (NEW USER)\n", hexid); + + + //to avoid registering same ID + already_count = 0; //variable initialization + for (int i = 0; i <= MAX_USER_NUMBER; i++) { + if (strcmp(hexid, userid[i]) == 0) { + //NEW_USER's_ID == userid[i]... already_count++ + already_count = already_count + 1; + pc.printf(">%d(debug)\n", already_count); + } + } + + + //if already_count is over 0, the ID is ALREADY REGISTERED ID. + if (already_count == 0) { + //registering + //first, add "\n" to the end of NEW_USER'S_ID(hexid) (hexid + "\n" = regid) + sprintf(regid, "%s\n", hexid); + int ret = fputs(regid, fp); //writing to SD + if(ret == EOF) { + error("Userid writing error"); + auth_ng = 1; + auth_ok = 0; + wait(2); + } else { + pc.printf("Registering was completed\n"); + auth_ok = 1; + auth_ng = 0; + wait(2); + } + } else if(already_count > 0) { + pc.printf("Sorry, this is already registered ID\n"); + auth_ng = 1; + auth_ok = 0; + wait(2); + } else { + pc.printf("Error\n"); + auth_ng = 1; + auth_ok = 0; + wait(2); + } + + fclose(fp); + + + //read userid from SD and setup userid list again. + logSetup(); + //printing userid again. + for (int xx = 0; xx <= MAX_USER_NUMBER; xx++) { + pc.printf("-%s \n", userid[xx]); + } + + + //if registering NEW_USER's_ID was completed successfully... + // reg_button_flag : down + reg_button = false; + int_led = 0; + + } else { + //first id �� master id... restart. + pc.printf(">Auth Failed\n"); + pc.printf("Sorry, you aren't allowed to use register mode\n"); + auth_ok = 0; + auth_ng = 1; + wait(2); + + } + regready = 0; + } + auth_ok = 0; + auth_ng = 0; + felicaready = 0; } - - //closing files - int_led = 0; +} - fclose(fp); - fprintf(fp2, "[ok] USERLIST.TXT closed."); - fprintf(fp2, "[end] thank you.\n \n"); - fclose(fp2); - - lcd.cls(); - lcd.printf("Thank you! bye!"); - wait(3); - //w.kick(); -} \ No newline at end of file +//used by Felica +char *itoh(uint8_t val, char *ptr) { + *--ptr = HEXSTRING[(val % 16)]; // (val & 0x0f) + val /= 16; // val = val >> 4; + *--ptr = HEXSTRING[(val % 16)]; // (val & 0x0f) + return(ptr); +}