Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit272989d

Browse files
Fix battery voltage scale and cleanup
1 parentaea5f7a commit272989d

File tree

4 files changed

+14
-70
lines changed

4 files changed

+14
-70
lines changed

‎internal_filesystem/boot.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@
8282

8383
# Battery voltage ADC measuring
8484
importmpos.battery_voltage
85-
mpos.battery_voltage.init_adc(5)
85+
mpos.battery_voltage.init_adc(5,262/100000)
8686

8787
print("boot.py finished")

‎internal_filesystem/boot_fri3d-2024.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,6 @@ def keypad_read_cb(indev, data):
214214

215215
# Battery voltage ADC measuring
216216
importmpos.battery_voltage
217-
mpos.battery_voltage.init_adc(13)
217+
mpos.battery_voltage.init_adc(13,2/1000)
218218

219219
print("boot.py finished")
Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,25 @@
1+
importtime
2+
3+
MIN_VOLTAGE=3.15
4+
MAX_VOLTAGE=4.15
5+
16
adc=None
2-
have_adc=True
7+
scale_factor=0
38

49
# This gets called by (the device-specific) boot*.py
5-
definit_adc(pinnr):
10+
definit_adc(pinnr,scale_factor):
611
globaladc
712
try:
813
frommachineimportADC,Pin
9-
print("setting adc")
14+
print("Initializing ADC pin {pinnr} with scale_factor {scale_factor}")
1015
adc=ADC(Pin(pinnr))
11-
print("adc set")
1216
# Set ADC to 11dB attenuation for 0–3.3V range (common for ESP32)
1317
adc.atten(ADC.ATTN_11DB)
1418
exceptExceptionase:
1519
print("Info: this platform has no ADC for measuring battery voltage")
16-
have_adc=False
17-
18-
importtime
1920

20-
# ADC parameters
21-
VREF=3.3# Reference voltage (3.3V for most boards, adjust if different)
22-
ADC_MAX=4095# 12-bit ADC resolution
23-
VOLTAGE_DIVIDER=3# (R1 + R2) / R2 = (200k + 100k) / 100k = 3
24-
25-
MIN_VOLTAGE=3.7
26-
MAX_VOLTAGE=4.2
27-
28-
# On fri3d-2024, it's 2367 for full
29-
# Battery voltage ranges from 3.15V (0%) to 4.15 (100%) as datasheet specifies
30-
# 3.0 +/- 0.1V (discharge cut-off) to 4.2V (no margin of error provided, assuming 0.05V)
31-
# Charger stops charging at 4.07V (92%) to reduce battery wear.
32-
#define RG_BATTERY_CALC_PERCENT(raw) (((raw) * 2.f - 3150.f) / (4150.f - 3150.f) * 100.f)
33-
#define RG_BATTERY_CALC_VOLTAGE(raw) ((raw) * 2.f * 0.001f)
34-
35-
36-
# USB connected, full battery: VBAT 4.179 5V: 5.1
37-
# read_battery_voltage raw_value: 1598.4
38-
# read_battery_voltage raw_value: 1598.1
39-
# read_battery_voltage raw_value: 1596.5
40-
# on display: 1596.8
41-
# => FULL is 1597 at 4.193V so * 2.6255 / 1000
42-
#
43-
# unplugged: VBAT: 4.157 5V: 0
44-
# 1591.3: 4.1572.6123
45-
# 1588.5: 4.156
46-
# 1580.4: 4.142.619
47-
# 1577.8: 4.12
48-
# 1561.2: 4.082.61
49-
# 1555: 4.06
50-
# 1505: 3.952.624
51-
# 1489: 3.90
52-
# 1470: 3.852.61
53-
# 1454: 3.8
54-
# 1445: 3.81
55-
# 1412 should be empty 3.7
56-
#
57-
# USB connected, no battery:
58-
# 1566 * 0.00261 = 4.08V = 75%
59-
# 1566 * 0.00241 = 3.77V = 14%
60-
# 1564-1567
61-
#
62-
# quite empty and charging:
63-
# 1594: 4.18V
64-
# 16..
65-
#
66-
# logically, it should be * 0.00241758241758 but emperically, it seems to be * 0.00261 which is 8% higher
67-
# => Let's go with 0.00262
6821
defread_battery_voltage():
69-
ifnothave_adc:
22+
ifnotadc:
7023
importrandom
7124
random_voltage=random.randint(round(MIN_VOLTAGE*100),round(MAX_VOLTAGE*100))/100
7225
#print(f"returning random voltage: {random_voltage}")
@@ -79,23 +32,12 @@ def read_battery_voltage():
7932
total=total+adc.read()
8033
raw_value=total/10
8134
#print(f"read_battery_voltage raw_value: {raw_value}")
82-
# Convert to voltage, accounting for divider and reference
83-
#voltage = (raw_value / ADC_MAX) * VREF * VOLTAGE_DIVIDER
84-
#voltage = raw_value * 262 / 100000 # 2inch
85-
voltage=raw_value*2/1000# fri3d-2024
35+
voltage=raw_value*scale_factor
8636
# Clamp to 0–4.2V range for LiPo battery
8737
voltage=max(0,min(voltage,MAX_VOLTAGE))
88-
#return raw_value
8938
returnvoltage
9039

9140
# Could be interesting to keep a "rolling average" of the percentage so that it doesn't fluctuate too quickly
9241
defget_battery_percentage():
9342
return (read_battery_voltage()-MIN_VOLTAGE)*100/ (MAX_VOLTAGE-MIN_VOLTAGE)
9443

95-
96-
# Main loop to read and print battery voltage
97-
ifFalse:
98-
#for _ in range(10):
99-
battery_voltage=read_battery_voltage()
100-
print("Battery Voltage: {:.2f} V".format(battery_voltage))
101-
time.sleep(1)# Wait 1 second

‎internal_filesystem/lib/mpos/ui/topmenu.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
importlvglaslv
22

33
importmpos.ui
4+
importmpos.battery_voltage
5+
46
frommpos.ui.animimportWidgetAnimator
57

68
NOTIFICATION_BAR_HEIGHT=24

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp