Me again!
Dang, I don’t get this code working on a 2040W… o.O
Is the Badger2040W different from the Tuffy???
puzzled
see what I did and what I get:
charging = Pin('WL_GPIO2', Pin.IN)
(...)
MPY: soft reboot
Traceback (most recent call last):
File "<stdin>", line 294, in <module>
File "<stdin>", line 269, in button
File "<stdin>", line 225, in render
File "<stdin>", line 127, in draw_battery_usage
ValueError: unknown named pin "WL_GPIO2"
The idea was, to write one function for the launcher.py for the Badger2040/Badger2040W and have a “battery bar”, similar to the “memory bar”.
Who stole the ‘WL_GPIO2’???
Please bring it back! (Or call me stupid and tell me, what I did possibly wrong…)
o.O
The Hitchhiker
the whole code
ef draw_battery_usage(x):
# Pico W voltage read function by darconeous on reddit:
#def draw_battery_usage(x):
# Pico W voltage read function by darconeous on reddit:
# https://www.reddit.com/r/raspberrypipico/comments/xalach/comment/ipigfzu/
# in reference of https://pico.pinout.xyz/ and https://picow.pinout.xyz/
# the pins and ports are transfered, to make it work on a badger2040 non-W
# these are our reference voltages for a full/empty battery, in volts
# the values could vary by battery size/manufacturer so you might need to adjust them
# full/empty-pairs for some batteries:
# lipo: 4.2 / 2.8
# 2xAA/2xAAA alkaline: 3.2 / 2.4
# 2xCR20332: 6.0 / 4.0
# 1xCR2440: 3.0 / 2.0 experimental!
full_battery = 4.2 # lowered to 5.0V for CR2032, since pico draws 25mA
empty_battery = 2.8 # lowered to 3.5V for CR2032, since pico draws 25mA
con_fac_2040w = 3 * 3.3 / 2**16 # for 2040W internal 3.3V as referece
con_fac_2040 = 60 * 3.3 / 2**16 # for 2040 its all a bit different...
if badger2040.is_wireless() == True:
wlan = network.WLAN(network.STA_IF)
wlan_active = wlan.active()
try:
# Don't use the WLAN chip for a moment.
wlan.active(False)
# Make sure pin 25 is high.
Pin(25, mode=Pin.OUT, pull=Pin.PULL_DOWN).high()
# Reconfigure pin 29 as an input.
Pin(29, Pin.IN, pull=None)
val_vsys = ADC(29).read_u16() * con_fac_2040w
finally:
# reading 'WL_GPIO2' on a picoW tells us whether or not USB power is connected (VBUS Sense)
charging = Pin('WL_GPIO2', Pin.IN)
# Why the heck this is all over the forums and no one has a problem with it???
# since 'WL_GPIO2' seems not to work anymore on a 2040W
# Restore the pin state and possibly reactivate WLAN
Pin(25, Pin.OUT, value=0, pull=Pin.PULL_DOWN)
Pin(29, Pin.ALT, pull=Pin.PULL_DOWN, alt=7)
wlan.active(wlan_active)
else:
# reading pin24 on a pico tells us whether or not USB power is connected (VBUS Sense)
charging = Pin(24, Pin.IN)
# Configure pin 29 as an input. (Read VSYS/3 through resistor divider and FET Q1)
Pin(25, mode=Pin.OUT, pull=Pin.PULL_DOWN).high()
Pin(29, Pin.IN, pull=None)
val_vsys = ADC(29).read_u16() * con_fac_2040
# convert the val_sys (raw ADC read) into a voltage, and then a percentage
b_level = 100 * ( ( val_vsys - empty_battery) / (full_battery - empty_battery) )
if b_level > 100:
b_level = 100.00
display.set_pen(15)
display.image(
bytearray(
(
0b110011,
0b001100,
0b011110,
0b011110,
0b010010,
0b010010,
0b010010,
0b011110,
0b000001,
)
),
6,
9,
x,
3,
)
# assemble horizontal bar-graph beginning at position x+8 (bc. width of 6px the battery symbol)
# outer white box
display.rectangle(x + 8, 3, 80, 10)
display.set_pen(0)
# inner black box
display.rectangle(x + 9, 4, 78, 8)
# white bar according to percentage
display.set_pen(15)
#print(f"b_level: {b_level}")
# reading 'WL_GPIO2' on a picoW tells us whether or not USB power is connected (VBUS Sense)
# if it's not plugged into USB power...
if charging.value() == False:
# if charging is false, display the battery status:
# bar starts at coordinates x+10,5 and 6px high max length 76px (accordingly to percentage)
display.rectangle(x + 10, 5, int(76 / 100.0 * b_level), 6)
display.text("{:.2f}%".format(b_level), x + 91, 4, WIDTH, 1.0)
else:
# fake full power on USB when "charging" is true
display.rectangle(x + 10, 5, int(76 / 100.0 * 100 ), 6)
display.text("USB", x + 91, 4, WIDTH, 1.0)