It is a door opener with mbed and Felica(RFID).

Dependencies:   mbed Servo SDFileSystem

main.cpp

Committer:
ryought
Date:
2011-12-25
Revision:
5:4242d287f7f4
Parent:
4:f71760338b1e
Child:
6:9fe8caff6142

File content as of revision 5:4242d287f7f4:

#include "mbed.h"
#include "Servo.h"
#include "TextLCD.h"
#include "SDFileSystem.h"

TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
Serial pc(USBTX, USBRX);
SDFileSystem sd(p5, p6, p7, p8, "sd");
Servo myServo(p21); //port-21 = doorlock servo
//DigitalIn button1(p22);
InterruptIn int_p22(p22);
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)

bool exit_button = false;

//char servoStatus;

class Watchdog {
public:
    void kick(float s) {
        LPC_WDT->WDCLKSEL = 0x1;
        uint32_t clk = SystemCoreClock / 16;
        LPC_WDT->WDTC = s * (float)clk;
        LPC_WDT->WDMOD = 0x3;
        
        kick();
    }
    
    void kick() {
        LPC_WDT->WDFEED = 0xAA;
        LPC_WDT->WDFEED = 0x55;
    }
};

Watchdog w;

void int_rise() {
    int_led = !int_led;
    exit_button = !exit_button;
    /*
    lcd.locate(15, 1);
    lcd.printf("E"); */
}


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(){
    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);
    }
    lcd.cls();
    lcd.printf("-DOOR:OPENED-");
    fprintf(fp2, "[ok] Door:Opened.\n");
}



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);
    
    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");
    
}
    


int main() {
    //w.kick(10);
    lcd.printf("Hello World\n RFID_doorlock");
    printf("hello world"); //for debug
    wait(1);
    
    
    logSetup();
    
    //kokono aida ni felica
    int_p22.rise(&int_rise);
    
    while(1) {
        openDoor();
        wait(3);
        
        closeDoor();
        wait(3);
        if (exit_button == true) {
            break;
        }
    }

    //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(); 
}