/*
* fsm-adc.c: ADC (voltage, temperature) functions for SpaghettiMonster.
*
* Copyright (C) 2017 Selene ToyKeeper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#ifndef FSM_ADC_C
#define FSM_ADC_C
#ifdef USE_VOLTAGE_DIVIDER
// 1.1V / pin7
#define ADMUX_VOLTAGE ADMUX_VOLTAGE_DIVIDER
#else
// VCC / 1.1V reference
#define ADMUX_VOLTAGE ADMUX_VCC
#endif
inline void ADC_on()
{
// read voltage on VCC by default
ADMUX = ADMUX_VOLTAGE;
#ifdef USE_VOLTAGE_DIVIDER
// disable digital input on divider pin to reduce power consumption
DIDR0 |= (1 << VOLTAGE_ADC_DIDR);
#else
// disable digital input on VCC pin to reduce power consumption
//DIDR0 |= (1 << ADC_DIDR); // FIXME: unsure how to handle for VCC pin
#endif
// enable, start, prescale
ADCSRA = (1 << ADEN) | (1 << ADSC) | ADC_PRSCL;
}
inline void ADC_off() {
ADCSRA &= ~(1<> 2;
#ifdef USE_VOLTAGE_DIVIDER
voltage = calc_voltage_divider(total);
#else
voltage = (uint16_t)(1.1*1024*10)/total + VOLTAGE_FUDGE_FACTOR;
#endif
}
#else // no USE_LVP_AVG
#ifdef USE_VOLTAGE_DIVIDER
voltage = calc_voltage_divider(measurement);
#else
// calculate actual voltage: volts * 10
// ADC = 1.1 * 1024 / volts
// volts = 1.1 * 1024 / ADC
//voltage = (uint16_t)(1.1*1024*10)/measurement + VOLTAGE_FUDGE_FACTOR;
voltage = ((uint16_t)(2*1.1*1024*10)/measurement + VOLTAGE_FUDGE_FACTOR) >> 1;
#endif
#endif
// if low, callback EV_voltage_low / EV_voltage_critical
// (but only if it has been more than N ticks since last call)
if (lvp_timer) {
lvp_timer --;
} else { // it has been long enough since the last warning
if (voltage < VOLTAGE_LOW) {
if (lvp_lowpass < LVP_LOWPASS_STRENGTH) {
lvp_lowpass ++;
} else {
// try to send out a warning
//uint8_t err = emit(EV_voltage_low, 0);
//uint8_t err = emit_now(EV_voltage_low, 0);
emit(EV_voltage_low, 0);
//if (!err) {
// on successful warning, reset counters
lvp_timer = LVP_TIMER_START;
lvp_lowpass = 0;
//}
}
} else {
// voltage not low? reset count
lvp_lowpass = 0;
}
}
}
#endif // ifdef USE_LVP
#ifdef USE_THERMAL_REGULATION
// temperature
else if (adc_step == 3) {
// Convert ADC units to Celsius (ish)
int16_t temp = measurement - 275 + THERM_CAL_OFFSET + therm_cal_offset;
// prime on first execution
if (reset_thermal_history) {
reset_thermal_history = 0;
for(uint8_t i=0; i> 2;
// More precise method: use noise as extra precision
// (values are now basically fixed-point, signed 13.2)
//temperature = total;
// 14.1 is less prone to overflows
temperature = total >> 2;
}
// guess what the temperature will be in a few seconds
int16_t pt;
{
uint8_t i;
int16_t diff;
int16_t t = temperature;
// algorithm tweaking; not really intended to be modified
// how far ahead should we predict?
#define THERM_PREDICTION_STRENGTH 4
// how proportional should the adjustments be?
#define THERM_DIFF_ATTENUATION 3
// acceptable temperature window size in C
#define THERM_WINDOW_SIZE 10
// highest temperature allowed
// (convert configured value to 14.1 fixed-point)
#define THERM_CEIL (((int16_t)therm_ceil)<<1)
// bottom of target temperature window (14.1 fixed-point)
#define THERM_FLOOR (THERM_CEIL - (THERM_WINDOW_SIZE<<1))
// rotate measurements and add a new one
for (i=0; i THERM_FLOOR) {
underheat_lowpass = 0; // we're probably not too cold
} else if (pt < THERM_CEIL) {
overheat_lowpass = 0; // we're probably not too hot
}
if (temperature_timer) {
temperature_timer --;
} else { // it has been long enough since the last warning
// Too hot?
if (avg_projected_temperature > THERM_CEIL) {
if (overheat_lowpass < OVERHEAT_LOWPASS_STRENGTH) {
overheat_lowpass ++;
} else {
// reset counters
overheat_lowpass = 0;
temperature_timer = TEMPERATURE_TIMER_START;
// how far above the ceiling?
int16_t howmuch = (avg_projected_temperature - THERM_CEIL) >> THERM_DIFF_ATTENUATION;
if (howmuch > 0) {
// try to send out a warning
emit(EV_temperature_high, howmuch);
}
}
}
// Too cold?
else if (avg_projected_temperature < THERM_FLOOR) {
if (underheat_lowpass < UNDERHEAT_LOWPASS_STRENGTH) {
underheat_lowpass ++;
} else {
// reset counters
underheat_lowpass = 0;
temperature_timer = TEMPERATURE_TIMER_START;
// how far below the floor?
int16_t howmuch = (THERM_FLOOR - avg_projected_temperature) >> THERM_DIFF_ATTENUATION;
if (howmuch > 0) {
// try to send out a warning (unless voltage is low)
// (LVP and underheat warnings fight each other)
if (voltage > VOLTAGE_LOW)
emit(EV_temperature_low, howmuch);
}
}
}
}
}
#endif // ifdef USE_THERMAL_REGULATION
// set the correct type of measurement for next time
#ifdef USE_THERMAL_REGULATION
#ifdef USE_LVP
if (adc_step < 2) ADMUX = ADMUX_VOLTAGE;
else ADMUX = ADMUX_THERM;
#else
ADMUX = ADMUX_THERM;
#endif
#else
#ifdef USE_LVP
ADMUX = ADMUX_VOLTAGE;
#endif
#endif
}
#ifdef USE_BATTCHECK
#ifdef BATTCHECK_4bars
PROGMEM const uint8_t voltage_blinks[] = {
30, 35, 38, 40, 42, 99,
};
#endif
#ifdef BATTCHECK_6bars
PROGMEM const uint8_t voltage_blinks[] = {
30, 34, 36, 38, 40, 41, 43, 99,
};
#endif
#ifdef BATTCHECK_8bars
PROGMEM const uint8_t voltage_blinks[] = {
30, 33, 35, 37, 38, 39, 40, 41, 42, 99,
};
#endif
void battcheck() {
#ifdef BATTCHECK_VpT
blink_num(voltage);
#else
uint8_t i;
for(i=0;
voltage >= pgm_read_byte(voltage_blinks + i);
i++) {}
#ifdef DONT_DELAY_AFTER_BATTCHECK
blink_digit(i);
#else
if (blink_digit(i))
nice_delay_ms(1000);
#endif
#endif
}
#endif
#endif