]> git.tdb.fi Git - model-railway-devices.git/blobdiff - arducontrol/output.c
Add a function and command to find out if power is applied to the output
[model-railway-devices.git] / arducontrol / output.c
index c1d51488349359ae9101a9762c2837bf257aebce..38d1045476438cea33da205d0ecebf67ef9d8957 100644 (file)
@@ -3,21 +3,25 @@
 #include "output.h"
 #include "timer.h"
 
+#define POLARITY PORTD2
+#define ENABLE PORTD3
+#define BIT(x) (1<<(x))
+
 OutputPacket packet;
-uint8_t out_bit;
-uint8_t out_time;
-uint8_t out_data;
-uint8_t delay_time;
+static uint8_t out_bit;
+static uint8_t out_time;
+static uint8_t out_data;
+static uint8_t delay_time;
 
-void output_init()
+void output_init(void)
 {
        DDRD = (DDRD&0xF3)|0x0C;
-       PORTD &= ~0x08;
+       PORTD &= ~BIT(ENABLE);
 
-       timer_start_hz(0, 80000, 1);
+       timer_start_hz(2, 80000, 1);
 }
 
-void clear_packet()
+void clear_packet(void)
 {
        packet.ready = 0;
        packet.sending = 0;
@@ -27,12 +31,17 @@ void clear_packet()
 void output_set_power(uint8_t p)
 {
        if(p==POWER_ON)
-               PORTD |= 0x08;
+               PORTD |= BIT(ENABLE);
        else
-               PORTD &= ~0x08;
+               PORTD &= ~BIT(ENABLE);
 }
 
-uint8_t output_command()
+uint8_t output_is_power_on()
+{
+       return (PORTD&BIT(ENABLE))!=0;
+}
+
+uint8_t output_command(const uint8_t *cmd_buf, uint8_t cmd_length)
 {
        if(cmd_buf[0]==POWER_ON || cmd_buf[0]==POWER_OFF)
        {
@@ -41,13 +50,23 @@ uint8_t output_command()
 
                output_set_power(cmd_buf[0]==POWER_ON);
        }
+       else if(cmd_buf[0]==READ_POWER_STATE)
+       {
+               if(cmd_length!=1)
+                       return LENGTH_ERROR;
+
+               uint8_t reply[2];
+               reply[0] = POWER_STATE;
+               reply[1] = output_is_power_on();
+               interface_send(reply, 2);
+       }
        else
                return INVALID_COMMAND;
 
        return COMMAND_OK;
 }
 
-static inline void output_tick()
+static inline void output_tick(void)
 {
        if(delay_time && --delay_time)
                return;
@@ -57,7 +76,7 @@ static inline void output_tick()
                ++out_bit;
                if(out_bit>=packet.length)
                {
-                       PORTD &= ~0x04;
+                       PORTD &= ~BIT(POLARITY);
                        if(packet.repeat_count>1)
                        {
                                if(packet.repeat_count<0xFF)
@@ -79,9 +98,9 @@ static inline void output_tick()
                                out_data >>= 1;
 
                        if(out_data&1)
-                               PORTD |= 0x04;
+                               PORTD |= BIT(POLARITY);
                        else
-                               PORTD &= ~0x04;
+                               PORTD &= ~BIT(POLARITY);
 
                        out_time = packet.bit_duration;
                }
@@ -96,10 +115,10 @@ static inline void output_tick()
                out_time = packet.bit_duration;
                out_data = packet.data[0];
                if(out_data&1)
-                       PORTD |= 0x04;
+                       PORTD |= BIT(POLARITY);
                else
-                       PORTD &= ~0x04;
+                       PORTD &= ~BIT(POLARITY);
        }
 }
 
-TIMER_SET_CALLBACK(0, output_tick)
+TIMER_SET_CALLBACK(2, output_tick)