Reading the keyboard, writing to the shell, and sharing values with the calculator's lists. 12 functions in
ti_system, each with a working example.
Try any of these in CalcPlex PRGM, a browser code editor, which sends your program to a calculator over USB.
ti_system.disp_at(
ti_system.disp_at(row, "text", "align")
Write a line of text on the shell screen at the row you choose.
There are two ways to call it:
Aligned text: disp_at(row, "text", "align") where align is "left", "center", or "right". This clears the entire row before writing, so it works great in loops: the new text replaces the old text cleanly.
Exact position: disp_at(row, col, "text") places text at a specific column without clearing the row. Use this when you want two labels side by side on one line.
Rows go from 1 (top) to 11 (bottom).
This writes on the text shell screen, which is separate from ti_draw. If your program uses ti_draw for graphics, use ti_draw.draw_text() for text instead.
The aligned form clears the entire row before writing. If you call it twice on the same row, only the second call shows up. To put two labels side by side, use the exact position form: disp_at(row, col, "text").
import ti_system
ti_system.disp_clr() # clear the shell screen
ti_system.disp_at(1, "SCORE BOARD", "center") # centered heading
ti_system.disp_at(3, "Player 1", "left") # left-aligned label
ti_system.disp_at(4, "Player 2", "right") # right-aligned label
ti_system.disp_at(6, 10, "Exact spot") # exact form: row 6, column 10
ti_system.wait_key() # wait for a keypress
A centered heading, two aligned labels, and one label placed at an exact column position.
See also: ti_system.disp_clr(, ti_system.disp_cursor(, ti_system.disp_wait(, ti_draw.draw_text(
ti_system.disp_clr(
ti_system.disp_clr()
Clears the text shell screen, or just one row.
disp_clr() with no arguments clears the entire text screen. Call it at the top of a text-mode program so you start on a clean screen.
disp_clr(4) clears just row 4. Rows go from 1 to 11, same as disp_at. This is a quick way to erase one line, though disp_at(row, "text", "align") already clears its row automatically.
This only affects the text shell screen. To clear graphics drawn with ti_draw, use ti_draw.clear() instead.
disp_clr() only clears the text screen. It does nothing to graphics drawn with ti_draw. The two screens are completely separate.
import ti_system
import time
ti_system.disp_clr()
ti_system.disp_at(2, "Loading...", "center")
time.sleep(1)
ti_system.disp_clr(2) # clear just row 2
ti_system.disp_at(2, "Ready!", "center")
ti_system.wait_key()
Shows "Loading...", waits a second, then replaces it with "Ready!" by clearing just that one row.
See also: ti_system.disp_at(, ti_system.disp_cursor(, ti_draw.clear(
ti_system.disp_cursor(
ti_system.disp_cursor(show)
Meant to hide or show the shell's blinking cursor.
Pass 0 to hide and 1 to show.
In practice, on the current Evo firmware calling disp_cursor(0) does not reliably hide the cursor after disp_at or print: the blinking block still trails the last-written text. Treat the cursor as always visible in text-mode programs and lay text out with that in mind (for example, keep the last row empty, or write the final text where a trailing block is harmless).
Do not rely on disp_cursor(0) to clean up a text-mode screen. It does not. If a stray cursor would land on important text, arrange your layout so it lands somewhere else instead.
import ti_system
ti_system.disp_clr()
ti_system.disp_cursor(0) # call has no visible effect
ti_system.disp_at(1, "Hello", "center")
ti_system.wait_key()
Prints Hello centered on row 1. The cursor still blinks at the end of the text.
See also: ti_system.disp_at(, ti_system.disp_clr(, ti_system.disp_wait(
ti_system.disp_wait(
ti_system.disp_wait()
Freezes the program until CLEAR is pressed, then clears the text screen.
Stops your program and holds the text screen until the user presses CLEAR. After they press it, the screen is automatically cleared and the program continues.
Only CLEAR works. No other key will get past it. If you want any key to work, use wait_key() instead.
import ti_system
ti_system.disp_clr()
ti_system.disp_at(4, "Result: 1280", "center")
ti_system.disp_at(6, "Press CLEAR", "center")
ti_system.disp_wait()
ti_system.disp_at(4, "Done", "center")
ti_system.wait_key()
Shows a value, waits for CLEAR, then shows a follow-up message.
See also: ti_system.disp_at(, ti_system.disp_clr(, ti_system.wait_key(, ti_draw.show_draw(
ti_system.escape(
ti_system.escape()
Returns True if CLEAR has been pressed. A clean way to let users stop a loop.
escape() returns True when CLEAR has been pressed and False otherwise. It's a convenient way to let users stop a program that would otherwise run forever.
If your loop already reads get_key, just check for key code 45 (CLEAR) there instead. If your loop doesn't read the keyboard at all, while not escape(): is the simpler option.
Don't mix escape() and a get_key check for CLEAR in the same loop. They both watch the same key and can interfere with each other.
import ti_system
import time
n = 0
ti_system.disp_clr()
while not ti_system.escape():
n = n + 1
ti_system.disp_at(4, "Count: " + str(n), "center")
time.sleep(0.5)
ti_system.disp_at(6, "Stopped at " + str(n), "center")
ti_system.wait_key()
Counts up on screen until CLEAR is pressed.
See also: ti_system.get_key(, ti_system.disp_wait(, ti_system.disp_clr(
ti_system.get_key(
ti_system.get_key(wait)
Checks which key is pressed right now. Pass 0 to poll without waiting.
get_key(0) checks which key is being held down RIGHT NOW and returns immediately. If no key is pressed, it returns 0. If a key is down, it returns that key's code as a number.
You must pass an argument. get_key(0) polls instantly (what games use). get_key(1) blocks until a key is pressed, same as wait_key().
Common key codes:
- Arrows: Up
25, Down34, Left24, Right26 - Enter
105, Clear45, DEL23, 2nd21
Run the example below to find the code for any other key.
Because get_key(0) only sees keys that are held down at that exact instant, you need to poll it in a tight loop. A quick tap that lands between two polls will be missed.
In a game loop, save the last key you saw rather than overwriting it every pass. If you write direction = get_key(0) directly, the very next pass where no key is held down resets direction to 0 and loses the player's input.
Never use time.sleep() in a game loop. The keyboard can't be read while the program is sleeping, so any key pressed during the sleep is lost. Track time with time.monotonic() and keep polling get_key(0) the whole time.
import ti_system
CLEAR = 45
ti_system.disp_clr()
ti_system.disp_at(1, "Press keys!", "center")
ti_system.disp_at(2, "CLEAR to quit", "center")
while True:
k = ti_system.get_key(0) # 0 = poll without waiting
if k == CLEAR:
break
if k != 0:
ti_system.disp_at(5, "Key code: " + str(k), "center")
A key code explorer. Press any key to see its code on screen. Useful for finding the right code for your own programs.
See also: ti_system.wait_key(, ti_system.escape(
ti_system.recall_RegEQ(
var = ti_system.recall_RegEQ()
Read the last regression equation the calculator computed.
If you've run a regression on the calculator (STAT > CALC > LinReg, etc.), this pulls the fitted equation into Python as a text string like "1.5*x+2".
You can evaluate it by setting a variable named x and using eval().
The regression has to be computed on the calculator first. Python doesn't run the regression for you.
import ti_system
eq = ti_system.recall_RegEQ()
print(eq)
x = 4.0
print("Value at x=4:", eval(eq))
Prints the regression equation, then evaluates it at x = 4. The variable must be named exactly `x` for eval() to work.
See also: ti_system.recall_list(, ti_system.store_list(
ti_system.recall_list(
var = ti_system.recall_list("name")
Read a TI-Basic list variable into your Python program.
recall_list("name") reads a list variable from the calculator's memory (the same lists you can create on the home screen or in the list editor).
Naming: The built-in lists L1 through L6 are requested by number: "1" through "6". Custom lists use up to 5 uppercase letters/digits starting with a letter, like "SCORE" or "R12". Watch out: "L1" asks for a custom list literally named L1, NOT the built-in L1.
Values may come back as decimals. Use int() if you need whole numbers.
This is how a game saves data between runs. List variables survive the program ending and even the calculator turning off. Pair it with store_list() to save and load things like high scores.
A missing list raises an error, it does NOT return an empty list. Always use try/except when recalling a list that might not exist.
import ti_system
def load_high_score():
try:
return int(list(ti_system.recall_list("HISCR"))[0])
except:
return 0
best = load_high_score()
ti_system.disp_clr()
ti_system.disp_at(2, "Best: " + str(best), "center")
Loads a saved high score. The try/except handles the first run when no save data exists yet.
See also: ti_system.store_list(, ti_system.recall_RegEQ(
ti_system.sleep(
ti_system.sleep(seconds)
Pauses the program. Accepts fractions like 0.5 for half a second.
Pauses the program for the given number of seconds. ti_system.sleep(0.5) pauses for half a second.
time.sleep() does the same thing and is the standard Python way to do it. Use whichever you prefer.
The keyboard can't be read while sleeping. In a game, don't use sleep() for pacing: track time with time.monotonic() and keep polling get_key(0) instead.
import ti_system
print("Wait for it...")
ti_system.sleep(1)
print("Done!")
Prints a message, pauses for one second, then prints another.
See also: ti_system.get_key(, time.sleep(
ti_system.store_list(
ti_system.store_list("name", var)
Save a Python list to a TI-Basic list variable.
Saves a Python list of numbers into the calculator's memory. The data survives the program ending and even the calculator turning off.
Naming rules are the same as recall_list(): "1" through "6" for L1-L6, or up to 5 uppercase characters for a custom list like "HISCR". Lists hold at most 100 values.
Values must be numbers (ints or floats). Booleans and strings will be rejected.
There's no function for storing a single number, so to save one value (like a high score), wrap it in a one-item list: store_list("HISCR", [score]).
Same naming rule as recall_list: "1" means the built-in L1, but "L1" means a custom list literally named L1. They're different lists.
import ti_system
def save_high_score(score):
try:
ti_system.store_list("HISCR", [score])
except:
pass
save_high_score(1280)
ti_system.disp_clr()
ti_system.disp_at(2, "Saved!", "center")
Saves a high score. The try/except prevents a crash if memory is full.
See also: ti_system.recall_list(, ti_system.recall_RegEQ(
ti_system.wait(
ti_system.wait(seconds)
Pauses for a number of seconds. Same as time.sleep().
Pauses for the given number of seconds. Does the same thing as time.sleep() and ti_system.sleep().
Prefer time.sleep() in your own code: it's standard Python and works the same everywhere.
import ti_system
print("Waiting...")
ti_system.wait(1)
print("Done!")
# same thing in standard Python:
# import time
# time.sleep(1)
Pauses for one second. The commented version shows the standard Python equivalent.
See also: time.sleep(, ti_system.get_key(, time.monotonic(
ti_system.wait_key(
ti_system.wait_key()
Waits for a key press and returns its code.
Stops the program until a key is pressed, then returns that key's code as a number (the same codes as get_key).
Use it for "press any key" prompts, title screens, and turn-based games. Don't use it inside a real-time game loop, because nothing else runs while it waits.
import ti_system
print("Press any key to begin")
k = ti_system.wait_key()
print("You pressed key code", k)
Pauses until a key is pressed, then prints which key it was.
See also: ti_system.get_key(, ti_system.escape(, ti_system.disp_wait(