Fix filenames in examples README

Add formatting and info to examples README

Add user button to button test example

Add voltage comments for battery in examples
pull/296/head
Andy Piper 2022-03-13 17:22:59 +00:00
rodzic 80e4558209
commit 079be62746
5 zmienionych plików z 118 dodań i 48 usunięć

Wyświetl plik

@ -3,14 +3,16 @@
- [Function Examples](#function-examples)
- [Battery](#battery)
- [Button Test](#button-test)
- [LED](#LED)
- [LED](#led)
- [Pin interrupt](#pin-interrupt)
- [Application Examples](#application-examples)
- [Badge](#badge)
- [Checklist](#checklist)
- [Clock](#clock)
- [E-Reader](#e-reader)
- [E-Book](#e-book)
- [Fonts](#fonts)
- [Image](#image)
- [QR gen](#qr-gen)
- [Launcher](#launcher)
@ -31,6 +33,10 @@ An example of how to read Badger2040's buttons and display a unique message for
Blinks Badger's LED on and off.
### Pin interrupt
[pin_interrupt.py](pin_interrupt.py)
An example of drawing text and graphics and using the buttons.
## Application Examples
@ -38,13 +44,15 @@ Blinks Badger's LED on and off.
[badge.py](badge.py)
Create your own name badge! This application looks for two files on your MicroPython drive:
* "badge.txt" - A text file containing 6 lines, corresponding to the 6 different piece of text on the badge
* "badge-image.bin" - A 108x128px 1bit colour depth image to display alongside the text. You can use examples/badger2040/image_converter/convert.py to convert them:
* `badge.txt` - A text file containing 6 lines, corresponding to the 6 different pieces of text on the badge
* `badge-image.bin` - A 108x128px 1-bit colour depth image to display alongside the text. You can use `examples/badger2040/image_converter/convert.py` to convert them:
```shell
python3 convert.py --binary --resize image_file_1.png image_file_2.png image_file_3.png
```
### Checklist
[checklist.py](checklist.py)
[list.py](list.py)
A checklist application, letting you navigate through items and tick each of them off.
@ -53,24 +61,32 @@ A checklist application, letting you navigate through items and tick each of the
A simple clock showing the time and date, that uses the E Ink's fast speed to update every second
### E-Reader
[e-reader.py](e-reader.py)
### E-Book
[ebook.py](ebook.py)
A mini text file e-reader. Comes pre-loaded with an excerpt of The Wind In the Willows.
### Fonts
[Fonts.py](checklist.py)
[fonts.py](fonts.py)
A demonstration of the various fonts that can be used in your programs.
### Image
[image.py](image.py)
An image gallery. Displays and lets you cycle through any images stored within the MicroPython device's `/images` directory. Images must be 296x128 pixels with 1bit colour depth. You can use examples/badger2040/image_converter/convert.py to convert them:
An image gallery. Displays and lets you cycle through any images stored within the MicroPython device's `/images` directory. Images must be 296x128 pixels with 1-bit colour depth. You can use `examples/badger2040/image_converter/convert.py` to convert them:
```shell
python3 convert.py --binary --resize image_file_1.png image_file_2.png image_file_3.png
```
### QR gen
[qrgen.py](qrgen.py)
This application looks for a file on your MicroPython drive:
- `qrcode.txt` - A text file containing 9 lines. The first line should be a URL which will be converted into and displayed as a QR code. Up to 8 more lines of information can be added, which will be shown as plain text to the right of the QR code.
### Launcher
[Launcher.py](launcher.py)
[launcher.py](launcher.py)
A launcher style application, that gives a menu of other applications that can be loaded, as well as information such as battery level.
A launcher-style application, that provide a menu of other applications that can be loaded, as well as information such as battery level.

Wyświetl plik

@ -3,6 +3,7 @@ from machine import Pin, ADC
import time
# Global Constants
# for e.g. 2xAAA batteries, try max 3.4 min 3.0
MAX_BATTERY_VOLTAGE = 4.0
MIN_BATTERY_VOLTAGE = 3.2
@ -26,6 +27,7 @@ NUM_BATT_BARS = 4
# Utility functions
# ------------------------------
def map_value(input, in_min, in_max, out_min, out_max):
return (((input - in_min) * (out_max - out_min)) / (in_max - in_min)) + out_min
@ -43,15 +45,24 @@ def draw_battery(level, resolution):
# Draw the battery outline
display.pen(0)
display.rectangle((WIDTH - BATT_WIDTH) // 2, (HEIGHT - BATT_HEIGHT) // 2,
BATT_WIDTH, BATT_HEIGHT)
display.rectangle(
(WIDTH - BATT_WIDTH) // 2, (HEIGHT - BATT_HEIGHT) // 2, BATT_WIDTH, BATT_HEIGHT
)
display.rectangle((WIDTH + BATT_WIDTH) // 2, (HEIGHT - BATT_TERM_HEIGHT) // 2,
BATT_TERM_WIDTH, BATT_TERM_HEIGHT)
display.rectangle(
(WIDTH + BATT_WIDTH) // 2,
(HEIGHT - BATT_TERM_HEIGHT) // 2,
BATT_TERM_WIDTH,
BATT_TERM_HEIGHT,
)
display.pen(15)
display.rectangle((WIDTH - BATT_WIDTH) // 2 + BATT_BORDER, (HEIGHT - BATT_HEIGHT) // 2 + BATT_BORDER,
BATT_WIDTH - BATT_BORDER * 2, BATT_HEIGHT - BATT_BORDER * 2)
display.rectangle(
(WIDTH - BATT_WIDTH) // 2 + BATT_BORDER,
(HEIGHT - BATT_HEIGHT) // 2 + BATT_BORDER,
BATT_WIDTH - BATT_BORDER * 2,
BATT_HEIGHT - BATT_BORDER * 2,
)
# Add a special check for no battery
if level < 1:
@ -64,25 +75,39 @@ def draw_battery(level, resolution):
start_extra = thickness // 3
end_extra = (thickness * 2) // 3
for i in range(0, thickness):
excess = (i // 2)
display.line(X - (BATT_HEIGHT // 2) + i - excess - start_extra, Y - (BATT_HEIGHT // 2) - excess - start_extra,
X + (BATT_HEIGHT // 2) + i - excess + end_extra, Y + (BATT_HEIGHT // 2) - excess + end_extra)
excess = i // 2
display.line(
X - (BATT_HEIGHT // 2) + i - excess - start_extra,
Y - (BATT_HEIGHT // 2) - excess - start_extra,
X + (BATT_HEIGHT // 2) + i - excess + end_extra,
Y + (BATT_HEIGHT // 2) - excess + end_extra,
)
display.pen(15)
for i in range(0 - thickness, 0):
display.line(X - (BATT_HEIGHT // 2) + i, Y - (BATT_HEIGHT // 2),
X + (BATT_HEIGHT // 2) + i, Y + (BATT_HEIGHT // 2))
display.line(
X - (BATT_HEIGHT // 2) + i,
Y - (BATT_HEIGHT // 2),
X + (BATT_HEIGHT // 2) + i,
Y + (BATT_HEIGHT // 2),
)
else:
# Draw the battery bars
display.pen(0)
length = (BATT_BAR_END - BATT_BAR_START - ((NUM_BATT_BARS - 1) * BATT_BAR_PADDING)) // NUM_BATT_BARS
length = (
BATT_BAR_END - BATT_BAR_START - ((NUM_BATT_BARS - 1) * BATT_BAR_PADDING)
) // NUM_BATT_BARS
current_level = 0.0
normalised_level = level / resolution
for i in range(NUM_BATT_BARS):
current_level = (1.0 * i) / NUM_BATT_BARS
if normalised_level > current_level:
pos = i * (length + BATT_BAR_PADDING)
display.rectangle(BATT_BAR_START + pos, (HEIGHT - BATT_BAR_HEIGHT) // 2,
length, BATT_BAR_HEIGHT)
display.rectangle(
BATT_BAR_START + pos,
(HEIGHT - BATT_BAR_HEIGHT) // 2,
length,
BATT_BAR_HEIGHT,
)
display.update()
@ -115,7 +140,9 @@ while True:
# Calculate the logic supply voltage, as will be lower that the usual 3.3V when running off low batteries
vdd = 1.24 * (65535 / vref_adc.read_u16())
vbat = (vbat_adc.read_u16() / 65535) * 3 * vdd # 3 in this is a gain, not rounding of 3.3V
vbat = (
(vbat_adc.read_u16() / 65535) * 3 * vdd
) # 3 in this is a gain, not rounding of 3.3V
# Disable the onboard voltage reference
vref_en.value(0)
@ -124,7 +151,9 @@ while True:
print("Battery Voltage = ", vbat, "V", sep="")
# Convert the voltage to a level to display onscreen
level = int(map_value(vbat, MIN_BATTERY_VOLTAGE, MAX_BATTERY_VOLTAGE, 0, NUM_BATT_BARS))
level = int(
map_value(vbat, MIN_BATTERY_VOLTAGE, MAX_BATTERY_VOLTAGE, 0, NUM_BATT_BARS)
)
# Only draw if the battery level has changed significantly
if level != last_level:

Wyświetl plik

@ -13,6 +13,9 @@ button_b = machine.Pin(badger2040.BUTTON_B, machine.Pin.IN, machine.Pin.PULL_DOW
button_c = machine.Pin(badger2040.BUTTON_C, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_up = machine.Pin(badger2040.BUTTON_UP, machine.Pin.IN, machine.Pin.PULL_DOWN)
button_down = machine.Pin(badger2040.BUTTON_DOWN, machine.Pin.IN, machine.Pin.PULL_DOWN)
# the User button (boot/usr on back of board) is inverted from the others
button_user = machine.Pin(badger2040.BUTTON_USER, machine.Pin.IN, machine.Pin.PULL_UP)
message = None
message_y = 60
@ -23,19 +26,22 @@ def button(pin):
if message is not None:
return
if pin == button_a:
message = "Button A"
message = "Button a"
return
if pin == button_b:
message = "Button B"
message = "Button b"
return
if pin == button_c:
message = "Button C"
message = "Button c"
return
if pin == button_up:
message = "Button U"
message = "Button Up"
return
if pin == button_down:
message = "Button D"
message = "Button Down"
return
if pin == button_user:
message = "Button Usr"
return
@ -45,6 +51,7 @@ button_c.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
button_up.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
button_down.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
button_user.irq(trigger=machine.Pin.IRQ_RISING, handler=button)
while True:
@ -53,7 +60,7 @@ while True:
display.clear()
display.pen(0)
display.thickness(4)
display.text(message, 10, message_y, 2.0)
display.text(message, 6, message_y, 1.4)
for _ in range(2):
display.update()
message = None

Wyświetl plik

@ -7,6 +7,7 @@ import badger2040
from badger2040 import WIDTH
import launchericons
# for e.g. 2xAAA batteries, try max 3.4 min 3.0
MAX_BATTERY_VOLTAGE = 4.0
MIN_BATTERY_VOLTAGE = 3.2
@ -27,7 +28,7 @@ examples = [
("_badge", 5),
("_qrgen", 8),
("_info", 6),
("_help", 7)
("_help", 7),
]
font_sizes = (0.5, 0.7, 0.9)
@ -67,7 +68,9 @@ def get_battery_level():
# Calculate the logic supply voltage, as will be lower that the usual 3.3V when running off low batteries
vdd = 1.24 * (65535 / vref_adc.read_u16())
vbat = (vbat_adc.read_u16() / 65535) * 3 * vdd # 3 in this is a gain, not rounding of 3.3V
vbat = (
(vbat_adc.read_u16() / 65535) * 3 * vdd
) # 3 in this is a gain, not rounding of 3.3V
# Disable the onboard voltage reference
vref_en.value(0)
@ -112,15 +115,24 @@ def draw_disk_usage(x):
f_used = 100 / f_total_size * f_total_used
# f_free = 100 / f_total_size * f_total_free
display.image(bytearray((
0b00000000,
0b00111100,
0b00111100,
0b00111100,
0b00111000,
0b00000000,
0b00000000,
0b00000001)), 8, 8, x, 4)
display.image(
bytearray(
(
0b00000000,
0b00111100,
0b00111100,
0b00111100,
0b00111000,
0b00000000,
0b00000000,
0b00000001,
)
),
8,
8,
x,
4,
)
display.pen(15)
display.rectangle(x + 10, 3, 80, 10)
display.pen(0)
@ -136,7 +148,7 @@ def render():
display.pen(0)
display.thickness(2)
max_icons = min(3, len(examples[(page * 3):]))
max_icons = min(3, len(examples[(page * 3) :]))
for i in range(max_icons):
x = centers[i]
@ -175,8 +187,8 @@ def launch(file):
try:
__import__(file[1:]) # Try to import _[file] (drop underscore prefix)
except ImportError:
__import__(file) # Failover to importing [_file]
machine.reset() # Exit back to launcher
__import__(file) # Failover to importing [_file]
machine.reset() # Exit back to launcher
def launch_example(index):
@ -228,7 +240,13 @@ display.update_speed(badger2040.UPDATE_FAST)
# Wait for wakeup button to be released
while button_a.value() or button_b.value() or button_c.value() or button_up.value() or button_down.value():
while (
button_a.value()
or button_b.value()
or button_c.value()
or button_up.value()
or button_down.value()
):
pass