1 /*
2  * Copyright (c) 2021 Hubert Miś <hubert.mis@gmail.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <zephyr/kernel.h>
9 
10 #include <zephyr/drivers/misc/ft8xx/ft8xx.h>
11 #include <zephyr/drivers/misc/ft8xx/ft8xx_copro.h>
12 #include <zephyr/drivers/misc/ft8xx/ft8xx_dl.h>
13 
14 /**
15  * @file Display a counter using FT800.
16  */
17 
18 /* sleep time in msec */
19 #define SLEEPTIME  100
20 
21 /* touch tags */
22 #define TAG_PLUS 1
23 #define TAG_MINUS 2
24 
25 static volatile bool process_touch;
26 
touch_irq(const struct device * dev,void * user_data)27 static void touch_irq(const struct device *dev, void *user_data)
28 {
29 	(void)dev;
30 	(void)user_data;
31 
32 	process_touch = true;
33 }
34 
main(void)35 int main(void)
36 {
37 	const struct device *ft8xx = DEVICE_DT_GET_ONE(ftdi_ft800);
38 	int cnt;
39 	int val;
40 	struct ft8xx_touch_transform tt;
41 
42 	if (!device_is_ready(ft8xx)) {
43 		printk("FT8xx device is not ready. Aborting\n");
44 		return -1;
45 	}
46 
47 	/* To get touch events calibrate the display */
48 	ft8xx_calibrate(ft8xx, &tt);
49 
50 	/* Get interrupts on touch event */
51 	ft8xx_register_int(ft8xx, touch_irq, NULL);
52 
53 	/* Starting counting */
54 	val = 0;
55 	cnt = 0;
56 
57 	while (1) {
58 		/* Start Display List */
59 		ft8xx_copro_cmd_dlstart(ft8xx);
60 		ft8xx_copro_cmd(ft8xx, FT8XX_CLEAR_COLOR_RGB(0x00, 0x00, 0x00));
61 		ft8xx_copro_cmd(ft8xx, FT8XX_CLEAR(1, 1, 1));
62 
63 		/* Set color */
64 		ft8xx_copro_cmd(ft8xx, FT8XX_COLOR_RGB(0xf0, 0xf0, 0xf0));
65 
66 		/* Display the counter */
67 		ft8xx_copro_cmd_number(ft8xx, 20, 20, 29, FT8XX_OPT_SIGNED, cnt);
68 		cnt++;
69 
70 		/* Display the hello message */
71 		ft8xx_copro_cmd_text(ft8xx, 20, 70, 30, 0, "Hello,");
72 		/* Set Zephyr color */
73 		ft8xx_copro_cmd(ft8xx, FT8XX_COLOR_RGB(0x78, 0x29, 0xd2));
74 		ft8xx_copro_cmd_text(ft8xx, 20, 105, 30, 0, "Zephyr!");
75 
76 		/* Display value set with buttons */
77 		ft8xx_copro_cmd(ft8xx, FT8XX_COLOR_RGB(0xff, 0xff, 0xff));
78 		ft8xx_copro_cmd_number(ft8xx, 80, 170, 29,
79 					FT8XX_OPT_SIGNED | FT8XX_OPT_RIGHTX,
80 					val);
81 		ft8xx_copro_cmd(ft8xx, FT8XX_TAG(TAG_PLUS));
82 		ft8xx_copro_cmd_text(ft8xx, 90, 160, 31, 0, "+");
83 		ft8xx_copro_cmd(ft8xx, FT8XX_TAG(TAG_MINUS));
84 		ft8xx_copro_cmd_text(ft8xx, 20, 160, 31, 0, "-");
85 
86 		/* Finish Display List */
87 		ft8xx_copro_cmd(ft8xx, FT8XX_DISPLAY());
88 		/* Display created frame */
89 		ft8xx_copro_cmd_swap(ft8xx);
90 
91 		if (process_touch) {
92 			int tag = ft8xx_get_touch_tag(ft8xx);
93 
94 			if (tag == TAG_PLUS) {
95 				val++;
96 			} else if (tag == TAG_MINUS) {
97 				val--;
98 			}
99 
100 			process_touch = false;
101 		}
102 
103 		/* Wait a while */
104 		k_msleep(SLEEPTIME);
105 	}
106 	return 0;
107 }
108