]> git.tdb.fi Git - model-railway-devices.git/blob - arducontrol/monitor.c
Add functions for sending replies
[model-railway-devices.git] / arducontrol / monitor.c
1 #include "adc.h"
2 #include "interface.h"
3 #include "monitor.h"
4 #include "output.h"
5 #include "serial.h"
6
7 uint16_t track_current_samples[16] = { 0 };
8 uint8_t track_current_head = 0;
9 volatile uint16_t track_current_sum = 0;
10 uint16_t overcurrent_limit = 8796;
11 uint8_t overcurrent_sent = 0;
12
13 uint16_t input_voltage_samples[16] = { 0 };
14 uint8_t input_voltage_head = 0;
15 volatile uint16_t input_voltage_sum = 0;
16
17 volatile uint8_t adc_state = 0;
18 volatile uint16_t adc_value = 0;
19
20 uint16_t track_current_milliamps(void);
21 uint16_t input_voltage_millivolts(void);
22
23 void monitor_init(void)
24 {
25         adc_init();
26 }
27
28 void monitor_check(void)
29 {
30         if(!(adc_state&1))
31         {
32                 uint16_t value = adc_value;
33
34                 if(adc_state==2)
35                 {
36                         uint8_t i = track_current_head;
37                         track_current_sum -= track_current_samples[i];
38                         track_current_samples[i] = value;
39                         track_current_sum += value;
40                         track_current_head = (i+1)&15;
41
42                         if(track_current_sum>overcurrent_limit)
43                         {
44                                 output_set_power(0);
45                                 if(!overcurrent_sent)
46                                 {
47                                         overcurrent_sent = 1;
48                                         interface_send1(OVERCURRENT);
49                                 }
50                         }
51                         else
52                                 overcurrent_sent = 0;
53                 }
54                 else if(adc_state==4)
55                 {
56                         uint8_t i = input_voltage_head;
57                         input_voltage_sum -= input_voltage_samples[i];
58                         input_voltage_samples[i] = value;
59                         input_voltage_sum += value;
60                         input_voltage_head = (i+1)&15;
61                 }
62
63                 adc_state = (adc_state+1)&3;
64                 adc_read_async(adc_state>>1);
65         }
66 }
67
68 uint8_t monitor_command(const uint8_t *cmd_buf, uint8_t cmd_length)
69 {
70         if(cmd_buf[0]==READ_TRACK_CURRENT)
71         {
72                 uint8_t reply[3];
73
74                 if(cmd_length!=1)
75                         return LENGTH_ERROR;
76
77                 uint16_t value = track_current_milliamps();
78                 reply[0] = TRACK_CURRENT;
79                 reply[1] = value>>8;
80                 reply[2] = value;
81                 interface_send(reply, sizeof(reply));
82         }
83         else if(cmd_buf[0]==SET_OVERCURRENT_LIMIT)
84         {
85                 if(cmd_length!=3)
86                         return LENGTH_ERROR;
87
88                 uint16_t value = (cmd_buf[1]<<8) | cmd_buf[2];
89                 if(value>4000)  // Safe maximum value
90                         return INVALID_VALUE;
91
92                 // Convert from milliamps: (512+v/1000*0.185/5*1024)*16
93                 // multiply by 16384*0.185/5000 = 0.1001101100110b
94                 uint16_t v_3 = value*3;
95                 overcurrent_limit = 8192+(value>>1)+(v_3>>5)+(v_3>>8)+(v_3>>12);
96         }
97         else if(cmd_buf[0]==READ_INPUT_VOLTAGE)
98         {
99                 uint8_t reply[3];
100
101                 if(cmd_length!=1)
102                         return LENGTH_ERROR;
103
104                 uint16_t value = input_voltage_millivolts();
105                 reply[0] = INPUT_VOLTAGE;
106                 reply[1] = value>>8;
107                 reply[2] = value;
108                 interface_send(reply, sizeof(reply));
109         }
110         else
111                 return INVALID_COMMAND;
112
113         return COMMAND_OK;
114 }
115
116 uint16_t track_current_milliamps(void)
117 {
118         uint16_t value = track_current_sum;
119
120         // Convert to milliamps: (v/16*5/1024-2.5)*1000/0.185
121         if(value<8192)  // Ignore negative current readings
122                 return 0;
123         else
124         {
125                 value -= 8192;
126
127                 // multiply by 5000/0.185/16384 = 1.1010011001001b
128                 int16_t v_3 = value*3;
129                 return (v_3>>1)+(value>>3)+(v_3>>7)+(value>>10)+(v_3>>13);
130         }
131 }
132
133 uint16_t input_voltage_millivolts(void)
134 {
135         uint16_t value = input_voltage_sum;
136
137         // Convert to millivolts: (v/16*5/1024)*1000*11
138         // multiply by 55000/16384 = 11.0101101101100b
139         uint16_t v_3 = value*3;
140         return v_3+(value>>2)+(v_3>>5)+(v_3>>8)+(v_3>>11);
141 }
142
143 static inline void adc_complete(uint16_t value)
144 {
145         adc_value = value;
146         ++adc_state;
147 }
148
149 ADC_SET_CALLBACK(adc_complete)