I’ve made a few changes to ebook.py to make the badger more usable for me as an ereader, but am really struggling to add a more legible font.
The best I can get from the pre-installed fonts is 8-bit double sized (so it’s very pixellated).
Any simple way to get a pixel font that’s designed to be larger on ebook.py? For example the larger python fonts here: Fonts — st7789py 0.1.0 documentation
I’m not much of a developer though. Just tried to integrate it with ebook.py but doubt I’ll figure it out. This is my reader code (a cut-down version of the standard example)…
import badger2040
import time
import gc
import badger_os
text_file = "book.txt"
gc.collect()
# Global Constants
WIDTH = badger2040.WIDTH
HEIGHT = badger2040.HEIGHT
TEXT_PADDING = 0
TEXT_WIDTH = badger2040.WIDTH
# ------------------------------
# Drawing functions
# ------------------------------
# Draw the frame of the reader
def draw_frame():
display.pen(15)
display.clear()
display.pen(15)
display.rectangle(WIDTH, 0, 0, HEIGHT)
display.pen(15)
# ------------------------------
# Program setup
# ------------------------------
# Global variables
state = {
"last_offset": 0,
"current_page": 0,
"text_size": 1,
"offsets": []
}
badger_os.state_load("ebook", state)
text_spacing = int(12 * state["text_size"])
# Create a new Badger and set it to update speed
display = badger2040.Badger2040()
display.led(1) # LED off
display.update_speed(badger2040.UPDATE_FAST)
badger2040.system_speed(4) # Highest system speed
# ------------------------------
# Render page
# ------------------------------
def render_page():
row = 0
line = ""
pos = ebook.tell()
next_pos = pos
add_newline = False
display.font("bitmap8")
while True:
# Read a full line and split it into words
words = ebook.readline().split(" ")
# Take the length of the first word and advance our position
next_word = words[0]
if len(words) > 1:
next_pos += len(next_word) + 1
else:
next_pos += len(next_word) # This is the last word on the line
# Advance our position further if the word contains special characters
if '\u201c' in next_word:
next_word = next_word.replace('\u201c', '\"')
next_pos += 2
if '\u201d' in next_word:
next_word = next_word.replace('\u201d', '\"')
next_pos += 2
if '\u2019' in next_word:
next_word = next_word.replace('\u2019', '\'')
next_pos += 2
# Rewind the file back from the line end to the start of the next word
ebook.seek(next_pos)
# Strip out any new line characters from the word
next_word = next_word.strip()
# If an empty word is encountered assume that means there was a blank line
if len(next_word) == 0:
add_newline = True
# Append the word to the current line and measure its length
appended_line = line
if len(line) > 0 and len(next_word) > 0:
appended_line += " "
appended_line += next_word
appended_length = display.measure_text(appended_line, state["text_size"])
# Would this appended line be longer than the text display area, or was a blank line spotted?
if appended_length >= TEXT_WIDTH or add_newline:
# Yes, so write out the line prior to the append
print(line)
display.pen(0)
display.thickness(1)
display.text(line, TEXT_PADDING, (row * text_spacing) + (text_spacing // 2) + TEXT_PADDING, state["text_size"])
# Clear the line and move on to the next row
line = ""
row += 1
# Have we reached the end of the page?
if (row * text_spacing) + text_spacing >= HEIGHT:
print("+++++")
display.update()
# Reset the position to the start of the word that made this line too long
ebook.seek(pos)
return
else:
# Set the line to the word and advance the current position
line = next_word
pos = next_pos
# A new line was spotted, so advance a row
if add_newline:
print("")
row += 1
if (row * text_spacing) + text_spacing >= HEIGHT:
print("+++++")
display.update()
return
add_newline = False
else:
# The appended line was not too long, so set it as the line and advance the current position
line = appended_line
pos = next_pos
# ------------------------------
# Main program loop
# ------------------------------
launch = True
changed = False
# Open the book file
ebook = open(text_file, "r")
if len(state["offsets"]) > state["current_page"]:
ebook.seek(state["offsets"][state["current_page"]])
else:
state["current_page"] = 0
state["offsets"] = []
while True:
# Was the next page button pressed?
if display.pressed(badger2040.BUTTON_DOWN):
state["current_page"] += 1
changed = True
# Was the previous page button pressed?
if display.pressed(badger2040.BUTTON_UP):
if state["current_page"] > 0:
state["current_page"] -= 1
if state["current_page"] == 0:
ebook.seek(0)
else:
ebook.seek(state["offsets"][state["current_page"] - 1]) # Retrieve the start position of the last page
changed = True
if launch and not changed:
if state["current_page"] > 0 and len(state["offsets"]) > state["current_page"] - 1:
ebook.seek(state["offsets"][state["current_page"] - 1])
changed = True
launch = False
if changed:
draw_frame()
render_page()
# Is the next page one we've not displayed before?
if state["current_page"] >= len(state["offsets"]):
state["offsets"].append(ebook.tell()) # Add its start position to the state["offsets"] list
badger_os.state_save("ebook", state)
changed = False
display.halt()