~toykeeper/flashlight-firmware/trunk

188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
1
/*
2
 * fsm-adc.h: ADC (voltage, temperature) functions for SpaghettiMonster.
3
 *
4
 * Copyright (C) 2017 Selene Scriven
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
#ifndef FSM_ADC_H
21
#define FSM_ADC_H
22
188.1.16 by Selene Scriven
Added thermal regulation to SpaghettiMonster / Baton.
23
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
24
#ifdef USE_LVP
188.1.16 by Selene Scriven
Added thermal regulation to SpaghettiMonster / Baton.
25
// default 5 seconds between low-voltage warning events
26
#ifndef VOLTAGE_WARNING_SECONDS
27
#define VOLTAGE_WARNING_SECONDS 5
28
#endif
29
// low-battery threshold in volts * 10
30
#ifndef VOLTAGE_LOW
31
#define VOLTAGE_LOW 29
32
#endif
188.1.81 by Selene Scriven
Slightly increased resolution of VOLTAGE_FUDGE_FACTOR.
33
// MCU sees voltage 0.X volts lower than actual, add X/2 to readings
188.1.16 by Selene Scriven
Added thermal regulation to SpaghettiMonster / Baton.
34
#ifndef VOLTAGE_FUDGE_FACTOR
188.2.2 by Selene Scriven
Use separate voltage adjustment value for pin7 readings.
35
#ifdef USE_VOLTAGE_DIVIDER
36
#define VOLTAGE_FUDGE_FACTOR 0
37
#else
188.1.81 by Selene Scriven
Slightly increased resolution of VOLTAGE_FUDGE_FACTOR.
38
#define VOLTAGE_FUDGE_FACTOR 5
188.1.16 by Selene Scriven
Added thermal regulation to SpaghettiMonster / Baton.
39
#endif
188.2.2 by Selene Scriven
Use separate voltage adjustment value for pin7 readings.
40
#endif
188.13.1 by Selene Scriven
refactored how interrupts work...
41
42
volatile uint8_t irq_adc = 0;  // ADC interrupt happened?
188.15.15 by Selene Scriven
the ADC sample count doesn't need to be 16-bit any more, and isn't really a count any more...
43
uint8_t adc_sample_count = 0;  // skip the first sample; it's junk
188.13.3 by Selene Scriven
fixed ADC code; measures and behaves correctly now, and is easier to read...
44
uint8_t adc_channel = 0;  // 0=voltage, 1=temperature
188.15.1 by Selene Scriven
rewrote ADC code to use a continuous lowpass system on all measurements, to eliminate noise and maybe increase precision
45
uint16_t adc_raw[2];  // last ADC measurements (0=voltage, 1=temperature)
46
uint16_t adc_smooth[2];  // lowpassed ADC measurements (0=voltage, 1=temperature)
47
// ADC code is split into two parts:
48
// - ISR: runs immediately at each interrupt, does the bare minimum because time is critical here
49
// - deferred: the bulk of the logic runs later when time isn't so critical
50
uint8_t adc_deferred_enable = 0;  // stop waiting and run the deferred code
51
void adc_deferred();  // do the actual ADC-related calculations
188.13.1 by Selene Scriven
refactored how interrupts work...
52
188.13.2 by Selene Scriven
started refactoring ADC code to split voltage and temperature into their own functions
53
static inline void ADC_voltage_handler();
188.16.1 by Selene Scriven
merged a sanitized copy of the Emisar D4v2 branch; history summarized below:
54
volatile uint8_t voltage = 0;
188.15.20 by Selene Scriven
merged some misc fixes from pakutrai, cleaned up comments, removed unused symbols
55
#ifdef USE_LVP
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
56
void low_voltage();
188.15.20 by Selene Scriven
merged some misc fixes from pakutrai, cleaned up comments, removed unused symbols
57
#endif
188.13.1 by Selene Scriven
refactored how interrupts work...
58
188.1.24 by Selene Scriven
Added battcheck mode to ramping-ui. It's bigger than I had hoped. :(
59
#ifdef USE_BATTCHECK
60
void battcheck();
61
#ifdef BATTCHECK_VpT
62
#define USE_BLINK_NUM
63
#endif
188.1.67 by Selene Scriven
Started a Meteor M43 clone UI.
64
#if defined(BATTCHECK_8bars) || defined(BATTCHECK_6bars) || defined(BATTCHECK_4bars)
188.1.25 by Selene Scriven
Made 4bar and 8bar battcheck styles work.
65
#define USE_BLINK_DIGIT
66
#endif
188.1.24 by Selene Scriven
Added battcheck mode to ramping-ui. It's bigger than I had hoped. :(
67
#endif
188.13.2 by Selene Scriven
started refactoring ADC code to split voltage and temperature into their own functions
68
#endif  // ifdef USE_LVP
188.1.16 by Selene Scriven
Added thermal regulation to SpaghettiMonster / Baton.
69
70
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
71
#ifdef USE_THERMAL_REGULATION
188.1.16 by Selene Scriven
Added thermal regulation to SpaghettiMonster / Baton.
72
// try to keep temperature below 45 C
73
#ifndef DEFAULT_THERM_CEIL
74
#define DEFAULT_THERM_CEIL 45
75
#endif
76
// don't allow user to set ceiling above 70 C
77
#ifndef MAX_THERM_CEIL
78
#define MAX_THERM_CEIL 70
79
#endif
80
// Local per-MCU calibration value
81
#ifndef THERM_CAL_OFFSET
82
#define THERM_CAL_OFFSET 0
83
#endif
188.15.20 by Selene Scriven
merged some misc fixes from pakutrai, cleaned up comments, removed unused symbols
84
// temperature now, in C (ish)
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
85
volatile int16_t temperature;
188.1.127 by Selene Scriven
FSM: added ability to adjust temperature calibration in UI (lower-case therm_cal_offset var).
86
uint8_t therm_ceil = DEFAULT_THERM_CEIL;
87
int8_t therm_cal_offset = 0;
188.1.73 by Selene Scriven
Reworked thermal regulation.
88
volatile uint8_t reset_thermal_history = 1;
188.13.2 by Selene Scriven
started refactoring ADC code to split voltage and temperature into their own functions
89
static inline void ADC_temperature_handler();
90
#endif  // ifdef USE_THERMAL_REGULATION
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
91
188.1.16 by Selene Scriven
Added thermal regulation to SpaghettiMonster / Baton.
92
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
93
inline void ADC_on();
94
inline void ADC_off();
188.16.1 by Selene Scriven
merged a sanitized copy of the Emisar D4v2 branch; history summarized below:
95
inline void ADC_start_measurement();
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
96
188.1.16 by Selene Scriven
Added thermal regulation to SpaghettiMonster / Baton.
97
188.1.11 by Selene Scriven
Completely reorganized SpaghettiMonster code into smaller logical pieces: fsm-*.c and fsm-*.h.
98
#endif