+
+static inline void adc_complete(uint16_t value)
+{
+ if(adc_state==1)
+ {
+ // Convert to milliamps: (v*5/1024-2.5)*1000/0.185
+ if(value<512) // Ignore negative current readings
+ value = 0;
+ else if(value>663) // Limit range so averaging won't overflow
+ value = 4000;
+ else
+ value = (value-512)*132/5;
+
+ uint8_t i = track_current_head;
+ track_current_sum -= track_current_samples[i];
+ track_current_samples[i] = value;
+ track_current_sum += value;
+ track_current_head = (i+1)&15;
+ }
+ else if(adc_state==3)
+ {
+ // Convert to centivolts: (v*5/1024)*100*11
+ if(value>744) // Limit range so averaging won't overflow
+ value = 4000;
+ else
+ value = value*43/8;
+
+ uint8_t i = input_voltage_head;
+ input_voltage_sum -= input_voltage_samples[i];
+ input_voltage_samples[i] = value;
+ input_voltage_sum += value;
+ input_voltage_head = (i+1)&15;
+ }
+
+ ++adc_state;
+}
+
+ADC_SET_CALLBACK(adc_complete)