| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

onoffbutton.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 <Adafruit_STMPE610.h>
11
12 // This is calibration data for the raw touch data to the screen coordinates
13 #define TS_MINX 150
14 #define TS_MINY 130
15 #define TS_MAXX 3800
16 #define TS_MAXY 4000
17
18 #define STMPE_CS 8
19 Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
20 #define TFT_CS 10
21 #define TFT_DC 9
22 Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
23
24 boolean RecordOn = false;
25
26 #define FRAME_X 210
27 #define FRAME_Y 180
28 #define FRAME_W 100
29 #define FRAME_H 50
30
31 #define REDBUTTON_X FRAME_X
32 #define REDBUTTON_Y FRAME_Y
33 #define REDBUTTON_W (FRAME_W/2)
34 #define REDBUTTON_H FRAME_H
35
36 #define GREENBUTTON_X (REDBUTTON_X + REDBUTTON_W)
37 #define GREENBUTTON_Y FRAME_Y
38 #define GREENBUTTON_W (FRAME_W/2)
39 #define GREENBUTTON_H FRAME_H
40
41 void drawFrame()
42 {
43 tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, ILI9341_BLACK);
44 }
45
46 void redBtn()
47 {
48 tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, ILI9341_RED);
49 tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, ILI9341_BLUE);
50 drawFrame();
51 tft.setCursor(GREENBUTTON_X + 6 , GREENBUTTON_Y + (GREENBUTTON_H/2));
52 tft.setTextColor(ILI9341_WHITE);
53 tft.setTextSize(2);
54 tft.println("ON");
55 RecordOn = false;
56 }
57
58 void greenBtn()
59 {
60 tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, ILI9341_GREEN);
61 tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, ILI9341_BLUE);
62 drawFrame();
63 tft.setCursor(REDBUTTON_X + 6 , REDBUTTON_Y + (REDBUTTON_H/2));
64 tft.setTextColor(ILI9341_WHITE);
65 tft.setTextSize(2);
66 tft.println("OFF");
67 RecordOn = true;
68 }
69
70 void setup(void)
71 {
72 Serial.begin(9600);
73 tft.begin();
74 if (!ts.begin())
75 {
76 Serial.println("Unable to start touchscreen.");
77 }
78 else
79 {
80 Serial.println("Touchscreen started.");
81 }
82
83 tft.fillScreen(ILI9341_BLUE);
84 // origin = left,top landscape (USB left upper)
85 tft.setRotation(1);
86 redBtn();
87 }
88
89 void loop()
90 {
91 // See if there's any touch data for us
92 if (!ts.bufferEmpty())
93 {
94 // Retrieve a point
95 TS_Point p = ts.getPoint();
96 // Scale using the calibration #'s
97 // and rotate coordinate system
98 p.x = map(p.x, TS_MINY, TS_MAXY, 0, tft.height());
99 p.y = map(p.y, TS_MINX, TS_MAXX, 0, tft.width());
100 int y = tft.height() - p.x;
101 int x = p.y;
102
103 if (RecordOn)
104 {
105 if ((x > REDBUTTON_X) && (x < (REDBUTTON_X + REDBUTTON_W)))
106 {
107 if ((y > REDBUTTON_Y) && (y <= (REDBUTTON_Y + REDBUTTON_H)))
108 {
109 Serial.println("Red btn hit");
110 redBtn();
111 }
112 }
113 }
114 else //Record is off (RecordOn == false)
115 {
116 if ((x > GREENBUTTON_X) && (x < (GREENBUTTON_X + GREENBUTTON_W)))
117 {
118 if ((y > GREENBUTTON_Y) && (y <= (GREENBUTTON_Y + GREENBUTTON_H)))
119 {
120 Serial.println("Green btn hit");
121 greenBtn();
122 }
123 }
124 }
125
126 Serial.println(RecordOn);
127 }
128 }
129
130
131
TS_Point
Definition: Adafruit_FT6206.h:49
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
TS_Point::x
int16_t x
Definition: Adafruit_FT6206.h:58
setup
static void setup(void)
Definition: onoffbutton.ino:70
Adafruit_GFX::height
int16_t height(void) const
Definition: Adafruit_GFX.cpp:1025
REDBUTTON_H
#define REDBUTTON_H
Definition: onoffbutton.ino:34
FRAME_Y
#define FRAME_Y
Definition: onoffbutton.ino:27
GREENBUTTON_Y
#define GREENBUTTON_Y
Definition: onoffbutton.ino:37
TFT_CS
#define TFT_CS
Definition: onoffbutton.ino:20
TFT_DC
#define TFT_DC
Definition: onoffbutton.ino:21
Adafruit_GFX::setCursor
void setCursor(int16_t x, int16_t y)
Definition: Adafruit_GFX.cpp:709
redBtn
static void redBtn()
Definition: onoffbutton.ino:46
Adafruit_GFX::width
int16_t width(void) const
Definition: Adafruit_GFX.cpp:1020
Adafruit_ILI9341::begin
void begin(void)
Definition: Adafruit_ILI9341.cpp:219
ILI9341_WHITE
#define ILI9341_WHITE
Definition: Adafruit_ILI9341.h:113
drawFrame
static void drawFrame()
Definition: onoffbutton.ino:41
REDBUTTON_W
#define REDBUTTON_W
Definition: onoffbutton.ino:33
FRAME_W
#define FRAME_W
Definition: onoffbutton.ino:28
ILI9341_BLACK
#define ILI9341_BLACK
Definition: Adafruit_ILI9341.h:98
GREENBUTTON_W
#define GREENBUTTON_W
Definition: onoffbutton.ino:38
TS_MAXX
#define TS_MAXX
Definition: onoffbutton.ino:15
ILI9341_RED
#define ILI9341_RED
Definition: Adafruit_ILI9341.h:110
TS_Point::y
int16_t y
Definition: Adafruit_FT6206.h:58
TS_MINX
#define TS_MINX
Definition: onoffbutton.ino:13
tft
Adafruit_ILI9341 tft
Definition: onoffbutton.ino:22
STMPE_CS
#define STMPE_CS
Definition: onoffbutton.ino:18
Adafruit_GFX.h
TS_MAXY
#define TS_MAXY
Definition: onoffbutton.ino:16
REDBUTTON_X
#define REDBUTTON_X
Definition: onoffbutton.ino:31
TS_MINY
#define TS_MINY
Definition: onoffbutton.ino:14
Adafruit_GFX::setTextColor
void setTextColor(uint16_t c)
Definition: Adafruit_GFX.cpp:730
RecordOn
boolean RecordOn
Definition: onoffbutton.ino:24
Adafruit_ILI9341::setRotation
void setRotation(uint8_t r)
Definition: Adafruit_ILI9341.cpp:613
ILI9341_BLUE
#define ILI9341_BLUE
Definition: Adafruit_ILI9341.h:107
loop
static void loop()
Definition: onoffbutton.ino:89
ILI9341_GREEN
#define ILI9341_GREEN
Definition: Adafruit_ILI9341.h:108
GREENBUTTON_H
#define GREENBUTTON_H
Definition: onoffbutton.ino:39
x
int32_t x
Definition: LTC2492_Thermocouple_Meter.ino:119
Adafruit_ILI9341::fillScreen
void fillScreen(uint16_t color)
Definition: Adafruit_ILI9341.cpp:552
Adafruit_GFX::setTextSize
void setTextSize(uint8_t s)
Definition: Adafruit_GFX.cpp:725
FRAME_H
#define FRAME_H
Definition: onoffbutton.ino:29
greenBtn
static void greenBtn()
Definition: onoffbutton.ino:58
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
GREENBUTTON_X
#define GREENBUTTON_X
Definition: onoffbutton.ino:36
Adafruit_ILI9341.h
ts
Adafruit_STMPE610 ts
Definition: onoffbutton.ino:19
Adafruit_ILI9341
Definition: Adafruit_ILI9341.h:118
REDBUTTON_Y
#define REDBUTTON_Y
Definition: onoffbutton.ino:32
FRAME_X
#define FRAME_X
Definition: onoffbutton.ino:26