get an input from RFID to control the LCD

Dependencies:   mbed

Fork of LCD4884 by Dan Ghiciulescu

main.cpp

Committer:
Pinmanee
Date:
2017-12-10
Revision:
1:baf91b6482eb
Child:
2:fb07a736e132

File content as of revision 1:baf91b6482eb:

#include "mbed.h"
#include "LCD4884.h"
#include "MFRC522.h"

#define MF_RESET    D8
#define SPI_CS      D10

int ReadRFID();

//LCD
AnalogIn analog_button_select(A0);
AnalogIn analog_button_press(A1);
AnalogIn analog_button_alarm(A2);
LCD4884 p;
//Serial pc(D1 , D0);

//RFID
DigitalOut LedGreen(LED1);
Serial pc(SERIAL_TX, SERIAL_RX);
MFRC522    RfChip   (PC_12, PC_11, PC_10, SPI_CS, MF_RESET);


int main(void)
{
    p.LCD_init();
    p.LCD_write_string(28,2,"Hello",MENU_NORMAL);
    uint8_t state = 0;
    double select, press, alarm;
    int line, rfid_ID;

    while(1) {

        select = analog_button_select.read();
        press = analog_button_press.read();

        printf("x = %f\n",select);

        if (state == 0) {                                                       // Home Page
            p.backlight(0);
            p.LCD_write_string(28,2,"Hello",MENU_NORMAL);
            printf("I am in home page\n");
            alarm = analog_button_alarm.read();
            rfid_ID = ReadRFID();

            if (alarm == 0) {                                                   // Alarm Page
                printf("Alarm detected");
                p.LCD_clear();
                state = 2;
            } else {
                if (rfid_ID == 0) {
                    printf("Admin detected");
                    p.LCD_clear();
                    state = 1;
                }
            }

        }

        else if (state == 1) {                                                  //Admin Page
            p.backlight(1);
            p.LCD_write_string(25,0,"Admin",MENU_HIGHLIGHT);
            p.LCD_write_string(0,1,"- Motion Graph",MENU_NORMAL);
            p.LCD_write_string(0,2,"- AC Graph",MENU_NORMAL);
            p.LCD_write_string(0,3,"- LDR Graph",MENU_NORMAL);
            p.LCD_write_string(0,4,"Back",MENU_NORMAL);


            while(1) {
                select = analog_button_select.read();
                press = analog_button_press.read();

                if (select != 0 && press != 0) {                          // no press
                    if (line == 1) {
                        p.LCD_write_string(25,0,"Admin",MENU_NORMAL);
                        p.LCD_write_string(0,1,"- Motion Graph",MENU_HIGHLIGHT);    //line = 0
                        p.LCD_write_string(0,2,"- AC Graph",MENU_NORMAL);
                        p.LCD_write_string(0,3,"- LDR Graph",MENU_NORMAL);
                        p.LCD_write_string(0,4,"Back",MENU_NORMAL);
                    }

                    else if (line == 2) {
                        p.LCD_write_string(25,0,"Admin",MENU_NORMAL);
                        p.LCD_write_string(0,1,"- Motion Graph",MENU_NORMAL);
                        p.LCD_write_string(0,2,"- AC Graph",MENU_HIGHLIGHT);        //line = 1
                        p.LCD_write_string(0,3,"- LDR Graph",MENU_NORMAL);
                        p.LCD_write_string(0,4,"Back",MENU_NORMAL);
                    }

                    else if (line == 3) {
                        p.LCD_write_string(25,0,"Admin",MENU_NORMAL);
                        p.LCD_write_string(0,1,"- Motion Graph",MENU_NORMAL);
                        p.LCD_write_string(0,2,"- AC Graph",MENU_NORMAL);
                        p.LCD_write_string(0,3,"- LDR Graph",MENU_HIGHLIGHT);       //line = 2
                        p.LCD_write_string(0,4,"Back",MENU_NORMAL);
                    }

                    else if (line == 4) {
                        p.LCD_write_string(25,0,"Admin",MENU_NORMAL);
                        p.LCD_write_string(0,1,"- Motion Graph",MENU_NORMAL);
                        p.LCD_write_string(0,2,"- AC Graph",MENU_NORMAL);
                        p.LCD_write_string(0,3,"- LDR Graph",MENU_NORMAL);
                        p.LCD_write_string(0,4,"Back",MENU_HIGHLIGHT);              //line = 3
                    }
                }

                else if (select == 0) {      // channel selecting button (down)
                    if (line < 4) {
                        //printf("line = %d\n",line);
                        line = line + 1;
                        wait_ms(250);
                    } else line = 0;
                }

                else if (press == 0) {      // press button
                    //printf("i am in\n");
                    //printf("line = %d\n",line);

                    if(line == 0) {
                        state = 1;
                        line = 0;
                        break;
                    } else if(line == 1) {
                        state = 10;
                        break;
                    } else if(line == 2) {
                        state = 11;
                        break;
                    } else if(line == 3) {
                        state = 12;
                        break;
                    } else {
                        p.LCD_clear();
                        state = 0;
                        line = 0;
                        break;
                    }
                }

            }
        }

        else if (state == 10) {                                                //Admin Page - Motion Graph
            p.LCD_clear();
            p.LCD_write_string(7,0,"Motion Graph",MENU_HIGHLIGHT);
            wait_ms(250);

            while(1) {
                press = analog_button_press.read();

                if (press == 0) {
                    printf("press\n");
                    p.LCD_clear();
                    state = 1;
                    line = 0;
                    break;
                }
            }
        }

        else if (state == 11) {                                                 //Admin Page - Air Conditioner Graph
            p.LCD_clear();
            p.LCD_write_string(20,0,"AC Graph",MENU_HIGHLIGHT);
            wait_ms(250);

            while(1) {
                press = analog_button_press.read();

                if (press == 0) {
                    printf("press\n");
                    p.LCD_clear();
                    state = 1;
                    line = 0;
                    break;
                }
            }
        }

        else if (state == 12) {                                                 //Admin Page - LDR Graph
            p.LCD_clear();
            p.LCD_write_string(17,0,"LDR Graph",MENU_HIGHLIGHT);
            wait_ms(250);

            while(1) {
                press = analog_button_press.read();

                if (press == 0) {
                    printf("press\n");
                    p.LCD_clear();
                    state = 1;
                    line = 0;
                    break;
                }
            }
        }

        else if (state == 2) {
            p.LCD_clear();
            while(1) {
                p.backlight(0);
                wait_ms(500);
                p.backlight(1);
                wait_ms(500);
                p.LCD_write_string(25,1,"PEASE",MENU_NORMAL);
                p.LCD_write_string(10,3,"TAP ID CARD",MENU_NORMAL);
                wait_ms(250);
            }
        }

    }
}

int ReadRFID()
{
    int data[10];
    int card;
    pc.printf("starting...\n");

    // Init. RC522 Chip
    RfChip.PCD_Init();

    while(true) {
        LedGreen != 0;

        // Look for new cards
        if(data[0] == 199 && data[1] == 230 &&data[2] == 46 &&data[3] == 0 ) {
            //pc.printf(" admin \n");
            data[0] = 0;
            data[1] = 0;
            data[2] = 0;
            data[3] = 0;
            card = 0;
            return card;
        }
        if ( ! RfChip.PICC_IsNewCardPresent()) {
            wait_ms(500);
            continue;
        }

        // Select one of the cards
        if ( ! RfChip.PICC_ReadCardSerial()) {
            wait_ms(500);
            continue;
        }

        LedGreen = 0;

        // Print Card UID
        pc.printf("Card UID: ");
        for (uint8_t i = 0; i < RfChip.uid.size; i++) {
            data[i] =  RfChip.uid.uidByte[i];
            pc.printf("%d ", data[i]);

        }

        //pc.printf("\n\r");
        //pc.printf("\n\r");
        //char data[0];
        //pc.printf("  %X02", data[0]);
        //pc.printf("  %s\n", data[0]);

        pc.printf("\n\r");
    }
}