| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

onoffbutton_breakout.ino
Go to the documentation of this file.
1 //This example implements a simple sliding On/Off button. The example
2 // demonstrates drawing and touch operations.
3 //
4 //Thanks to Adafruit forums member Asteroid for the original sketch!
5 //
6 #include <Adafruit_GFX.h>
7 #include <SPI.h>
8 #include <Wire.h>
9 #include <Adafruit_ILI9341.h>
10 #include <TouchScreen.h>
11
12 //Touchscreen X+ X- Y+ Y- pins
13 #define YP A3 // must be an analog pin, use "An" notation!
14 #define XM A2 // must be an analog pin, use "An" notation!
15 #define YM 5 // can be a digital pin
16 #define XP 4 // can be a digital pin
17
18 // This is calibration data for the raw touch data to the screen coordinates
19 #define TS_MINX 150
20 #define TS_MINY 120
21 #define TS_MAXX 920
22 #define TS_MAXY 940
23
24 #define MINPRESSURE 10
25 #define MAXPRESSURE 1000
26
27 // For better pressure precision, we need to know the resistance
28 // between X+ and X- Use any multimeter to read it
29 // For the one we're using, its 300 ohms across the X plate
30 TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
31
32
33 #define TFT_CS 10
34 #define TFT_DC 9
35 Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
36
37 boolean RecordOn = false;
38
39 #define FRAME_X 210
40 #define FRAME_Y 180
41 #define FRAME_W 100
42 #define FRAME_H 50
43
44 #define REDBUTTON_X FRAME_X
45 #define REDBUTTON_Y FRAME_Y
46 #define REDBUTTON_W (FRAME_W/2)
47 #define REDBUTTON_H FRAME_H
48
49 #define GREENBUTTON_X (REDBUTTON_X + REDBUTTON_W)
50 #define GREENBUTTON_Y FRAME_Y
51 #define GREENBUTTON_W (FRAME_W/2)
52 #define GREENBUTTON_H FRAME_H
53
54 void drawFrame()
55 {
56 tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ILI9341_BLACK);
57 }
58
59 void redBtn()
60 {
61 tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, ILI9341_RED);
62 tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, ILI9341_BLUE);
63 drawFrame();
64 tft.setCursor(GREENBUTTON_X + 6 , GREENBUTTON_Y + (GREENBUTTON_H/2));
65 tft.setTextColor(ILI9341_WHITE);
66 tft.setTextSize(2);
67 tft.println("ON");
68 RecordOn = false;
69 }
70
71 void greenBtn()
72 {
73 tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, ILI9341_GREEN);
74 tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, ILI9341_BLUE);
75 drawFrame();
76 tft.setCursor(REDBUTTON_X + 6 , REDBUTTON_Y + (REDBUTTON_H/2));
77 tft.setTextColor(ILI9341_WHITE);
78 tft.setTextSize(2);
79 tft.println("OFF");
80 RecordOn = true;
81 }
82
83 void setup(void)
84 {
85 Serial.begin(9600);
86 tft.begin();
87
88 tft.fillScreen(ILI9341_BLUE);
89 // origin = left,top landscape (USB left upper)
90 tft.setRotation(1);
91 redBtn();
92 }
93
94 void loop()
95 {
96 // Retrieve a point
97 TSPoint p = ts.getPoint();
98
99 // See if there's any touch data for us
100 if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
101 {
102 // Scale using the calibration #'s
103 // and rotate coordinate system
104 p.x = map(p.x, TS_MINY, TS_MAXY, 0, tft.height());
105 p.y = map(p.y, TS_MINX, TS_MAXX, 0, tft.width());
106 int y = tft.height() - p.x;
107 int x = p.y;
108
109 if (RecordOn)
110 {
111 if ((x > REDBUTTON_X) && (x < (REDBUTTON_X + REDBUTTON_W)))
112 {
113 if ((y > REDBUTTON_Y) && (y <= (REDBUTTON_Y + REDBUTTON_H)))
114 {
115 Serial.println("Red btn hit");
116 redBtn();
117 }
118 }
119 }
120 else //Record is off (RecordOn == false)
121 {
122 if ((x > GREENBUTTON_X) && (x < (GREENBUTTON_X + GREENBUTTON_W)))
123 {
124 if ((y > GREENBUTTON_Y) && (y <= (GREENBUTTON_Y + GREENBUTTON_H)))
125 {
126 Serial.println("Green btn hit");
127 greenBtn();
128 }
129 }
130 }
131
132 Serial.println(RecordOn);
133 }
134 }
135
136
137
Adafruit_GFX::drawRect
virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
Definition: Adafruit_GFX.cpp:264
redBtn
static void redBtn()
Definition: onoffbutton_breakout.ino:59
Adafruit_GFX::height
int16_t height(void) const
Definition: Adafruit_GFX.cpp:1025
FRAME_W
#define FRAME_W
Definition: onoffbutton_breakout.ino:41
TFT_CS
#define TFT_CS
Definition: onoffbutton_breakout.ino:33
Adafruit_GFX::setCursor
void setCursor(int16_t x, int16_t y)
Definition: Adafruit_GFX.cpp:709
drawFrame
static void drawFrame()
Definition: onoffbutton_breakout.ino:54
TFT_DC
#define TFT_DC
Definition: onoffbutton_breakout.ino:34
Adafruit_GFX::width
int16_t width(void) const
Definition: Adafruit_GFX.cpp:1020
Adafruit_ILI9341::begin
void begin(void)
Definition: Adafruit_ILI9341.cpp:219
greenBtn
static void greenBtn()
Definition: onoffbutton_breakout.ino:71
ILI9341_WHITE
#define ILI9341_WHITE
Definition: Adafruit_ILI9341.h:113
Point::x
int x
Definition: SeeedTouchScreen.h:11
REDBUTTON_Y
#define REDBUTTON_Y
Definition: onoffbutton_breakout.ino:45
ts
TouchScreen ts
Definition: onoffbutton_breakout.ino:30
ILI9341_BLACK
#define ILI9341_BLACK
Definition: Adafruit_ILI9341.h:98
GREENBUTTON_X
#define GREENBUTTON_X
Definition: onoffbutton_breakout.ino:49
XM
#define XM
Definition: onoffbutton_breakout.ino:14
ILI9341_RED
#define ILI9341_RED
Definition: Adafruit_ILI9341.h:110
YM
#define YM
Definition: onoffbutton_breakout.ino:15
loop
static void loop()
Definition: onoffbutton_breakout.ino:94
TouchScreen::getPoint
Point getPoint()
Definition: SeeedTouchScreen.cpp:74
Adafruit_GFX.h
RecordOn
boolean RecordOn
Definition: onoffbutton_breakout.ino:37
TS_MAXY
#define TS_MAXY
Definition: onoffbutton_breakout.ino:22
Adafruit_GFX::setTextColor
void setTextColor(uint16_t c)
Definition: Adafruit_GFX.cpp:730
REDBUTTON_X
#define REDBUTTON_X
Definition: onoffbutton_breakout.ino:44
REDBUTTON_W
#define REDBUTTON_W
Definition: onoffbutton_breakout.ino:46
GREENBUTTON_W
#define GREENBUTTON_W
Definition: onoffbutton_breakout.ino:51
Adafruit_ILI9341::setRotation
void setRotation(uint8_t r)
Definition: Adafruit_ILI9341.cpp:613
REDBUTTON_H
#define REDBUTTON_H
Definition: onoffbutton_breakout.ino:47
FRAME_H
#define FRAME_H
Definition: onoffbutton_breakout.ino:42
XP
#define XP
Definition: onoffbutton_breakout.ino:16
ILI9341_BLUE
#define ILI9341_BLUE
Definition: Adafruit_ILI9341.h:107
setup
static void setup(void)
Definition: onoffbutton_breakout.ino:83
ILI9341_GREEN
#define ILI9341_GREEN
Definition: Adafruit_ILI9341.h:108
tft
Adafruit_ILI9341 tft
Definition: onoffbutton_breakout.ino:35
MINPRESSURE
#define MINPRESSURE
Definition: onoffbutton_breakout.ino:24
x
int32_t x
Definition: LTC2492_Thermocouple_Meter.ino:119
Adafruit_ILI9341::fillScreen
void fillScreen(uint16_t color)
Definition: Adafruit_ILI9341.cpp:552
MAXPRESSURE
#define MAXPRESSURE
Definition: onoffbutton_breakout.ino:25
Adafruit_GFX::setTextSize
void setTextSize(uint8_t s)
Definition: Adafruit_GFX.cpp:725
TouchScreen
Definition: SeeedTouchScreen.h:21
TS_MINY
#define TS_MINY
Definition: onoffbutton_breakout.ino:20
GREENBUTTON_Y
#define GREENBUTTON_Y
Definition: onoffbutton_breakout.ino:50
GREENBUTTON_H
#define GREENBUTTON_H
Definition: onoffbutton_breakout.ino:52
Adafruit_ILI9341::fillRect
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
Definition: Adafruit_ILI9341.cpp:558
Adafruit_ILI9341.h
YP
#define YP
Definition: onoffbutton_breakout.ino:13
FRAME_X
#define FRAME_X
Definition: onoffbutton_breakout.ino:39
TS_MAXX
#define TS_MAXX
Definition: onoffbutton_breakout.ino:21
Adafruit_ILI9341
Definition: Adafruit_ILI9341.h:118
FRAME_Y
#define FRAME_Y
Definition: onoffbutton_breakout.ino:40
TS_MINX
#define TS_MINX
Definition: onoffbutton_breakout.ino:19