diff options
| -rw-r--r-- | arch/attiny1616.c | 12 | ||||
| -rw-r--r-- | arch/attiny1616.h | 2 | ||||
| -rw-r--r-- | arch/attiny1634.c | 12 | ||||
| -rw-r--r-- | arch/attiny1634.h | 2 | ||||
| -rw-r--r-- | arch/attiny85.c | 10 | ||||
| -rw-r--r-- | arch/attiny85.h | 2 | ||||
| -rw-r--r-- | arch/avr32dd20.c | 6 | ||||
| -rw-r--r-- | arch/avr32dd20.h | 2 | ||||
| -rw-r--r-- | fsm/adc.c | 58 | ||||
| -rw-r--r-- | fsm/adc.h | 10 | ||||
| -rw-r--r-- | hw/sofirn/sp10-pro/hwdef.h | 4 | ||||
| -rw-r--r-- | hw/thefreeman/avr32dd20-devkit/hwdef.c | 6 | ||||
| -rw-r--r-- | hw/thefreeman/avr32dd20-devkit/hwdef.h | 4 | ||||
| -rw-r--r-- | hw/thefreeman/boost-fwaa-mp3432-hdr-dac-rgb/hwdef.h | 4 | ||||
| -rw-r--r-- | ui/anduril/aux-leds.c | 39 |
15 files changed, 87 insertions, 86 deletions
diff --git a/arch/attiny1616.c b/arch/attiny1616.c index c5499dd..02e64b1 100644 --- a/arch/attiny1616.c +++ b/arch/attiny1616.c @@ -104,18 +104,18 @@ inline uint16_t mcu_adc_result_volts() { inline uint8_t mcu_vdd_raw2cooked(uint16_t measurement) { // In : 65535 * 1.5 / Vbat - // Out: uint8_t: Vbat * 40 + // Out: uint8_t: Vbat * 50 // 1.5 = ADC Vref #if 0 // 1024 = how much ADC resolution we're using (10 bits) // (12 bits available, but it costs an extra 84 bytes of ROM to calculate) - uint8_t vbat40 = (uint16_t)(40 * 1.5 * 1024) / (measurement >> 6); + uint8_t vbat = (uint16_t)(10 * dV * 1.5 * 1024) / (measurement >> 6); #else // ... spend the extra 84 bytes of ROM for better precision // 4096 = how much ADC resolution we're using (12 bits) - uint8_t vbat40 = (uint32_t)(40 * 1.5 * 4096) / (measurement >> 4); + uint8_t vbat = (uint32_t)(10 * dV * 1.5 * 4096) / (measurement >> 4); #endif - return vbat40; + return vbat; } #if 0 // fine voltage, 0 to 10.24V in 1/6400th V steps @@ -130,12 +130,12 @@ inline uint16_t mcu_vdd_raw2fine(uint16_t measurement) { #ifdef USE_VOLTAGE_DIVIDER inline uint8_t mcu_vdivider_raw2cooked(uint16_t measurement) { // In : 4095 * Vdiv / 1.1V - // Out: uint8_t: Vbat * 40 + // Out: uint8_t: Vbat * 50 // Vdiv = Vbat / 4.3 (typically) // 1.1 = ADC Vref const uint16_t adc_per_volt = (((uint16_t)ADC_44 << 4) - ((uint16_t)ADC_22 << 4)) - / (4 * (44-22)); + / (dV * (44-22)); uint8_t result = measurement / adc_per_volt; return result; } diff --git a/arch/attiny1616.h b/arch/attiny1616.h index 940973e..711452d 100644 --- a/arch/attiny1616.h +++ b/arch/attiny1616.h @@ -85,7 +85,7 @@ inline void mcu_adc_vect_clear(); inline uint16_t mcu_adc_result_temp(); inline uint16_t mcu_adc_result_volts(); -// return Volts * 40, range 0 to 6.375V +// return Volts * 50, range 0 to 5.10V #define voltage_raw2cooked mcu_vdd_raw2cooked inline uint8_t mcu_vdd_raw2cooked(uint16_t measurement); inline uint8_t mcu_vdivider_raw2cooked(uint16_t measurement); diff --git a/arch/attiny1634.c b/arch/attiny1634.c index e29d1c3..314ca52 100644 --- a/arch/attiny1634.c +++ b/arch/attiny1634.c @@ -97,30 +97,30 @@ inline uint16_t mcu_adc_result() { inline uint8_t mcu_vdd_raw2cooked(uint16_t measurement) { // In : 65535 * 1.1 / Vbat - // Out: uint8_t: Vbat * 40 + // Out: uint8_t: Vbat * 50 // 1.1 = ADC Vref #if 0 // 1024 = how much ADC resolution we're using (10 bits) // (12 bits available, but it costs an extra 84 bytes of ROM to calculate) - uint8_t vbat40 = (uint16_t)(40 * 1.1 * 1024) / (measurement >> 6); + uint8_t vbat = (uint16_t)(10 * dV * 1.1 * 1024) / (measurement >> 6); #else // ... spend the extra 84 bytes of ROM for better precision // 4096 = how much ADC resolution we're using (12 bits) - uint8_t vbat40 = (uint32_t)(40 * 1.1 * 4096) / (measurement >> 4); + uint8_t vbat = (uint32_t)(10 * dV * 1.1 * 4096) / (measurement >> 4); #endif - return vbat40; + return vbat; } #ifdef USE_VOLTAGE_DIVIDER inline uint8_t mcu_vdivider_raw2cooked(uint16_t measurement) { // In : 4095 * Vdiv / 1.1V - // Out: uint8_t: Vbat * 40 + // Out: uint8_t: Vbat * 50 // Vdiv = Vbat / 4.3 (typically) // 1.1 = ADC Vref const uint16_t adc_per_volt = (((uint16_t)ADC_44 << 4) - ((uint16_t)ADC_22 << 4)) - / (4 * (44-22)); + / (dV * (44-22)); uint8_t result = measurement / adc_per_volt; return result; } diff --git a/arch/attiny1634.h b/arch/attiny1634.h index 559d04e..19920fe 100644 --- a/arch/attiny1634.h +++ b/arch/attiny1634.h @@ -66,7 +66,7 @@ inline void mcu_adc_off(); inline uint16_t mcu_adc_result(); -// return Volts * 40, range 0 to 6.375V +// return Volts * 50, range 0 to 5.10V #define voltage_raw2cooked mcu_vdd_raw2cooked inline uint8_t mcu_vdd_raw2cooked(uint16_t measurement); inline uint8_t mcu_vdivider_raw2cooked(uint16_t measurement); diff --git a/arch/attiny85.c b/arch/attiny85.c index 9e298cc..4ca4b87 100644 --- a/arch/attiny85.c +++ b/arch/attiny85.c @@ -103,24 +103,24 @@ inline uint16_t mcu_adc_result() { return ADC; } inline uint8_t mcu_vdd_raw2cooked(uint16_t measurement) { // In : 65535 * 1.1 / Vbat - // Out: uint8_t: Vbat * 40 + // Out: uint8_t: Vbat * 50 // 1.1 = ADC Vref // 1024 = how much ADC resolution we're using (10 bits) // (12 bits available, but it costs an extra 84 bytes of ROM to calculate) - uint8_t vbat40 = (uint16_t)(40 * 1.1 * 1024) / (measurement >> 6); - return vbat40; + uint8_t vbat = (uint16_t)(10 * dV * 1.1 * 1024) / (measurement >> 6); + return vbat; } #ifdef USE_VOLTAGE_DIVIDER inline uint8_t mcu_vdivider_raw2cooked(uint16_t measurement) { // In : 4095 * Vdiv / 1.1V - // Out: uint8_t: Vbat * 40 + // Out: uint8_t: Vbat * 50 // Vdiv = Vbat / 4.3 (typically) // 1.1 = ADC Vref const uint16_t adc_per_volt = (((uint16_t)ADC_44 << 4) - ((uint16_t)ADC_22 << 4)) - / (4 * (44-22)); + / (dV * (44-22)); uint8_t result = measurement / adc_per_volt; return result; } diff --git a/arch/attiny85.h b/arch/attiny85.h index 3f6ffcb..06a1061 100644 --- a/arch/attiny85.h +++ b/arch/attiny85.h @@ -53,7 +53,7 @@ inline void mcu_adc_off(); inline uint16_t mcu_adc_result(); -// return Volts * 40, range 0 to 6.375V +// return Volts * 50, range 0 to 5.10V #define voltage_raw2cooked mcu_vdd_raw2cooked inline uint8_t mcu_vdd_raw2cooked(uint16_t measurement); inline uint8_t mcu_vdivider_raw2cooked(uint16_t measurement); diff --git a/arch/avr32dd20.c b/arch/avr32dd20.c index 2ac3526..3ada2ee 100644 --- a/arch/avr32dd20.c +++ b/arch/avr32dd20.c @@ -141,10 +141,10 @@ inline uint16_t mcu_adc_result() { inline uint8_t mcu_vdd_raw2cooked(uint16_t measurement) { // In : 65535 * (Vbat / 10) / 1.024V - // Out: uint8_t: Vbat * 40 + // Out: uint8_t: Vbat * 50 // (add 80 to round up near a boundary) - uint8_t vbat40 = (uint16_t)(measurement + 80) / 160; - return vbat40; + uint8_t vbat50 = (uint16_t)(measurement + 64) / 128; + return vbat50; } #if 0 diff --git a/arch/avr32dd20.h b/arch/avr32dd20.h index 09b4096..7d06863 100644 --- a/arch/avr32dd20.h +++ b/arch/avr32dd20.h @@ -71,7 +71,7 @@ inline uint16_t mcu_adc_result(); //inline uint16_t mcu_adc_result_temp(); //inline uint16_t mcu_adc_result_volts(); -// return Volts * 40, range 0 to 6.375V +// return Volts * 50, range 0 to 5.10V #define voltage_raw2cooked mcu_vdd_raw2cooked inline uint8_t mcu_vdd_raw2cooked(uint16_t measurement); @@ -209,8 +209,8 @@ static void ADC_voltage_handler() { #endif else measurement = adc_smooth[0]; - // convert raw ADC value to FSM voltage units: Volts * 40 - // 0 .. 200 = 0.0V .. 5.0V + // convert raw ADC value to FSM voltage units: Volts * 50 + // 0 .. 250 = 0.0V .. 5.0V voltage = voltage_raw2cooked(measurement) + (VOLTAGE_FUDGE_FACTOR << 1) #ifdef USE_VOLTAGE_CORRECTION @@ -392,46 +392,46 @@ static void ADC_temperature_handler() { #ifdef USE_BATTCHECK #ifdef BATTCHECK_4bars PROGMEM const uint8_t voltage_blinks[] = { - 4*30, - 4*35, - 4*38, - 4*40, - 4*42, - 255, + 30*dV, + 35*dV, + 38*dV, + 40*dV, + 42*dV, + 255, }; #endif #ifdef BATTCHECK_6bars PROGMEM const uint8_t voltage_blinks[] = { - 4*30, - 4*34, - 4*36, - 4*38, - 4*40, - 4*41, - 4*43, - 255, + 30*dV, + 34*dV, + 36*dV, + 38*dV, + 40*dV, + 41*dV, + 43*dV, + 255, }; #endif #ifdef BATTCHECK_8bars PROGMEM const uint8_t voltage_blinks[] = { - 4*30, - 4*33, - 4*35, - 4*37, - 4*38, - 4*39, - 4*40, - 4*41, - 4*42, - 255, + 30*dV, + 33*dV, + 35*dV, + 37*dV, + 38*dV, + 39*dV, + 40*dV, + 41*dV, + 42*dV, + 255, }; #endif void battcheck() { #ifdef BATTCHECK_VpT - blink_num(voltage / 4); + blink_num(voltage / dV); #ifdef USE_EXTRA_BATTCHECK_DIGIT - // 0 1 2 3 --> 0 2 5 7, representing x.x00 x.x25 x.x50 x.x75 - blink_num(((voltage % 4)<<1) + ((voltage % 4)>>1)); + // 0.02V precision, 0 1 2 3 4 remainder -> .00 .02 .04 .06 .08V + blink_num((voltage % dV) * (10/dV)); #endif #else uint8_t i; @@ -4,6 +4,10 @@ #pragma once +// voltage is 0.00V to 5.10V in 0.02V steps, from 0 to 255 +// so one deci-Volt is 5 steps +#define dV 5 + #if defined(USE_LVP) || defined(USE_THERMAL_REGULATION) // use raw value instead of lowpassed value for the next N measurements // (2 = 1 for voltage + 1 for temperature) @@ -15,13 +19,13 @@ volatile uint8_t adc_reset = 2; #ifndef VOLTAGE_WARNING_SECONDS #define VOLTAGE_WARNING_SECONDS 5 #endif -// low-battery threshold in volts * 40 +// low-battery threshold in volts * 50 #ifndef VOLTAGE_LOW -#define VOLTAGE_LOW (4*29) +#define VOLTAGE_LOW (29*dV) #endif // battery is low but not critical #ifndef VOLTAGE_RED -#define VOLTAGE_RED (4*33) +#define VOLTAGE_RED (33*dV) #endif // MCU sees voltage 0.X volts lower than actual, add X/2 to readings #ifndef VOLTAGE_FUDGE_FACTOR diff --git a/hw/sofirn/sp10-pro/hwdef.h b/hw/sofirn/sp10-pro/hwdef.h index f220318..43da9d0 100644 --- a/hw/sofirn/sp10-pro/hwdef.h +++ b/hw/sofirn/sp10-pro/hwdef.h @@ -62,8 +62,8 @@ enum CHANNEL_MODES { // Voltage divider battLVL #define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is regulated -#define DUAL_VOLTAGE_FLOOR (4*21) // for AA/14500 boost drivers, don't indicate low voltage if below this level -#define DUAL_VOLTAGE_LOW_LOW (4*7) // the lower voltage range's danger zone 0.7 volts (NiMH) +#define DUAL_VOLTAGE_FLOOR (21*dV) // for AA/14500 boost drivers, don't indicate low voltage if below this level +#define DUAL_VOLTAGE_LOW_LOW ( 7*dV) // the lower voltage range's danger zone 0.7 volts (NiMH) #define ADMUX_VOLTAGE_DIVIDER ADC_MUXPOS_AIN9_gc // which ADC channel to read #undef voltage_raw2cooked diff --git a/hw/thefreeman/avr32dd20-devkit/hwdef.c b/hw/thefreeman/avr32dd20-devkit/hwdef.c index 460082f..5b534d2 100644 --- a/hw/thefreeman/avr32dd20-devkit/hwdef.c +++ b/hw/thefreeman/avr32dd20-devkit/hwdef.c @@ -119,13 +119,13 @@ bool gradual_tick_main(uint8_t gt) { uint8_t voltage_raw2cooked(uint16_t measurement) { // In : 65535 * BATTLVL / 1.024V - // Out: uint8_t: Vbat * 40 + // Out: uint8_t: Vbat * 50 // BATTLVL = Vbat * (100.0/(330+100)) = Vbat / 4.3 - // So, Out = In * 4.3 / 1600 + // So, Out = In * 4.3 / 1280 // (plus a bit of fudging to fix the slope and offset, // based on measuring actual hardware) uint8_t result = (uint32_t)(measurement + (65535 * 4 / 1024)) - * 43 / 16128; + * 43 / 12880; return result; } diff --git a/hw/thefreeman/avr32dd20-devkit/hwdef.h b/hw/thefreeman/avr32dd20-devkit/hwdef.h index 38b508d..5015c24 100644 --- a/hw/thefreeman/avr32dd20-devkit/hwdef.h +++ b/hw/thefreeman/avr32dd20-devkit/hwdef.h @@ -109,8 +109,8 @@ enum CHANNEL_MODES { // AVR datasheet table 3.1 I/O Multiplexing, PA6 ADC0 = AIN26 #define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is regulated #define ADMUX_VOLTAGE_DIVIDER ADC_MUXPOS_AIN26_gc -#define DUAL_VOLTAGE_FLOOR (4*21) // for AA/14500 boost drivers, don't indicate low voltage if below this level -#define DUAL_VOLTAGE_LOW_LOW (4*7) // the lower voltage range's danger zone 0.7 volts (NiMH) +#define DUAL_VOLTAGE_FLOOR (21*dV) // for AA/14500 boost drivers, don't indicate low voltage if below this level +#define DUAL_VOLTAGE_LOW_LOW ( 7*dV) // the lower voltage range's danger zone 0.7 volts (NiMH) // don't use the default VDD converter // convert BATT LVL pin readings to FSM volt units #undef voltage_raw2cooked diff --git a/hw/thefreeman/boost-fwaa-mp3432-hdr-dac-rgb/hwdef.h b/hw/thefreeman/boost-fwaa-mp3432-hdr-dac-rgb/hwdef.h index bc1d9a7..1c6e6be 100644 --- a/hw/thefreeman/boost-fwaa-mp3432-hdr-dac-rgb/hwdef.h +++ b/hw/thefreeman/boost-fwaa-mp3432-hdr-dac-rgb/hwdef.h @@ -94,8 +94,8 @@ enum CHANNEL_MODES { // Voltage divider battLVL #define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is regulated #define ADMUX_VOLTAGE_DIVIDER ADC_MUXPOS_AIN2_gc // which ADC channel to read -#define DUAL_VOLTAGE_FLOOR (4*21) // for AA/14500 boost drivers, don't indicate low voltage if below this level -#define DUAL_VOLTAGE_LOW_LOW (4*7) // the lower voltage range's danger zone 0.7 volts (NiMH) +#define DUAL_VOLTAGE_FLOOR (21*dV) // for AA/14500 boost drivers, don't indicate low voltage if below this level +#define DUAL_VOLTAGE_LOW_LOW ( 7*dV) // the lower voltage range's danger zone 0.7 volts (NiMH) // don't use the default VDD converter #undef voltage_raw2cooked #define voltage_raw2cooked mcu_vdivider_raw2cooked diff --git a/ui/anduril/aux-leds.c b/ui/anduril/aux-leds.c index fd184fc..7356666 100644 --- a/ui/anduril/aux-leds.c +++ b/ui/anduril/aux-leds.c @@ -58,27 +58,27 @@ void indicator_led_update(uint8_t mode, uint8_t tick) { uint8_t voltage_to_rgb() { static const uint8_t levels[] = { // voltage, color - 0, 0, // black + 0, 0, // black #ifdef DUAL_VOLTAGE_FLOOR // AA / NiMH voltages - 4* 9, 1, // R - 4*10, 2, // R+G - 4*11, 3, // G - 4*12, 4, // G+B - 4*13, 5, // B - 4*14, 6, // R + B - 4*16, 7, // R+G+B - 4*20, 0, // black + 9*dV, 1, // R + 10*dV, 2, // R+G + 11*dV, 3, // G + 12*dV, 4, // G+B + 13*dV, 5, // B + 14*dV, 6, // R + B + 16*dV, 7, // R+G+B + 20*dV, 0, // black #endif // li-ion voltages - 4*29, 1, // R - 4*33, 2, // R+G - 4*35, 3, // G - 4*37, 4, // G+B - 4*39, 5, // B - 4*41, 6, // R + B - 4*44, 7, // R+G+B // skip; looks too similar to G+B - 255, 7, // R+G+B + 29*dV, 1, // R + 33*dV, 2, // R+G + 35*dV, 3, // G + 37*dV, 4, // G+B + 39*dV, 5, // B + 41*dV, 6, // R + B + 44*dV, 7, // R+G+B // skip; looks too similar to G+B + 255, 7, // R+G+B }; uint8_t volts = voltage; //if (volts < VOLTAGE_LOW) return 0; @@ -151,11 +151,8 @@ void rgb_led_update(uint8_t mode, uint16_t arg) { else { // voltage // show actual voltage while asleep... if (go_to_standby) { - actual_color = voltage_to_rgb(); // choose a color based on battery voltage - //if (volts >= 38) actual_color = pgm_read_byte(colors + 4); - //else if (volts >= 33) actual_color = pgm_read_byte(colors + 2); - //else actual_color = pgm_read_byte(colors + 0); + actual_color = voltage_to_rgb(); } // ... but during preview, cycle colors quickly else { |
