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
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 {
44 }
45 
46 void redBtn()
47 {
50  drawFrame();
53  tft.setTextSize(2);
54  tft.println("ON");
55  RecordOn = false;
56 }
57 
58 void greenBtn()
59 {
62  drawFrame();
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 
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 
virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
int16_t x
static void setup(void)
Definition: onoffbutton.ino:70
int16_t height(void) const
#define REDBUTTON_H
Definition: onoffbutton.ino:34
#define FRAME_Y
Definition: onoffbutton.ino:27
#define GREENBUTTON_Y
Definition: onoffbutton.ino:37
#define TFT_CS
Definition: onoffbutton.ino:20
#define TFT_DC
Definition: onoffbutton.ino:21
void setCursor(int16_t x, int16_t y)
static void redBtn()
Definition: onoffbutton.ino:46
int16_t width(void) const
#define ILI9341_WHITE
static void drawFrame()
Definition: onoffbutton.ino:41
#define REDBUTTON_W
Definition: onoffbutton.ino:33
#define FRAME_W
Definition: onoffbutton.ino:28
#define ILI9341_BLACK
#define GREENBUTTON_W
Definition: onoffbutton.ino:38
#define TS_MAXX
Definition: onoffbutton.ino:15
#define ILI9341_RED
int16_t y
#define TS_MINX
Definition: onoffbutton.ino:13
Adafruit_ILI9341 tft
Definition: onoffbutton.ino:22
#define STMPE_CS
Definition: onoffbutton.ino:18
#define TS_MAXY
Definition: onoffbutton.ino:16
#define REDBUTTON_X
Definition: onoffbutton.ino:31
#define TS_MINY
Definition: onoffbutton.ino:14
void setTextColor(uint16_t c)
boolean RecordOn
Definition: onoffbutton.ino:24
void setRotation(uint8_t r)
#define ILI9341_BLUE
static void loop()
Definition: onoffbutton.ino:89
#define ILI9341_GREEN
#define GREENBUTTON_H
Definition: onoffbutton.ino:39
void fillScreen(uint16_t color)
void setTextSize(uint8_t s)
#define FRAME_H
Definition: onoffbutton.ino:29
static void greenBtn()
Definition: onoffbutton.ino:58
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
#define GREENBUTTON_X
Definition: onoffbutton.ino:36
Adafruit_STMPE610 ts
Definition: onoffbutton.ino:19
#define REDBUTTON_Y
Definition: onoffbutton.ino:32
#define FRAME_X
Definition: onoffbutton.ino:26