#include"WirelessRelay.h" #include #include volatile unsigned char *_port = &PORTB; unsigned char _pin = PB5; void WirelessRelay::sendId() { writePin(_port,_pin,true); _delay_us(SMALL_TIME); writePin(_port,_pin,false); _delay_us(LARGE_TIME); for(short i = 0; i<10; i++) { sendBit( _id & 1 << (15 - i) ); } } void WirelessRelay::sendBit(const bool in) { switch(in) { case true: //Der Code fuer '0' writePin(_port,_pin,true); _delay_us(SMALL_TIME); writePin(_port,_pin,false); _delay_us(LARGE_TIME); writePin(_port,_pin,true); _delay_us(SMALL_TIME); writePin(_port,_pin,false); _delay_us(LARGE_TIME); break; case false: //Der Code fuer '1' writePin(_port,_pin,true); _delay_us(LARGE_TIME); writePin(_port,_pin,false); _delay_us(SMALL_TIME); writePin(_port,_pin,true); _delay_us(SMALL_TIME); writePin(_port,_pin,false); _delay_us(LARGE_TIME); break; } } void WirelessRelay::sync() { writePin(_port,_pin,false); _delay_us(SMALL_TIME*31); } void WirelessRelay::on() { _state = true; for(short z = 0; z<10; z++) { sendId(); sendBit(true); sendBit(false); sync(); } } void WirelessRelay::off() { _state = false; for(short z = 0; z<10; z++) { sendId(); sendBit(false); sendBit(true); sync(); } } uint16_t WirelessRelay::getId() { return _id; } bool WirelessRelay::getExpectedState() { return _state; } char* WirelessRelay::getName() { return _name; } void WirelessRelay::setName(char name[]) { memcpy(_name, name, strlen(name)+1); } void WirelessRelay::init( const uint16_t id, char nameIn[]) { setName(nameIn); _id=id; } void WirelessRelay::resend() { _state ? on() : off(); } WirelessRelay::WirelessRelay(const uint16_t id, char nameIn[]) { init(id, nameIn); } WirelessRelay::WirelessRelay(){}