Files
RGBController/WirelessRelay.cpp
2017-11-05 17:41:40 +01:00

101 lines
1.9 KiB
C++

#include"WirelessRelay.h"
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) ? true : false );
}
}
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()
{
for(short z = 0; z<10; z++)
{
sendId();
sendBit(true);
sendBit(false);
sync();
}
_state = true;
}
void WirelessRelay::off()
{
for(short z = 0; z<10; z++)
{
sendId();
sendBit(false);
sendBit(true);
sync();
}
_state = false;
}
uint16_t WirelessRelay::getId()
{
return _id;
}
bool WirelessRelay::getExpectedState()
{
return _state;
}
void WirelessRelay::init( volatile unsigned char *port, const unsigned char pin, const uint16_t id)
{
_port=port;
_id=id;
_pin=pin;
}
WirelessRelay::WirelessRelay( volatile unsigned char *port, const unsigned char pin, const uint16_t id)
{
init( port, pin, id);
}
WirelessRelay::WirelessRelay(){}