Skip to content

Instantly share code, notes, and snippets.

@treeherder
Created April 24, 2015 08:10
Show Gist options
  • Select an option

  • Save treeherder/7d2419bf6223e90cf3d6 to your computer and use it in GitHub Desktop.

Select an option

Save treeherder/7d2419bf6223e90cf3d6 to your computer and use it in GitHub Desktop.
touchscreeen for garden controller
//tft macros
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
//touchscreen macros
#define YP A3
#define XM A2
#define YM 9
#define XP 8
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
#define MINPRESSURE 10
#define MAXPRESSURE 1000
// color macro
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#include "TFTLCD.h"
#include "TouchScreen.h"
//lcd setup according to macro
TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// 300 Ohm resistance on x ground
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
String in_str = ""; //for one character at a time input stream
boolean nl_flg = false; //flag for line complete
int a_line; // line numbers for displaying text in menu
boolean submenu = false; //toggle menu state from main
void setup(void) {
Serial.begin(9600);
in_str.reserve(512); // malloc up some memory
Serial.println("welcome to grobot");
tft.reset();
tft.initDisplay();
tft.setRotation(3);
delay(50);
tft.fillScreen(BLACK);
delay(50);
}
void loop(void) {
//set up the screen
main_menu();
}
void fmt_txt(uint16_t color, uint8_t x, uint8_t y, uint8_t font_size) {
//format the text
tft.setTextColor(color);
tft.setTextSize(font_size);
tft.setCursor(x, y);
}
void output_data(uint8_t line){
//tft.fillScreen(BLACK);
int tmp, inc;
tmp = 60;
inc = 18;
for (int i=0; i<=line; i++){
tmp += inc;
}
// print the line onto a black background
tft.drawRect(0, tmp, tft.width(), inc, YELLOW);
tft.fillRect(0, tmp, tft.width()-1, inc-1, BLACK);
fmt_txt(MAGENTA, 0, tmp, 2);
tft.println(in_str);
}
void main_menu(){//header
fmt_txt(GREEN, 5, 10, 3);
tft.println("grobot controller");
tft.print("--------------------");
//navigation bar
fmt_txt(CYAN, 0, 50, 1);
tft.println(" _____________________________________________________");
tft.println("| humidity | pressure | temp | water temp | luminance |");
tft.println("|__________|__________|______|____________|___________|");
menu_input();
if (nl_flg) {
Serial.println(in_str);
output_data(a_line);
// clear the string:
in_str = "";
nl_flg = false;
a_line ++;
if (a_line > 8){a_line = 0;}
}
}
void serialEvent() {
while (Serial.available()) {
char in_char = (char)Serial.read();
in_str += in_char;
if (in_char == '\n') {
nl_flg = true;
}
}
}
void menu_input(){
digitalWrite(13, HIGH);
Point p = ts.getPoint();
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//pinMode(YM, OUTPUT);
// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
Serial.print(p.x);
Serial.print(" ");
Serial.println(p.y);
// navbar buttons
if(p.x < 50){
//top of the screen
//rotation causes x-y swap
if(p.y <= 40){nav_bar(4);}
else if(p.y > 40 && p.y <= 90){nav_bar(3);}
else if(p.y > 90 && p.y <= 140){nav_bar(2);}
else if(p.y > 140 && p.y <= 200){nav_bar(1);}
else if(p.y > 200 && p.y <= 260){nav_bar(0);}
}
}
}
void nav_bar(uint8_t button){
switch(button){
case 0:
Serial.println("Humidity Menu");
in_str = "";
nl_flg = true;
break;
case 1:
Serial.println("Pressure Menu");
in_str = "";
nl_flg = true;
break;
case 2:
Serial.println("Temperature Menu");
in_str = "";
nl_flg = true;
break;
case 3:
Serial.println("Water Temperature Menu");
in_str = "";
nl_flg = true;
break;
case 4:
Serial.println("Luminance Menu");
in_str = "";
nl_flg = true;
fill_rects(YELLOW, MAGENTA);
break;
}
}
void fill_rects(uint16_t color1, uint16_t color2) {
for (uint16_t x=tft.width()-1; x > 6; x-=6) {
tft.fillRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color1);
tft.drawRect(tft.width()/2 -x/2, tft.height()/2 -x/2 , x, x, color2);
}
tft.fillScreen(BLACK);
a_line =0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment