103 lines
1.8 KiB
C++
103 lines
1.8 KiB
C++
#include"WirelessRelay.h"
|
|
#include <avr/io.h>
|
|
#include "eeprom.h"
|
|
|
|
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) ? 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( const uint16_t id)
|
|
{
|
|
_id=id;
|
|
}
|
|
|
|
WirelessRelay::WirelessRelay(const uint16_t id)
|
|
{
|
|
init(id);
|
|
}
|
|
|
|
WirelessRelay::WirelessRelay(){}
|