Nathanaël Semhoun / Mbed OS mdot_commonsense

Dependencies:   libmDot-mbed5

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Sensors.cpp Source File

Sensors.cpp

00001 #include "Sensors.h"
00002 #include "Logging.h"
00003 
00004 Sensors::Sensors(SensorsConfig_t p_config)
00005     : m_sDht(p_config.dht, DHT11),
00006       m_sMotion(p_config.motion),
00007       m_sLight(p_config.light),
00008       m_sSwitch1(p_config.switch1),
00009       m_sSwitch2(p_config.switch2),
00010       m_updatePeriod_s(p_config.UpdatePeriod_s),
00011       m_nextUpdate(time(NULL))
00012 {
00013     m_switch1 = true;
00014     m_sSwitch1 = 1;
00015     m_switch2 = true;
00016     m_sSwitch2 = 1;
00017 
00018     updateValues();
00019 }
00020 
00021 Sensors::~Sensors()
00022 {
00023 }
00024 
00025 void Sensors::updateValues()
00026 {
00027     if (m_nextUpdate > time(NULL)) {
00028         return; // <==
00029     }
00030     
00031     int err = m_sDht.readData();
00032     if (err != 0) {
00033         LOG_DEBUG("Can't read DHT %d", err);
00034     } else {
00035         m_temp = m_sDht.ReadTemperature(CELCIUS);
00036         m_humidity = m_sDht.ReadHumidity();
00037         LOG_DEBUG("Temperature is %4.2f C", m_temp);
00038         LOG_DEBUG("Humidity is %4.2f", m_humidity);
00039     }
00040     int val;
00041 
00042     val = m_sMotion;
00043     m_motion = (val != 0);
00044     LOG_DEBUG("Motion is %s", val != 0 ? "Triggered" : "Idle");
00045 
00046     m_light = m_sLight.read() * 3.3f;
00047     LOG_DEBUG("Light is %4.2f", m_light);
00048     
00049     m_nextUpdate = time(NULL) + m_updatePeriod_s;
00050 }
00051 
00052 float Sensors::getTemp()
00053 {
00054     return m_temp;
00055 }
00056 
00057 float Sensors::getHumidity()
00058 {
00059     return m_humidity;
00060 }
00061 
00062 bool Sensors::getMotion()
00063 {
00064     return m_motion;
00065 }
00066 
00067 float Sensors::getLight()
00068 {
00069     return m_light;
00070 }
00071 
00072 bool Sensors::getSwitch1()
00073 {
00074     return m_switch1;
00075 }
00076 
00077 bool Sensors::getSwitch2()
00078 {
00079     return m_switch2;
00080 }
00081 
00082 void Sensors::setSwitch1(bool value)
00083 {
00084     m_switch1 = value;
00085     m_sSwitch1 = (value ? 1 : 0);
00086 }
00087 
00088 void Sensors::setSwitch2(bool value)
00089 {
00090     m_switch2 = value;
00091     m_sSwitch2 = (value ? 1 : 0);
00092 }