~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/spaghetti-monster/fsm-adc.h

  • Committer: Selene ToyKeeper
  • Date: 2023-11-04 15:09:10 UTC
  • mfrom: (483.1.175 anduril2)
  • Revision ID: bzr@toykeeper.net-20231104150910-ddd3afw4nhfvof2l
merged anduril2 branch -> fsm, with *years* of changes
(this also means this code is now Anduril 2 instead of Anduril 1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
 
1
// fsm-adc.h: ADC (voltage, temperature) functions for SpaghettiMonster.
 
2
// Copyright (C) 2017-2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
 
 
5
#pragma once
23
6
 
24
7
#if defined(USE_LVP) || defined(USE_THERMAL_REGULATION)
25
8
// use raw value instead of lowpassed value for the next N measurements
36
19
#ifndef VOLTAGE_LOW
37
20
#define VOLTAGE_LOW 29
38
21
#endif
 
22
// battery is low but not critical
 
23
#ifndef VOLTAGE_RED
 
24
#define VOLTAGE_RED 33
 
25
#endif
39
26
// MCU sees voltage 0.X volts lower than actual, add X/2 to readings
40
27
#ifndef VOLTAGE_FUDGE_FACTOR
41
28
#ifdef USE_VOLTAGE_DIVIDER
45
32
#endif
46
33
#endif
47
34
 
 
35
#ifdef TICK_DURING_STANDBY
 
36
volatile uint8_t adc_active_now = 0;  // sleep LVP needs a different sleep mode
 
37
#endif
48
38
volatile uint8_t irq_adc = 0;  // ADC interrupt happened?
49
39
uint8_t adc_sample_count = 0;  // skip the first sample; it's junk
50
40
uint8_t adc_channel = 0;  // 0=voltage, 1=temperature
57
47
void adc_deferred();  // do the actual ADC-related calculations
58
48
 
59
49
static inline void ADC_voltage_handler();
60
 
volatile uint8_t voltage = 0;
 
50
uint8_t voltage = 0;
 
51
#ifdef USE_VOLTAGE_CORRECTION
 
52
    #ifdef USE_CFG
 
53
        #define VOLT_CORR cfg.voltage_correction
 
54
    #else
 
55
        // same 0.05V units as fudge factor,
 
56
        // but 7 is neutral, and the expected range is from 1 to 13
 
57
        uint8_t voltage_correction = 7;
 
58
        #define VOLT_CORR voltage_correction
 
59
    #endif
 
60
#endif
61
61
#ifdef USE_LVP
62
62
void low_voltage();
63
63
#endif
88
88
#define THERM_CAL_OFFSET 0
89
89
#endif
90
90
// temperature now, in C (ish)
91
 
volatile int16_t temperature;
92
 
uint8_t therm_ceil = DEFAULT_THERM_CEIL;
93
 
int8_t therm_cal_offset = 0;
 
91
int16_t temperature;
 
92
#ifdef USE_CFG
 
93
    #define TH_CEIL cfg.therm_ceil
 
94
    #define TH_CAL cfg.therm_cal_offset
 
95
#else
 
96
    #define TH_CEIL therm_ceil
 
97
    #define TH_CAL therm_cal_offset
 
98
    uint8_t therm_ceil = DEFAULT_THERM_CEIL;
 
99
    int8_t therm_cal_offset = 0;
 
100
#endif
94
101
static inline void ADC_temperature_handler();
95
102
#endif  // ifdef USE_THERMAL_REGULATION
96
103
 
99
106
inline void ADC_off();
100
107
inline void ADC_start_measurement();
101
108
 
102
 
 
 
109
#ifdef TICK_DURING_STANDBY
 
110
inline void adc_sleep_mode();
103
111
#endif
 
112