split TinySensor into TinySwitch

This commit is contained in:
Nicu Hodos 2021-12-28 19:02:15 +01:00
parent 542ca3eddf
commit 2b414d3778
7 changed files with 43 additions and 37 deletions

View File

@ -6,8 +6,8 @@ class ContactSensor: public TinySensor {
SensorType sensorType = CONTACT;
public:
ContactSensor(short id, short senderPin) :
TinySensor(id, senderPin) {
ContactSensor(short id) :
TinySensor(id) {
}
void sendStateAndVoltage(bool state) {

View File

@ -6,8 +6,8 @@ class HumiditySensor : public TinySensor {
SensorType sensorType = HUMIDITY;
public:
HumiditySensor(short id, short senderPin) :
TinySensor(id, senderPin) {
HumiditySensor(short id) :
TinySensor(id) {
}
void sendHumidityAndVoltage(int humidity) {

View File

@ -6,8 +6,8 @@ class TempSensor : public TinySensor {
SensorType sensorType = TEMPERATURE;
public:
TempSensor(short id, short senderPin) :
TinySensor(id, senderPin) {
TempSensor(short id) :
TinySensor(id) {
}
void sendTempAndVoltage(int temp) {

View File

@ -1,13 +1,13 @@
#pragma once
#include <RCSwitch.h>
#include "TinySwitch.h"
#include "Tiny.h"
using TinySwitch::sendInfo;
class TinySensor {
protected:
short id;
short senderPin;
RCSwitch mySwitch = RCSwitch();
long readVcc() {
// Read 1.1V reference against AVcc
@ -32,18 +32,8 @@ protected:
return result; // Vcc in millivolts
}
void sendInfo(unsigned long value) {
mySwitch.send(value, 32);
}
public:
TinySensor(short id, short senderPin) {
TinySensor(short id) {
this->id = id;
this->senderPin = senderPin;
}
void setup() {
mySwitch.enableTransmit(senderPin);
mySwitch.setProtocol(2);
}
};

View File

@ -0,0 +1,16 @@
#pragma once
#include <RCSwitch.h>
namespace TinySwitch {
RCSwitch mySwitch = RCSwitch();
void sendInfo(unsigned long value) {
mySwitch.send(value, 32);
}
void setup(short senderPin) {
mySwitch.enableTransmit(senderPin);
mySwitch.setProtocol(2);
}
}

View File

@ -14,26 +14,26 @@ class TinyPower {
public:
static void setup() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
enable_pin_interrupts();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
enable_pin_interrupts();
}
static void sleep(byte pin = PCINT0) {
PCMSK |= _BV(pin); // Use PB0 as interrupt pin
adc_disable();
static void sleep(byte pin = -1) {
if (pin >= 0) PCMSK |= _BV(pin); // Use PB0 as interrupt pin
adc_disable();
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sleep_bod_disable();
sei(); // Enable interrupts
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sleep_bod_disable();
sei(); // Enable interrupts
sleep_cpu(); // sleep
sleep_cpu(); // sleep
sleep_disable(); // Clear SE bit
cli(); // Disable interrupts
PCMSK &= ~_BV(pin); // Turn off PB0 as interrupt pin
adc_enable();
sleep_disable(); // Clear SE bit
cli(); // Disable interrupts
if (pin >= 0) PCMSK &= ~_BV(pin); // Turn off PB0 as interrupt pin
adc_enable();
sei(); // Enable interrupts
sei(); // Enable interrupts
}
static void enableWdt(byte time) {

View File

@ -18,19 +18,19 @@ struct DhtValues {
};
DhtValues readTemp();
TempSensor tempSensor = TempSensor(TEMP_SENSOR, SENDER);
HumiditySensor humidSensor = HumiditySensor(TEMP_SENSOR, SENDER);
TempSensor tempSensor = TempSensor(TEMP_SENSOR);
HumiditySensor humidSensor = HumiditySensor(TEMP_SENSOR);
DHT dht(DHT_PIN, DHT22);
volatile int counter = 0;
void setup() {
tempSensor.setup();
pinMode(TEMP_POSITIVE, OUTPUT);
digitalWrite(TEMP_POSITIVE, HIGH);
dht.begin();
TinySwitch::setup(SENDER);
TinyPower::setup();
TinyPower::enableWdt(WDTO_8S);
}