Added AM2302 sensor reading to one of the screens
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#ifndef AM2302_SENSOR_H
|
||||
#define AM2302_SENSOR_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
struct Am2302Reading
|
||||
{
|
||||
float temperatureC;
|
||||
float humidityPercent;
|
||||
bool valid;
|
||||
};
|
||||
|
||||
class Am2302Sensor
|
||||
{
|
||||
public:
|
||||
explicit Am2302Sensor(uint8_t pin);
|
||||
|
||||
void begin();
|
||||
bool update(unsigned long now);
|
||||
Am2302Reading reading() const;
|
||||
|
||||
private:
|
||||
uint8_t pin_;
|
||||
unsigned long nextRead_;
|
||||
Am2302Reading reading_;
|
||||
};
|
||||
|
||||
#endif
|
||||
+2
-1
@@ -15,6 +15,7 @@ public:
|
||||
Carousel(Screen *screens, size_t screenCount);
|
||||
void draw(U8G2 &u8g2);
|
||||
bool consumeDirty();
|
||||
void requestRedraw();
|
||||
void nextScreen();
|
||||
void update(unsigned long now);
|
||||
|
||||
@@ -26,4 +27,4 @@ private:
|
||||
bool dirty_;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
+4
-1
@@ -3,8 +3,11 @@
|
||||
|
||||
#include <U8g2lib.h>
|
||||
|
||||
#include "am2302_sensor.h"
|
||||
|
||||
void drawScreen1(U8G2 &u8g2);
|
||||
void drawScreen2(U8G2 &u8g2);
|
||||
void setEnvironmentReading(Am2302Reading reading);
|
||||
void drawEnvironmentScreen(U8G2 &u8g2);
|
||||
void drawWeatherIconScreen(U8G2 &u8g2);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
#include "am2302_sensor.h"
|
||||
|
||||
#include <DHT.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
const unsigned long sensorReadInterval = 30000;
|
||||
|
||||
DHT *dht = nullptr;
|
||||
}
|
||||
|
||||
Am2302Sensor::Am2302Sensor(uint8_t pin)
|
||||
: pin_(pin),
|
||||
nextRead_(0),
|
||||
reading_{0.0F, 0.0F, false} {}
|
||||
|
||||
void Am2302Sensor::begin()
|
||||
{
|
||||
static DHT sensor(pin_, DHT22);
|
||||
dht = &sensor;
|
||||
dht->begin();
|
||||
}
|
||||
|
||||
bool Am2302Sensor::update(unsigned long now)
|
||||
{
|
||||
if (now < nextRead_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
nextRead_ = now + sensorReadInterval;
|
||||
const float humidity = dht->readHumidity();
|
||||
const float temperature = dht->readTemperature();
|
||||
|
||||
if (isnan(humidity) || isnan(temperature)) {
|
||||
reading_.valid = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
reading_.temperatureC = temperature;
|
||||
reading_.humidityPercent = humidity;
|
||||
reading_.valid = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
Am2302Reading Am2302Sensor::reading() const
|
||||
{
|
||||
return reading_;
|
||||
}
|
||||
@@ -22,6 +22,11 @@ bool Carousel::consumeDirty()
|
||||
return d;
|
||||
}
|
||||
|
||||
void Carousel::requestRedraw()
|
||||
{
|
||||
dirty_ = true;
|
||||
}
|
||||
|
||||
void Carousel::nextScreen()
|
||||
{
|
||||
currentIndex_ = (currentIndex_ + 1) % screenCount_;
|
||||
|
||||
+10
-2
@@ -2,23 +2,26 @@
|
||||
#include <U8g2lib.h>
|
||||
#include <Wire.h>
|
||||
|
||||
#include "am2302_sensor.h"
|
||||
#include "carousel.h"
|
||||
#include "screens.h"
|
||||
|
||||
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, U8X8_PIN_NONE);
|
||||
|
||||
int lastButtonNextState = LOW;
|
||||
uint8_t buttonNext = A3;
|
||||
uint8_t buttonNext = A4;
|
||||
Am2302Sensor am2302(A3);
|
||||
|
||||
Screen screens[] = {
|
||||
{ drawScreen1 },
|
||||
{ drawScreen2 },
|
||||
{ drawEnvironmentScreen },
|
||||
{ drawWeatherIconScreen }
|
||||
};
|
||||
Carousel carousel(screens, sizeof(screens) / sizeof(screens[0]));
|
||||
|
||||
void setup() {
|
||||
pinMode(buttonNext, INPUT);
|
||||
am2302.begin();
|
||||
|
||||
u8g2.begin();
|
||||
u8g2.clearBuffer();
|
||||
@@ -29,6 +32,11 @@ void loop(){
|
||||
const unsigned long now = millis();
|
||||
carousel.update(now);
|
||||
|
||||
if (am2302.update(now)) {
|
||||
setEnvironmentReading(am2302.reading());
|
||||
carousel.requestRedraw();
|
||||
}
|
||||
|
||||
const int currentButtonState = digitalRead(buttonNext);
|
||||
if (lastButtonNextState == LOW && currentButtonState == HIGH) {
|
||||
carousel.nextScreen();
|
||||
|
||||
+23
-2
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace
|
||||
{
|
||||
Am2302Reading environmentReading = {0.0F, 0.0F, false};
|
||||
|
||||
void drawSun(U8G2 &u8g2, uint8_t x, uint8_t y)
|
||||
{
|
||||
u8g2.drawDisc(x, y, 11);
|
||||
@@ -31,10 +33,29 @@ void drawScreen1(U8G2 &u8g2)
|
||||
u8g2.drawStr(0, 20, "Screen 1");
|
||||
}
|
||||
|
||||
void drawScreen2(U8G2 &u8g2)
|
||||
void setEnvironmentReading(Am2302Reading reading)
|
||||
{
|
||||
environmentReading = reading;
|
||||
}
|
||||
|
||||
void drawEnvironmentScreen(U8G2 &u8g2)
|
||||
{
|
||||
u8g2.setFont(u8g2_font_6x12_tr);
|
||||
u8g2.drawStr(0, 12, "AM2302");
|
||||
|
||||
if (!environmentReading.valid) {
|
||||
u8g2.setFont(u8g2_font_ncenB14_tr);
|
||||
u8g2.drawStr(0, 40, "No data");
|
||||
return;
|
||||
}
|
||||
|
||||
u8g2.setFont(u8g2_font_ncenB14_tr);
|
||||
u8g2.drawStr(0, 40, "Screen 2");
|
||||
u8g2.setCursor(0, 34);
|
||||
u8g2.print(environmentReading.temperatureC, 1);
|
||||
u8g2.print(" C");
|
||||
u8g2.setCursor(0, 60);
|
||||
u8g2.print(environmentReading.humidityPercent, 0);
|
||||
u8g2.print(" %");
|
||||
}
|
||||
|
||||
void drawWeatherIconScreen(U8G2 &u8g2)
|
||||
|
||||
Reference in New Issue
Block a user