]> 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 f961a436b99c2d24bf9d73255fe0a951d30df37c..38d1045476438cea33da205d0ecebf67ef9d8957 100644 (file)
@@ -1,5 +1,5 @@
 #include <avr/io.h>
-#include "commands.h"
+#include "interface.h"
 #include "output.h"
 #include "timer.h"
 
@@ -8,17 +8,17 @@
 #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)
 {
        DDRD = (DDRD&0xF3)|0x0C;
        PORTD &= ~BIT(ENABLE);
 
-       timer_start_hz(0, 80000, 1);
+       timer_start_hz(2, 80000, 1);
 }
 
 void clear_packet(void)
@@ -36,6 +36,11 @@ void output_set_power(uint8_t p)
                PORTD &= ~BIT(ENABLE);
 }
 
+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)
@@ -45,6 +50,16 @@ uint8_t output_command(const uint8_t *cmd_buf, uint8_t cmd_length)
 
                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;
 
@@ -106,4 +121,4 @@ static inline void output_tick(void)
        }
 }
 
-TIMER_SET_CALLBACK(0, output_tick)
+TIMER_SET_CALLBACK(2, output_tick)