]> git.tdb.fi Git - model-railway-devices.git/commitdiff
Hide details of output packet lifecycle
authorMikko Rasa <tdb@tdb.fi>
Sat, 2 Nov 2013 13:46:01 +0000 (15:46 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 2 Nov 2013 13:46:01 +0000 (15:46 +0200)
Other modules should have no need to mess with the packet state directly.

arducontrol/motorola.c
arducontrol/output.c
arducontrol/output.h

index 3e1a41c6a3f6d2dbdb12aefa15cab7739ca4e938..ff686caf8ac072a00c6cafbc407eacdd77c33cd8 100644 (file)
@@ -12,82 +12,85 @@ static uint8_t motorola_speed_to_value(uint8_t speed)
                return 0;
 }
 
-static void motorola_common_packet(uint8_t addr, uint8_t aux, uint8_t value)
+static OutputPacket *motorola_common_packet(uint8_t addr, uint8_t aux, uint8_t value)
 {
        uint8_t i;
 
-       clear_packet();
+       OutputPacket *packet = output_create_packet();
 
-       packet.bit_duration = 2;
-       packet.length = 18*8;
+       packet->bit_duration = 2;
+       packet->length = 18*8;
 
        for(i=0; i<4; ++i)
        {
                uint8_t d = addr%3;
                addr /= 3;
 
-               packet.data[i*2] = (d==0 ? 0x01 : 0x7F);
-               packet.data[i*2+1] = (d==1 ? 0x7F : 0x01);
+               packet->data[i*2] = (d==0 ? 0x01 : 0x7F);
+               packet->data[i*2+1] = (d==1 ? 0x7F : 0x01);
        }
 
-       packet.data[8] = (aux ? 0x7F : 0x01);
-       packet.data[9] = packet.data[8];
+       packet->data[8] = (aux ? 0x7F : 0x01);
+       packet->data[9] = packet->data[8];
 
        for(i=0; i<4; ++i)
        {
-               packet.data[10+i*2] = ((value&1) ? 0x7F : 0x01);
+               packet->data[10+i*2] = ((value&1) ? 0x7F : 0x01);
                value >>= 1;
        }
 
-       packet.repeat_count = 2;
+       packet->repeat_count = 2;
        // Duration of three trits
-       packet.repeat_delay = 96;
-       packet.final_delay = 224;
+       packet->repeat_delay = 96;
+       packet->final_delay = 224;
+
+       return packet;
 }
 
-static void motorola_old_packet(uint8_t addr, uint8_t aux, uint8_t value)
+static OutputPacket *motorola_old_packet(uint8_t addr, uint8_t aux, uint8_t value)
 {
        uint8_t i;
 
-       motorola_common_packet(addr, aux, value);
+       OutputPacket *packet = motorola_common_packet(addr, aux, value);
 
        for(i=0; i<4; ++i)
-               packet.data[11+i*2] = packet.data[10+i*2];
+               packet->data[11+i*2] = packet->data[10+i*2];
+
+       return packet;
 }
 
 void motorola_locomotive_speed_packet(uint8_t addr, uint8_t aux, uint8_t speed)
 {
        motorola_old_packet(addr, aux, motorola_speed_to_value(speed));
-
-       packet.ready = 1;
+       output_send_packet();
 }
 
 void motorola_locomotive_reverse_packet(uint8_t addr, uint8_t aux)
 {
        motorola_old_packet(addr, aux, 1);
-
-       packet.ready = 1;
+       output_send_packet();
 }
 
 void motorola_locomotive_speed_direction_packet(uint8_t addr, uint8_t aux, uint8_t speed, uint8_t dir)
 {
-       motorola_common_packet(addr, aux, motorola_speed_to_value(speed));
+       OutputPacket *packet = motorola_common_packet(addr, aux, motorola_speed_to_value(speed));
 
-       packet.data[11] = (dir ? 0x01 : 0x7F);
-       packet.data[13] = (dir ? 0x7F : 0x01);
-       packet.data[15] = (dir ? 0x01 : 0x7F);
-       packet.data[17] = 0x80-packet.data[16];
+       packet->data[11] = (dir ? 0x01 : 0x7F);
+       packet->data[13] = (dir ? 0x7F : 0x01);
+       packet->data[15] = (dir ? 0x01 : 0x7F);
+       packet->data[17] = 0x80-packet->data[16];
 
-       packet.ready = 1;
+       output_send_packet();
 }
 
 void motorola_locomotive_speed_function_packet(uint8_t addr, uint8_t aux, uint8_t speed, uint8_t func, uint8_t state)
 {
        uint8_t i;
        uint8_t value;
+       OutputPacket *packet;
 
        value = motorola_speed_to_value(speed);
-       motorola_common_packet(addr, aux, value);
+       packet = motorola_common_packet(addr, aux, value);
 
        /*
        001 -> 011
@@ -105,11 +108,11 @@ void motorola_locomotive_speed_function_packet(uint8_t addr, uint8_t aux, uint8_
 
        for(i=0; i<4; ++i)
        {
-               packet.data[11+i*2] = ((func&1) ? 0x7F : 0x01);
+               packet->data[11+i*2] = ((func&1) ? 0x7F : 0x01);
                func >>= 1;
        }
 
-       packet.ready = 1;
+       output_send_packet();
 }
 
 void motorola_solenoid_packet(uint8_t addr, uint8_t output, uint8_t state)
@@ -118,11 +121,11 @@ void motorola_solenoid_packet(uint8_t addr, uint8_t output, uint8_t state)
        if(state)
                value |= 8;
 
-       motorola_old_packet(addr, 0, value);
-       packet.repeat_delay >>= 1;
-       packet.bit_duration = 1;
+       OutputPacket *packet = motorola_old_packet(addr, 0, value);
+       packet->repeat_delay >>= 1;
+       packet->bit_duration = 1;
 
-       packet.ready = 1;
+       output_send_packet();
 }
 
 uint8_t motorola_command(const uint8_t *cmd_buf, uint8_t cmd_length)
@@ -156,8 +159,6 @@ uint8_t motorola_command(const uint8_t *cmd_buf, uint8_t cmd_length)
 
                uint8_t dir = !(cmd_buf[3]&0x80);
 
-               while(packet.ready && !packet.done) ;
-
                if(cmd_buf[0]==MOTOROLA_SPEED)
                        motorola_locomotive_speed_packet(addr, aux, speed);
                else if(cmd_buf[0]==MOTOROLA_SPEED_DIRECTION)
@@ -178,8 +179,6 @@ uint8_t motorola_command(const uint8_t *cmd_buf, uint8_t cmd_length)
                        return INVALID_VALUE;
                uint8_t aux = cmd_buf[2]&0x01;
 
-               while(packet.ready && !packet.done) ;
-
                motorola_locomotive_reverse_packet(addr, aux);
        }
        else if(cmd_buf[0]==MOTOROLA_SOLENOID)
@@ -196,8 +195,6 @@ uint8_t motorola_command(const uint8_t *cmd_buf, uint8_t cmd_length)
                uint8_t output = (cmd_buf[2]&0x70)>>4;
                uint8_t state = cmd_buf[2]&1;
 
-               while(packet.ready && !packet.done) ;
-
                motorola_solenoid_packet(addr, output, state);
        }
        else
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];
index 988c16ed2e5e717cdc4dd67760756d1206b67d72..162e1e010b4a168f87773583bcd2c3aa1a10946a 100644 (file)
@@ -5,10 +5,7 @@
 
 typedef struct
 {
-       uint8_t bit_duration:5;
-       uint8_t ready:1;
-       uint8_t sending:1;
-       volatile uint8_t done:1;
+       uint8_t bit_duration;
        uint8_t length;
        uint8_t data[32];
        uint8_t repeat_count;
@@ -16,10 +13,9 @@ typedef struct
        uint8_t final_delay;
 } OutputPacket;
 
-extern OutputPacket packet;
-
 void output_init(void);
-void clear_packet(void);
+OutputPacket *output_create_packet(void);
+void output_send_packet(void);
 void output_set_power(uint8_t);
 uint8_t output_is_power_on();
 uint8_t output_command(const uint8_t *, uint8_t);