Skip to content
Snippets Groups Projects
Verified Commit c7f59d3e authored by rahix's avatar rahix
Browse files

fix(text_reader): Convert to unix line-endings


Signed-off-by: default avatarRahix <rahix@rahix.de>
parent 78a7a7f4
Branches master
No related tags found
No related merge requests found
Pipeline #2607 passed
""" """
Text Reader Script Text Reader Script
================= =================
This script will list and display text files This script will list and display text files
""" """
import buttons import buttons
import color import color
import display import display
import os import os
import utime import utime
STATE_LIST = "List" STATE_LIST = "List"
STATE_SHOW = "Show" STATE_SHOW = "Show"
SPECIAL_NO_FILES = "# no txt files" SPECIAL_NO_FILES = "# no txt files"
SPECIAL_EXIT = "[ exit ]" SPECIAL_EXIT = "[ exit ]"
SPECIAL_EMPTY = "# empty file" SPECIAL_EMPTY = "# empty file"
BUTTON_TIMER_POPPED = -1 BUTTON_TIMER_POPPED = -1
def list_files(): def list_files():
"""Create a list of available text files.""" """Create a list of available text files."""
files = sorted(os.listdir("/")) files = sorted(os.listdir("/"))
# Filter for text files # Filter for text files
files = [txt for txt in files if txt.endswith(".txt")] files = [txt for txt in files if txt.endswith(".txt")]
return files return files
def triangle(disp, x, y, left): def triangle(disp, x, y, left):
"""Draw a triangle to show there's more text in this line""" """Draw a triangle to show there's more text in this line"""
yf = 1 if left else -1 yf = 1 if left else -1
scale = 6 scale = 6
disp.line(x - scale * yf, int(y + scale / 2), x, y, col=[255,0,0]) disp.line(x - scale * yf, int(y + scale / 2), x, y, col=[255,0,0])
disp.line(x, y, x, y + scale, col=[255,0,0]) disp.line(x, y, x, y + scale, col=[255,0,0])
disp.line(x, y + scale, x - scale * yf, y + int(scale / 2), col=[255,0,0]) disp.line(x, y + scale, x - scale * yf, y + int(scale / 2), col=[255,0,0])
def button_events(timeout=0): def button_events(timeout=0):
"""Iterate over button presses (event-loop).""" """Iterate over button presses (event-loop)."""
yield 0 yield 0
button_pressed = False button_pressed = False
count = 0 count = 0
while True: while True:
v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT)
if timeout > 0 and count > 0 and count % timeout == 0: if timeout > 0 and count > 0 and count % timeout == 0:
yield BUTTON_TIMER_POPPED yield BUTTON_TIMER_POPPED
if timeout > 0: if timeout > 0:
count += 1 count += 1
if v == 0: if v == 0:
button_pressed = False button_pressed = False
if not button_pressed and v & buttons.BOTTOM_LEFT != 0: if not button_pressed and v & buttons.BOTTOM_LEFT != 0:
button_pressed = True button_pressed = True
yield buttons.BOTTOM_LEFT yield buttons.BOTTOM_LEFT
if not button_pressed and v & buttons.BOTTOM_RIGHT != 0: if not button_pressed and v & buttons.BOTTOM_RIGHT != 0:
button_pressed = True button_pressed = True
yield buttons.BOTTOM_RIGHT yield buttons.BOTTOM_RIGHT
if not button_pressed and v & buttons.TOP_RIGHT != 0: if not button_pressed and v & buttons.TOP_RIGHT != 0:
button_pressed = True button_pressed = True
yield buttons.TOP_RIGHT yield buttons.TOP_RIGHT
utime.sleep_ms(10) utime.sleep_ms(10)
COLOR1, COLOR2 = (color.CHAOSBLUE_DARK, color.CHAOSBLUE) COLOR1, COLOR2 = (color.CHAOSBLUE_DARK, color.CHAOSBLUE)
def file_len(filename): def file_len(filename):
i = -1 i = -1
with open(filename) as fh: with open(filename) as fh:
for i, l in enumerate(fh): for i, l in enumerate(fh):
pass pass
return i + 1 return i + 1
def draw_filecontent(disp, filename, pos, linecount, lineoffset = 0): def draw_filecontent(disp, filename, pos, linecount, lineoffset = 0):
disp.clear() disp.clear()
with open(filename) as fh: with open(filename) as fh:
# stop if file is empty # stop if file is empty
if linecount <= 0: if linecount <= 0:
disp.print( disp.print(
SPECIAL_EMPTY, SPECIAL_EMPTY,
posy=20, posy=20,
bg=color.BLACK bg=color.BLACK
) )
return return
# calc start position # calc start position
start = 0 start = 0
if pos > 0: if pos > 0:
start = pos-1 start = pos-1
if start + 4 > linecount: if start + 4 > linecount:
start = linecount - 4 start = linecount - 4
if start < 0: if start < 0:
start = 0 start = 0
# loop throuhg all lines # loop throuhg all lines
for i, line in enumerate(fh): for i, line in enumerate(fh):
if i >= start + 4 or i >= linecount: if i >= start + 4 or i >= linecount:
break break
if i >= start: if i >= start:
disp.rect( disp.rect(
0, (i - start) * 20, 159, (i - start) * 20 + 20, 0, (i - start) * 20, 159, (i - start) * 20 + 20,
col=COLOR1 if i == pos else COLOR2 col=COLOR1 if i == pos else COLOR2
) )
off = 0 off = 0
linelength = len(line) linelength = len(line)
if i == pos and linelength > 11 and lineoffset > 0: if i == pos and linelength > 11 and lineoffset > 0:
off = lineoffset if lineoffset + 11 < linelength else linelength - 11 off = lineoffset if lineoffset + 11 < linelength else linelength - 11
if lineoffset > linelength: if lineoffset > linelength:
off = 0 off = 0
disp.print( disp.print(
line[off:(off+11)], line[off:(off+11)],
posy=(i - start) * 20, posy=(i - start) * 20,
bg=COLOR1 if i == pos else COLOR2 bg=COLOR1 if i == pos else COLOR2
) )
if linelength > 11 and off < linelength - 11: if linelength > 11 and off < linelength - 11:
triangle(disp, 153, (i - start) * 20 + 6, False) triangle(disp, 153, (i - start) * 20 + 6, False)
if off > 0: if off > 0:
triangle(disp, 6, (i - start) * 20 + 6, True) triangle(disp, 6, (i - start) * 20 + 6, True)
disp.update() disp.update()
def draw_filelist(disp, filelist, pos, filecount, lineoffset): def draw_filelist(disp, filelist, pos, filecount, lineoffset):
disp.clear() disp.clear()
start = 0 start = 0
if pos > 0: if pos > 0:
start = pos-1 start = pos-1
if start + 4 > filecount: if start + 4 > filecount:
start = filecount - 4 start = filecount - 4
if start < 0: if start < 0:
start = 0 start = 0
for i, line in enumerate(filelist): for i, line in enumerate(filelist):
if i >= start + 4 or i >= filecount: if i >= start + 4 or i >= filecount:
break break
if i >= start: if i >= start:
disp.rect( disp.rect(
0, (i - start) * 20, 159, (i - start) * 20 + 20, 0, (i - start) * 20, 159, (i - start) * 20 + 20,
col=COLOR1 if i == pos else COLOR2 col=COLOR1 if i == pos else COLOR2
) )
off = 0 off = 0
linelength = len(line) linelength = len(line)
if i == pos and linelength > 10 and lineoffset > 0: if i == pos and linelength > 10 and lineoffset > 0:
off = lineoffset if lineoffset + 10 < linelength else linelength - 10 off = lineoffset if lineoffset + 10 < linelength else linelength - 10
if lineoffset > linelength: if lineoffset > linelength:
off = 0 off = 0
disp.print( disp.print(
" " + line[off:(off+10)], " " + line[off:(off+10)],
posy=(i - start) * 20, posy=(i - start) * 20,
bg=COLOR1 if i == pos else COLOR2 bg=COLOR1 if i == pos else COLOR2
) )
if i == pos: if i == pos:
disp.print(">", posy=(i - start) * 20, fg=color.COMMYELLOW, bg=COLOR1) disp.print(">", posy=(i - start) * 20, fg=color.COMMYELLOW, bg=COLOR1)
if linelength > 10 and off < linelength - 10: if linelength > 10 and off < linelength - 10:
triangle(disp, 153, (i - start) * 20 + 6, False) triangle(disp, 153, (i - start) * 20 + 6, False)
if off > 0: if off > 0:
triangle(disp, 24, (i - start) * 20 + 6, True) triangle(disp, 24, (i - start) * 20 + 6, True)
disp.update() disp.update()
def main(): def main():
disp = display.open() disp = display.open()
current_state = STATE_LIST current_state = STATE_LIST
# list files variables # list files variables
filelist = list_files() filelist = list_files()
if len(filelist) == 0: if len(filelist) == 0:
filelist.append(SPECIAL_NO_FILES) filelist.append(SPECIAL_NO_FILES)
filelist.append(SPECIAL_EXIT) filelist.append(SPECIAL_EXIT)
numfiles = len(filelist) numfiles = len(filelist)
current_file = 0 current_file = 0
# show files variables # show files variables
filename = "" filename = ""
linecount = 0 linecount = 0
linepos = 0 linepos = 0
lineoffset = 0 lineoffset = 0
lineoffdir = 0 lineoffdir = 0
timerscrollspeed = 1 timerscrollspeed = 1
timerstartscroll = 5 timerstartscroll = 5
timercountpopped = 0 timercountpopped = 0
for ev in button_events(10): for ev in button_events(10):
# list files # list files
if current_state == STATE_LIST: if current_state == STATE_LIST:
if ev == buttons.BOTTOM_RIGHT: if ev == buttons.BOTTOM_RIGHT:
# Scroll down # Scroll down
current_file = (current_file + 1) % numfiles current_file = (current_file + 1) % numfiles
lineoffset = 0 lineoffset = 0
timercountpopped = 0 timercountpopped = 0
elif ev == buttons.BOTTOM_LEFT: elif ev == buttons.BOTTOM_LEFT:
# Scroll up # Scroll up
current_file = (current_file + numfiles - 1) % numfiles current_file = (current_file + numfiles - 1) % numfiles
lineoffset = 0 lineoffset = 0
timercountpopped = 0 timercountpopped = 0
elif ev == BUTTON_TIMER_POPPED: elif ev == BUTTON_TIMER_POPPED:
timercountpopped += 1 timercountpopped += 1
if timercountpopped >= timerstartscroll and (timercountpopped - timerstartscroll) % timerscrollspeed == 0: if timercountpopped >= timerstartscroll and (timercountpopped - timerstartscroll) % timerscrollspeed == 0:
lineoffset += 1 lineoffset += 1
elif ev == buttons.TOP_RIGHT: elif ev == buttons.TOP_RIGHT:
filename = filelist [ current_file % numfiles ] filename = filelist [ current_file % numfiles ]
# exit or ignore # exit or ignore
if filename == SPECIAL_EXIT: if filename == SPECIAL_EXIT:
os.exit() os.exit()
elif filename == SPECIAL_NO_FILES: elif filename == SPECIAL_NO_FILES:
continue continue
# show file, switch state and draw # show file, switch state and draw
current_state = STATE_SHOW current_state = STATE_SHOW
disp.clear().update() disp.clear().update()
# reset variables # reset variables
linepos = 0 linepos = 0
lineoffset = 0 lineoffset = 0
timercountpopped = 0 timercountpopped = 0
linecount = file_len(filename) linecount = file_len(filename)
# draw # draw
draw_filecontent(disp, filename, linepos, linecount, lineoffset) draw_filecontent(disp, filename, linepos, linecount, lineoffset)
continue continue
draw_filelist(disp, filelist, current_file, numfiles, lineoffset) draw_filelist(disp, filelist, current_file, numfiles, lineoffset)
# show files # show files
elif current_state == STATE_SHOW: elif current_state == STATE_SHOW:
if ev == buttons.BOTTOM_RIGHT: if ev == buttons.BOTTOM_RIGHT:
if linepos < (linecount - 1): if linepos < (linecount - 1):
# Scroll down # Scroll down
linepos += 1 linepos += 1
else: else:
# goto first line # goto first line
linepos = 0 linepos = 0
lineoffset = 0 lineoffset = 0
timercountpopped = 0 timercountpopped = 0
elif ev == buttons.BOTTOM_LEFT: elif ev == buttons.BOTTOM_LEFT:
if linepos > 0: if linepos > 0:
# Scroll up # Scroll up
linepos -= 1 linepos -= 1
else: else:
# got to last line # got to last line
linepos = linecount - 1 linepos = linecount - 1
lineoffset = 0 lineoffset = 0
timercountpopped = 0 timercountpopped = 0
elif ev == BUTTON_TIMER_POPPED: elif ev == BUTTON_TIMER_POPPED:
timercountpopped += 1 timercountpopped += 1
if timercountpopped >= timerstartscroll and (timercountpopped - timerstartscroll) % timerscrollspeed == 0: if timercountpopped >= timerstartscroll and (timercountpopped - timerstartscroll) % timerscrollspeed == 0:
lineoffset += 1 lineoffset += 1
elif ev == buttons.TOP_RIGHT: elif ev == buttons.TOP_RIGHT:
# go back to file menu # go back to file menu
current_state = STATE_LIST current_state = STATE_LIST
lineoffset = 0 lineoffset = 0
timercountpopped = 0 timercountpopped = 0
draw_filelist(disp, filelist, current_file, numfiles, 0) draw_filelist(disp, filelist, current_file, numfiles, 0)
continue continue
draw_filecontent(disp, filename, linepos, linecount, lineoffset) draw_filecontent(disp, filename, linepos, linecount, lineoffset)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment