keyseq: start a feature to print input sequence

This commit is contained in:
Lephe 2022-01-15 11:48:10 +01:00
parent 8c3341d09f
commit 8d8707f3a9
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
2 changed files with 108 additions and 1 deletions

10
fx92.py
View File

@ -9,6 +9,7 @@ from fx92.lexer import UrlLexer, TextLexer
from fx92.printer import print_ast
from fx92.drawing import Window, InterruptException
from fx92.interpreter import Context
from fx92.keyseq import print_key_sequence
#---
# Main program
@ -28,6 +29,7 @@ Output options:
--save=<file> Save a copy of the screen output in a bitmap file
--scale=<n> Window scale up (default 4, max 16)
--stream=<dir> Capture rendering in real time, outputs a series of bitmaps
--keyseq Output key sequence for Aditya02's hardware mod
""".format(sys.argv[0]).strip()
def usage(exitcode=None):
@ -40,7 +42,7 @@ def main():
# Read command-line arguments
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "hus",
["help", "quiet", "save=", "scale=", "debug=", "stream="])
["help", "quiet", "keyseq", "save=", "scale=", "debug=", "stream="])
opts = dict(opts)
if len(sys.argv) == 1 or "-h" in opts or "--help" in opts:
@ -65,6 +67,7 @@ def main():
out = opts.get("--save", None)
debug = opts.get("--debug", None)
stream = opts.get("--stream", False)
keyseq = opts.get("--keyseq", None)
scale = int(opts.get("--scale", "4"))
if scale < 1:
@ -101,6 +104,11 @@ def main():
print_ast(ast, lang="ast")
return 0
# Key sequence mode, print a key sequence
if keyseq is not None:
print_key_sequence(ast)
return 0
# Create the folder for streaming capture
if stream:
try:

99
fx92/keyseq.py Normal file
View File

@ -0,0 +1,99 @@
# fx-92 Scientifique Collège+ language interpreter: Input key sequences
#
# This module compiles programs to sequences of key presses that can be used to
# input them on the fx-92 SC+. This is used for Aditya02's hardware mod of the
# fx-92 SC+ which can automatically perform these presses:
# <https://www.planet-casio.com/Fr/forums/topic16979-1-une-memoire-externe-pour-la-casio-fx92.html>
from fx92.ast import N, Node
def print_key_sequence(n, indent=0):
if isinstance(n, Node) and n.type == N.PROGRAM:
for arg in n.args:
print_key_sequence(arg, indent)
return
def p(*args):
for arg in args:
print(" "*indent + arg)
var_numbering = {
"A": "1", "B": "2", "C": "3", "D": "4",
"E": "5", "F": "6", "M": "7"
}
if not isinstance(n, Node):
print("[Not a node?] {}: {}".format(n, type (n)))
return
if n.type == N.CONST:
p("# CONST {}".format(n.args[0]))
p("<TODO>")
elif n.type == N.VAR:
p("# VAR {}".format(n.args[0]))
p("<TODO>")
elif n.type == N.REL:
p("# REL {}".format(n.args[0]))
p("<TODO>")
elif n.type == N.FUN:
p("# FUN {}".format(n.args[0]))
p(n.args[0] + "()")
for arg in n.args[1]:
print_key_sequence(arg, indent+1)
return
# OPTN, first page
elif n.type == N.FORWARD:
p("# FORWARD {}".format(n.args[0]))
p("OPTN", "1")
print_key_sequence(n.args[0], indent=indent+2)
p("EXE", "EXE")
elif n.type == N.ROTATE:
p("# ROTATE {}".format(n.args[0]))
p("OPTN", "2")
print_key_sequence(n.args[0], indent=indent+2)
p("EXE", "EXE")
elif n.type == N.ORIENT:
p("# ORIENT {}".format(n.args[0]))
p("OPTN", "3")
print_key_sequence(n.args[0], indent=indent+2)
p("EXE", "EXE")
elif n.type == N.GOTO:
p("# GOTO {} {}".format(n.args[0], n.args[1]))
p("OPTN", "4")
print_key_sequence(n.args[0], indent=indent+2)
p("EXE")
print_key_sequence(n.args[1], indent=indent+2)
p("EXE", "EXE")
# OPTN, second page
elif n.type == N.PENDOWN:
p("# PENDOWN")
p("OPTN", "DOWN", "1")
elif n.type == N.PENUP:
p("# PENUP")
p("OPTN", "DOWN", "2")
elif n.type == N.ASSIGN:
p("# ASSIGN {} {}".format(n.args[0], n.args[1]))
p("OPTN", "DOWN", "3")
print_key_sequence(n.args[0], indent=indent+2)
p("EXE")
if n.args[1] != "A":
p("RIGHT", var_numbering[n.args[1]])
p("EXE")
else:
print("[???]", n.type, n.type.name.lower())
#
__all__ = ["print_key_sequence"]