Toggle Navigation
Hatchery
Eggs
ed10
__init__.py
Users
Badges
Login
Register
MCH2022 badge?
go to mch2022.badge.team
__init__.py
raw
Content
import display import buttons from htmlcolor import WHITE from htmlcolor import BLACK from utime import sleep charwidth = 14 def rotate(buffer, pos): return buffer[:pos] + chr((ord(buffer[pos]) + 1) % 0x7f) + buffer[pos+1:] def append(buffer, pos): move(buffer, pos) buffer = buffer[:pos] + 'A' + buffer[pos:] return buffer, pos + 1 def move(buffer, pos): if len(buffer) > 0: return (pos + 1) % len(buffer) else: return pos def chunks(buffer, n): for i in range(0, len(buffer), n): yield buffer[i:i + n] def printwithmarker(display, line, pos, posy): if pos > 0: display.print(line[:pos], posy=posy) display.print('' + line[pos], fg=BLACK, bg=WHITE, posx=pos * charwidth, posy=posy) if len(line) - (pos + 1) > 0: display.print(line[pos+1:], posx=(pos + 1)*charwidth, posy=posy) def update(buffer, pos): charwidth = 10 line = 0 with display.open() as d: d.clear() for c in chunks(buffer, 10): if line * charwidth <= pos < (line + 1) * charwidth: printwithmarker(d, c, pos % 10, posy=20*line) else: d.print(c, posy=20*line) line += 1 d.update() def delete(buffer, pos): buffer = buffer[:pos] + buffer[pos+1:] if pos == len(buffer): pos -= 1 print(buffer, pos) return buffer, pos def input(buffer): pos = 0 while True: sleep(0.2) pressed = buttons.read( buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) if pressed == (buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT): buffer, pos = delete(buffer, pos) if pressed == buttons.BOTTOM_LEFT != 0: buffer = rotate(buffer, pos) elif pressed == buttons.TOP_RIGHT != 0: buffer, pos = append(buffer, pos) elif pressed == buttons.BOTTOM_RIGHT != 0: pos = move(buffer, pos) elif pressed == (buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT): return buffer update(buffer, pos) def edit(name): with open(name, 'rw') as f: buffer = input(f.read()) print(buffer) f.write(buffer) name = input('f.py') print(name) edit(name)