|
@@ -42,21 +42,47 @@
|
42
|
42
|
#define TOUCH_TOP 230
|
43
|
43
|
#define TOUCH_BOTTOM 3800
|
44
|
44
|
|
45
|
|
-TS_Point touchToScreen(TS_Point p) {
|
|
45
|
+static SPIClass mySpi = SPIClass(HSPI);
|
|
46
|
+static XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
|
|
47
|
+
|
|
48
|
+static TFT_eSPI tft = TFT_eSPI();
|
|
49
|
+
|
|
50
|
+static TS_Point touchToScreen(TS_Point p) {
|
46
|
51
|
p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
|
47
|
52
|
p.y = map(p.y, TOUCH_TOP, TOUCH_BOTTOM, 0, LCD_HEIGHT);
|
48
|
53
|
return p;
|
49
|
54
|
}
|
50
|
55
|
|
51
|
|
-void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
|
|
56
|
+static void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
|
52
|
57
|
uint32_t duty = (4095 / valueMax) * min(value, valueMax);
|
53
|
58
|
ledcWrite(channel, duty);
|
54
|
59
|
}
|
55
|
60
|
|
56
|
|
-SPIClass mySpi = SPIClass(HSPI);
|
57
|
|
-XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
|
|
61
|
+#define BTN_W 120
|
|
62
|
+#define BTN_H 60
|
|
63
|
+#define BTN_GAP 20
|
|
64
|
+
|
|
65
|
+#define BTNS_OFF_X ((LCD_WIDTH - (2 * BTN_W) - (1 * BTN_GAP)) / 2)
|
|
66
|
+#define BTNS_OFF_Y ((LCD_HEIGHT - (3 * BTN_H) - (2 * BTN_GAP)) / 2)
|
|
67
|
+
|
|
68
|
+static void draw_button(const char *name, uint32_t x, uint32_t y, bool state) {
|
|
69
|
+ tft.fillRect(x - BTN_W / 2, y - BTN_H / 2, BTN_W, BTN_H, state ? TFT_GREEN : TFT_RED);
|
|
70
|
+
|
|
71
|
+ tft.setTextDatum(MC_DATUM); // middle center
|
|
72
|
+ tft.drawString(name, x, y, 2);
|
|
73
|
+}
|
|
74
|
+
|
|
75
|
+static bool lc, lw, lk, sa, lt, lp;
|
|
76
|
+
|
|
77
|
+static void draw_livingroom(void) {
|
|
78
|
+ draw_button("Lights Corner", BTNS_OFF_X + BTN_W / 2, BTNS_OFF_Y + BTN_H / 2, lc);
|
|
79
|
+ draw_button("Lights Workspace", BTNS_OFF_X + BTN_W / 2, BTNS_OFF_Y + BTN_H / 2 + BTN_H + BTN_GAP, lw);
|
|
80
|
+ draw_button("Lights Kitchen", BTNS_OFF_X + BTN_W / 2, BTNS_OFF_Y + BTN_H / 2 + (BTN_H + BTN_GAP) * 2, lk);
|
58
|
81
|
|
59
|
|
-TFT_eSPI tft = TFT_eSPI();
|
|
82
|
+ draw_button("Sound Amp.", BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP, BTNS_OFF_Y + BTN_H / 2, sa);
|
|
83
|
+ draw_button("Lights TV", BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP, BTNS_OFF_Y + BTN_H / 2 + BTN_H + BTN_GAP, lt);
|
|
84
|
+ draw_button("Lights PC", BTNS_OFF_X + BTN_W / 2 + BTN_W + BTN_GAP, BTNS_OFF_Y + BTN_H / 2 + (BTN_H + BTN_GAP) * 2, lp);
|
|
85
|
+}
|
60
|
86
|
|
61
|
87
|
void ui_init(void) {
|
62
|
88
|
mySpi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
|
|
@@ -72,50 +98,36 @@ void ui_init(void) {
|
72
|
98
|
|
73
|
99
|
tft.fillScreen(TFT_BLACK);
|
74
|
100
|
|
75
|
|
- int x = LCD_WIDTH / 2;
|
76
|
|
- int y = LCD_HEIGHT / 2;
|
77
|
|
- int fontSize = 2;
|
78
|
|
- tft.drawCentreString("Initializing ESP-ENV", x, y - 16, fontSize);
|
79
|
|
- tft.drawCentreString("xythobuz.de", x, y + 16, fontSize);
|
|
101
|
+ tft.setTextDatum(MC_DATUM); // middle center
|
|
102
|
+ tft.drawString("Initializing ESP-ENV", LCD_WIDTH / 2, LCD_HEIGHT / 2 - 16, 2);
|
|
103
|
+ tft.drawString("xythobuz.de", LCD_WIDTH / 2, LCD_HEIGHT / 2 + 16, 2);
|
80
|
104
|
}
|
81
|
105
|
|
82
|
|
-void printTouchToDisplay(TS_Point p) {
|
|
106
|
+void ui_draw_menu(void) {
|
83
|
107
|
tft.fillScreen(TFT_BLACK);
|
84
|
|
-
|
85
|
|
- tft.fillRect(0, 0, 100, LCD_HEIGHT, TFT_RED);
|
86
|
|
- tft.fillRect(LCD_WIDTH - 100, 0, 100, LCD_HEIGHT, TFT_GREEN);
|
87
|
|
-
|
88
|
|
- tft.setTextColor(TFT_WHITE, TFT_BLACK);
|
89
|
|
-
|
90
|
|
- int x = LCD_WIDTH / 2;
|
91
|
|
- int y = 100;
|
92
|
|
- int fontSize = 2;
|
93
|
|
-
|
94
|
|
- String temp = "Pressure = " + String(p.z);
|
95
|
|
- tft.drawCentreString(temp, x, y, fontSize);
|
96
|
|
-
|
97
|
|
- y += 16;
|
98
|
|
- temp = "X = " + String(p.x);
|
99
|
|
- tft.drawCentreString(temp, x, y, fontSize);
|
100
|
|
-
|
101
|
|
- y += 16;
|
102
|
|
- temp = "Y = " + String(p.y);
|
103
|
|
- tft.drawCentreString(temp, x, y, fontSize);
|
|
108
|
+ draw_livingroom();
|
104
|
109
|
}
|
105
|
110
|
|
106
|
111
|
void ui_run(void) {
|
107
|
112
|
if (ts.tirqTouched() && ts.touched()) {
|
108
|
113
|
TS_Point p = touchToScreen(ts.getPoint());
|
109
|
|
- printTouchToDisplay(p);
|
110
|
|
-
|
111
|
|
- if (p.x < 100) {
|
112
|
|
- writeMQTTtopic("livingroom/light_kitchen", "off");
|
113
|
|
- tft.drawCentreString("Off", LCD_WIDTH / 2, 100 + 4 * 16, 2);
|
114
|
|
- } else if (p.x > (LCD_WIDTH - 100)) {
|
115
|
|
- writeMQTTtopic("livingroom/light_kitchen", "on");
|
116
|
|
- tft.drawCentreString("On", LCD_WIDTH / 2, 100 + 4 * 16, 2);
|
|
114
|
+
|
|
115
|
+ if ((p.x >= BTNS_OFF_X) && (p.x <= BTNS_OFF_X + BTN_W) && (p.y >= BTNS_OFF_Y) && (p.y <= BTNS_OFF_Y + BTN_H)) {
|
|
116
|
+ lc = !lc;
|
|
117
|
+ } else if ((p.x >= BTNS_OFF_X) && (p.x <= BTNS_OFF_X + BTN_W) && (p.y >= (BTNS_OFF_Y + BTN_H + BTN_GAP)) && (p.y <= (BTNS_OFF_Y + BTN_H + BTN_GAP + BTN_H))) {
|
|
118
|
+ lw = !lw;
|
|
119
|
+ } else if ((p.x >= BTNS_OFF_X) && (p.x <= BTNS_OFF_X + BTN_W) && (p.y >= (BTNS_OFF_Y + BTN_H * 2 + BTN_GAP * 2)) && (p.y <= (BTNS_OFF_Y + BTN_H * 2 + BTN_GAP * 2 + BTN_H))) {
|
|
120
|
+ lk = !lk;
|
|
121
|
+ } else if ((p.x >= BTNS_OFF_X + BTN_W + BTN_GAP) && (p.x <= BTNS_OFF_X + BTN_W + BTN_GAP + BTN_W) && (p.y >= BTNS_OFF_Y) && (p.y <= BTNS_OFF_Y + BTN_H)) {
|
|
122
|
+ sa = !sa;
|
|
123
|
+ } else if ((p.x >= BTNS_OFF_X + BTN_W + BTN_GAP) && (p.x <= BTNS_OFF_X + BTN_W + BTN_GAP + BTN_W) && (p.y >= (BTNS_OFF_Y + BTN_H + BTN_GAP)) && (p.y <= (BTNS_OFF_Y + BTN_H + BTN_GAP + BTN_H))) {
|
|
124
|
+ lt = !lt;
|
|
125
|
+ } else if ((p.x >= BTNS_OFF_X + BTN_W + BTN_GAP) && (p.x <= BTNS_OFF_X + BTN_W + BTN_GAP + BTN_W) && (p.y >= (BTNS_OFF_Y + BTN_H * 2 + BTN_GAP * 2)) && (p.y <= (BTNS_OFF_Y + BTN_H * 2 + BTN_GAP * 2 + BTN_H))) {
|
|
126
|
+ lp = !lp;
|
117
|
127
|
}
|
118
|
128
|
|
|
129
|
+ // TODO
|
|
130
|
+ draw_livingroom();
|
119
|
131
|
delay(100);
|
120
|
132
|
}
|
121
|
133
|
}
|