![](/media/cache/group/mouse.jpg.50x50_q85.jpg)
Full layout 'HUB' with functioning navigation between all screens and active temp/state display
Dependencies: ID12RFID SPI_TFT_ILI9341 TFT_fonts mbed
Fork of Welcome_Screen_TFT by
Diff: main.cpp
- Revision:
- 7:ceeced183454
- Parent:
- 6:18c7288b5e00
- Child:
- 8:95cd847648cd
--- a/main.cpp Tue Jun 14 11:01:24 2016 +0000 +++ b/main.cpp Thu Jun 16 15:41:28 2016 +0000 @@ -1,8 +1,14 @@ -// example to test the TFT Display from Mikroelectronika + +//%%%%%% 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" @@ -11,29 +17,40 @@ Serial pc (USBTX,USBRX); +ID12RFID rfid(PTE1); +DigitalOut rled(LED1); +DigitalOut gled(LED2); +DigitalOut bled(LED3); + +int touching = 0; // the display has a backlight switch on board -DigitalOut LCD_LED(PTA13); // may not be needed on mikroelectronika board -DigitalOut pwr(PTD7); // ditto +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 -// DigitalOut LCD_LED(PTC17); + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +//%%%%%%%%%%% Subroutine to read the x location of the touch point %%%%%%%%%% + -// 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() { - AnalogIn yp(PTB2); - AnalogIn ym(PTB3); + int delta=0,xv1=0,xv2=0,k=0; + + AnalogIn yp(PTB3); + AnalogIn ym(PTB2); DigitalOut xp(PTB0); DigitalOut xm(PTB1); @@ -42,14 +59,23 @@ // dont need to do anyhting to set low side as it should be fine. // but do need to disconnect yp //yp.PinMode(PullNone) - int xval=(int)ym.read_u16(); // get value - return(xval); + 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(PTB2); - DigitalOut ym(PTB3); + DigitalOut yp(PTB3); + DigitalOut ym(PTB2); AnalogIn xp(PTB0); AnalogIn xm(PTB1); @@ -59,70 +85,362 @@ // 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); + TFT.rect(165,200,295,140,0xffff); + //text for buttons + TFT.set_font((unsigned char*) Arial12x12); + TFT.locate(65,165); + TFT.printf("Lighting"); + TFT.locate(195,165); + TFT.printf("AC Control"); +} + +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,200,295,140,0xffff); + //text for buttons + TFT.set_font((unsigned char*) Arial12x12); + TFT.locate(50,165); + TFT.printf("LIGHTS OFF"); + TFT.locate(190,165); + TFT.printf("LIGHTS ON"); +} + +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"); +} + +//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +//%%%%% IDs %%%%%% + +void ben() +{ + + rled = 0; + bled = 1; + gled = 1; + + TFT.printf("B"); + wait(0.05); + TFT.printf("e"); + wait(0.05); + TFT.printf("n"); + wait(0.05); +} + +void joe() +{ + rled = 1; + bled = 0; + gled = 1; + + TFT.printf("J"); + wait(0.05); + TFT.printf("o"); + wait(0.05); + TFT.printf("e"); + wait(0.05); + +} + +void arthur() +{ + rled = 1; + bled = 0; + gled = 1; + + 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"); + wait(0.05); + +} + +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() { + int display = 0; + int reading = 0; + int id = 0; +// int light = 0; +// int temp = 0; + pc.baud(115200); int xpos=0,ypos=0,xp=0,yp=0; - pwr=1; - wait(0.2); + + while (1) { + + pc.printf("%d",id); + + switch (display) { + + case 0 : //start screen + + start(); + + while (display == 0) { - int i; - LCD_LED = 1; // backlight on + reading = rfid.readable(); + if (reading) { + display = 1; //FOR TESTING LIGHTING, SHOULD BE 1 + id = rfid.read(); + } + + } + break; + + case 1 : //welcome screen + + // id = rfid.read(); + home(); - TFT.claim(stdout); // send stdout to the TFT display - TFT.set_orientation(1); - TFT.background(Black); // set background to black - TFT.foreground(White); // set chars to white - TFT.cls(); // clear the screen + //Personalised + switch (id) { + + case 22311780 : //Ben + welcome(); + ben(); + break; + + case 22320978 : //Joe + welcome(); + joe(); + break; - //first show the 4 directions - TFT.set_orientation(0); - TFT.background(Black); - TFT.cls(); + case 22352757 : //Arthur + welcome(); + arthur(); + break; + + default : //intruder case + intruder(); + display = 0; + break; + } + if (display == 0) { //intruder restarts system + break; + } + while (display == 1) { + //%%%%% TOUCH %%%%%% + TFT.set_orientation(0); + xpos= readX(); + ypos= readY(); - TFT.set_font((unsigned char*) Arial12x12); - TFT.locate(0,0); - printf(" 0 Hello Mbed 0"); - TFT.set_orientation(1); - TFT.locate(0,0); - printf(" 1 Hello Mbed 1"); - TFT.set_orientation(2); - TFT.locate(0,0); - printf(" 2 Hello Mbed 2"); - TFT.set_orientation(3); - TFT.locate(0,0); - printf(" 3 Hello Mbed 3"); - TFT.set_orientation(3); - TFT.set_font((unsigned char*) Arial24x23); - TFT.locate(50,100); - TFT.printf("TFT orientation 3"); - TFT.set_orientation(1); - for(i=0; i<10; i++) { - wait(1); // wait one seconds - TFT.locate(50,160); - TFT.printf("count %d",i); + xp=(240*(xpos-5800))/51200; + yp=320-(320*(ypos-3000))/58300; + + if (yp<50 && touching==1) { //button for lighting + + display = 2; //jump to lights + TFT.set_orientation(1); + } + + if (yp>270 && touching==1) { //button for ac system + + display = 3; //jump to ac + TFT.set_orientation(1); + } + + + //%%%%%% RFID Override %%%%%%%% + reading = rfid.readable(); + if (reading) { + id = rfid.read(); + TFT.set_orientation(1); + break; + } + } + break; + + + + case 2 : //lighting screen + + + lights(); + TFT.locate(50,100); + TFT.printf("I reached the light screen"); + + while (display == 2) { + + wait(2); + display = 1; + } + break; + + case 3 : //temperature screen + + + temps(); + TFT.locate(50,100); + TFT.printf("I reached the temp screen"); + + while (display == 3) { + + wait(2); + display = 1; + } + break; + + + } } - TFT.set_orientation(1); - TFT.cls(); - // LCD_LED = 1; - TFT.circle(120,120,10,0xffff); - while(1==1) { - - xpos=readX(); - ypos=readY(); - pc.printf("xpos=%d\t,\typo=%d",xpos,ypos); - xp=(240*xpos)/60000; - yp=(320*ypos)/60000; - pc.printf("\txp=%d\t,\typo=%d\n\r",xp,yp); - if(xp>5 && yp>5) TFT.circle(xp,yp,5,Yellow); - wait(0.1); - - } - -} +} \ No newline at end of file