Sunday, September 10, 2023

TI-84 Plus CE Python and TI-83 Premium CE Python Edition: Blinking Binary Strings

TI-84 Plus CE Python and TI-83 Premium CE Python Edition:  Blinking Binary Strings

Introduction

The script BINBLANK.py draws a circle's whose color changes based on a list of binary numbers:  green for 1, yellow for 0.

The code listed is made for the TI-84 Plus CE Python and TI-83 Premium CE Python Edition.

Program Script: BINBLANK.py

# ews 2023-06-24

# blink of lights: binary bits

from random import *

from ti_system import *

from ti_draw import *

# set subroutines

def black():

from ti_draw import *

set_color(0,0,0)

def white():

from ti_draw import *

set_color(255,255,255)

def yellow():

from ti_draw import *

set_color(255,255,0)

def lime():

from ti_draw import *

set_color(0,255,0)

def dcircle():

from ti_draw import *

fill_circle(150,100,50)

# using pixels, not coordinates

# initialization

blist=[]

print("Binary String Lighting")

print("Choose 1 or 2:")

print("1. generate random string")

print("2. enter a bit list")

# key press

k=0

# flag

f=0

while f==0:

k=wait_key()

if k==143:

# 1 key

n=int(input("Number of bits? "))

blist=[randint(0,1) for i in range(n)]

f=1

if k==144:

# 2 key

blist=eval(input("List of bits: "))

# data check

m=len(blist)

for i in range(m):

if blist[i]!=0 and blist[i]!=1:

1/0

# force an error and terminate

f=1

# draw the light

clear()

black()

fill_rect(0,0,319,209)

white()

dcircle()

# loop

for i in range(len(blist)):

sleep(.1)

if blist[i]==0:yellow()

if blist[i]==1:lime()

dcircle()

black()

sleep(.55)

# reset

white()

dcircle()

# end indicator

black()

dcircle()

white()

# no line breaks in draw_text

draw_text(50,50,"THE END: PRESS CLEAR")

show_draw()

Notes:

Three modules are used:  random, ti_system, and ti_draw.  The modules ti_system and ti_draw are Texas Instruments-specific modules.

The ti_system module allows for a get key type of function called wait_key().   wait_key() stops execution and waits for the user to press a key.   The value of the key is returned.   There are separate key combinations with [ 2nd ] or [ alpha ].

The [ 1 ] key returns a value of 143.

The [ 2 ] key returns a value of 144.

Here are some other key values:

[ 3 ]   145

[ 4 ]   146

[ 5 ]   147

[ 6 ]   148

[ 7 ]   149

[ 8 ]   150

[ 9 ]   151

[ 0 ]   142

[ enter ]   5

[ ← ]   2

[ → ]   1

[  ↑  ]  3

[  ↓  ]  4

[ clear ]  9

The script also checks self-entered lists to see that each entry is a 0 or 1.   If not, the program "calculates" 1 ÷ 0 to cause a program-stopping error.

Eddie

All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.