split TinySensor into TinySwitch

This commit is contained in:
Nicu Hodos 2021-12-28 19:02:15 +01:00
parent ca2b003f5d
commit 8dd23aeb07
7 changed files with 42 additions and 36 deletions

View File

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

View File

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

View File

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

View File

@ -1,13 +1,13 @@
#pragma once #pragma once
#include <RCSwitch.h> #include "TinySwitch.h"
#include "Tiny.h" #include "Tiny.h"
using TinySwitch::sendInfo;
class TinySensor { class TinySensor {
protected: protected:
short id; short id;
short senderPin;
RCSwitch mySwitch = RCSwitch();
long readVcc() { long readVcc() {
// Read 1.1V reference against AVcc // Read 1.1V reference against AVcc
@ -32,18 +32,8 @@ protected:
return result; // Vcc in millivolts return result; // Vcc in millivolts
} }
void sendInfo(unsigned long value) {
mySwitch.send(value, 32);
}
public: public:
TinySensor(short id, short senderPin) { TinySensor(short id) {
this->id = 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: public:
static void setup() { static void setup() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN); set_sleep_mode(SLEEP_MODE_PWR_DOWN);
enable_pin_interrupts(); enable_pin_interrupts();
} }
static void sleep(byte pin = PCINT0) { static void sleep(byte pin = -1) {
PCMSK |= _BV(pin); // Use PB0 as interrupt pin if (pin >= 0) PCMSK |= _BV(pin); // Use PB0 as interrupt pin
adc_disable(); adc_disable();
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT) sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sleep_bod_disable(); sleep_bod_disable();
sei(); // Enable interrupts sei(); // Enable interrupts
sleep_cpu(); // sleep sleep_cpu(); // sleep
sleep_disable(); // Clear SE bit sleep_disable(); // Clear SE bit
cli(); // Disable interrupts cli(); // Disable interrupts
PCMSK &= ~_BV(pin); // Turn off PB0 as interrupt pin if (pin >= 0) PCMSK &= ~_BV(pin); // Turn off PB0 as interrupt pin
adc_enable(); adc_enable();
sei(); // Enable interrupts sei(); // Enable interrupts
} }
static void enableWdt(byte time) { static void enableWdt(byte time) {

View File

@ -20,7 +20,7 @@ SoftwareSerial AttinySerial(RxD, TxD);
int readTemp(); int readTemp();
TempSensor sensor = TempSensor(TEMP_SENSOR, SENDER); TempSensor sensor = TempSensor(TEMP_SENSOR);
volatile int counter = 0; volatile int counter = 0;
@ -30,11 +30,11 @@ void setup() {
AttinySerial.begin(9600); AttinySerial.begin(9600);
AttinySerial.println("starting..."); AttinySerial.println("starting...");
#endif #endif
sensor.setup();
analogReference(INTERNAL); analogReference(INTERNAL);
pinMode(TEMP_POSITIVE, OUTPUT); pinMode(TEMP_POSITIVE, OUTPUT);
digitalWrite(TEMP_POSITIVE, LOW); digitalWrite(TEMP_POSITIVE, LOW);
TinySwitch::setup(SENDER);
TinyPower::setup(); TinyPower::setup();
TinyPower::enableWdt(WDTO_8S); TinyPower::enableWdt(WDTO_8S);
} }