X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=arducontrol%2Foutput.c;h=508b411953f2ec7230b0c66568d87a93af0e6da3;hb=188391bc43da8330aa4f9642bb1a23ef180d66e2;hp=38d1045476438cea33da205d0ecebf67ef9d8957;hpb=9106943c465894bd1a8862b0a6d7ffdd89f8b643;p=model-railway-devices.git diff --git a/arducontrol/output.c b/arducontrol/output.c index 38d1045..508b411 100644 --- a/arducontrol/output.c +++ b/arducontrol/output.c @@ -7,7 +7,18 @@ #define ENABLE PORTD3 #define BIT(x) (1<<(x)) -OutputPacket packet; +enum State +{ + IDLE, + READY, + SENDING +}; + +OutputPacket packets[4]; +uint8_t chain_length; +uint8_t current_packet; +volatile uint8_t trigger_value; +volatile uint8_t packet_state = IDLE; static uint8_t out_bit; static uint8_t out_time; static uint8_t out_data; @@ -21,11 +32,34 @@ void output_init(void) timer_start_hz(2, 80000, 1); } -void clear_packet(void) +OutputPacket *output_create_packet(void) +{ + while(packet_state!=IDLE) ; + chain_length = 1; + packets[0].trigger_position = 0xFF; + return &packets[0]; +} + +OutputPacket *output_create_chained_packet(void) { - packet.ready = 0; - packet.sending = 0; - packet.done = 0; + uint8_t i = chain_length++; + packets[i].trigger_position = 0xFF; + return &packets[i]; +} + +void output_send_packet(void) +{ + if(packet_state!=IDLE) + return; + + current_packet = 0; + trigger_value = 0; + packet_state = READY; +} + +uint8_t output_get_trigger(void) +{ + return trigger_value; } void output_set_power(uint8_t p) @@ -73,27 +107,29 @@ static inline void output_tick(void) if(out_time && !--out_time) { + OutputPacket *packet = &packets[current_packet]; ++out_bit; - if(out_bit>=packet.length) + if(out_bit>=packet->length) { PORTD &= ~BIT(POLARITY); - if(packet.repeat_count>1) + if(--packet->repeat_count) { - if(packet.repeat_count<0xFF) - --packet.repeat_count; - delay_time = packet.repeat_delay; - packet.sending = 0; + delay_time = packet->repeat_delay; + packet_state = READY; } else { - delay_time = packet.final_delay; - packet.done = 1; + delay_time = packet->final_delay; + if(++current_packet>3]; + out_data = packet->data[out_bit>>3]; else out_data >>= 1; @@ -102,18 +138,22 @@ static inline void output_tick(void) else PORTD &= ~BIT(POLARITY); - out_time = packet.bit_duration; + if(out_bit==packet->trigger_position) + trigger_value = packet->trigger_value; + + out_time = packet->bit_duration; } return; } - if(packet.ready && !packet.sending) + if(packet_state==READY) { - packet.sending = 1; + OutputPacket *packet = &packets[current_packet]; + packet_state = SENDING; out_bit = 0; - out_time = packet.bit_duration; - out_data = packet.data[0]; + out_time = packet->bit_duration; + out_data = packet->data[0]; if(out_data&1) PORTD |= BIT(POLARITY); else