kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Update LiPo examples to use PicoGraphics
rodzic
2cbd710620
commit
b8110f3905
|
@ -1,28 +1,30 @@
|
||||||
# This example shows how to read the voltage from a LiPo battery connected to a Raspberry Pi Pico via our Pico Lipo SHIM...
|
# This example shows how to read the voltage from a LiPo battery connected to a Raspberry Pi Pico via our Pico Lipo SHIM
|
||||||
# ...and uses this reading to calculate how much charge is left in the battery.
|
# and uses this reading to calculate how much charge is left in the battery.
|
||||||
# It then displays the info on the screen of Pico Display or Pico Explorer.
|
# It then displays the info on the screen of Pico Display.
|
||||||
# Remember to save this code as main.py on your Pico if you want it to run automatically!
|
# Remember to save this code as main.py on your Pico if you want it to run automatically!
|
||||||
|
|
||||||
from machine import ADC, Pin
|
from machine import ADC, Pin
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# Uncomment one of these lines, depending on what display you have
|
# change to DISPLAY_PICO_DISPLAY_2 for Pico Display 2.0
|
||||||
import picodisplay as display
|
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY
|
||||||
# import picodisplay2 as display
|
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, rotate=0)
|
||||||
# import picoexplorer as display
|
|
||||||
|
|
||||||
# Set up and initialise display
|
display.set_backlight(0.8)
|
||||||
buf = bytearray(display.get_width() * display.get_height() * 2)
|
|
||||||
display.init(buf)
|
|
||||||
display.set_backlight(0.8) # comment out this line if you have a Pico Explorer as it doesn't have a controllable backlight
|
|
||||||
|
|
||||||
vsys = ADC(29) # reads the system input voltage
|
vsys = ADC(29) # reads the system input voltage
|
||||||
charging = Pin(24, Pin.IN) # reading GP24 tells us whether or not USB power is connected
|
charging = Pin(24, Pin.IN) # reading GP24 tells us whether or not USB power is connected
|
||||||
conversion_factor = 3 * 3.3 / 65535
|
conversion_factor = 3 * 3.3 / 65535
|
||||||
|
|
||||||
full_battery = 4.2 # these are our reference voltages for a full/empty battery, in volts
|
full_battery = 4.2 # these are our reference voltages for a full/empty battery, in volts
|
||||||
empty_battery = 2.8 # the values could vary by battery size/manufacturer so you might need to adjust them
|
empty_battery = 2.8 # the values could vary by battery size/manufacturer so you might need to adjust them
|
||||||
|
|
||||||
|
# Create some pen colours for drawing with
|
||||||
|
BLACK = display.create_pen(0, 0, 0)
|
||||||
|
GREY = display.create_pen(190, 190, 190)
|
||||||
|
GREEN = display.create_pen(0, 255, 0)
|
||||||
|
RED = display.create_pen(255, 0, 0)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# convert the raw ADC read into a voltage, and then a percentage
|
# convert the raw ADC read into a voltage, and then a percentage
|
||||||
voltage = vsys.read_u16() * conversion_factor
|
voltage = vsys.read_u16() * conversion_factor
|
||||||
|
@ -31,30 +33,25 @@ while True:
|
||||||
percentage = 100.00
|
percentage = 100.00
|
||||||
|
|
||||||
# draw the battery outline
|
# draw the battery outline
|
||||||
display.set_pen(0, 0, 0)
|
display.set_pen(BLACK)
|
||||||
display.clear()
|
display.clear()
|
||||||
display.set_pen(190, 190, 190)
|
display.set_pen(GREY)
|
||||||
display.rectangle(0, 0, 220, 135)
|
display.rectangle(0, 0, 220, 135)
|
||||||
display.rectangle(220, 40, 20, 55)
|
display.rectangle(220, 40, 20, 55)
|
||||||
display.set_pen(0, 0, 0)
|
display.set_pen(GREEN)
|
||||||
display.rectangle(3, 3, 214, 129)
|
display.rectangle(3, 3, 214, 129)
|
||||||
|
|
||||||
# draw a green box for the battery level
|
# draw a green box for the battery level
|
||||||
display.set_pen(0, 255, 0)
|
display.set_pen(GREEN)
|
||||||
display.rectangle(5, 5, round(210 / 100 * percentage), 125)
|
display.rectangle(5, 5, round(210 / 100 * percentage), 125)
|
||||||
|
|
||||||
# add text
|
# add text
|
||||||
display.set_pen(255, 0, 0)
|
display.set_pen(RED)
|
||||||
if charging.value() == 1: # if it's plugged into USB power...
|
if charging.value() == 1: # if it's plugged into USB power...
|
||||||
display.text("Charging!", 15, 55, 240, 4)
|
display.text("Charging!", 15, 55, 240, 4)
|
||||||
else: # if not, display the battery stats
|
else: # if not, display the battery stats
|
||||||
display.text('{:.2f}'.format(voltage) + "v", 15, 10, 240, 5)
|
display.text('{:.2f}'.format(voltage) + "v", 15, 10, 240, 5)
|
||||||
display.text('{:.0f}%'.format(percentage), 15, 50, 240, 5)
|
display.text('{:.0f}%'.format(percentage), 15, 50, 240, 5)
|
||||||
|
|
||||||
# uncomment for low battery alarm (Pico Explorer only, you'll need to have GP0 and AUDIO connected with a jumper wire)
|
|
||||||
# display.set_audio_pin(0)
|
|
||||||
# if percentage < 5: # if the battery is less than 5%
|
|
||||||
# display.set_tone(262) # then make an annoying noise
|
|
||||||
|
|
||||||
display.update()
|
display.update()
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
|
@ -1,21 +1,17 @@
|
||||||
# This example reads the voltage from a LiPo battery connected to Pimoroni Pico LiPo...
|
# This example reads the voltage from a LiPo battery connected to Pimoroni Pico LiPo
|
||||||
# ...and uses this reading to calculate how much charge is left in the battery.
|
# and uses this reading to calculate how much charge is left in the battery.
|
||||||
# It then displays the info on the screen of Pico Display or Pico Explorer.
|
# It then displays the info on the screen of Pico Display (or Pico Display 2.0).
|
||||||
# With Pimoroni Pico LiPo, you can read the battery percentage while it's charging.
|
# With Pimoroni Pico LiPo, you can read the battery percentage while it's charging.
|
||||||
# Save this code as main.py on your Pico if you want it to run automatically!
|
# Save this code as main.py on your Pico if you want it to run automatically!
|
||||||
|
|
||||||
from machine import ADC, Pin
|
from machine import ADC, Pin
|
||||||
import time
|
import time
|
||||||
|
# change to DISPLAY_PICO_DISPLAY_2 for Pico Display 2.0
|
||||||
|
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY
|
||||||
|
|
||||||
# Uncomment one of these lines, depending on what display you have
|
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, rotate=0)
|
||||||
import picodisplay as display
|
|
||||||
# import picodisplay2 as display
|
|
||||||
# import picoexplorer as display
|
|
||||||
|
|
||||||
# Set up and initialise display
|
display.set_backlight(0.8)
|
||||||
buf = bytearray(display.get_width() * display.get_height() * 2)
|
|
||||||
display.init(buf)
|
|
||||||
display.set_backlight(0.8) # comment out this line if you have a Pico Explorer, it doesn't have a controllable backlight
|
|
||||||
|
|
||||||
vsys = ADC(29) # reads the system input voltage
|
vsys = ADC(29) # reads the system input voltage
|
||||||
charging = Pin(24, Pin.IN) # reading GP24 tells us whether or not USB power is connected
|
charging = Pin(24, Pin.IN) # reading GP24 tells us whether or not USB power is connected
|
||||||
|
@ -24,6 +20,12 @@ conversion_factor = 3 * 3.3 / 65535
|
||||||
full_battery = 4.2 # reference voltages for a full/empty battery, in volts
|
full_battery = 4.2 # reference voltages for a full/empty battery, in volts
|
||||||
empty_battery = 2.8 # the values could vary by battery size/manufacturer so you might need to adjust them
|
empty_battery = 2.8 # the values could vary by battery size/manufacturer so you might need to adjust them
|
||||||
|
|
||||||
|
# Create some pen colours for drawing with
|
||||||
|
BLACK = display.create_pen(0, 0, 0)
|
||||||
|
GREY = display.create_pen(190, 190, 190)
|
||||||
|
GREEN = display.create_pen(0, 255, 0)
|
||||||
|
RED = display.create_pen(255, 0, 0)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
# convert the raw ADC read into a voltage, and then a percentage
|
# convert the raw ADC read into a voltage, and then a percentage
|
||||||
voltage = vsys.read_u16() * conversion_factor
|
voltage = vsys.read_u16() * conversion_factor
|
||||||
|
@ -32,30 +34,25 @@ while True:
|
||||||
percentage = 100
|
percentage = 100
|
||||||
|
|
||||||
# draw the battery outline
|
# draw the battery outline
|
||||||
display.set_pen(0, 0, 0)
|
display.set_pen(BLACK)
|
||||||
display.clear()
|
display.clear()
|
||||||
display.set_pen(190, 190, 190)
|
display.set_pen(GREY)
|
||||||
display.rectangle(0, 0, 220, 135)
|
display.rectangle(0, 0, 220, 135)
|
||||||
display.rectangle(220, 40, 20, 55)
|
display.rectangle(220, 40, 20, 55)
|
||||||
display.set_pen(0, 0, 0)
|
display.set_pen(BLACK)
|
||||||
display.rectangle(3, 3, 214, 129)
|
display.rectangle(3, 3, 214, 129)
|
||||||
|
|
||||||
# draw a green box for the battery level
|
# draw a green box for the battery level
|
||||||
display.set_pen(0, 255, 0)
|
display.set_pen(GREEN)
|
||||||
display.rectangle(5, 5, int((210 / 100) * percentage), 125)
|
display.rectangle(5, 5, int((210 / 100) * percentage), 125)
|
||||||
|
|
||||||
# add text
|
# add text
|
||||||
display.set_pen(255, 0, 0)
|
display.set_pen(RED)
|
||||||
if charging.value() == 1: # if it's plugged into USB power...
|
if charging.value() == 1: # if it's plugged into USB power...
|
||||||
display.text("Charging!", 15, 90, 240, 4)
|
display.text("Charging!", 15, 90, 240, 4)
|
||||||
|
|
||||||
display.text('{:.2f}'.format(voltage) + "v", 15, 10, 240, 5)
|
display.text('{:.2f}'.format(voltage) + "v", 15, 10, 240, 5)
|
||||||
display.text('{:.0f}%'.format(percentage), 15, 50, 240, 5)
|
display.text('{:.0f}%'.format(percentage), 15, 50, 240, 5)
|
||||||
|
|
||||||
# uncomment for low battery alarm (Pico Explorer only, you'll need to have GP0 and AUDIO connected with a jumper wire)
|
|
||||||
# display.set_audio_pin(0)
|
|
||||||
# if percentage < 5: # if the battery is less than 5%
|
|
||||||
# display.set_tone(262) # then make an annoying noise
|
|
||||||
|
|
||||||
display.update()
|
display.update()
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
|
@ -0,0 +1,64 @@
|
||||||
|
# This example reads the voltage from a LiPo battery connected to Pimoroni Pico LiPo
|
||||||
|
# and uses this reading to calculate how much charge is left in the battery.
|
||||||
|
# It then displays the info on the screen of Pico Explorer.
|
||||||
|
# With Pimoroni Pico LiPo, you can read the battery percentage while it's charging.
|
||||||
|
# Save this code as main.py on your Pico if you want it to run automatically!
|
||||||
|
|
||||||
|
from machine import ADC, Pin
|
||||||
|
import time
|
||||||
|
from pimoroni import Buzzer
|
||||||
|
from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER
|
||||||
|
|
||||||
|
display = PicoGraphics(display=DISPLAY_PICO_EXPLORER)
|
||||||
|
|
||||||
|
buzzer = Buzzer(0)
|
||||||
|
|
||||||
|
vsys = ADC(29) # reads the system input voltage
|
||||||
|
charging = Pin(24, Pin.IN) # reading GP24 tells us whether or not USB power is connected
|
||||||
|
conversion_factor = 3 * 3.3 / 65535
|
||||||
|
|
||||||
|
full_battery = 4.2 # reference voltages for a full/empty battery, in volts
|
||||||
|
empty_battery = 2.8 # the values could vary by battery size/manufacturer so you might need to adjust them
|
||||||
|
|
||||||
|
# Create some pen colours for drawing with
|
||||||
|
BLACK = display.create_pen(0, 0, 0)
|
||||||
|
GREY = display.create_pen(190, 190, 190)
|
||||||
|
GREEN = display.create_pen(0, 255, 0)
|
||||||
|
RED = display.create_pen(255, 0, 0)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
buzzer.set_tone(0)
|
||||||
|
|
||||||
|
# convert the raw ADC read into a voltage, and then a percentage
|
||||||
|
voltage = vsys.read_u16() * conversion_factor
|
||||||
|
percentage = 100 * ((voltage - empty_battery) / (full_battery - empty_battery))
|
||||||
|
if percentage > 100:
|
||||||
|
percentage = 100
|
||||||
|
|
||||||
|
# draw the battery outline
|
||||||
|
display.set_pen(BLACK)
|
||||||
|
display.clear()
|
||||||
|
display.set_pen(GREY)
|
||||||
|
display.rectangle(0, 0, 220, 135)
|
||||||
|
display.rectangle(220, 40, 20, 55)
|
||||||
|
display.set_pen(BLACK)
|
||||||
|
display.rectangle(3, 3, 214, 129)
|
||||||
|
|
||||||
|
# draw a green box for the battery level
|
||||||
|
display.set_pen(GREEN)
|
||||||
|
display.rectangle(5, 5, int((210 / 100) * percentage), 125)
|
||||||
|
|
||||||
|
# add text
|
||||||
|
display.set_pen(RED)
|
||||||
|
if charging.value() == 1: # if it's plugged into USB power...
|
||||||
|
display.text("Charging!", 15, 90, 240, 4)
|
||||||
|
|
||||||
|
display.text('{:.2f}'.format(voltage) + "v", 15, 10, 240, 5)
|
||||||
|
display.text('{:.0f}%'.format(percentage), 15, 50, 240, 5)
|
||||||
|
|
||||||
|
# Low battery alarm! (you'll need to have GP0 and AUDIO connected with a jumper wire)
|
||||||
|
if percentage < 5: # if the battery is less than 5%
|
||||||
|
buzzer.set_tone(262) # then make an annoying noise
|
||||||
|
|
||||||
|
display.update()
|
||||||
|
time.sleep(0.5)
|
Ładowanie…
Reference in New Issue