]> git.tdb.fi Git - model-railway-devices.git/blobdiff - arducontrol/monitor.c
Make current and voltage readings public to other modules
[model-railway-devices.git] / arducontrol / monitor.c
index de00fc1645b9ddc1258326eadcb2a880aaeb23d9..920f56ded6ccb579d2a025880449cbe58b2edfc7 100644 (file)
@@ -7,7 +7,7 @@
 static uint16_t track_current_samples[16] = { 0 };
 static uint8_t track_current_head = 0;
 static volatile uint16_t track_current_sum = 0;
-static uint16_t overcurrent_limit = 8796;
+static uint16_t overcurrent_limit = 9707;
 static uint8_t overcurrent_sent = 0;
 
 static uint16_t input_voltage_samples[16] = { 0 };
@@ -17,11 +17,9 @@ static volatile uint16_t input_voltage_sum = 0;
 static volatile uint8_t adc_state = 0;
 static volatile uint16_t adc_value = 0;
 
-static uint16_t track_current_milliamps(void);
-static uint16_t input_voltage_millivolts(void);
-
 void monitor_init(void)
 {
+       DDRB |= 0x02;
        adc_init();
 }
 
@@ -42,14 +40,18 @@ void monitor_check(void)
                        if(track_current_sum>overcurrent_limit)
                        {
                                output_set_power(0);
+                               PORTB |= 0x02;
                                if(!overcurrent_sent)
                                {
                                        overcurrent_sent = 1;
                                        interface_send1(OVERCURRENT);
                                }
                        }
-                       else
+                       else if(overcurrent_sent && output_is_power_on())
+                       {
+                               PORTB &= ~0x02;
                                overcurrent_sent = 0;
+                       }
                }
                else if(adc_state==4)
                {
@@ -69,12 +71,11 @@ uint8_t monitor_command(const uint8_t *cmd_buf, uint8_t cmd_length)
 {
        if(cmd_buf[0]==READ_TRACK_CURRENT)
        {
-               uint8_t reply[3];
-
                if(cmd_length!=1)
                        return LENGTH_ERROR;
 
-               uint16_t value = track_current_milliamps();
+               uint16_t value = monitor_track_current();
+               uint8_t reply[3];
                reply[0] = TRACK_CURRENT;
                reply[1] = value>>8;
                reply[2] = value;
@@ -96,12 +97,11 @@ uint8_t monitor_command(const uint8_t *cmd_buf, uint8_t cmd_length)
        }
        else if(cmd_buf[0]==READ_INPUT_VOLTAGE)
        {
-               uint8_t reply[3];
-
                if(cmd_length!=1)
                        return LENGTH_ERROR;
 
-               uint16_t value = input_voltage_millivolts();
+               uint16_t value = monitor_input_voltage();
+               uint8_t reply[3];
                reply[0] = INPUT_VOLTAGE;
                reply[1] = value>>8;
                reply[2] = value;
@@ -113,7 +113,7 @@ uint8_t monitor_command(const uint8_t *cmd_buf, uint8_t cmd_length)
        return COMMAND_OK;
 }
 
-static uint16_t track_current_milliamps(void)
+uint16_t monitor_track_current(void)
 {
        uint16_t value = track_current_sum;
 
@@ -130,7 +130,7 @@ static uint16_t track_current_milliamps(void)
        }
 }
 
-static uint16_t input_voltage_millivolts(void)
+uint16_t monitor_input_voltage(void)
 {
        uint16_t value = input_voltage_sum;