Compare commits

...

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

212
hbw.c

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

@ -13,9 +13,7 @@ volatile uint16_t hbw_timer;
void hbw_init(void); void hbw_init(void);
void hbw_loop(void); void hbw_loop(void);
uint8_t hbw_send_channel(uint8_t channel, uint8_t length, uint8_t const * const data); 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);
void hbw_receive_key(uint8_t channel, uint8_t count, uint8_t flag);
uint8_t hbw_get_channel(uint8_t channel, uint8_t data[]); 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_set_channel(uint8_t channel, uint8_t len, uint8_t data[]);
void hbw_read_config(void); void hbw_read_config(void);

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

@ -32,25 +32,13 @@
<frame id="INFO_LEVEL" type="#i" channel_field="10" direction="from_device" event="true"> <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"/> <parameter size="1.0" index="11.0" type="integer" param="LEVEL"/>
</frame> </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> </frames>
<channels> <channels>
<channel index="0" type="MAINTENANCE" count="1" class="maintenance" ui_flags="internal"> <channel index="0" type="MAINTENANCE" count="1" class="maintenance" ui_flags="internal">
</channel> </channel>
<channel index="1" type="SWITCH" count="1" physical_index_offset="-1"> <channel index="1" type="SWITCH" count="1" physical_index_offset="-1">
<paramset id="hmw_switch_ch_master" type="MASTER" address_step="1" address_start="0x06"> <paramset id="hmw_switch_ch_master" type="MASTER" address_step="2" address_start="0x06">
<parameter id="LOGGING"> <parameter id="LOGGING">
<logical type="option"> <logical type="option">
<option id="OFF"/> <option id="OFF"/>
@ -73,57 +61,29 @@
</parameter> </parameter>
</paramset> </paramset>
</channel> </channel>
<channel index="2" type="KEY" count="1" physical_index_offset="-1"> <channel index="2" type="SENSOR" count="1" physical_index_offset="-1">
<link_roles> <paramset id="hmw_sensor_ch_master" type="MASTER" address_step="2" address_start="0x08">
<source name="SWITCH"/> <parameter id="LOGGING">
</link_roles> <logical type="option">
<paramset id="hmw_input_ch_master" type="MASTER" address_step="1" address_start="0x07"> <option id="OFF"/>
<parameter id="LONG_PRESS_TIME"> <option id="ON" default="true"/>
<logical type="float" unit="s" default="1.0" max="5.0" min="0.4"/> </logical>
<physical size="1.0" type="integer" interface="eeprom"> <physical size="0.1" 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_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"/> <address index="+0"/>
</physical> </physical>
</parameter> </parameter>
<parameter hidden="true" id="ACTUATOR" operations="none"> </paramset>
<logical type="address"/> <paramset id="hmw_sensor_ch_values" type="VALUES">
<physical type="array"> <parameter id="STATE" operations="read,event" control="SWITCH.STATE">
<physical size="4.0" type="integer" interface="eeprom"> <logical type="boolean" default="false"/>
<address index="+1"/> <physical type="integer" interface="command" value_id="LEVEL">
</physical> <get request="LEVEL_GET" response="INFO_LEVEL"/>
<physical size="1.0" type="integer" interface="eeprom"> <event frame="INFO_LEVEL"/>
<address index="+5"/>
</physical>
</physical>
</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> </physical>
<conversion type="action_key_counter" counter_size="6" sim_counter="SIM_COUNTER"/> <conversion type="boolean_integer" true="200" false="0" threshold="1"/>
</parameter> </parameter>
</paramset> </paramset>
</channel> </channel>
</channels> </channels>
</device> </device>

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

@ -62,14 +62,6 @@ 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) int main(void)
{ {
uint8_t state; uint8_t state;
@ -99,7 +91,7 @@ int main(void)
if (bit_is_clear(PINC, PC0) && (!pc0state)) { if (bit_is_clear(PINC, PC0) && (!pc0state)) {
state = 200; state = 200;
if (hbw_send_channel(2, 1, &state)) { if (hbw_send_channel(2, 1, &state, 0)) {
pc0state = ~0; pc0state = ~0;
} }
} }
@ -107,7 +99,7 @@ int main(void)
if (bit_is_set(PINC, PC0) && pc0state) { if (bit_is_set(PINC, PC0) && pc0state) {
state = 0; state = 0;
if (hbw_send_channel(2, 1, &state)) { if (hbw_send_channel(2, 1, &state, 0)) {
pc0state = 0; pc0state = 0;
} }
} }
@ -115,7 +107,7 @@ int main(void)
if (bit_is_clear(PINC, PC1) && (!pc1state)) { if (bit_is_clear(PINC, PC1) && (!pc1state)) {
state = 200; state = 200;
if (hbw_send_channel(3, 1, &state)) { if (hbw_send_channel(3, 1, &state, 0)) {
pc1state = ~0; pc1state = ~0;
} }
} }
@ -123,7 +115,7 @@ int main(void)
if (bit_is_set(PINC, PC1) && pc1state) { if (bit_is_set(PINC, PC1) && pc1state) {
state = 0; state = 0;
if (hbw_send_channel(3, 1, &state)) { if (hbw_send_channel(3, 1, &state, 0)) {
pc1state = 0; pc1state = 0;
} }
} }

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

@ -6,28 +6,9 @@
uint8_t expled; uint8_t expled;
uint8_t expbutton; uint8_t expbutton;
uint16_t exptime;
uint16_t explong;
uint16_t longtime;
uint16_t logtime;
uint8_t logging;
void hbw_read_config(void) 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[]) uint8_t hbw_get_channel(uint8_t channel, uint8_t data[])
@ -59,17 +40,10 @@ 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) int main(void)
{ {
uint16_t now;
uint8_t state; uint8_t state;
DDRD |= (1<<ExpLED); DDRD |= (1<<ExpLED);
DDRD &= ~(1<<ExpButton); DDRD &= ~(1<<ExpButton);
@ -78,51 +52,26 @@ int main(void)
expled = 0; expled = 0;
expbutton = 0; expbutton = 0;
exptime = 0;
explong = 0;
hbw_init(); hbw_init();
state = 255;
while(1) { while(1) {
hbw_loop(); hbw_loop();
now = hbw_timer?hbw_timer:1; if (bit_is_clear(PIND, ExpButton) && (!expbutton)) {
state = 200;
if (bit_is_clear(PIND, ExpButton)) {
if (exptime) { if (hbw_send_channel(1, 1, &state, 0)) {
if (explong) { expbutton = ~0;
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)) { if (bit_is_set(PIND, ExpButton) && expbutton) {
if (exptime) { state = 0;
if (now - exptime > 50 && !explong) {
expbutton++; if (hbw_send_channel(1, 1, &state, 0)) {
state = 2; expbutton = 0;
}
if (now - exptime > 50 && explong)
state = 0;
exptime = 0;
} }
} }
if (state != 255)
if (hbw_send_key(1, expbutton, state))
state = 255;
} }
} }

Loading…
Cancel
Save