It is a door opener with mbed and Felica(RFID).
Dependencies: mbed Servo SDFileSystem
main.cpp@5:4242d287f7f4, 2011-12-25 (annotated)
- Committer:
- ryought
- Date:
- Sun Dec 25 11:33:15 2011 +0000
- Revision:
- 5:4242d287f7f4
- Parent:
- 4:f71760338b1e
- Child:
- 6:9fe8caff6142
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryought | 1:26e3869ba544 | 1 | #include "mbed.h" |
ryought | 1:26e3869ba544 | 2 | #include "Servo.h" |
ryought | 1:26e3869ba544 | 3 | #include "TextLCD.h" |
ryought | 1:26e3869ba544 | 4 | #include "SDFileSystem.h" |
ryought | 1:26e3869ba544 | 5 | |
ryought | 1:26e3869ba544 | 6 | TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7 |
ryought | 1:26e3869ba544 | 7 | Serial pc(USBTX, USBRX); |
ryought | 1:26e3869ba544 | 8 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
ryought | 4:f71760338b1e | 9 | Servo myServo(p21); //port-21 = doorlock servo |
ryought | 5:4242d287f7f4 | 10 | //DigitalIn button1(p22); |
ryought | 5:4242d287f7f4 | 11 | InterruptIn int_p22(p22); |
ryought | 5:4242d287f7f4 | 12 | DigitalOut int_led(LED4); |
ryought | 1:26e3869ba544 | 13 | |
ryought | 3:046e6f173f9e | 14 | FILE *fp = fopen("/sd/rdoor/userlist.txt", "r"); //user list(in sd) |
ryought | 3:046e6f173f9e | 15 | FILE *fp2 = fopen("/sd/rdoor/log.txt", "a"); //logfile(in sd) |
ryought | 3:046e6f173f9e | 16 | |
ryought | 5:4242d287f7f4 | 17 | bool exit_button = false; |
ryought | 5:4242d287f7f4 | 18 | |
ryought | 4:f71760338b1e | 19 | //char servoStatus; |
ryought | 4:f71760338b1e | 20 | |
ryought | 4:f71760338b1e | 21 | class Watchdog { |
ryought | 4:f71760338b1e | 22 | public: |
ryought | 4:f71760338b1e | 23 | void kick(float s) { |
ryought | 4:f71760338b1e | 24 | LPC_WDT->WDCLKSEL = 0x1; |
ryought | 4:f71760338b1e | 25 | uint32_t clk = SystemCoreClock / 16; |
ryought | 4:f71760338b1e | 26 | LPC_WDT->WDTC = s * (float)clk; |
ryought | 4:f71760338b1e | 27 | LPC_WDT->WDMOD = 0x3; |
ryought | 4:f71760338b1e | 28 | |
ryought | 4:f71760338b1e | 29 | kick(); |
ryought | 4:f71760338b1e | 30 | } |
ryought | 4:f71760338b1e | 31 | |
ryought | 4:f71760338b1e | 32 | void kick() { |
ryought | 4:f71760338b1e | 33 | LPC_WDT->WDFEED = 0xAA; |
ryought | 4:f71760338b1e | 34 | LPC_WDT->WDFEED = 0x55; |
ryought | 4:f71760338b1e | 35 | } |
ryought | 4:f71760338b1e | 36 | }; |
ryought | 4:f71760338b1e | 37 | |
ryought | 4:f71760338b1e | 38 | Watchdog w; |
ryought | 4:f71760338b1e | 39 | |
ryought | 5:4242d287f7f4 | 40 | void int_rise() { |
ryought | 5:4242d287f7f4 | 41 | int_led = !int_led; |
ryought | 5:4242d287f7f4 | 42 | exit_button = !exit_button; |
ryought | 5:4242d287f7f4 | 43 | /* |
ryought | 5:4242d287f7f4 | 44 | lcd.locate(15, 1); |
ryought | 5:4242d287f7f4 | 45 | lcd.printf("E"); */ |
ryought | 5:4242d287f7f4 | 46 | } |
ryought | 5:4242d287f7f4 | 47 | |
ryought | 5:4242d287f7f4 | 48 | |
ryought | 3:046e6f173f9e | 49 | void logSetup(){ |
ryought | 3:046e6f173f9e | 50 | if (fp2 == NULL) { |
ryought | 3:046e6f173f9e | 51 | lcd.cls(); |
ryought | 4:f71760338b1e | 52 | lcd.printf("couldnt read LOGFILE check SD!"); |
ryought | 3:046e6f173f9e | 53 | error("could not read LOGFILES\n"); |
ryought | 3:046e6f173f9e | 54 | } else { |
ryought | 3:046e6f173f9e | 55 | fprintf(fp2, "[start] Power souce is turned on now.\n"); |
ryought | 3:046e6f173f9e | 56 | } |
ryought | 3:046e6f173f9e | 57 | |
ryought | 3:046e6f173f9e | 58 | if (fp == NULL) { |
ryought | 3:046e6f173f9e | 59 | lcd.cls(); |
ryought | 3:046e6f173f9e | 60 | lcd.printf("could not read USERLIST!"); |
ryought | 3:046e6f173f9e | 61 | fprintf(fp2, "[error] could not read USERLIST.TXT!!\n"); //errorlog out |
ryought | 3:046e6f173f9e | 62 | error("could not read USERLIST\n"); |
ryought | 3:046e6f173f9e | 63 | } else { |
ryought | 3:046e6f173f9e | 64 | lcd.cls(); |
ryought | 3:046e6f173f9e | 65 | lcd.printf("files was completely opened"); |
ryought | 3:046e6f173f9e | 66 | fprintf(fp2, "[ok] USERLIST.TXT was loaded.\n"); |
ryought | 4:f71760338b1e | 67 | wait(1); |
ryought | 3:046e6f173f9e | 68 | } |
ryought | 4:f71760338b1e | 69 | fprintf(fp2, "[ok] motor&SD is ready.\n"); |
ryought | 4:f71760338b1e | 70 | fprintf(fp2, "[ok] system is ready.\n"); |
ryought | 3:046e6f173f9e | 71 | |
ryought | 1:26e3869ba544 | 72 | } |
ryought | 1:26e3869ba544 | 73 | |
ryought | 3:046e6f173f9e | 74 | |
ryought | 4:f71760338b1e | 75 | void openDoor(){ |
ryought | 4:f71760338b1e | 76 | lcd.cls(); |
ryought | 4:f71760338b1e | 77 | lcd.printf("-MOTOR DRIVING-"); |
ryought | 4:f71760338b1e | 78 | fprintf(fp2, "[ok] motor driving started opening.\n"); |
ryought | 4:f71760338b1e | 79 | lcd.locate(0,1); //yoko,retsu |
ryought | 4:f71760338b1e | 80 | |
ryought | 4:f71760338b1e | 81 | for(float p=0; p<=1.0; p += 0.1) { |
ryought | 4:f71760338b1e | 82 | fprintf(fp2, "[ok] motor status: %f\n",p); |
ryought | 4:f71760338b1e | 83 | lcd.printf("*"); |
ryought | 4:f71760338b1e | 84 | myServo = p; |
ryought | 4:f71760338b1e | 85 | wait(0.2); |
ryought | 4:f71760338b1e | 86 | } |
ryought | 4:f71760338b1e | 87 | lcd.cls(); |
ryought | 4:f71760338b1e | 88 | lcd.printf("-DOOR:OPENED-"); |
ryought | 4:f71760338b1e | 89 | fprintf(fp2, "[ok] Door:Opened.\n"); |
ryought | 4:f71760338b1e | 90 | } |
ryought | 4:f71760338b1e | 91 | |
ryought | 4:f71760338b1e | 92 | |
ryought | 4:f71760338b1e | 93 | |
ryought | 4:f71760338b1e | 94 | void closeDoor(){ |
ryought | 4:f71760338b1e | 95 | lcd.cls(); |
ryought | 4:f71760338b1e | 96 | lcd.printf("-MOTOR DRIVING"); |
ryought | 4:f71760338b1e | 97 | fprintf(fp2, "[ok] motor driving started closeing.\n"); |
ryought | 4:f71760338b1e | 98 | lcd.locate(0,1); //yoko,retsu |
ryought | 4:f71760338b1e | 99 | lcd.printf("**********"); |
ryought | 4:f71760338b1e | 100 | lcd.locate(0,1); |
ryought | 4:f71760338b1e | 101 | |
ryought | 4:f71760338b1e | 102 | for(float p=1.0; p>=0; p -= 0.1) { |
ryought | 4:f71760338b1e | 103 | fprintf(fp2, "[ok] motor status: %f\n",p); |
ryought | 4:f71760338b1e | 104 | lcd.printf(" "); |
ryought | 4:f71760338b1e | 105 | myServo = p; |
ryought | 4:f71760338b1e | 106 | wait(0.2); |
ryought | 4:f71760338b1e | 107 | } |
ryought | 4:f71760338b1e | 108 | lcd.cls(); |
ryought | 4:f71760338b1e | 109 | lcd.printf("-DOOR:CLOSED-"); |
ryought | 4:f71760338b1e | 110 | fprintf(fp2, "[ok] Door:Closed.\n"); |
ryought | 4:f71760338b1e | 111 | |
ryought | 4:f71760338b1e | 112 | } |
ryought | 4:f71760338b1e | 113 | |
ryought | 4:f71760338b1e | 114 | |
ryought | 4:f71760338b1e | 115 | |
ryought | 1:26e3869ba544 | 116 | int main() { |
ryought | 5:4242d287f7f4 | 117 | //w.kick(10); |
ryought | 1:26e3869ba544 | 118 | lcd.printf("Hello World\n RFID_doorlock"); |
ryought | 3:046e6f173f9e | 119 | printf("hello world"); //for debug |
ryought | 4:f71760338b1e | 120 | wait(1); |
ryought | 1:26e3869ba544 | 121 | |
ryought | 5:4242d287f7f4 | 122 | |
ryought | 3:046e6f173f9e | 123 | logSetup(); |
ryought | 4:f71760338b1e | 124 | |
ryought | 4:f71760338b1e | 125 | //kokono aida ni felica |
ryought | 5:4242d287f7f4 | 126 | int_p22.rise(&int_rise); |
ryought | 4:f71760338b1e | 127 | |
ryought | 5:4242d287f7f4 | 128 | while(1) { |
ryought | 5:4242d287f7f4 | 129 | openDoor(); |
ryought | 5:4242d287f7f4 | 130 | wait(3); |
ryought | 5:4242d287f7f4 | 131 | |
ryought | 5:4242d287f7f4 | 132 | closeDoor(); |
ryought | 5:4242d287f7f4 | 133 | wait(3); |
ryought | 5:4242d287f7f4 | 134 | if (exit_button == true) { |
ryought | 5:4242d287f7f4 | 135 | break; |
ryought | 5:4242d287f7f4 | 136 | } |
ryought | 5:4242d287f7f4 | 137 | } |
ryought | 3:046e6f173f9e | 138 | |
ryought | 3:046e6f173f9e | 139 | //closing files |
ryought | 5:4242d287f7f4 | 140 | int_led = 0; |
ryought | 5:4242d287f7f4 | 141 | |
ryought | 5:4242d287f7f4 | 142 | fclose(fp); |
ryought | 5:4242d287f7f4 | 143 | fprintf(fp2, "[ok] USERLIST.TXT closed."); |
ryought | 3:046e6f173f9e | 144 | fprintf(fp2, "[end] thank you.\n \n"); |
ryought | 1:26e3869ba544 | 145 | fclose(fp2); |
ryought | 5:4242d287f7f4 | 146 | |
ryought | 1:26e3869ba544 | 147 | lcd.cls(); |
ryought | 5:4242d287f7f4 | 148 | lcd.printf("Thank you! bye!"); |
ryought | 5:4242d287f7f4 | 149 | wait(3); |
ryought | 5:4242d287f7f4 | 150 | //w.kick(); |
ryought | 1:26e3869ba544 | 151 | } |