浏览代码

try to read ldr, but not working for some reason

Thomas Buck 5 个月前
父节点
当前提交
c3b08424e9
共有 4 个文件被更改,包括 80 次插入9 次删除
  1. 8
    0
      include/config.h
  2. 4
    7
      src/html.cpp
  3. 1
    1
      src/sensors.cpp
  4. 67
    1
      src/ui.cpp

+ 8
- 0
include/config.h 查看文件

@@ -93,4 +93,12 @@
93 93
 #define BUILTIN_LED_PIN 13
94 94
 #endif
95 95
 
96
+#if defined(ARDUINO_ARCH_ESP8266)
97
+#define ESP_PLATFORM_NAME "ESP8266"
98
+#elif defined(ARDUINO_ARCH_ESP32)
99
+#define ESP_PLATFORM_NAME "ESP32"
100
+#elif defined(ARDUINO_ARCH_AVR)
101
+#define ESP_PLATFORM_NAME "Uno WiFi"
102
+#endif
103
+
96 104
 #endif // __ESP_ENV_CONFIG__

+ 4
- 7
src/html.cpp 查看文件

@@ -18,20 +18,17 @@
18 18
 #include <ESP8266WiFi.h>
19 19
 #include <ESP8266WebServer.h>
20 20
 #include <ESP8266mDNS.h>
21
-#define ESP_PLATFORM_NAME "ESP8266"
22 21
 
23 22
 #elif defined(ARDUINO_ARCH_ESP32)
24 23
 
25 24
 #include <WiFi.h>
26 25
 #include <WebServer.h>
27 26
 #include <ESPmDNS.h>
28
-#define ESP_PLATFORM_NAME "ESP32"
29 27
 
30 28
 #elif defined(ARDUINO_ARCH_AVR)
31 29
 
32 30
 #include <UnoWiFiDevEdSerial1.h>
33 31
 #include <WiFiLink.h>
34
-#define ESP_PLATFORM_NAME "Uno WiFi"
35 32
 
36 33
 #endif
37 34
 
@@ -128,7 +125,7 @@ void handlePage(WiFiClient &client, int mode, int id) {
128 125
     ARDUINO_SEND_PARTIAL_PAGE();
129 126
 
130 127
 #if defined(ARDUINO_ARCH_ESP8266)
131
-    
128
+
132 129
     message += F("<p>");
133 130
     message += F("Reset reason: ");
134 131
     message += ESP.getResetReason();
@@ -151,9 +148,9 @@ void handlePage(WiFiClient &client, int mode, int id) {
151 148
         message += (ESP.getFlashChipSize());
152 149
         message += F(") does not match!");
153 150
     }
154
-    
151
+
155 152
     message += F("</p>");
156
-    
153
+
157 154
 #elif defined(ARDUINO_ARCH_ESP32)
158 155
 
159 156
     message += F("<p>");
@@ -166,7 +163,7 @@ void handlePage(WiFiClient &client, int mode, int id) {
166 163
     message += F("Flash chip size: ");
167 164
     message += String(ESP.getFlashChipSize() / 1024.0);
168 165
     message += F("k</p><hr>");
169
-    
166
+
170 167
 #endif
171 168
 
172 169
 #ifdef ENABLE_BME280

+ 1
- 1
src/sensors.cpp 查看文件

@@ -271,7 +271,7 @@ void initSensors() {
271 271
 
272 272
 void runSensors() {
273 273
     unsigned long time = millis();
274
-    
274
+
275 275
     if ((time - last_sensor_handle_time) >= SENSOR_HANDLE_INTERVAL) {
276 276
         last_sensor_handle_time = time;
277 277
 

+ 67
- 1
src/ui.cpp 查看文件

@@ -36,6 +36,8 @@
36 36
 #define LEDC_TIMER_12_BIT 12
37 37
 #define LEDC_BASE_FREQ 5000
38 38
 
39
+#define LDR_PIN 34
40
+
39 41
 #define TOUCH_LEFT 180
40 42
 #define TOUCH_RIGHT 3750
41 43
 
@@ -51,6 +53,8 @@
51 53
 
52 54
 #define INVERT_BOOL(x) (x) = !(x);
53 55
 
56
+#define LDR_CHECK_MS 1000
57
+
54 58
 static SPIClass mySpi = SPIClass(HSPI);
55 59
 static XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ);
56 60
 static TFT_eSPI tft = TFT_eSPI();
@@ -62,12 +66,15 @@ enum ui_pages {
62 66
     UI_LIVINGROOM1,
63 67
     UI_LIVINGROOM2,
64 68
     UI_BATHROOM,
69
+    UI_INFO,
65 70
 
66 71
     UI_NUM_PAGES
67 72
 };
68 73
 
69 74
 static enum ui_pages ui_page = UI_START;
70 75
 static bool is_touched = false;
76
+static unsigned long last_ldr = 0;
77
+static int ldr_value = 0;
71 78
 
72 79
 static TS_Point touchToScreen(TS_Point p) {
73 80
     p.x = map(p.x, TOUCH_LEFT, TOUCH_RIGHT, 0, LCD_WIDTH);
@@ -171,6 +178,22 @@ static void draw_bathroom(void) {
171 178
                 ui_status.bathroom_lights == BATH_LIGHT_SMALL ? TFT_GREEN : TFT_RED);
172 179
 }
173 180
 
181
+static void draw_info(void) {
182
+    tft.fillScreen(TFT_BLACK);
183
+
184
+    tft.setTextDatum(TC_DATUM); // top center
185
+    tft.drawString(ESP_PLATFORM_NAME " " NAME_OF_FEATURE " V" ESP_ENV_VERSION, LCD_WIDTH / 2, 0, 2);
186
+    tft.drawString("by xythobuz.de", LCD_WIDTH / 2, 16, 2);
187
+
188
+    tft.setTextDatum(TL_DATUM); // top left
189
+    tft.drawString("Build Date: " __DATE__, 0, 40 + 16 * 0, 1);
190
+    tft.drawString("Build Time: " __TIME__, 0, 40 + 16 * 1, 1);
191
+    tft.drawString("Location: " SENSOR_LOCATION, 0, 40 + 16 * 2, 1);
192
+    tft.drawString("ID: " SENSOR_ID, 0, 40 + 16 * 3, 1);
193
+
194
+    tft.drawString("LDR: " + String(ldr_value), 0, 40 + 16 * 5, 1);
195
+}
196
+
174 197
 void ui_init(void) {
175 198
     mySpi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
176 199
     ts.begin(mySpi);
@@ -183,6 +206,12 @@ void ui_init(void) {
183 206
     ledcAttachPin(TFT_BL, LEDC_CHANNEL_0);
184 207
     ledcAnalogWrite(LEDC_CHANNEL_0, 255);
185 208
 
209
+    pinMode(LDR_PIN, ANALOG);
210
+    analogSetAttenuation(ADC_0db);
211
+    analogReadResolution(12);
212
+    analogSetPinAttenuation(LDR_PIN, ADC_0db);
213
+    ldr_value = analogRead(LDR_PIN);
214
+
186 215
     ui_progress(UI_INIT);
187 216
 }
188 217
 
@@ -205,6 +234,10 @@ static void ui_draw_menu(void) {
205 234
             draw_bathroom();
206 235
             break;
207 236
 
237
+        case UI_INFO:
238
+            draw_info();
239
+            return; // no next button
240
+
208 241
         default:
209 242
             ui_page = UI_START;
210 243
             ui_draw_menu();
@@ -260,11 +293,44 @@ void ui_progress(enum ui_state state) {
260 293
 }
261 294
 
262 295
 void ui_run(void) {
296
+    unsigned long now = millis();
297
+    if (now >= (last_ldr + LDR_CHECK_MS)) {
298
+        last_ldr = now;
299
+        int ldr = analogRead(LDR_PIN);
300
+        //ldr_value = (ldr_value * 0.9f) + (ldr * 0.1f);
301
+        ldr_value = ldr;
302
+        if (ui_page == UI_INFO) {
303
+            ui_draw_menu();
304
+        }
305
+    }
306
+
263 307
     bool touched = ts.tirqTouched() && ts.touched();
308
+    TS_Point p;
309
+
310
+    if (touched) {
311
+        p = touchToScreen(ts.getPoint());
312
+
313
+        // minimum pressure
314
+        if (p.z < 100) {
315
+            touched = false;
316
+        }
317
+    }
264 318
 
265 319
     if (touched && (!is_touched)) {
266 320
         is_touched = true;
267
-        TS_Point p = touchToScreen(ts.getPoint());
321
+
322
+        if (ui_page == UI_INFO) {
323
+            // switch to next page
324
+            ui_page = (enum ui_pages)((ui_page + 1) % UI_NUM_PAGES);
325
+            if (ui_page == UI_START) {
326
+                // skip init screen
327
+                ui_page = (enum ui_pages)((ui_page + 1) % UI_NUM_PAGES);
328
+            }
329
+            tft.fillScreen(TFT_BLACK);
330
+
331
+            ui_draw_menu();
332
+            return;
333
+        }
268 334
 
269 335
         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)) {
270 336
             // 1

正在加载...
取消
保存