rc-gateway/libraries/Tiny/TinySensor.h

50 lines
1.3 KiB
C++

#pragma once
#include <RCSwitch.h>
#include "Tiny.h"
class TinySensor {
protected:
short id;
short senderPin;
RCSwitch mySwitch = RCSwitch();
long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA, ADSC)); // measuring
uint16_t result = ADC; // read ADC in one step
result = 1126400L / result; // Calculate Vcc (in mV); 1126400 = 1.1*1024*1000
return result; // Vcc in millivolts
}
void sendInfo(unsigned long value) {
mySwitch.send(value, 32);
}
public:
TinySensor(short id, short senderPin) {
this->id = id;
this->senderPin = senderPin;
}
void setup() {
mySwitch.enableTransmit(senderPin);
mySwitch.setProtocol(2);
}
};