Compare commits

...

No commits in common. '48d1fc0bb060f6165a63e8077b4a7467d4564968' and '6ebc9faa1ed5d9f6d847ceb118c22b9a6ce6536c' have entirely different histories.

212
hbw.c

@ -26,13 +26,20 @@ uint8_t sesc;
uint32_t saddress;
uint8_t slen;
uint8_t smessage[62];
uint8_t slocked;
uint8_t sbusy;
uint16_t sdelay;
volatile uint16_t sdelay;
uint8_t waitack;
uint32_t aaddress;
uint8_t actl;
uint8_t alen;
uint8_t alink;
uint8_t amessage[62];
uint8_t asent;
uint8_t dsent;
uint8_t gotack;
uint8_t configled;
@ -60,7 +67,6 @@ static uint16_t crc16shift(uint16_t crc, uint8_t data)
static void readaddr(void)
{
// my_address=eeprom_read_dword((const uint32_t *)EESIZE-4);
((uint8_t*)&my_address)[0]=eeprom_read_byte((const uint8_t *)EESIZE-4);
((uint8_t*)&my_address)[1]=eeprom_read_byte((const uint8_t *)EESIZE-3);
((uint8_t*)&my_address)[2]=eeprom_read_byte((const uint8_t *)EESIZE-2);
@ -197,16 +203,18 @@ static uint8_t senddsc(void)
UDR0 = 0xFE;
sind = 1;
sesc = 0;
dsent = 1;
sdisc = 1;
sdelay = 1000 + (rand() & 0x1F);
return ~0;
}
static uint8_t is_bus_free(void)
{
if(sdelay - hbw_timer > 0x7FFF)
return 1;
return 0;
if(sdelay || slocked)
return 0;
return 1;
}
static void announce(void)
@ -233,9 +241,9 @@ static void announce(void)
smessage[13] = HBWSERIAL7;
smessage[14] = HBWSERIAL8;
smessage[15] = HBWSERIAL9;
sendmsg();
asent = 1;
if (sendmsg())
asent = 1;
}
void hbw_init(void)
@ -261,30 +269,104 @@ void hbw_init(void)
UCSR0B = (1 << TXEN0) | (1 << RXEN0) | (1 << RXCIE0);
UCSR0C = (1 << UPM01) | (1 << UCSZ01) | (1 << UCSZ00);
OCR2A = 0xF9;
OCR2A = 249;
TIMSK2 |= (1 << OCIE2A);
TCCR2A |= (1 << WGM21);
TCCR2B = TCCR2B | (1 << CS21) | (1 << CS20);
TCCR2B = TCCR2B | (1 << CS22);
rind = 0;
sind = 0;
sdisc = 0;
rready = 0;
asent = 0;
dsent = 0;
hbw_timer = 0;
gotack = 0;
waitack = 0;
configled = 0;
configbutton = 0;
configstate = 0;
slocked = 0;
sbusy = 0;
alink = 0;
readaddr();
hbw_read_config();
srand(my_address);
sdelay = 1000 + (rand() & 0x1F);
sei();
}
static void process(void)
void snext(void)
{
uint8_t linkchannel;
if (amessage[0] != 'K') {
alink = 0;
return;
}
while (alink < HBW_MAXLINKS) {
linkchannel = eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS);
if (amessage[1] == linkchannel) {
((uint8_t*)&aaddress)[0]=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+1);
((uint8_t*)&aaddress)[1]=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+2);
((uint8_t*)&aaddress)[2]=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+3);
((uint8_t*)&aaddress)[3]=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+4);
linkchannel=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+5);
if(aaddress == my_address) {
hbw_receive_key(linkchannel, amessage[3] >> 2, amessage[3] & 0x3);
} else {
amessage[2] = linkchannel;
sbusy = 1;
alink++;
return;
}
}
alink++;
}
alink = 0;
}
void sprocess(void)
{
if (gotack) {
gotack = 0;
waitack = 0;
sbusy= 0;
snext();
return;
}
if (!is_bus_free())
return;
if(waitack > 2) {
waitack = 0;
sbusy= 0;
snext();
return;
}
saddress = aaddress;
sctl = actl;
slen = alen;
memcpy(smessage,amessage,slen);
if (!sendmsg())
return;
if (aaddress == 0xFFFFFFFF) {
sbusy = 0;
snext();
return;
}
waitack++;
gotack = 0;
sdelay = 100 + (rand() & 0x1F);
}
static void rprocess(void)
{
uint8_t seq,i;
uint16_t addr;
@ -342,8 +424,6 @@ static void process(void)
sendmsg();
break;
case 'v':
if (rlen != 2)
return;
smessage[0] = HBWMAJOR;
smessage[1] = HBWMINOR;
slen = 2;
@ -399,6 +479,20 @@ static void process(void)
break;
}
}
if ((rdest == 0xFFFFFFFF) &&
((rctl & 0x11) == 0x10)) {
if(!rlen)
return;
switch (rmessage[0]) {
case 'z':
slocked = 1;
break;
case 'Z':
slocked = 0;
break;
}
}
}
static void eeprom_clear(void)
@ -413,12 +507,21 @@ void hbw_loop(void)
{
rloop();
if (rready) {
process();
rprocess();
rready = 0;
}
if (!asent) {
if (is_bus_free()) {
announce();
if (sbusy) {
sprocess();
} else {
if (!dsent) {
if (is_bus_free()) {
senddsc();
}
}
if (!asent) {
if (is_bus_free()) {
announce();
}
}
}
@ -450,14 +553,14 @@ void hbw_loop(void)
}
break;
case 3:
configled = ((hbw_timer % 400) > 200)?0:1;
configled = (hbw_timer & 128)?0:1;
if (bit_is_set(PIND, ConfigButton)) {
configstate = 4;
configtimer = hbw_timer;
}
break;
case 4:
configled = ((hbw_timer % 400) > 200)?0:1;
configled = (hbw_timer & 128)?0:1;
if (((hbw_timer - configtimer) > 2000) &&
(bit_is_set(PIND, ConfigButton))) {
configstate = 0;
@ -467,12 +570,13 @@ void hbw_loop(void)
(bit_is_clear(PIND, ConfigButton))) {
configstate = 5;
configled = 0;
eeprom_clear();
}
break;
case 5:
configled = (hbw_timer & 512)?0:1;
if (((hbw_timer - configtimer) > 20) &&
(bit_is_set(PIND, ConfigButton))) {
eeprom_clear();
configstate = 0;
configled = 0;
asent = 0;
@ -488,52 +592,54 @@ void hbw_loop(void)
#endif
}
uint8_t hbw_send_channel(uint8_t channel, uint8_t length, uint8_t const * const data, uint32_t target_address)
uint8_t hbw_send_key(uint8_t channel, uint8_t count, uint8_t flag)
{
if (gotack) {
gotack = 0;
waitack = 0;
return 1;
}
if (sbusy)
return 0;
if (!is_bus_free())
if (alink)
return 0;
if(waitack > 2) {
waitack = 0;
return 2;
}
alen = 4;
actl = 0xF8;
amessage[0] = 'K';
amessage[1] = channel;
amessage[2] = 0;
amessage[3] = (flag & 3) | (count << 2);
aaddress = 0xFFFFFFFF;
sbusy = 1;
alink = 0;
return 1;
}
slen = length + 2;
sctl = 0xF8;
aaddress = target_address;
if(aaddress == 0) {
((uint8_t*)&aaddress)[0]=eeprom_read_byte((const uint8_t *)2);
((uint8_t*)&aaddress)[1]=eeprom_read_byte((const uint8_t *)3);
((uint8_t*)&aaddress)[2]=eeprom_read_byte((const uint8_t *)4);
((uint8_t*)&aaddress)[3]=eeprom_read_byte((const uint8_t *)5);
// aaddress = eeprom_read_dword((const uint32_t *)2);
}
uint8_t hbw_send_channel(uint8_t channel, uint8_t length, uint8_t const * const data)
{
if (sbusy)
return 0;
saddress = aaddress;
smessage[0] = 'i';
smessage[1] = channel;
memcpy(&(smessage[2]), data, length);
sendmsg();
if (aaddress == 0xFFFFFFFF)
return 1;
if (alink)
return 0;
waitack++;
gotack = 0;
sdelay = hbw_timer + 100 + (rand() & 0x1F);
return 0;
alen = length + 2;
actl = 0xF8;
((uint8_t*)&aaddress)[0]=eeprom_read_byte((const uint8_t *)2);
((uint8_t*)&aaddress)[1]=eeprom_read_byte((const uint8_t *)3);
((uint8_t*)&aaddress)[2]=eeprom_read_byte((const uint8_t *)4);
((uint8_t*)&aaddress)[3]=eeprom_read_byte((const uint8_t *)5);
amessage[0] = 'i';
amessage[1] = channel;
memcpy(&(amessage[2]), data, length);
sbusy = 1;
return 1;
}
ISR(USART_RX_vect)
{
uint8_t c;
sdelay = hbw_timer + 100 + (rand() & 0x1F);
sdelay = 100 + (rand() & 0x1F);
c = UDR0;
if (c == 0xFE) {
@ -600,5 +706,7 @@ ISR(TIMER2_COMPA_vect)
{
cli();
hbw_timer++;
if (sdelay)
sdelay--;
sei();
}

@ -13,7 +13,9 @@ volatile uint16_t hbw_timer;
void hbw_init(void);
void hbw_loop(void);
uint8_t hbw_send_channel(uint8_t channel, uint8_t length, uint8_t const * const data, uint32_t target_address);
uint8_t hbw_send_channel(uint8_t channel, uint8_t length, uint8_t const * const data);
uint8_t hbw_send_key(uint8_t channel, uint8_t count, uint8_t flag);
void hbw_receive_key(uint8_t channel, uint8_t count, uint8_t flag);
uint8_t hbw_get_channel(uint8_t channel, uint8_t data[]);
void hbw_set_channel(uint8_t channel, uint8_t len, uint8_t data[]);
void hbw_read_config(void);

@ -63,7 +63,7 @@
</paramset>
</channel>
<channel index="3" type="SENSOR" count="2" physical_index_offset="-1">
<paramset id="hmw_sensor_ch_master" type="MASTER" address_step="2" address_start="0x08">
<paramset id="hmw_sensor_ch_master" type="MASTER" address_step="2" address_start="0x0a">
<parameter id="LOGGING">
<logical type="option">
<option id="OFF"/>
@ -86,5 +86,4 @@
</paramset>
</channel>
</channels>
</device>

@ -32,13 +32,25 @@
<frame id="INFO_LEVEL" type="#i" channel_field="10" direction="from_device" event="true">
<parameter size="1.0" index="11.0" type="integer" param="LEVEL"/>
</frame>
<frame id="KEY_EVENT_SHORT" type="#K" channel_field="10" direction="from_device" event="true">
<parameter const_value="2" size="0.2" index="12.0" type="integer"/>
<parameter size="0.6" index="12.2" type="integer" param="COUNTER"/>
</frame>
<frame id="KEY_EVENT_LONG" type="#K" channel_field="10" direction="from_device" event="true">
<parameter const_value="3" size="0.2" index="12.0" type="integer"/>
<parameter size="0.6" index="12.2" type="integer" param="COUNTER"/>
</frame>
<frame id="KEY_EVENT_LONGRELEASE" type="#K" channel_field="10" direction="from_device" event="true">
<parameter const_value="0" size="0.2" index="12.0" type="integer"/>
<parameter size="0.6" index="12.2" type="integer" param="COUNTER"/>
</frame>
</frames>
<channels>
<channel index="0" type="MAINTENANCE" count="1" class="maintenance" ui_flags="internal">
</channel>
<channel index="1" type="SWITCH" count="1" physical_index_offset="-1">
<paramset id="hmw_switch_ch_master" type="MASTER" address_step="2" address_start="0x06">
<paramset id="hmw_switch_ch_master" type="MASTER" address_step="1" address_start="0x06">
<parameter id="LOGGING">
<logical type="option">
<option id="OFF"/>
@ -61,29 +73,57 @@
</parameter>
</paramset>
</channel>
<channel index="2" type="SENSOR" count="1" physical_index_offset="-1">
<paramset id="hmw_sensor_ch_master" type="MASTER" address_step="2" address_start="0x08">
<parameter id="LOGGING">
<logical type="option">
<option id="OFF"/>
<option id="ON" default="true"/>
</logical>
<physical size="0.1" type="integer" interface="eeprom">
<address index="+0"/>
<channel index="2" type="KEY" count="1" physical_index_offset="-1">
<link_roles>
<source name="SWITCH"/>
</link_roles>
<paramset id="hmw_input_ch_master" type="MASTER" address_step="1" address_start="0x07">
<parameter id="LONG_PRESS_TIME">
<logical type="float" unit="s" default="1.0" max="5.0" min="0.4"/>
<physical size="1.0" type="integer" interface="eeprom">
<address index="+1"/>
</physical>
<conversion type="float_integer_scale" factor="10"/>
<conversion type="integer_integer_map">
<value_map to_device="false" from_device="true" parameter_value="10" device_value="0xff"/>
</conversion>
</parameter>
</paramset>
<paramset id="hmw_sensor_ch_values" type="VALUES">
<parameter id="STATE" operations="read,event" control="SWITCH.STATE">
<logical type="boolean" default="false"/>
<physical type="integer" interface="command" value_id="LEVEL">
<get request="LEVEL_GET" response="INFO_LEVEL"/>
<event frame="INFO_LEVEL"/>
<paramset id="hmw_input_ch_link" type="LINK" count="6" address_step="6" address_start="0x10" channel_param="CHANNEL" peer_param="ACTUATOR">
<parameter hidden="true" id="CHANNEL" operations="none">
<logical type="integer" default="255" max="255" min="0"/>
<physical size="1.0" type="integer" interface="eeprom">
<address index="+0"/>
</physical>
</parameter>
<parameter hidden="true" id="ACTUATOR" operations="none">
<logical type="address"/>
<physical type="array">
<physical size="4.0" type="integer" interface="eeprom">
<address index="+1"/>
</physical>
<physical size="1.0" type="integer" interface="eeprom">
<address index="+5"/>
</physical>
</physical>
<conversion type="boolean_integer" true="200" false="0" threshold="1"/>
</parameter>
</paramset>
<paramset id="hmw_input_ch_values" type="VALUES">
<parameter id="PRESS_SHORT" operations="event,write" loopback="true" control="BUTTON.SHORT">
<logical type="action"/>
<physical type="integer" interface="command" value_id="COUNTER">
<event frame="KEY_EVENT_SHORT"/>
</physical>
<conversion type="action_key_counter" counter_size="6" sim_counter="SIM_COUNTER"/>
</parameter>
<parameter id="PRESS_LONG" operations="event,write" loopback="true" control="BUTTON.LONG">
<logical type="action"/>
<physical type="integer" interface="command" value_id="COUNTER">
<event frame="KEY_EVENT_LONG"/>
</physical>
<conversion type="action_key_counter" counter_size="6" sim_counter="SIM_COUNTER"/>
</parameter>
</paramset>
</channel>
</channel>
</channels>
</device>

@ -20,4 +20,7 @@
#define HBWSERIAL8 '0'
#define HBWSERIAL9 '1'
#define HBW_MAXLINKS 16
#define HBW_LINKOFFS 0x10
#endif

@ -62,6 +62,14 @@ void hbw_set_channel(uint8_t channel, uint8_t len, uint8_t data[])
}
}
void hbw_receive_key(uint8_t channel, uint8_t count, uint8_t flag)
{
if(channel == 0)
PORTB ^= (1<<Relais1);
if(channel == 1)
PORTB ^= (1<<Relais2);
}
int main(void)
{
uint8_t state;
@ -91,7 +99,7 @@ int main(void)
if (bit_is_clear(PINC, PC0) && (!pc0state)) {
state = 200;
if (hbw_send_channel(2, 1, &state, 0)) {
if (hbw_send_channel(2, 1, &state)) {
pc0state = ~0;
}
}
@ -99,7 +107,7 @@ int main(void)
if (bit_is_set(PINC, PC0) && pc0state) {
state = 0;
if (hbw_send_channel(2, 1, &state, 0)) {
if (hbw_send_channel(2, 1, &state)) {
pc0state = 0;
}
}
@ -107,7 +115,7 @@ int main(void)
if (bit_is_clear(PINC, PC1) && (!pc1state)) {
state = 200;
if (hbw_send_channel(3, 1, &state, 0)) {
if (hbw_send_channel(3, 1, &state)) {
pc1state = ~0;
}
}
@ -115,7 +123,7 @@ int main(void)
if (bit_is_set(PINC, PC1) && pc1state) {
state = 0;
if (hbw_send_channel(3, 1, &state, 0)) {
if (hbw_send_channel(3, 1, &state)) {
pc1state = 0;
}
}

@ -20,4 +20,6 @@
#define HBWSERIAL8 '0'
#define HBWSERIAL9 '1'
#define HBW_MAXLINKS 16
#define HBW_LINKOFFS 0x10
#endif

@ -6,9 +6,28 @@
uint8_t expled;
uint8_t expbutton;
uint16_t exptime;
uint16_t explong;
uint16_t longtime;
uint16_t logtime;
uint8_t logging;
void hbw_read_config(void)
{
uint8_t ee;
ee = eeprom_read_byte((const uint8_t *)1);
logtime = 100L * ee;
ee = eeprom_read_byte((const uint8_t *)6);
logging = ee & 0x01;
ee = eeprom_read_byte((const uint8_t *)8);
if (ee == 0xFF)
longtime = 1000;
else
longtime = 100L * ee;
}
uint8_t hbw_get_channel(uint8_t channel, uint8_t data[])
@ -40,10 +59,17 @@ void hbw_set_channel(uint8_t channel, uint8_t len, uint8_t data[])
}
}
void hbw_receive_key(uint8_t channel, uint8_t count, uint8_t flag)
{
if(channel == 0)
PORTD ^= (1<<ExpLED);
}
int main(void)
{
uint16_t now;
uint8_t state;
DDRD |= (1<<ExpLED);
DDRD &= ~(1<<ExpButton);
@ -52,26 +78,51 @@ int main(void)
expled = 0;
expbutton = 0;
exptime = 0;
explong = 0;
hbw_init();
state = 255;
while(1) {
hbw_loop();
if (bit_is_clear(PIND, ExpButton) && (!expbutton)) {
state = 200;
if (hbw_send_channel(1, 1, &state, 0)) {
expbutton = ~0;
now = hbw_timer?hbw_timer:1;
if (bit_is_clear(PIND, ExpButton)) {
if (exptime) {
if (explong) {
if (now - explong >= 300) {
explong = now;
state = 3;
}
} else {
if (now - exptime > longtime) {
explong = now;
expbutton++;
state = 3;
}
}
} else {
exptime = now;
explong = 0;
}
}
if (bit_is_set(PIND, ExpButton) && expbutton) {
state = 0;
if (hbw_send_channel(1, 1, &state, 0)) {
expbutton = 0;
if (bit_is_set(PIND, ExpButton)) {
if (exptime) {
if (now - exptime > 50 && !explong) {
expbutton++;
state = 2;
}
if (now - exptime > 50 && explong)
state = 0;
exptime = 0;
}
}
if (state != 255)
if (hbw_send_key(1, expbutton, state))
state = 255;
}
}

Loading…
Cancel
Save