H753_CPP_SPI_BT817_EVE_02 1.0
STM32H753 draws its own touch screens on a Bridgetek BT817 (ME817EV + DP-0701-01A) over SPI with the STM32_Bridgetek_EVE_API library: a two-page widget tour
Loading...
Searching...
No Matches
widgetTour.c
Go to the documentation of this file.
1
6#include <stdio.h>
7#include <stdint.h>
8#include <stdbool.h>
9
10#include "main.h"
11#include <EVE.h>
12
16#define TAG_NONE 0U
17#define TAG_BUTTON 1U
18#define TAG_TOGGLE 2U
19#define TAG_SLIDER 3U
20#define TAG_NEXT 4U
21#define TAG_BACK 5U
22#define TAG_CLEAR 6U
23#define TAG_DIAL 7U
24#define TAG_DONE 8U
26/* The keypad keys use their own character as the tag: '0' (48) to '9' (57). */
27
30#define HEADER_HEIGHT 64
31#define BUTTON_X 40
32#define BUTTON_Y 100
33#define BUTTON_W 200
34#define BUTTON_H 70
35#define COUNT_X 270
36#define TOGGLE_X 520
37#define TOGGLE_Y 122
38#define TOGGLE_W 100
39#define LED_X 710
40#define LED_Y 135
41#define LED_RADIUS 22
42#define GAUGE_X 200
43#define GAUGE_Y 330
44#define GAUGE_RADIUS 115
45#define SLIDER_X 420
46#define SLIDER_Y 280
47#define SLIDER_W 280
48#define SLIDER_H 24
49#define PROGRESS_Y 380
50#define VALUE_X 730
51#define NAV_W 150
52#define NAV_H 44
53#define NAV_X (EVE_DISP_WIDTH - NAV_W - 14)
54#define NAV_Y 10
55#define TYPED_Y 110
56#define CLEAR_X 600
57#define CLEAR_W 150
58#define KEYS_X 40
59#define KEYS_Y 160
60#define KEYS_W 720
61#define KEYS_H 70
62#define CLOCK_X 170
63#define DIAL_X 470
64#define ROUND_Y 355
65#define ROUND_RADIUS 95
66#define DIAL_TEXT_X 600
67#define POPUP_W 500
68#define POPUP_H 260
69#define POPUP_X ((EVE_DISP_WIDTH - POPUP_W) / 2)
70#define POPUP_Y ((EVE_DISP_HEIGHT - POPUP_H) / 2)
71#define POPUP_RADIUS 16
76#define FONT_TITLE 30
77#define FONT_WIDGET 29
78#define FONT_SMALL 27
81#define SLIDER_RANGE 100U
82#define FRAME_DELAY_MS 15U
83#define TRACKER_FULL 65536UL
84#define TYPED_MAX 10U
85#define KEYPAD_KEYS "1234567890"
88typedef enum
89{
93
95static uint32_t pressCount = 0U;
97static bool toggleOn = false;
99static uint16_t sliderValue = 42U;
101static uint8_t previousTag = TAG_NONE;
105static char typedDigits[TYPED_MAX + 1U] = "";
107static uint8_t typedLength = 0U;
109static uint16_t dialValue = 0U;
111static bool popupShown = false;
112
121static void awaitCoPro(const char *step)
122{
123 if (EVE_LIB_AwaitCoProEmpty() != 0)
124 {
125 printf("EVE: co-processor exception after %s.\r\n", step);
126 Error_Handler();
127 }
128}
129
135static void calibrateTouch(void)
136{
137 EVE_LIB_BeginCoProList();
138 EVE_CMD_DLSTART();
139 EVE_CLEAR_COLOR_RGB(0, 0, 0);
140 EVE_CLEAR(1, 1, 1);
141 EVE_COLOR_RGB(255, 255, 255);
142 EVE_CMD_TEXT(EVE_DISP_WIDTH / 2, EVE_DISP_HEIGHT / 2, FONT_WIDGET,
143 EVE_OPT_CENTER, "Tap the dots to calibrate touch");
144 EVE_CMD_CALIBRATE(0);
145 EVE_LIB_EndCoProList();
146 awaitCoPro("touch calibration");
147}
148
156static void registerTrackers(void)
157{
158 EVE_LIB_BeginCoProList();
159 EVE_CMD_TRACK(SLIDER_X, SLIDER_Y, SLIDER_W, SLIDER_H, TAG_SLIDER);
160 /* A width and height of 1 makes a rotary track around the dial centre. */
161 EVE_CMD_TRACK(DIAL_X, ROUND_Y, 1, 1, TAG_DIAL);
162 EVE_LIB_EndCoProList();
163 awaitCoPro("tracker set-up");
164}
165
173static void readTouch(void)
174{
175 uint8_t tag = EVE_LIB_MemRead8(EVE_REG_TOUCH_TAG);
176 uint32_t tracker = EVE_LIB_MemRead32(EVE_REG_TRACKER);
177 /* A press counts only when the finger first touches the screen (no touch in
178 * the previous frame). Otherwise, when a page changes under a finger that is
179 * still down (Next and Done share a position), the button drawn under it
180 * would be "pressed" too. */
181 bool newPress = (previousTag == TAG_NONE) && (tag != TAG_NONE);
182
183 if (popupShown)
184 {
185 /* The tour is finished: nothing reacts any more (until reset). */
186 previousTag = tag;
187 return;
188 }
189
190 if (newPress && (tag == TAG_DONE))
191 {
192 popupShown = true;
193 }
194 else if (newPress && (tag == TAG_NEXT))
195 {
197 }
198 else if (newPress && (tag == TAG_BACK))
199 {
201 }
202 else if (newPress && (tag == TAG_BUTTON))
203 {
204 pressCount++;
205 }
206 else if (newPress && (tag == TAG_TOGGLE))
207 {
209 }
210 else if (newPress && (tag == TAG_CLEAR))
211 {
212 typedLength = 0U;
213 typedDigits[0] = '\0';
214 }
215 else if (newPress && (tag >= (uint8_t)'0') && (tag <= (uint8_t)'9') && (typedLength < TYPED_MAX))
216 {
217 typedDigits[typedLength] = (char)tag;
218 typedLength++;
219 typedDigits[typedLength] = '\0';
220 }
221
222 if ((tracker & 0xFFFFUL) == TAG_SLIDER)
223 {
224 sliderValue = (uint16_t)(((tracker >> 16) * SLIDER_RANGE) / TRACKER_FULL);
225 }
226 else if ((tracker & 0xFFFFUL) == TAG_DIAL)
227 {
228 dialValue = (uint16_t)(tracker >> 16);
229 }
230 previousTag = tag;
231}
232
239static void drawHeader(const char *title)
240{
241 EVE_TAG_MASK(0);
242 EVE_COLOR_RGB(20, 40, 110);
243 EVE_BEGIN(EVE_BEGIN_RECTS);
244 EVE_VERTEX2F(0, 0);
245 EVE_VERTEX2F(EVE_DISP_WIDTH * 16, HEADER_HEIGHT * 16);
246 EVE_END();
247 EVE_COLOR_RGB(255, 255, 255);
249 {
250 EVE_CMD_TEXT(20, HEADER_HEIGHT / 2, FONT_TITLE, EVE_OPT_CENTERY, title);
251 }
252 else
253 {
254 EVE_CMD_TEXT(EVE_DISP_WIDTH / 2, HEADER_HEIGHT / 2, FONT_TITLE, EVE_OPT_CENTER, title);
255 }
256 EVE_TAG_MASK(1);
257
258 EVE_CMD_FGCOLOR(0x3A5AC8UL);
260 {
261 EVE_TAG(TAG_NEXT);
262 EVE_CMD_BUTTON(NAV_X, NAV_Y, NAV_W, NAV_H, FONT_WIDGET,
263 (previousTag == TAG_NEXT) ? EVE_OPT_FLAT : 0U, "Next >");
264 }
265 else
266 {
267 EVE_TAG(TAG_BACK);
268 EVE_CMD_BUTTON(14, NAV_Y, NAV_W, NAV_H, FONT_WIDGET,
269 (previousTag == TAG_BACK) ? EVE_OPT_FLAT : 0U, "< Back");
270 EVE_TAG(TAG_DONE);
271 EVE_CMD_BUTTON(NAV_X, NAV_Y, NAV_W, NAV_H, FONT_WIDGET,
272 (previousTag == TAG_DONE) ? EVE_OPT_FLAT : 0U, "Done");
273 }
274}
275
280static void drawButton(void)
281{
282 EVE_CMD_FGCOLOR(0x2060C0UL);
283 EVE_TAG(TAG_BUTTON);
284 EVE_CMD_BUTTON(BUTTON_X, BUTTON_Y, BUTTON_W, BUTTON_H, FONT_WIDGET,
285 (previousTag == TAG_BUTTON) ? EVE_OPT_FLAT : 0U, "Press me");
286 EVE_TAG_MASK(0);
287 EVE_COLOR_RGB(255, 255, 255);
288 /* EVE_OPT_FORMAT makes the BT817 fill in %d (without it the text is shown as is). */
289 EVE_CMD_TEXT(COUNT_X, BUTTON_Y + (BUTTON_H / 2), FONT_WIDGET,
290 EVE_OPT_CENTERY | EVE_OPT_FORMAT, "Count: %d", (int)pressCount);
291 EVE_TAG_MASK(1);
292}
293
298static void drawToggle(void)
299{
300 EVE_CMD_FGCOLOR(0xC0C0C0UL);
301 EVE_CMD_BGCOLOR(0x404040UL);
302 EVE_TAG(TAG_TOGGLE);
303 EVE_CMD_TOGGLE(TOGGLE_X, TOGGLE_Y, TOGGLE_W, FONT_WIDGET, 0U,
304 toggleOn ? 65535U : 0U, "OFF\xffON");
305
306 EVE_TAG_MASK(0);
307 if (toggleOn)
308 {
309 EVE_COLOR_RGB(40, 230, 60);
310 }
311 else
312 {
313 EVE_COLOR_RGB(50, 60, 50);
314 }
315 EVE_POINT_SIZE(LED_RADIUS * 16);
316 EVE_BEGIN(EVE_BEGIN_POINTS);
317 EVE_VERTEX2F(LED_X * 16, LED_Y * 16);
318 EVE_END();
319 EVE_TAG_MASK(1);
320}
321
326static void drawSliderGroup(void)
327{
328 EVE_TAG_MASK(0);
329 EVE_COLOR_RGB(255, 255, 255);
330 EVE_CMD_BGCOLOR(0x202840UL);
331 EVE_CMD_GAUGE(GAUGE_X, GAUGE_Y, GAUGE_RADIUS, 0U, 10U, 5U, sliderValue, SLIDER_RANGE);
332
333 EVE_CMD_TEXT(SLIDER_X, SLIDER_Y - 36, FONT_SMALL, 0U, "Slider");
334 EVE_CMD_NUMBER(VALUE_X, SLIDER_Y + (SLIDER_H / 2), FONT_WIDGET, EVE_OPT_CENTERY, sliderValue);
335 EVE_CMD_TEXT(SLIDER_X, PROGRESS_Y - 36, FONT_SMALL, 0U, "Progress");
336 EVE_CMD_BGCOLOR(0x404040UL);
337 EVE_CMD_PROGRESS(SLIDER_X, PROGRESS_Y, SLIDER_W, SLIDER_H, 0U, sliderValue, SLIDER_RANGE);
338
339 EVE_TAG_MASK(1);
340 EVE_TAG(TAG_SLIDER);
341 EVE_CMD_FGCOLOR(0x2060C0UL);
343}
344
350static void drawPageMore(void)
351{
352 uint32_t seconds = HAL_GetTick() / 1000UL;
353 char typedLine[sizeof("Typed: ") + TYPED_MAX];
354 uint8_t pressedKey = ((previousTag >= (uint8_t)'0') && (previousTag <= (uint8_t)'9')) ? previousTag : 0U;
355
356 EVE_TAG_MASK(0);
357 EVE_COLOR_RGB(255, 255, 255);
358 /* Build the line on the STM32: with EVE_OPT_FORMAT, %s would send the STM32
359 * address of the string, which the BT817 reads as its own memory address
360 * (co-processor exception). %d is fine because the number itself is sent. */
361 (void)snprintf(typedLine, sizeof(typedLine), "Typed: %s", typedDigits);
362 EVE_CMD_TEXT(KEYS_X, TYPED_Y, FONT_TITLE, EVE_OPT_CENTERY, typedLine);
363 EVE_TAG_MASK(1);
364
365 EVE_CMD_FGCOLOR(0x2060C0UL);
366 EVE_TAG(TAG_CLEAR);
367 EVE_CMD_BUTTON(CLEAR_X, TYPED_Y - (NAV_H / 2), CLEAR_W, NAV_H, FONT_WIDGET,
368 (previousTag == TAG_CLEAR) ? EVE_OPT_FLAT : 0U, "Clear");
369
370 /* Each key's tag is its own character; adding the pressed character to the
371 * options draws that key pressed. */
372 EVE_CMD_KEYS(KEYS_X, KEYS_Y, KEYS_W, KEYS_H, FONT_WIDGET, pressedKey, KEYPAD_KEYS);
373
374 EVE_TAG_MASK(0);
375 EVE_CMD_BGCOLOR(0x202840UL);
376 EVE_CMD_CLOCK(CLOCK_X, ROUND_Y, ROUND_RADIUS, 0U,
377 (uint16_t)((seconds / 3600UL) % 12UL), (uint16_t)((seconds / 60UL) % 60UL),
378 (uint16_t)(seconds % 60UL), 0U);
379 EVE_COLOR_RGB(255, 255, 255);
380 EVE_CMD_TEXT(DIAL_TEXT_X, ROUND_Y, FONT_WIDGET, EVE_OPT_CENTERY | EVE_OPT_FORMAT,
381 "Dial: %d deg", (int)(((uint32_t)dialValue * 360UL) / TRACKER_FULL));
382 EVE_TAG_MASK(1);
383
384 EVE_TAG(TAG_DIAL);
385 EVE_CMD_FGCOLOR(0x2060C0UL);
386 EVE_CMD_DIAL(DIAL_X, ROUND_Y, ROUND_RADIUS, 0U, dialValue);
387}
388
396static void drawPopup(void)
397{
398 EVE_TAG_MASK(0);
399 EVE_COLOR_RGB(30, 50, 120);
400 EVE_LINE_WIDTH(POPUP_RADIUS * 16);
401 EVE_BEGIN(EVE_BEGIN_RECTS);
402 EVE_VERTEX2F((POPUP_X + POPUP_RADIUS) * 16, (POPUP_Y + POPUP_RADIUS) * 16);
403 EVE_VERTEX2F((POPUP_X + POPUP_W - POPUP_RADIUS) * 16, (POPUP_Y + POPUP_H - POPUP_RADIUS) * 16);
404 EVE_END();
405 EVE_LINE_WIDTH(16);
406
407 EVE_COLOR_RGB(255, 255, 255);
408 EVE_CMD_TEXT(EVE_DISP_WIDTH / 2, POPUP_Y + (POPUP_H / 2) - 50, FONT_TITLE, EVE_OPT_CENTER,
409 "Congratulations!");
410 EVE_CMD_TEXT(EVE_DISP_WIDTH / 2, POPUP_Y + (POPUP_H / 2) + 5, FONT_WIDGET, EVE_OPT_CENTER,
411 "Widget tour completed");
412 EVE_COLOR_RGB(190, 200, 230);
413 EVE_CMD_TEXT(EVE_DISP_WIDTH / 2, POPUP_Y + (POPUP_H / 2) + 60, FONT_SMALL, EVE_OPT_CENTER,
414 "Press reset to start over");
415 EVE_TAG_MASK(1);
416}
417
422static void drawScreen(void)
423{
424 EVE_LIB_BeginCoProList();
425 EVE_CMD_DLSTART();
426 EVE_CLEAR_COLOR_RGB(10, 14, 28);
427 EVE_CLEAR(1, 1, 1);
428
429 if (popupShown)
430 {
431 /* Final screen: only the pop-up on the plain background. */
432 drawPopup();
433 }
434 else if (currentPage == PAGE_WIDGETS)
435 {
436 drawHeader("BT817 Widget Tour - NUCLEO-H753ZI");
437 drawButton();
438 drawToggle();
440
441 EVE_TAG_MASK(0);
442 EVE_COLOR_RGB(160, 170, 190);
443 EVE_CMD_TEXT(EVE_DISP_WIDTH / 2, EVE_DISP_HEIGHT - 20, FONT_SMALL, EVE_OPT_CENTER,
444 "Touch the button, the toggle and the slider");
445 }
446 else
447 {
448 drawHeader("Widget Tour - page 2");
449 drawPageMore();
450 }
451
452 EVE_DISPLAY();
453 EVE_CMD_SWAP();
454 EVE_LIB_EndCoProList();
455 awaitCoPro("screen update");
456}
457
466{
467 if (EVE_Init() != 0)
468 {
469 printf("EVE: EVE_Init() failed, BT817 not found or not supported.\r\n");
470 Error_Handler();
471 }
474 printf("Widget tour running.\r\n");
475}
476
483{
484 for (;;)
485 {
486 readTouch();
487 drawScreen();
488 HAL_Delay(FRAME_DELAY_MS);
489 }
490}
: Header for main.c file. This file contains the common defines of the application.
static void drawScreen(void)
Builds and shows one complete frame of the widget tour.
Definition widgetTour.c:422
static void drawHeader(const char *title)
Draws the title bar across the top of the screen, with "Next >" on page 1, or "< Back" and "Done" on ...
Definition widgetTour.c:239
static bool popupShown
true once Done was tapped: the "Completed" pop-up stays until reset.
Definition widgetTour.c:111
#define CLOCK_X
Definition widgetTour.c:62
#define TAG_NONE
Definition widgetTour.c:16
#define GAUGE_X
Definition widgetTour.c:42
#define DIAL_TEXT_X
Definition widgetTour.c:66
#define TYPED_Y
Definition widgetTour.c:55
#define GAUGE_Y
Definition widgetTour.c:43
#define TAG_BUTTON
Definition widgetTour.c:17
#define POPUP_RADIUS
Definition widgetTour.c:71
#define VALUE_X
Definition widgetTour.c:50
#define CLEAR_W
Definition widgetTour.c:57
#define KEYS_X
Definition widgetTour.c:58
#define TAG_SLIDER
Definition widgetTour.c:19
static void drawButton(void)
Draws the "Press me" button and the press count.
Definition widgetTour.c:280
#define TOGGLE_W
Definition widgetTour.c:38
void widgetTourInit(void)
Starts the BT817 and prepares the widget tour.
Definition widgetTour.c:465
#define NAV_Y
Definition widgetTour.c:54
#define KEYS_H
Definition widgetTour.c:61
#define CLEAR_X
Definition widgetTour.c:56
#define TOGGLE_Y
Definition widgetTour.c:37
static char typedDigits[TYPED_MAX+1U]
Digits typed on the keypad (page 2), NUL-terminated.
Definition widgetTour.c:105
static void readTouch(void)
Reads the touch registers and updates the screen state.
Definition widgetTour.c:173
#define BUTTON_W
Definition widgetTour.c:33
void widgetTourRun(void)
Runs the widget tour forever: read touch, redraw, short pause.
Definition widgetTour.c:482
#define POPUP_Y
Definition widgetTour.c:70
WidgetPage
Screen pages.
Definition widgetTour.c:89
@ PAGE_WIDGETS
Definition widgetTour.c:90
@ PAGE_MORE
Definition widgetTour.c:91
#define COUNT_X
Definition widgetTour.c:35
#define TAG_BACK
Definition widgetTour.c:21
#define NAV_W
Definition widgetTour.c:51
#define FONT_WIDGET
Definition widgetTour.c:77
#define HEADER_HEIGHT
Definition widgetTour.c:30
#define SLIDER_W
Definition widgetTour.c:47
static void drawPopup(void)
Draws the final "Completed" pop-up.
Definition widgetTour.c:396
#define POPUP_X
Definition widgetTour.c:69
#define NAV_H
Definition widgetTour.c:52
static void drawToggle(void)
Draws the toggle and the LED it controls.
Definition widgetTour.c:298
static bool toggleOn
Toggle state; true = ON (LED lit).
Definition widgetTour.c:97
#define LED_Y
Definition widgetTour.c:40
#define PROGRESS_Y
Definition widgetTour.c:49
#define KEYS_W
Definition widgetTour.c:60
#define TAG_DIAL
Definition widgetTour.c:23
#define GAUGE_RADIUS
Definition widgetTour.c:44
#define TOGGLE_X
Definition widgetTour.c:36
#define BUTTON_X
Definition widgetTour.c:31
static uint16_t dialValue
Dial position, 0..65535 (one full turn).
Definition widgetTour.c:109
#define KEYS_Y
Definition widgetTour.c:59
#define NAV_X
Definition widgetTour.c:53
#define LED_X
Definition widgetTour.c:39
static uint8_t previousTag
Tag touched in the previous frame, used to detect a new press.
Definition widgetTour.c:101
static void calibrateTouch(void)
Runs the BT817 touch calibration (tap the three dots).
Definition widgetTour.c:135
#define TAG_CLEAR
Definition widgetTour.c:22
static void registerTrackers(void)
Registers the slider and the dial with the touch tracker (CMD_TRACK).
Definition widgetTour.c:156
static uint16_t sliderValue
Slider value, 0..SLIDER_RANGE; also shown by the gauge and progress bar.
Definition widgetTour.c:99
#define POPUP_H
Definition widgetTour.c:68
#define ROUND_RADIUS
Definition widgetTour.c:65
#define TRACKER_FULL
Definition widgetTour.c:83
static void awaitCoPro(const char *step)
Waits for the co-processor to finish the current command list.
Definition widgetTour.c:121
#define FONT_SMALL
Definition widgetTour.c:78
#define TAG_NEXT
Definition widgetTour.c:20
#define SLIDER_H
Definition widgetTour.c:48
#define POPUP_W
Definition widgetTour.c:67
#define SLIDER_Y
Definition widgetTour.c:46
static void drawSliderGroup(void)
Draws the gauge, the slider with its value and the progress bar.
Definition widgetTour.c:326
#define TAG_TOGGLE
Definition widgetTour.c:18
static void drawPageMore(void)
Draws page 2: typed digits with a Clear button, the keypad, an analog clock (time since reset) and a ...
Definition widgetTour.c:350
#define TAG_DONE
Definition widgetTour.c:24
#define BUTTON_H
Definition widgetTour.c:34
#define SLIDER_RANGE
Definition widgetTour.c:81
#define FONT_TITLE
Definition widgetTour.c:76
#define FRAME_DELAY_MS
Definition widgetTour.c:82
#define BUTTON_Y
Definition widgetTour.c:32
static uint8_t typedLength
Number of digits in typedDigits.
Definition widgetTour.c:107
#define SLIDER_X
Definition widgetTour.c:45
static WidgetPage currentPage
Page currently shown.
Definition widgetTour.c:103
#define KEYPAD_KEYS
Definition widgetTour.c:85
#define DIAL_X
Definition widgetTour.c:63
#define ROUND_Y
Definition widgetTour.c:64
#define TYPED_MAX
Definition widgetTour.c:84
static uint32_t pressCount
Number of completed "Press me" presses.
Definition widgetTour.c:95
#define LED_RADIUS
Definition widgetTour.c:41