X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=firmware%2Fs88w-t.c;h=b7d0a94f74a7ce89b8152aaf4a5bc5cd53f30c06;hb=a99e18c8e7ec3276ba6a881d1eaccf2d7fc26ea6;hp=70c646c19984fb5583899d6a2b1a45c6ecab83b7;hpb=238cd5e5bd9c6fa86ae85f5eb7a8ce00158f2f7c;p=model-railway-devices.git diff --git a/firmware/s88w-t.c b/firmware/s88w-t.c index 70c646c..b7d0a94 100644 --- a/firmware/s88w-t.c +++ b/firmware/s88w-t.c @@ -1,6 +1,6 @@ /* $Id$ -This file is part of the MSP Märklin suite +This file is part of R²C² Copyright © 2010 Mikkosoft Productions, Mikko Rasa Distributed under the GPL @@ -52,6 +52,7 @@ uint8_t rx_fill = 0xFF; volatile uint8_t nibbles = 2; volatile uint8_t offset = 0; volatile uint16_t state = 0; +volatile uint8_t time_since_send = 0; int main() { @@ -68,23 +69,25 @@ int main() PORTB = 0x3F; // 00111111 serial_init(9600); - timer_start_hz(1, 1, 2); + timer_start_hz(1, 100, 1); sei(); while(1) { - uint8_t i; + uint8_t i, j; uint16_t input = 0; uint16_t valid = 0xFFF; for(i=0; i<100; ++i) { - uint16_t pins; + uint16_t pins = 0; + for(j=0; j<100; ++j) + pins |= ~((PIND>>2) | ((PINB&0x3F)<<6)); - pins = ~((PIND>>2) | ((PINB&0x3F)<<6)); if(i==0) input = pins; + valid &= ~(pins^input); } @@ -92,7 +95,7 @@ int main() input |= state&~valid; input &= (1<<(nibbles*4))-1; - if(input!=state) + if(input!=state && time_since_send>5) { state = input; send_state(); @@ -138,6 +141,7 @@ SERIAL_SET_CALLBACK(receive) void send_state(void) { uint8_t i; + serial_write(':'); serial_write(hexdigit(offset>>8)); serial_write(hexdigit(offset>>4)); @@ -145,9 +149,18 @@ void send_state(void) for(i=nibbles; i--;) serial_write(hexdigit(state>>(i*4))); serial_write('.'); + + time_since_send = 0; +} + +void timer(void) +{ + ++time_since_send; + if(time_since_send>200) + send_state(); } -TIMER_SET_CALLBACK(1, send_state) +TIMER_SET_CALLBACK(1, timer) uint8_t hexdigit(uint8_t n) {