Skip to content

Commit 6de7729

Browse files
author
gaaat
committed
Merge branch '0_15' of https://github.com/Aircoookie/WLED into esp8266-audioreactive-sync
2 parents 0ac53d8 + b88c300 commit 6de7729

File tree

11 files changed

+596
-243
lines changed

11 files changed

+596
-243
lines changed

pio-scripts/obj-dump.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
# Little convenience script to get an object dump
2+
# You may add "-S" to the objdump commandline (i.e. replace "-D -C " with "-d -S -C ")
3+
# to get source code intermixed with disassembly (SLOW !)
24

35
Import('env')
46

57
def obj_dump_after_elf(source, target, env):
8+
platform = env.PioPlatform()
9+
board = env.BoardConfig()
10+
mcu = board.get("build.mcu", "esp32")
11+
612
print("Create firmware.asm")
7-
env.Execute("xtensa-lx106-elf-objdump "+ "-D " + str(target[0]) + " > "+ "${PROGNAME}.asm")
8-
13+
if mcu == "esp8266":
14+
env.Execute("xtensa-lx106-elf-objdump "+ "-D -C " + str(target[0]) + " > "+ "$BUILD_DIR/${PROGNAME}.asm")
15+
if mcu == "esp32":
16+
env.Execute("xtensa-esp32-elf-objdump "+ "-D -C " + str(target[0]) + " > "+ "$BUILD_DIR/${PROGNAME}.asm")
17+
if mcu == "esp32s2":
18+
env.Execute("xtensa-esp32s2-elf-objdump "+ "-D -C " + str(target[0]) + " > "+ "$BUILD_DIR/${PROGNAME}.asm")
19+
if mcu == "esp32s3":
20+
env.Execute("xtensa-esp32s3-elf-objdump "+ "-D -C " + str(target[0]) + " > "+ "$BUILD_DIR/${PROGNAME}.asm")
21+
if mcu == "esp32c3":
22+
env.Execute("riscv32-esp-elf-objdump "+ "-D -C " + str(target[0]) + " > "+ "$BUILD_DIR/${PROGNAME}.asm")
23+
924
env.AddPostAction("$BUILD_DIR/${PROGNAME}.elf", [obj_dump_after_elf])

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ extra_scripts =
115115
post:pio-scripts/strip-floats.py
116116
pre:pio-scripts/user_config_copy.py
117117
pre:pio-scripts/build_ui.py
118+
; post:pio-scripts/obj-dump.py ;; convenience script to create a disassembly dump of the firmware (hardcore debugging)
118119

119120
# ------------------------------------------------------------------------------
120121
# COMMON SETTINGS:

usermods/Battery/UMBattery.h

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#ifndef UMBBattery_h
2+
#define UMBBattery_h
3+
4+
#include "battery_defaults.h"
5+
6+
/**
7+
* Battery base class
8+
* all other battery classes should inherit from this
9+
*/
10+
class UMBattery
11+
{
12+
private:
13+
14+
protected:
15+
float minVoltage;
16+
float maxVoltage;
17+
float voltage;
18+
int8_t level = 100;
19+
float calibration; // offset or calibration value to fine tune the calculated voltage
20+
float voltageMultiplier; // ratio for the voltage divider
21+
22+
float linearMapping(float v, float min, float max, float oMin = 0.0f, float oMax = 100.0f)
23+
{
24+
return (v-min) * (oMax-oMin) / (max-min) + oMin;
25+
}
26+
27+
public:
28+
UMBattery()
29+
{
30+
this->setVoltageMultiplier(USERMOD_BATTERY_VOLTAGE_MULTIPLIER);
31+
this->setCalibration(USERMOD_BATTERY_CALIBRATION);
32+
}
33+
34+
virtual void update(batteryConfig cfg)
35+
{
36+
if(cfg.minVoltage) this->setMinVoltage(cfg.minVoltage);
37+
if(cfg.maxVoltage) this->setMaxVoltage(cfg.maxVoltage);
38+
if(cfg.level) this->setLevel(cfg.level);
39+
if(cfg.calibration) this->setCalibration(cfg.calibration);
40+
if(cfg.voltageMultiplier) this->setVoltageMultiplier(cfg.voltageMultiplier);
41+
}
42+
43+
/**
44+
* Corresponding battery curves
45+
* calculates the level in % (0-100) with given voltage and possible voltage range
46+
*/
47+
virtual float mapVoltage(float v, float min, float max) = 0;
48+
// {
49+
// example implementation, linear mapping
50+
// return (v-min) * 100 / (max-min);
51+
// };
52+
53+
virtual void calculateAndSetLevel(float voltage) = 0;
54+
55+
56+
57+
/*
58+
*
59+
* Getter and Setter
60+
*
61+
*/
62+
63+
/*
64+
* Get lowest configured battery voltage
65+
*/
66+
virtual float getMinVoltage()
67+
{
68+
return this->minVoltage;
69+
}
70+
71+
/*
72+
* Set lowest battery voltage
73+
* can't be below 0 volt
74+
*/
75+
virtual void setMinVoltage(float voltage)
76+
{
77+
this->minVoltage = max(0.0f, voltage);
78+
}
79+
80+
/*
81+
* Get highest configured battery voltage
82+
*/
83+
virtual float getMaxVoltage()
84+
{
85+
return this->maxVoltage;
86+
}
87+
88+
/*
89+
* Set highest battery voltage
90+
* can't be below minVoltage
91+
*/
92+
virtual void setMaxVoltage(float voltage)
93+
{
94+
this->maxVoltage = max(getMinVoltage()+.5f, voltage);
95+
}
96+
97+
float getVoltage()
98+
{
99+
return this->voltage;
100+
}
101+
102+
/**
103+
* check if voltage is within specified voltage range, allow 10% over/under voltage
104+
*/
105+
void setVoltage(float voltage)
106+
{
107+
// this->voltage = ( (voltage < this->getMinVoltage() * 0.85f) || (voltage > this->getMaxVoltage() * 1.1f) )
108+
// ? -1.0f
109+
// : voltage;
110+
this->voltage = voltage;
111+
}
112+
113+
float getLevel()
114+
{
115+
return this->level;
116+
}
117+
118+
void setLevel(float level)
119+
{
120+
this->level = constrain(level, 0.0f, 110.0f);
121+
}
122+
123+
/*
124+
* Get the configured calibration value
125+
* a offset value to fine-tune the calculated voltage.
126+
*/
127+
virtual float getCalibration()
128+
{
129+
return calibration;
130+
}
131+
132+
/*
133+
* Set the voltage calibration offset value
134+
* a offset value to fine-tune the calculated voltage.
135+
*/
136+
virtual void setCalibration(float offset)
137+
{
138+
calibration = offset;
139+
}
140+
141+
/*
142+
* Get the configured calibration value
143+
* a value to set the voltage divider ratio
144+
*/
145+
virtual float getVoltageMultiplier()
146+
{
147+
return voltageMultiplier;
148+
}
149+
150+
/*
151+
* Set the voltage multiplier value
152+
* a value to set the voltage divider ratio.
153+
*/
154+
virtual void setVoltageMultiplier(float multiplier)
155+
{
156+
voltageMultiplier = multiplier;
157+
}
158+
};
159+
160+
#endif

usermods/Battery/battery_defaults.h

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#ifndef UMBDefaults_h
2+
#define UMBDefaults_h
3+
4+
#include "wled.h"
5+
16
// pin defaults
27
// for the esp32 it is best to use the ADC1: GPIO32 - GPIO39
38
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html
@@ -14,19 +19,55 @@
1419
#define USERMOD_BATTERY_MEASUREMENT_INTERVAL 30000
1520
#endif
1621

17-
// default for 18650 battery
18-
// https://batterybro.com/blogs/18650-wholesale-battery-reviews/18852515-when-to-recycle-18650-batteries-and-how-to-start-a-collection-center-in-your-vape-shop
19-
// Discharge voltage: 2.5 volt + .1 for personal safety
20-
#ifndef USERMOD_BATTERY_MIN_VOLTAGE
21-
#ifdef USERMOD_BATTERY_USE_LIPO
22-
// LiPo "1S" Batteries should not be dischared below 3V !!
23-
#define USERMOD_BATTERY_MIN_VOLTAGE 3.2f
24-
#else
25-
#define USERMOD_BATTERY_MIN_VOLTAGE 2.6f
26-
#endif
22+
23+
/* Default Battery Type
24+
* 0 = unkown
25+
* 1 = Lipo
26+
* 2 = Lion
27+
*/
28+
#ifndef USERMOD_BATTERY_DEFAULT_TYPE
29+
#define USERMOD_BATTERY_DEFAULT_TYPE 0
30+
#endif
31+
/*
32+
*
33+
* Unkown 'Battery' defaults
34+
*
35+
*/
36+
#ifndef USERMOD_BATTERY_UNKOWN_MIN_VOLTAGE
37+
// Extra save defaults
38+
#define USERMOD_BATTERY_UNKOWN_MIN_VOLTAGE 3.3f
39+
#endif
40+
#ifndef USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE
41+
#define USERMOD_BATTERY_UNKOWN_MAX_VOLTAGE 4.2f
2742
#endif
2843

29-
//the default ratio for the voltage divider
44+
/*
45+
*
46+
* Lithium polymer (Li-Po) defaults
47+
*
48+
*/
49+
#ifndef USERMOD_BATTERY_LIPO_MIN_VOLTAGE
50+
// LiPo "1S" Batteries should not be dischared below 3V !!
51+
#define USERMOD_BATTERY_LIPO_MIN_VOLTAGE 3.2f
52+
#endif
53+
#ifndef USERMOD_BATTERY_LIPO_MAX_VOLTAGE
54+
#define USERMOD_BATTERY_LIPO_MAX_VOLTAGE 4.2f
55+
#endif
56+
57+
/*
58+
*
59+
* Lithium-ion (Li-Ion) defaults
60+
*
61+
*/
62+
#ifndef USERMOD_BATTERY_LION_MIN_VOLTAGE
63+
// default for 18650 battery
64+
#define USERMOD_BATTERY_LION_MIN_VOLTAGE 2.6f
65+
#endif
66+
#ifndef USERMOD_BATTERY_LION_MAX_VOLTAGE
67+
#define USERMOD_BATTERY_LION_MAX_VOLTAGE 4.2f
68+
#endif
69+
70+
// the default ratio for the voltage divider
3071
#ifndef USERMOD_BATTERY_VOLTAGE_MULTIPLIER
3172
#ifdef ARDUINO_ARCH_ESP32
3273
#define USERMOD_BATTERY_VOLTAGE_MULTIPLIER 2.0f
@@ -35,25 +76,15 @@
3576
#endif
3677
#endif
3778

38-
#ifndef USERMOD_BATTERY_MAX_VOLTAGE
39-
#define USERMOD_BATTERY_MAX_VOLTAGE 4.2f
40-
#endif
41-
42-
// a common capacity for single 18650 battery cells is between 2500 and 3600 mAh
43-
#ifndef USERMOD_BATTERY_TOTAL_CAPACITY
44-
#define USERMOD_BATTERY_TOTAL_CAPACITY 3100
79+
#ifndef USERMOD_BATTERY_AVERAGING_ALPHA
80+
#define USERMOD_BATTERY_AVERAGING_ALPHA 0.1f
4581
#endif
4682

4783
// offset or calibration value to fine tune the calculated voltage
4884
#ifndef USERMOD_BATTERY_CALIBRATION
4985
#define USERMOD_BATTERY_CALIBRATION 0
5086
#endif
5187

52-
// calculate remaining time / the time that is left before the battery runs out of power
53-
// #ifndef USERMOD_BATTERY_CALCULATE_TIME_LEFT_ENABLED
54-
// #define USERMOD_BATTERY_CALCULATE_TIME_LEFT_ENABLED false
55-
// #endif
56-
5788
// auto-off feature
5889
#ifndef USERMOD_BATTERY_AUTO_OFF_ENABLED
5990
#define USERMOD_BATTERY_AUTO_OFF_ENABLED true
@@ -78,4 +109,26 @@
78109

79110
#ifndef USERMOD_BATTERY_LOW_POWER_INDICATOR_DURATION
80111
#define USERMOD_BATTERY_LOW_POWER_INDICATOR_DURATION 5
112+
#endif
113+
114+
// battery types
115+
typedef enum
116+
{
117+
unknown=0,
118+
lipo=1,
119+
lion=2
120+
} batteryType;
121+
122+
// used for initial configuration after boot
123+
typedef struct bconfig_t
124+
{
125+
batteryType type;
126+
float minVoltage;
127+
float maxVoltage;
128+
float voltage; // current voltage
129+
int8_t level; // current level
130+
float calibration; // offset or calibration value to fine tune the calculated voltage
131+
float voltageMultiplier;
132+
} batteryConfig;
133+
81134
#endif

usermods/Battery/readme.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ define `USERMOD_BATTERY` in `wled00/my_config.h`
3636
| Name | Unit | Description |
3737
| ----------------------------------------------- | ----------- |-------------------------------------------------------------------------------------- |
3838
| `USERMOD_BATTERY` | | define this (in `my_config.h`) to have this usermod included wled00\usermods_list.cpp |
39-
| `USERMOD_BATTERY_USE_LIPO` | | define this (in `my_config.h`) if you use LiPo rechargeables (1S) |
4039
| `USERMOD_BATTERY_MEASUREMENT_PIN` | | defaults to A0 on ESP8266 and GPIO35 on ESP32 |
4140
| `USERMOD_BATTERY_MEASUREMENT_INTERVAL` | ms | battery check interval. defaults to 30 seconds |
42-
| `USERMOD_BATTERY_MIN_VOLTAGE` | v | minimum battery voltage. default is 2.6 (18650 battery standard) |
43-
| `USERMOD_BATTERY_MAX_VOLTAGE` | v | maximum battery voltage. default is 4.2 (18650 battery standard) |
44-
| `USERMOD_BATTERY_TOTAL_CAPACITY` | mAh | the capacity of all cells in parallel summed up |
45-
| `USERMOD_BATTERY_CALIBRATION` | | offset / calibration number, fine tune the measured voltage by the microcontroller |
41+
| `USERMOD_BATTERY_{TYPE}_MIN_VOLTAGE` | v | minimum battery voltage. default is 2.6 (18650 battery standard) |
42+
| `USERMOD_BATTERY_{TYPE}_MAX_VOLTAGE` | v | maximum battery voltage. default is 4.2 (18650 battery standard) |
43+
| `USERMOD_BATTERY_{TYPE}_TOTAL_CAPACITY` | mAh | the capacity of all cells in parallel summed up |
44+
| `USERMOD_BATTERY_{TYPE}_CALIBRATION` | | offset / calibration number, fine tune the measured voltage by the microcontroller |
4645
| Auto-Off | --- | --- |
4746
| `USERMOD_BATTERY_AUTO_OFF_ENABLED` | true/false | enables auto-off |
4847
| `USERMOD_BATTERY_AUTO_OFF_THRESHOLD` | % (0-100) | when this threshold is reached master power turns off |
@@ -54,6 +53,13 @@ define `USERMOD_BATTERY` in `wled00/my_config.h`
5453

5554
All parameters can be configured at runtime via the Usermods settings page.
5655

56+
**NOTICE:** Each Battery type can be pre-configured individualy (in `my_config.h`)
57+
58+
| Name | Alias | `my_config.h` example |
59+
| --------------- | ------------- | ------------------------------------- |
60+
| Lithium Polymer | lipo (Li-Po) | `USERMOD_BATTERY_lipo_MIN_VOLTAGE` |
61+
| Lithium Ionen | lion (Li-Ion) | `USERMOD_BATTERY_lion_TOTAL_CAPACITY` |
62+
5763
## ⚠️ Important
5864

5965
- Make sure you know your battery specifications! All batteries are **NOT** the same!
@@ -80,6 +86,11 @@ Specification from: [Molicel INR18650-M35A, 3500mAh 10A Lithium-ion battery, 3.
8086

8187
## 📝 Change Log
8288

89+
2024-04-30
90+
91+
- integrate factory pattern to make it easier to add other / custom battery types
92+
- update readme
93+
8394
2023-01-04
8495

8596
- basic support for LiPo rechargeable batteries ( `-D USERMOD_BATTERY_USE_LIPO`)

0 commit comments

Comments
 (0)