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
31 
32 
33 #define TFT_CS 10
34 #define TFT_DC 9
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 {
57 }
58 
59 void redBtn()
60 {
63  drawFrame();
66  tft.setTextSize(2);
67  tft.println("ON");
68  RecordOn = false;
69 }
70 
71 void greenBtn()
72 {
75  drawFrame();
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 
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 
virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
static void redBtn()
int16_t height(void) const
#define FRAME_W
#define TFT_CS
void setCursor(int16_t x, int16_t y)
static void drawFrame()
#define TFT_DC
int16_t width(void) const
static void greenBtn()
#define ILI9341_WHITE
#define REDBUTTON_Y
TouchScreen ts
#define ILI9341_BLACK
#define GREENBUTTON_X
#define XM
#define ILI9341_RED
#define YM
static void loop()
boolean RecordOn
#define TS_MAXY
void setTextColor(uint16_t c)
#define REDBUTTON_X
#define REDBUTTON_W
#define GREENBUTTON_W
void setRotation(uint8_t r)
#define REDBUTTON_H
#define FRAME_H
#define XP
#define ILI9341_BLUE
static void setup(void)
#define ILI9341_GREEN
Adafruit_ILI9341 tft
#define MINPRESSURE
void fillScreen(uint16_t color)
#define MAXPRESSURE
void setTextSize(uint8_t s)
#define TS_MINY
#define GREENBUTTON_Y
#define GREENBUTTON_H
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
#define YP
#define FRAME_X
#define TS_MAXX
#define FRAME_Y
#define TS_MINX