1 | // UTouch_ButtonTest
|
2 | // Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
|
3 | // web: http://www.RinkyDinkElectronics.com/
|
4 | //
|
5 | // This program is a quick demo of how create and use buttons.
|
6 | //
|
7 | // This program requires the UTFT library.
|
8 | //
|
9 | // It is assumed that the display module is connected to an
|
10 | // appropriate shield or that you know how to change the pin
|
11 | // numbers in the setup.
|
12 | //
|
13 |
|
14 | #include <UTFT.h>
|
15 | #include <UTouch.h>
|
16 | #include <UTouchCD.h>
|
17 |
|
18 | // Initialize display
|
19 | // ------------------
|
20 | // Set the pins to the correct ones for your development board
|
21 | // -----------------------------------------------------------
|
22 | // Standard Arduino Uno/2009 Shield : <display model>,19,18,17,16
|
23 | // Standard Arduino Mega/Due shield : <display model>,38,39,40,41
|
24 | // CTE TFT LCD/SD Shield for Arduino Due : <display model>,25,26,27,28
|
25 | // Teensy 3.x TFT Test Board : <display model>,23,22, 3, 4
|
26 | // ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
|
27 | //
|
28 | // Remember to change the model parameter to suit your display module!
|
29 | UTFT myGLCD(ITDB32S,38,39,40,41);
|
30 |
|
31 | // Initialize touchscreen
|
32 | // ----------------------
|
33 | // Set the pins to the correct ones for your development board
|
34 | // -----------------------------------------------------------
|
35 | // Standard Arduino Uno/2009 Shield : 15,10,14, 9, 8
|
36 | // Standard Arduino Mega/Due shield : 6, 5, 4, 3, 2
|
37 | // CTE TFT LCD/SD Shield for Arduino Due : 6, 5, 4, 3, 2
|
38 | // Teensy 3.x TFT Test Board : 26,31,27,28,29
|
39 | // ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
|
40 | //
|
41 | UTouch myTouch( 6, 5, 4, 3, 2);
|
42 |
|
43 | // Declare which fonts we will be using
|
44 | extern uint8_t BigFont[];
|
45 |
|
46 | int x, y;
|
47 | char stCurrent[20]="";
|
48 | int stCurrentLen=0;
|
49 | char stLast[20]="";
|
50 |
|
51 | /*************************
|
52 | ** Custom functions **
|
53 | *************************/
|
54 |
|
55 |
|
56 | void drawButtons()
|
57 | {
|
58 | // Draw the upper row of buttons
|
59 | for (x=0; x<5; x++)
|
60 | {
|
61 | myGLCD.setColor(0, 0, 255);
|
62 | myGLCD.fillRoundRect (10+(x*60), 10, 60+(x*60), 60);
|
63 | myGLCD.setColor(255, 255, 255);
|
64 | myGLCD.drawRoundRect (10+(x*60), 10, 60+(x*60), 60);
|
65 | myGLCD.printNumI(x+1, 27+(x*60), 27);
|
66 | }
|
67 | // Draw the center row of buttons
|
68 | for (x=0; x<5; x++)
|
69 | {
|
70 | myGLCD.setColor(0, 0, 255);
|
71 | myGLCD.fillRoundRect (10+(x*60), 70, 60+(x*60), 120);
|
72 | myGLCD.setColor(255, 255, 255);
|
73 | myGLCD.drawRoundRect (10+(x*60), 70, 60+(x*60), 120);
|
74 | if (x<4)
|
75 | myGLCD.printNumI(x+6, 27+(x*60), 87);
|
76 | }
|
77 | myGLCD.print("0", 267, 87);
|
78 | // Draw the lower row of buttons
|
79 | myGLCD.setColor(0, 0, 255);
|
80 | myGLCD.fillRoundRect (10, 130, 150, 180);
|
81 | myGLCD.setColor(255, 255, 255);
|
82 | myGLCD.drawRoundRect (10, 130, 150, 180);
|
83 | myGLCD.print("Clear", 40, 147);
|
84 | myGLCD.setColor(0, 0, 255);
|
85 | myGLCD.fillRoundRect (160, 130, 300, 180);
|
86 | myGLCD.setColor(255, 255, 255);
|
87 | myGLCD.drawRoundRect (160, 130, 300, 180);
|
88 | myGLCD.print("Enter", 190, 147);
|
89 | myGLCD.setBackColor (0, 0, 0);
|
90 | }
|
91 |
|
92 | void updateStr(int val)
|
93 | {
|
94 | if (stCurrentLen<20)
|
95 | {
|
96 | stCurrent[stCurrentLen]=val;
|
97 | stCurrent[stCurrentLen+1]='\0';
|
98 | stCurrentLen++;
|
99 | myGLCD.setColor(0, 255, 0);
|
100 | myGLCD.print(stCurrent, LEFT, 224);
|
101 | }
|
102 | else
|
103 | {
|
104 | myGLCD.setColor(255, 0, 0);
|
105 | myGLCD.print("BUFFER FULL!", CENTER, 192);
|
106 | delay(500);
|
107 | myGLCD.print(" ", CENTER, 192);
|
108 | delay(500);
|
109 | myGLCD.print("BUFFER FULL!", CENTER, 192);
|
110 | delay(500);
|
111 | myGLCD.print(" ", CENTER, 192);
|
112 | myGLCD.setColor(0, 255, 0);
|
113 | }
|
114 | }
|
115 |
|
116 | // Draw a red frame while a button is touched
|
117 | void waitForIt(int x1, int y1, int x2, int y2)
|
118 | {
|
119 | myGLCD.setColor(255, 0, 0);
|
120 | myGLCD.drawRoundRect (x1, y1, x2, y2);
|
121 | while (myTouch.dataAvailable())
|
122 | myTouch.read();
|
123 | myGLCD.setColor(255, 255, 255);
|
124 | myGLCD.drawRoundRect (x1, y1, x2, y2);
|
125 | }
|
126 |
|
127 | /*************************
|
128 | ** Required functions **
|
129 | *************************/
|
130 |
|
131 | void setup()
|
132 | {
|
133 | // Initial setup
|
134 | myGLCD.InitLCD();
|
135 | myGLCD.clrScr();
|
136 |
|
137 | myTouch.InitTouch();
|
138 | myTouch.setPrecision(PREC_MEDIUM);
|
139 |
|
140 | myGLCD.setFont(BigFont);
|
141 | myGLCD.setBackColor(0, 0, 255);
|
142 | drawButtons();
|
143 | }
|
144 |
|
145 | void loop()
|
146 | {
|
147 | while (true)
|
148 | {
|
149 | if (myTouch.dataAvailable())
|
150 | {
|
151 | myTouch.read();
|
152 | x=myTouch.getX();
|
153 | y=myTouch.getY();
|
154 |
|
155 | if ((y>=10) && (y<=60)) // Upper row
|
156 | {
|
157 | if ((x>=10) && (x<=60)) // Button: 1
|
158 | {
|
159 | waitForIt(10, 10, 60, 60);
|
160 | updateStr('1');
|
161 | }
|
162 | if ((x>=70) && (x<=120)) // Button: 2
|
163 | {
|
164 | waitForIt(70, 10, 120, 60);
|
165 | updateStr('2');
|
166 | }
|
167 | if ((x>=130) && (x<=180)) // Button: 3
|
168 | {
|
169 | waitForIt(130, 10, 180, 60);
|
170 | updateStr('3');
|
171 | }
|
172 | if ((x>=190) && (x<=240)) // Button: 4
|
173 | {
|
174 | waitForIt(190, 10, 240, 60);
|
175 | updateStr('4');
|
176 | }
|
177 | if ((x>=250) && (x<=300)) // Button: 5
|
178 | {
|
179 | waitForIt(250, 10, 300, 60);
|
180 | updateStr('5');
|
181 | }
|
182 | }
|
183 |
|
184 | if ((y>=70) && (y<=120)) // Center row
|
185 | {
|
186 | if ((x>=10) && (x<=60)) // Button: 6
|
187 | {
|
188 | waitForIt(10, 70, 60, 120);
|
189 | updateStr('6');
|
190 | }
|
191 | if ((x>=70) && (x<=120)) // Button: 7
|
192 | {
|
193 | waitForIt(70, 70, 120, 120);
|
194 | updateStr('7');
|
195 | }
|
196 | if ((x>=130) && (x<=180)) // Button: 8
|
197 | {
|
198 | waitForIt(130, 70, 180, 120);
|
199 | updateStr('8');
|
200 | }
|
201 | if ((x>=190) && (x<=240)) // Button: 9
|
202 | {
|
203 | waitForIt(190, 70, 240, 120);
|
204 | updateStr('9');
|
205 | }
|
206 | if ((x>=250) && (x<=300)) // Button: 0
|
207 | {
|
208 | waitForIt(250, 70, 300, 120);
|
209 | updateStr('0');
|
210 | }
|
211 | }
|
212 |
|
213 | if ((y>=130) && (y<=180)) // Upper row
|
214 | {
|
215 | if ((x>=10) && (x<=150)) // Button: Clear
|
216 | {
|
217 | waitForIt(10, 130, 150, 180);
|
218 | stCurrent[0]='\0';
|
219 | stCurrentLen=0;
|
220 | myGLCD.setColor(0, 0, 0);
|
221 | myGLCD.fillRect(0, 224, 319, 239);
|
222 | }
|
223 | if ((x>=160) && (x<=300)) // Button: Enter
|
224 | {
|
225 | waitForIt(160, 130, 300, 180);
|
226 | if (stCurrentLen>0)
|
227 | {
|
228 | for (x=0; x<stCurrentLen+1; x++)
|
229 | {
|
230 | stLast[x]=stCurrent[x];
|
231 | }
|
232 | stCurrent[0]='\0';
|
233 | stCurrentLen=0;
|
234 | myGLCD.setColor(0, 0, 0);
|
235 | myGLCD.fillRect(0, 208, 319, 239);
|
236 | myGLCD.setColor(0, 255, 0);
|
237 | myGLCD.print(stLast, LEFT, 208);
|
238 | }
|
239 | else
|
240 | {
|
241 | myGLCD.setColor(255, 0, 0);
|
242 | myGLCD.print("BUFFER EMPTY", CENTER, 192);
|
243 | delay(500);
|
244 | myGLCD.print(" ", CENTER, 192);
|
245 | delay(500);
|
246 | myGLCD.print("BUFFER EMPTY", CENTER, 192);
|
247 | delay(500);
|
248 | myGLCD.print(" ", CENTER, 192);
|
249 | myGLCD.setColor(0, 255, 0);
|
250 | }
|
251 | }
|
252 | }
|
253 | }
|
254 | }
|
255 | }
|