added photoresistor and light page in carousel, minor display fixes

This commit is contained in:
Dmitriy Shishkov 2022-04-23 12:57:21 +03:00
parent 8e0a071af5
commit 1843d32e05
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060

View File

@ -4,25 +4,52 @@
#define BTN_PORT 7 #define BTN_PORT 7
#define DHT_PORT 8 #define DHT_PORT 8
#define PHOTO_RESISTOR A0
#define CAROUSEL_LENGTH 2 #define READ_INTERVAL 4000
#define CAROUSEL_INTERVAL 2000
#define CAROUSEL_LENGTH 3
DHT dht(DHT_PORT, DHT11); DHT dht(DHT_PORT, DHT11);
GyverOLED<SSD1306_128x64, OLED_BUFFER> oled; GyverOLED<SSD1306_128x64, OLED_BUFFER> oled;
OneButton button(BTN_PORT, true, true); OneButton button(BTN_PORT, true, true);
const unsigned char bmp_moon [] PROGMEM = {
0x00, 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfc, 0xfe, 0xfe, 0xff, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xf0, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xf0, 0xc0, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xfe, 0xfc, 0xfc, 0xf8, 0xf8, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0x70, 0x18,
0x00, 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x3f, 0x7f, 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x7f, 0x7f, 0x7f, 0x3f, 0x3f, 0x1f, 0x0f, 0x0f, 0x03, 0x01, 0x00, 0x00
};
const unsigned char bmp_sun [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x20, 0x70, 0xe0, 0xc0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f,
0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xc0, 0xe0, 0x70, 0x20, 0x00, 0x00, 0x00, 0x00,
0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x01, 0xe0, 0xf8, 0xfc, 0x1e, 0x0e, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x0e, 0x1e, 0xfc, 0xf8, 0xe0, 0x01, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80,
0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x80, 0x07, 0x1f, 0x3f, 0x78, 0x70, 0xe0, 0xe0, 0xe0,
0xe0, 0xe0, 0xe0, 0x70, 0x78, 0x3f, 0x1f, 0x07, 0x80, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,
0x00, 0x00, 0x00, 0x00, 0x04, 0x0e, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8,
0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0e, 0x04, 0x00, 0x00, 0x00, 0x00
};
float h = -1; float h = -1;
float c = -1; float c = -1;
float f = -1; float f = -1;
float l = -1;
unsigned long timer_read = 0; unsigned long timer_read = 0;
unsigned long timer_print = 0; unsigned long timer_carousel = 0;
int prev = CAROUSEL_LENGTH; byte prev = CAROUSEL_LENGTH;
bool show_carousel = true; bool show_carousel = true;
bool temp_far = false; bool temp_far = false;
void with_header(String title, void (*cb)()) { void with_header(const String& title, void (*cb)()) {
oled.rect(0, 0, 128, 11, 1); oled.rect(0, 0, 128, 11, 1);
oled.setCursorXY(2, 2); oled.setCursorXY(2, 2);
@ -31,11 +58,12 @@ void with_header(String title, void (*cb)()) {
oled.print(title); oled.print(title);
oled.textMode(BUF_REPLACE); oled.textMode(BUF_REPLACE);
oled.setCursorXY(0, 20); oled.setCursorXY(2, 20);
cb(); cb();
} }
void with_scaleup(int factor, void (*cb)()) { void with_scaleup(int factor, void (*cb)()) {
oled.setCursorXY(2, 29);
oled.setScale(factor); oled.setScale(factor);
cb(); cb();
oled.setScale(1); oled.setScale(1);
@ -50,6 +78,7 @@ void print_hum() {
oled.roundRect(107, 20, 127, 63, 1); oled.roundRect(107, 20, 127, 63, 1);
oled.roundRect(109, 22, 125, 61, 0); oled.roundRect(109, 22, 125, 61, 0);
if (h > 0)
oled.roundRect(110, 60 - (60 - 23)*(h / 100), 124, 60, 1); oled.roundRect(110, 60 - (60 - 23)*(h / 100), 124, 60, 1);
} }
@ -60,31 +89,49 @@ void print_temp() {
oled.print(fallback(c, 1) + "C"); oled.print(fallback(c, 1) + "C");
} }
void print_light() {
bool is_night = l > 450;
String str = F("It's ");
if (is_night)
str += F("night");
else
str += F("day");
oled.setCursorXY((127 - str.length()*6)/2, 55);
oled.println(str);
oled.drawBitmap(48, 17, is_night ? bmp_moon : bmp_sun, 32, 32, BITMAP_NORMAL, BUF_ADD);
}
void print_settings() { void print_settings() {
oled.invertText(temp_far); oled.invertText(temp_far);
oled.print("Fahrenheit"); oled.print(F("Fahrenheit"));
oled.invertText(false); oled.invertText(false);
oled.print("/"); oled.print("/");
oled.invertText(!temp_far); oled.invertText(!temp_far);
oled.print("Celsius"); oled.print(F("Celsius"));
oled.invertText(false); oled.invertText(false);
} }
void display(int screen) { void display(byte screen) {
oled.clear(); oled.clear();
oled.home(); oled.home();
switch(screen) { switch(screen) {
case 0: case 0:
with_header("Humidity", []() { with_scaleup(3, &print_hum); }); with_header(F("Humidity"), []() { with_scaleup(3, &print_hum); });
break; break;
case 1: case 1:
with_header("Temperature", []() { with_scaleup(3, &print_temp); }); with_header(F("Temperature"), []() { with_scaleup(3, &print_temp); });
break;
case 2:
with_header(F("Lightness"), &print_light);
break; break;
case CAROUSEL_LENGTH: case CAROUSEL_LENGTH:
with_header("Settings", &print_settings); with_header(F("Settings"), &print_settings);
break; break;
} }
@ -92,9 +139,8 @@ void display(int screen) {
} }
void show_next() { void show_next() {
timer_print = millis(); timer_carousel = millis();
prev = (prev + 1) % CAROUSEL_LENGTH; prev = (prev + 1) % CAROUSEL_LENGTH;
Serial.println("Next is " + String(prev));
display(prev); display(prev);
} }
@ -109,6 +155,8 @@ void read_data() {
h = dht.readHumidity(); h = dht.readHumidity();
c = dht.readTemperature(); c = dht.readTemperature();
f = dht.readTemperature(true); f = dht.readTemperature(true);
l = analogRead(PHOTO_RESISTOR);
} }
void handle_click() { void handle_click() {
@ -146,7 +194,7 @@ void setup() {
button.attachLongPressStart(&handle_lpress); button.attachLongPressStart(&handle_lpress);
timer_read = millis(); timer_read = millis();
timer_print = millis(); timer_carousel = millis();
display(0); display(0);
} }
@ -156,6 +204,6 @@ void loop() {
oled.clear(); oled.clear();
every(4000, timer_read, &read_data); every(READ_INTERVAL, timer_read, &read_data);
every(2000, timer_print, []() { if (show_carousel) show_next(); }); every(CAROUSEL_INTERVAL, timer_carousel, []() { if (show_carousel) show_next(); });
} }