Showing posts with label casioplot. Show all posts
Showing posts with label casioplot. Show all posts

Sunday, May 18, 2025

Casio fx-CG100: The getkey command in Python

Casio fx-CG100: The getkey command in Python


The getkey command: A Demonstration


The fx-CG100 adds a command to its Casioplot module, the beloved getkey command. The getkey returns the key code of the last key pressed from the command’s execution. The getkey allows for custom keyboard, movement of characters, or easy input of menu choices.


The getkey code that is assigned to each key on the fx-CG100 is thankfully fairly simple: it is a combination of the row number and a column number. For instance, the key code 21 is assigned to the [SETTINGS] key while the key code 95 is assigned to the [EXE] key.


Some key codes include:

Left arrow (←)

23

Up arrow (↑)

14

OK key

24

Down arrow (↓)

34

Right arrow (→)

25

EXE

95

1 Key

81

2 Key

82

3 Key

83

4 Key

71

5 Key

72

6 Key

73

7 Key

61

8 Key

62

9 Key

63

0 Key

91


Other key assignments can be found in the manual under the getkey() command in the Casioplot section: https://support.casio.com/global/en/calc/manual/fx-CG100_1AUGRAPH_en/BONDSYlkjdoxxc.html#BONDSYhxeuohnh


Note that the [ ON ] and [ AC ] buttons have no key assigned to them and can not be used with the getkey command.


Example: Calculating Horsepower


The script, hp.py calculates horsepower by one of three sets of inputs:


Press [ 1 ] to input weight (pounds) and time (seconds).

Press [ 2 ] to input weight (pounds) and speed (mi/hr).

Press [ 3 ] to input RPM (revolutions per minute) and torque (foot-pounds).


Using Getkey for Menus

To set up the getkey, I first make a “choice variable”, such as ch, and set it to 0. The while loop contains the menu and does not end until the user presses [ 1 ], [ 2 ], or [ 3 ] (or [ AC ] to terminate the program).   


from casioplot import *


# setup

ch=0


# title screen

while ch==0:

  clear_screen()

  draw_string(0,0,"**HORSEPOWRER CALCULATOR**")

  draw_string(0,20,"**     Inputs Menu     **")

  draw_string(0,40,"1) weight, time",(192,0,0))

  draw_string(0,60,"2) weight, speed",(0,0,192))

  draw_string(0,80,"3) RPM, torque",(0,128,0))

  show_screen()

  k=getkey()

  if k==81:

    ch=1

  elif k==82:

    ch=2

  elif k==83:

    ch=3

  


# calculation

if ch==1:

  w=float(input("weight in lbs:  "))

  t=float(input("time in sec:  "))

  hp=w*(t/5.825)**(-3)

elif ch==2:

  w=float(input("weight in lbs:  "))

  s=float(input("speed in mph:  "))

  hp=w*(s/234)**3

elif ch==3:

  r=float(input("RPM:  "))

  t=float(input("torque (ft-lbs): "))

  hp=(r*t)/5252


clear_screen()

txt="{0:.5f} hp".format(hp)

draw_string(0,0,"Horsepower=")

draw_string(0,20,txt,(192,0,0))

draw_string(0,100,"Press AC to exit.",(0,128,128))

show_screen()


Notes: The getkey is meant to work with the Casioplot commands. Instead of the built-in print command, we use draw_string and show_screen commands to display output. The plus here is that we can have text in color. The show_screen is displayed until either the [ EXE ] or the back buttons is pressed.


The input command returns us to the Shell and is not part of the Casioplot commands.


Source for the Formulas Used: 


Inch Calculator “Engine Horsepower Calculator”. Calc Hub, LLC. 2025. https://www.inchcalculator.com/engine-horsepower-calculator/ Retrieved May 5, 2025.


Eddie


All original content copyright, © 2011-2025. 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.


Saturday, March 15, 2025

Casio Python with fx-CG 50: Drawing Shapes with Casioplot (draws.py)

Casio Python with fx-CG 50: Drawing Shapes with Casioplot (draws.py)


The Casio file draws.py is a file that use both the casioplot and math modules and contains four drawing functions.


The plot functions uses the pixel system.


Upper left hand corner: (0, 0)

Upper right hand corner: (383, 0)

Lower left hand corner: (0, 191)

Lower right hand corner: (383, 191)


The x axis increases going right and the y axis increases going down. This orientation is common for a pixel-orientated coordinate system.


Note that the pixels must be integers. A non-integer value for a pixel will cause an error.



Importing the draws module


After copying the file draws.py to your calculator, type: from draws import *


The casioplot and math modules will also be imported because the draws module is imported. This is the case of the fx-CG 50, and I’m pretty sure it will work with the other Casio calculators with Python (fx-9750GIII, fx-9860GIII, fx-CG 100, Graph Math+).


To clear the drawing screen, use the casioplot’s command clear_screen().


To show the picture, use the casioplot’s command show_screen().


The functions from the draws must be manually typed, as they will not appear in the catalog or VARS menu.



sline(x,sx,y,sy,l,c)


Draws a line from (x,y) of length l and color c. The color is a three-element tuple in the RGB format ((red, green, blue)).


The arguments sx and sy are direction/slope arguments which dictate the direction of the line.


To draw a line going left (←)

Set sx = -1 and sy = 0

To draw a line going right (→)

Set sx = 1 and sy = 0

To draw a line going up (↑)

Set sx = 0 and sy = -1

To draw a line going down (↓)

Set sx = 0 and sy = 1

To draw a line going right and up (↗)

Set sx = 1 and sy = -1

To draw a line going left and up (↖)

Set sx = -1 and sy = -1

To draw a line going right and down (↘)

Set sx = 1 and sy = 1

To draw a line going left and down (↙)

Set sx = -1 and sy = 1


Examples:

sline(1,1,1,0,140,(255,0,0)) draws a red line starting from (1,1) going right with length of 140 pixels


sline(180,0,100,-1,50,(0,255,0)) draws a green line starting from (180,100) going up with length of 50 pixels





The sample file draw1.py is a demonstration of the sline command.






box(x,xl,y,yl,c)


The box function draws a box with the upper left hand corner (x,y) with color c, horizontal length (width) xl, and vertical length (height) yl. The box draws to the right and down. The box is filled with the specified color. If xl = yl, the function will draw a square.


Example:

box(20,100,40,150,(128,128,128)) draws a gray rectangle with upper-left corner at (20,40) with horizontal length of100 and vertical length of 150.


box(300,60,0,60,(0,0,0)) draws a black square with upper-left corner at (300, 0) with the side length of 60.





The sample file draw2.py uses the box function to generate a random game map of land (green) and water (blue).






tri(x,xd,y,yd,l,c)


The tri function draws a 45-45-90 right triangle. The point (x,y) is the corner point that contains the right angle of 90°. The arguments xd and yd dictate the direction of the triangle (see table below).





The triangle is filled with color c. The sides of the triangle will be drawn with length l.


Example:


tri(80,1,80,-1,75,(0,128,128)) draws a teal 45-45-90 right triangle with the right angle located at (80, 80). The length is 75 pixels.





The sample file draw3.py uses the tri function to draw four triangles, one with each proper orientation.






circ(x,y,r,c)


The circ function draws a circle centered at (x, y) with radius r and color c. The circle is drawn as outline and not filled. To make the circle appear as smooth as possible, 720 points are plotted.


Example:

circ(190,100,85,(255,128,0)) draws an orange circle with radius 85, centered at (190, 100).





The file draw4.py demonstrates the circ function.






Casio fx-CG 50 Python: draws.py code


# drawing utilities

# 12-04-2024


from casioplot import *

from math import *


def sline(x,sx,y,sy,l,c):

  for i in range(l+1):

    set_pixel(x+sx*i,y+sy*i,c)


# upper left corner

def box(x,xl,y,yl,c):

  for i in range(xl+1):

    for j in range(yl+1):

      set_pixel(x+i,y+j,c)


# right triangle

# (x,y) point with right angle

# l=length

# xd=1 right, xd=-1 left

# yd=1 down, yd=-1 up

def tri(x,xd,y,yd,l,c):

  for i in range(l+1):

    k=l-i

    for j in range(k+1):

      set_pixel(x+xd*i,y+yd*j,c)


# hallow circle

def circ(x,y,r,c):

for i in range(721):

  t=2*pi*i/720

  xr=int(x+r*cos(t))

  yr=int(y+r*sin(t))

  set_pixel(xr,yr,c)



You can download the draws.py and the example Python files here:

https://drive.google.com/file/d/1rGQHa60V7ZCE9Vs0kYy0cfIe0pOXqVaZ/view?usp=sharing


I hope you enjoy this program as much as I have making it,



Eddie


All original content copyright, © 2011-2025. 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.

Solving Simple Arcsine and Arccosine Equations

  Solving Simple Arcsine and Arccosine Equations Angle Measure This document will focus on angle measurement in degrees. For radia...