Full layout 'HUB' with functioning navigation between all screens and active temp/state display, now with outbound signals to leds and buzzer. Final Project.

Dependencies:   ID12RFID SPI_TFT_ILI9341 TFT_fonts mbed

Fork of Welcome_Screen_TFT_2_point_oh by 'SmOuse'

main.cpp

Committer:
bendavis96
Date:
2016-06-17
Revision:
9:0adb037a25a4
Parent:
8:95cd847648cd

File content as of revision 9:0adb037a25a4:


//%%%%%% SERVER HUB %%%%%%%



//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%  Initialisations  %%%%%%%%
#include "stdio.h"
#include "mbed.h"
#include "SPI_TFT_ILI9341.h"
#include "ID12RFID.h"
#include "string"
#include "Arial12x12.h"
#include "Arial24x23.h"
#include "Arial28x28.h"
#include "font_big.h"


Serial pc (USBTX,USBRX);
ID12RFID rfid(PTE23);
Serial dev(PTE0,PTE1);  // for KL25Z... asuming one can't use the PTA1 version which is the stdio
DigitalOut rled(LED1);
DigitalOut gled(LED2);
DigitalOut bled(LED3);
DigitalOut rst(PTD7); // single digital pin to drive the esp8266 reset line

int touching = 0;




// the display has a backlight switch on board
DigitalOut LCD_LED(PTA13);   // LED Display
DigitalOut pwr(PTD7); // Power

// the TFT is connected to SPI pin 5-7
//SPI_TFT_ILI9341 TFT(p5, p6, p7, p8, p9, p10,"TFT"); // mosi, miso, sclk, cs, reset, dc for lpc1768
SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTD5, PTD0, PTA13,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
//NB better combination to use a coherent 2x4 block for lcd
//   SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTA16, PTA17, PTC16,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%% WIFI %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// subroutine to run anytime a serial interrupt arrives from the device
// this basically passes everything thatthe device produces on to the pc terminal screen

void dev_recv()
{
    while(dev.readable()) {
        pc.putc(dev.getc());
        wait_us(1);
    }
}
// subroutine to service the serial interrupt on the pc connection
// this is a bit more complex - it takes what the use sends on the pc and copies it on to the device
// the esp should echo these straight back to the the pc if all is well
// this also detects the end of command character which is ascii 13 (0x0d) adn adds a linefeed after it =asscii 10 (0x0a)
void pc_recv()
{
    char c;
    while(pc.readable()) {
        c = pc.getc();
        dev.putc(c);
        //  pc.putc(c); // echo back
        if(c==13) {
            dev.putc(10); // send the linefeed to complement the carriage return generated by return key on the pc
            pc.putc(10);
        }
    }
}

/*void client() {
    
     i = dev.getc(); */
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%  Subroutine to read the x location of the touch point  %%%%%%%%%%



// need to set x+ to 3V and ground x- then read analogue voltage on ym
//nb need to add a check for actual touch as opposed to random crap

int readX()
{
    int delta=0,xv1=0,xv2=0,k=0;

    AnalogIn yp(PTB3);
    AnalogIn ym(PTB2);
    DigitalOut xp(PTB0);
    DigitalOut xm(PTB1);

    xp=1; // set positive sdie of x high
    xm=0;
    // dont need to do anyhting to set low side as it should be fine.
    // but do need to disconnect yp
    //yp.PinMode(PullNone)
    for(k=0; k<10; k++) { // make 10 readings to average
        xv1+=(int)ym.read_u16();  // get value
        xv2+=(int)yp.read_u16(); // get other value
    }
    delta=abs(xv2-xv1)/10;
    if(delta<300) touching=1;
    else touching=0;
// pc.printf("delta=%d \t %d\n\r",delta,touching);
    xp=0;
    xm=0;
    return(xv2/10); //maybe better to return the average of both....
}
// subroutine to read y values - has different pin functions ..
int readY()
{
    DigitalOut yp(PTB3);
    DigitalOut ym(PTB2);
    AnalogIn xp(PTB0);
    AnalogIn xm(PTB1);

    yp=1; // set positive sdie of x high
    ym=0;
    // dont need to do anyhting to set low side as it should be fine.
    // but do need to disconnect yp
    //yp.PinMode(PullNone)
    int yval=(int)xm.read_u16();  // get value

    yp=0;
    ym=0;
    return(yval);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%  Home Display  %%%%%%%

void start()
{

    pwr=1;
    wait(0.2);

    LCD_LED = 1;                // backlight on
    rled = 1;
    bled = 1;
    gled = 1;

    TFT.claim(stdout);          // send stdout to the TFT display
    TFT.set_orientation(1);
    TFT.background(Black);      // set background to black
    TFT.foreground(Cyan);      // set chars to white
    TFT.locate(80,110);
    TFT.set_font((unsigned char*) Neu42x35);
    TFT.cls();                  // clear the screen
    TFT.printf("SmOuse");

}

void welcome()
{
    //Greeting
    TFT.set_font((unsigned char*) Arial24x23);
    TFT.set_orientation(1);
    TFT.locate(50,100);
    TFT.printf("W");
    wait(0.05);
    TFT.printf("e");
    wait(0.05);
    TFT.printf("l");
    wait(0.05);
    TFT.printf("c");
    wait(0.05);
    TFT.printf("o");
    wait(0.05);
    TFT.printf("m");
    wait(0.05);
    TFT.printf("e");
    wait(0.05);
    TFT.printf(",");
    wait(0.05);
    TFT.printf(" ");
    wait(0.05);
}


void home()
{
    TFT.cls();
    //show corners
    TFT.circle(10,10,10,0xffff);
    wait(0.1);
    TFT.circle(308,10,10,0xffff);
    wait(0.1);
    TFT.circle(10,229,10,0xffff);
    wait(0.1);
    TFT.circle(308,229,10,0xffff);
    wait(0.1);
    //add menu buttons
    TFT.rect(25,200,155,140,0xffff); //lighting
    TFT.rect(165,200,295,140,0xffff); //AC
    TFT.rect(25,25,93,0,Red); //auto controls

    //text for buttons
    TFT.set_font((unsigned char*) Arial12x12);
    TFT.locate(65,165);
    TFT.printf("Lighting");
    TFT.locate(195,165);
    TFT.printf("AC Control");
    TFT.locate(40,10);
    TFT.printf("AUTO");
}

void lights()
{

    TFT.cls();
    TFT.locate(55,15);
    TFT.set_font((unsigned char*) Arial24x23);
    TFT.printf("Lighting System");
    //show corners
    TFT.circle(10,10,10,0xffff);
    wait(0.1);
    TFT.circle(308,10,10,0xffff);
    wait(0.1);
    TFT.circle(10,229,10,0xffff);
    wait(0.1);
    TFT.circle(308,229,10,0xffff);
    wait(0.1);
    //add menu buttons
    TFT.rect(25,200,155,140,0xffff);
    TFT.rect(165,110,295,80,0xffff);
    TFT.rect(165,155,295,125,0xffff);
    TFT.rect(165,200,295,170,0xffff);
    //text for buttons
    TFT.set_font((unsigned char*) Arial12x12);
    TFT.locate(50,165);
    TFT.printf("LIGHTS OFF");
    TFT.locate(230,92);
    TFT.printf("3");
    TFT.locate(230,137);
    TFT.printf("2");
    TFT.locate(230,182);
    TFT.printf("1");
    TFT.locate(6,6);
    TFT.printf("X");
}

void temps()
{

    TFT.cls();
    TFT.locate(90,15);
    TFT.set_font((unsigned char*) Arial24x23);
    TFT.printf("AC System");
    //show corners
    TFT.circle(10,10,10,0xffff);
    wait(0.1);
    TFT.circle(308,10,10,0xffff);
    wait(0.1);
    TFT.circle(10,229,10,0xffff);
    wait(0.1);
    TFT.circle(308,229,10,0xffff);
    wait(0.1);
    //add menu buttons
    TFT.rect(25,200,155,140,0xffff);
    TFT.rect(165,200,295,140,0xffff);
    //text for buttons
    TFT.set_font((unsigned char*) Arial12x12);
    TFT.locate(60,165);
    TFT.printf("FAN OFF");
    TFT.locate(200,165);
    TFT.printf("FAN ON");
    TFT.locate(6,6);
    TFT.printf("X");
}

void autos()
{

    TFT.cls();
    TFT.locate(30,15);
    TFT.set_font((unsigned char*) Arial24x23);
    TFT.printf("Automatic Control");
    //show corners
    TFT.circle(10,10,10,0xffff);
    wait(0.1);
    TFT.circle(308,10,10,0xffff);
    wait(0.1);
    TFT.circle(10,229,10,0xffff);
    wait(0.1);
    TFT.circle(308,229,10,0xffff);
    wait(0.1);
    //add menu buttons
    TFT.rect(25,200,155,140,0xffff);
    TFT.rect(165,200,295,140,0xffff);
    //text for buttons
    TFT.set_font((unsigned char*) Arial12x12);
    TFT.locate(75,165);
    TFT.printf("OFF");
    TFT.locate(215,165);
    TFT.printf("ON");
    TFT.locate(6,6);
    TFT.printf("X");
}

//%%%%%%%%% OUTPUTS %%%%%%%%%%%%%%%

void alarm_off()
{
    dev.printf("AT+CIPSEND=0,1\r\n");
    wait(0.1);
    dev.printf("x");
    wait(0.1);
}

void control_start(){
    dev.printf("AT+CIPSEND=1,1\r\n");
    wait(0.1);
    dev.printf("G");
    wait(0.1);
}

void lights_off()
{
    dev.printf("AT+CIPSEND=1,1\r\n");
    wait(0.1);
    dev.printf("Z");
    wait(0.1);
}

void lights_1()
{
    dev.printf("AT+CIPSEND=1,1\r\n");
    wait(0.1);
    dev.printf("X");
    wait(0.1);
}

void lights_2()
{
    dev.printf("AT+CIPSEND=1,1\r\n");
    wait(0.1);
    dev.printf("Y");
    wait(0.1);
}

void lights_3()
{
    dev.printf("AT+CIPSEND=1,1\r\n");
    wait(0.1);
    dev.printf("W");
    wait(0.1);
}

void fan_off()
{
    dev.printf("AT+CIPSEND=1,1\r\n");
    wait(0.1);
    dev.printf("L");
    wait(0.1);
}

void fan_on()
{
    dev.printf("AT+CIPSEND=1,1\r\n");
    wait(0.1);
    dev.printf("K");
    wait(0.1);
}

void autom_togg()
{
    dev.printf("AT+CIPSEND=1,1\r\n");
    wait(0.1);
    dev.printf("Q");
    wait(0.1);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%  IDs  %%%%%%

void ben()
{

    rled = 0;
    bled = 1;
    gled = 1;

    alarm_off();
    control_start();
    TFT.printf("B");
    wait(0.05);
    TFT.printf("e");
    wait(0.05);
    TFT.printf("n");

}

void joe()
{
    rled = 1;
    bled = 0;
    gled = 1;

    alarm_off();
    control_start();
    TFT.printf("J");
    wait(0.05);
    TFT.printf("o");
    wait(0.05);
    TFT.printf("e");

}

void arthur()
{
    rled = 1;
    bled = 0;
    gled = 1;

    alarm_off();
    control_start();
    TFT.printf("A");
    wait(0.05);
    TFT.printf("r");
    wait(0.05);
    TFT.printf("t");
    wait(0.05);
    TFT.printf("h");
    wait(0.05);
    TFT.printf("u");
    wait(0.05);
    TFT.printf("r");

}

void intruder()
{
    rled = 0;
    bled = 0;
    gled = 0;
    TFT.cls();                // clear the screen
    TFT.set_font((unsigned char*) Neu42x35);
    TFT.locate(45,105);
    TFT.foreground(Red);
    TFT.printf("INTRUDER!");
    wait(0.4);
    TFT.cls();
    wait(0.2);
    TFT.locate(45,105);
    TFT.printf("INTRUDER!");
    wait(0.4);
    TFT.cls();
    wait(0.2);
    TFT.locate(45,105);
    TFT.printf("INTRUDER!");
    wait(0.4);
    TFT.cls();
    wait(0.2);
    TFT.locate(45,105);
    TFT.printf("INTRUDER!");
    wait(0.4);
    TFT.cls();
    wait(0.2);
    TFT.locate(45,105);
    TFT.foreground(White);
    TFT.cls();
    rled = 1;
    bled = 1;
    gled = 1;
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




int main()
{
//%%%%%%%%%%%%%%%% WIFI %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    pc.printf("start");
    pc.baud(115200);
    dev.baud(115200);
    rst = 0;
    wait(1);
    rst = 1; // send the esp8266 reset
    wait(1);

    rled = 0, gled = 1;         //Red LED indicates startup in progress

    pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services
    dev.attach(&dev_recv, Serial::RxIrq);
    dev.printf("AT+RST\r\n");
    wait(10);
    dev.printf("AT+CWJAP=\"BTHub3-WXWX\",\"fdd6f7c682\"\r\n");
    wait(10);
    dev.printf("AT+CIPMUX=1\r\n");
    wait(2);
    dev.printf("AT+CIPSERVER=1,333\r\n");
    wait(2);
    dev.printf("AT+CIPSTO=3000\r\n");

    rled = 1, gled = 0;     //Green LED indicates startup complete

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    int display = 0;
    int reading = 0;
    int id = 0;

    pc.baud(115200);

    int xpos=0,ypos=0,xp=0,yp=0;

    while (1) {

        pc.printf("%d",id);

        switch (display) {

            case 0 : //start screen

                start();

                while (display == 0) {

                    reading = rfid.readable();
                    if (reading) {
                        display = 1;    //FOR TESTING LIGHTING, SHOULD BE 1
                        id = rfid.read();
                    }

                }
                break;

            case 1 : //welcome screen

                home();

                //Personalised
                switch (id) {

                    case 22311780 : //Ben
                        welcome();
                        ben();
                        break;

                    case 22320978 : //Joe
                        welcome();
                        joe();
                        break;

                    case 22352757 : //Arthur
                        welcome();
                        arthur();
                        break;

                    default : //intruder case
                        intruder();
                        display = 0;
                        break;
                }
                if (display == 0) { //intruder restarts system
                    break;
                }
                while (display == 1) {
                    //%%%%%% RFID Override %%%%%%%%
                    reading = rfid.readable();
                    if (reading) {
                        id = rfid.read();
                        TFT.set_orientation(1);
                        break;
                    }
                    //%%%%% INCOMING DATA %%%%%%
                    /*TFT.set_orientation(1);
                    TFT.foreground(White);
                    TFT.locate(145,0);
                    TFT.set_font((unsigned char*) Arial12x12);
                    TFT.printf("Temperature: 25.0 C");
                    TFT.locate(145,15);
                    TFT.printf("Fan: On");
                    TFT.locate(145, 30);
                    TFT.printf("Lights: Low");
                    TFT.foreground(Cyan); */

                    //%%%%% TOUCH %%%%%%
                    TFT.set_orientation(0);
                    xpos= readX();
                    ypos= readY();

                    xp=(240*(xpos-5800))/51200;
                    yp=320-(320*(ypos-3000))/58300;

                    if (yp<120 && xp<120 && touching==1) { //button for lighting

                        display = 2;            //jump to lights
                        TFT.set_orientation(1);
                    }

                    if (yp>230 && xp<120 && touching==1) { //button for ac system

                        display = 3;            //jump to ac
                        TFT.set_orientation(1);
                    }

                    if (yp<100 && xp>120 && touching==1) { //button for auto control

                        display = 4;            //jump to automatic control
                        TFT.set_orientation(1);
                    }
                }
                break;

            case 2 : //lighting screen


                lights();

                while (display == 2) {

                    //%%%%% TOUCH %%%%%%
                    TFT.set_orientation(0);
                    xpos= readX();
                    ypos= readY();

                    xp=(240*(xpos-5800))/51200;
                    yp=320-(320*(ypos-3000))/58300;

                    if (yp<120 && xp<120 && touching==1) { //button for LIGHTS OFF

                        lights_off();
                    }

                    if (yp>250 && xp<60 && touching==1) { //button for LIGHTS 1

                        lights_1();
                    }

                    if (yp>250 && 70<xp<110 && touching==1) { //button for LIGHTS 2

                        lights_2();
                    }

                    if (yp>250 && xp>115 && touching==1) { //button for LIGHTS 3

                        lights_3();
                    }

                    if (yp<30 && xp>210 && touching==1) { //RETURN TO MAIN SCREEN

                        display = 1;
                        TFT.set_orientation(1);
                    }


                    //%%%%%% RFID Override %%%%%%%%
                    reading = rfid.readable();
                    if (reading) {
                        id = rfid.read();
                        TFT.set_orientation(1);
                        display = 1;
                    }
                }
                break;

            case 3 : //temperature screen


                temps();

                while (display == 3) {

                    //%%%%% TOUCH %%%%%%
                    TFT.set_orientation(0);
                    xpos= readX();
                    ypos= readY();

                    xp=(240*(xpos-5800))/51200;
                    yp=320-(320*(ypos-3000))/58300;

                    if (yp<120 && xp<120 && touching==1) { //button for FAN OFF

                        fan_off();
                    }

                    if (yp>230 && xp<120 && touching==1) { //button for FAN ON

                        fan_on();
                    }

                    if (yp<30 && xp>210 && touching==1) { //RETURN TO MAIN SCREEN

                        display = 1;
                        TFT.set_orientation(1);
                    }


                    //%%%%%% RFID Override %%%%%%%%
                    reading = rfid.readable();
                    if (reading) {
                        id = rfid.read();
                        TFT.set_orientation(1);
                        display = 1;
                    }
                }
                break;

            case 4 : //auto controls


                autos();

                while (display == 4) {

                    //%%%%% TOUCH %%%%%%
                    TFT.set_orientation(0);
                    xpos= readX();
                    ypos= readY();

                    xp=(240*(xpos-5800))/51200;
                    yp=320-(320*(ypos-3000))/58300;

                    if (yp<120 && xp<120 && touching==1) { //button for AUTO OFF

                        autom_togg();
                    }

                    if (yp>230 && xp<120 && touching==1) { //button for AUTO ON

                        autom_togg();
                    }

                    if (yp<30 && xp>210 && touching==1) { //RETURN TO MAIN SCREEN

                        display = 1;
                        TFT.set_orientation(1);
                    }


                    //%%%%%% RFID Override %%%%%%%%
                    reading = rfid.readable();
                    if (reading) {
                        id = rfid.read();
                        TFT.set_orientation(1);
                        display = 1;
                    }
                }
                break;
        }
    }
}