use one Tiny librariy for all sensors

This commit is contained in:
Nicu Hodos 2017-04-01 10:26:39 +02:00
parent 023859489c
commit 863e8aec99
6 changed files with 27 additions and 22 deletions

View File

@ -1,4 +1,5 @@
#include <TinyPower.h>
#include <Tiny.h>
#include <RCSwitch.h>
#include <SoftwareSerial_Tiny.h>

View File

@ -2,13 +2,13 @@
#include <RCSwitch.h>
class TinySensor {
class ContactSensor {
short id;
short senderPin;
RCSwitch mySwitch = RCSwitch();
public:
TinySensor(short id, short senderPin) {
ContactSensor(short id, short senderPin) {
this->id = id;
this->senderPin = senderPin;
}
@ -18,7 +18,7 @@ public:
mySwitch.setProtocol(2);
}
void sendWindowState(bool state) {
void sendStateAndVoltage(bool state) {
unsigned long value = 0x70000000;
value |= readVcc() << 6;
value |= !state << 5;

4
libraries/Tiny/Tiny.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#include <ContactSensor.h>

View File

@ -1,5 +1,5 @@
#include <TinyPower.h>
#include <TinySensor.h>
#include <Tiny.h>
// Pins
#define SWITCH 0
@ -8,7 +8,7 @@
#define SENSOR_ID 3
TinySensor sensor = TinySensor(SENSOR_ID, SENDER);
ContactSensor sensor = ContactSensor(SENSOR_ID, SENDER);
int counter = 0;
@ -19,7 +19,7 @@ void setup() {
digitalWrite(CONTROLLER, HIGH);
sensor.setup();
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
TinyPower::setup();
TinyPower::enableWdt(WDTO_8S);
@ -37,17 +37,17 @@ bool readState() {
}
ISR(PCINT0_vect) {
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
delay(5000);
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
}
ISR(WDT_vect) {
counter++;
if (counter % 220 == 0) {
counter = 0;
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
delay(10000);
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
}
}

View File

@ -1,5 +1,5 @@
#include <TinyPower.h>
#include <TinySensor.h>
#include <Tiny.h>
// Pins
#define SWITCH 0
@ -7,7 +7,7 @@
#define CONTROLLER 4
#define SENSOR_ID 1
TinySensor sensor = TinySensor(SENSOR_ID, SENDER);
ContactSensor sensor = ContactSensor(SENSOR_ID, SENDER);
int counter = 0;
bool currentState;
@ -21,7 +21,7 @@ void setup() {
sensor.setup();
updateState();
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
TinyPower::setup();
TinyPower::enableWdt(WDTO_8S);
@ -40,19 +40,19 @@ byte readState() {
}
ISR(PCINT0_vect) {
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
}
ISR(WDT_vect) {
bool state = readState();
if (state != currentState) {
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
updateState();
return;
}
counter++;
if (counter % 76 == 0) {
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
counter = 0;
}
}

View File

@ -1,12 +1,12 @@
#include <TinyPower.h>
#include <TinySensor.h>
#include <Tiny.h>
// Pins
#define SWITCH 0
#define SENDER 2
#define SENSOR_ID 2
TinySensor sensor = TinySensor(SENSOR_ID, SENDER);
ContactSensor sensor = ContactSensor(SENSOR_ID, SENDER);
int counter = 0;
bool currentState;
@ -18,7 +18,7 @@ void setup() {
sensor.setup();
updateState();
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
TinyPower::setup();
TinyPower::enableWdt(WDTO_8S);
@ -37,19 +37,19 @@ byte readState() {
}
ISR(PCINT0_vect) {
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
}
ISR(WDT_vect) {
bool state = readState();
if (state != currentState) {
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
updateState();
return;
}
counter++;
if (counter % 76 == 0) {
sensor.sendWindowState(readState());
sensor.sendStateAndVoltage(readState());
counter = 0;
}
}