]> git.tdb.fi Git - model-railway-devices.git/blobdiff - arducontrol/output.c
Hide details of output packet lifecycle
[model-railway-devices.git] / arducontrol / output.c
index 38d1045476438cea33da205d0ecebf67ef9d8957..673f23242d0b93aab450ea1eb4129ad3acb531a7 100644 (file)
@@ -7,7 +7,15 @@
 #define ENABLE PORTD3
 #define BIT(x) (1<<(x))
 
+enum State
+{
+       IDLE,
+       READY,
+       SENDING
+};
+
 OutputPacket packet;
+volatile uint8_t packet_state = IDLE;
 static uint8_t out_bit;
 static uint8_t out_time;
 static uint8_t out_data;
@@ -21,11 +29,18 @@ void output_init(void)
        timer_start_hz(2, 80000, 1);
 }
 
-void clear_packet(void)
+OutputPacket *output_create_packet(void)
 {
-       packet.ready = 0;
-       packet.sending = 0;
-       packet.done = 0;
+       while(packet_state!=IDLE) ;
+       return &packet;
+}
+
+void output_send_packet(void)
+{
+       if(packet_state!=IDLE)
+               return;
+
+       packet_state = READY;
 }
 
 void output_set_power(uint8_t p)
@@ -82,12 +97,12 @@ static inline void output_tick(void)
                                if(packet.repeat_count<0xFF)
                                        --packet.repeat_count;
                                delay_time = packet.repeat_delay;
-                               packet.sending = 0;
+                               packet_state = READY;
                        }
                        else
                        {
                                delay_time = packet.final_delay;
-                               packet.done = 1;
+                               packet_state = IDLE;
                        }
                }
                else
@@ -108,9 +123,9 @@ static inline void output_tick(void)
                return;
        }
 
-       if(packet.ready && !packet.sending)
+       if(packet_state==READY)
        {
-               packet.sending = 1;
+               packet_state = SENDING;
                out_bit = 0;
                out_time = packet.bit_duration;
                out_data = packet.data[0];