Showing posts with label automotive. Show all posts
Showing posts with label automotive. 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.


Friday, December 23, 2016

Fun with HP 71B IV

Fun with HP 71B IV

For the previous entries:

Links to other HP 71B Programs:
Fun with the 71B:
http://edspi31415.blogspot.com/2012/06/fun-with-hp-71b.html
Fun with the 71B III:


Rake Wall

The program RAKEWALL calculates:

*  The positions and lengths of studs on a rake wall
*  Angle of the incline




Program RAKEWALL
192 Bytes, 12/22/2016

10 DESTROY B,R,L,N,A
15 DESTROY I,T,O
50 INPUT “BASE:”; B
52 INPUT “RISE:”; R
54 INPUT “RUN:”; L
56 INPUT “O.C. :”; S
74 N = INT(L/S)
76 DEGREES
78 T = ATAN(R/L)
80 DISP “ANGLE =”; T; “°”  @ PAUSE
90 FOR I=L TO 0 STEP –S
92 A = I*TAN(T)+B
94 DISP I; “, “; A  @ PAUSE
96 NEXT I
98 DISP 0; “, “; B

Notes:
Degree symbol (°):  [ g ], [RUN] (CTRL), [ A ]

Example: (amounts are in feet)
BASE:  4
RISE:  3
RUN:  6
O.C.:  16/12  (1 foot, 4 inches)

Output: 
ANGLE = 26.5650511771 °
(Position from where the incline meets the base, length of studs)
6, 7
4.66666666667, 6.33333333334
3.33333333334, 5.66666666667
2.00000000001, 5
0.66666666668, 4.33333333334
0,  4


Automotive Cylinders:  Calculating Displacement and Piston Speed

The program supplies default values which can be accepted or changed.



Program AUTOCYN
217 Bytes, 12/22/2016

10 DESTROY N,B,S,R,E,P
15 INPUT “# CYLINDERS:”,  “6”; N
20 INPUT “BORE (IN):”, “4”; B
25 INPUT “STROKE (IN):”, “4”; S
30 E = PI/4 * B^2 * S * N
35 INPUT “RPM:”; R
40 P = S * R / 6
45 DISP “ENGINE DISPLACEMENT =” @ WAIT 1
50 DISP E; “ IN^3” @ PAUSE
55 DISP “PISTON SPEED =” @ WAIT 1
60 DIPS P; “FPM”

Example 1:
N = 6 cylinders, B = 4 in, S = 4 in, RPM = 3500
Output:  E ≈ 301.59290 in^3, P ≈ 2333.33333 FPM

Example 2:
N = 6 cylinders, B = 4 in, S = 3 in, RPM = 4500
Output:  E ≈ 226.19467 in^3, P = 2250 FPM

Right Triangle Solver

The program RIGHTTRI is a solver for the sides A, B, and C.  To solve for the third side, the desired side needs to have a 0 value, while the other two sides have non-zero values.  Each time a solution is found, the values of A, B, and C are reset (set to 0).



Program RIGHTTRI
502 Bytes, 12/22/2016

10 DESTROY A,B,C,Z$
11 ! LOOP
12 DISP “A, B, C, Solve, Exit”
14 DELAY 0, 0
16 Z$ = KEY$
18 IF Z$ = “A” THEN 30
20 IF Z$ = “B” THEN 40
22 IF Z$ = “C” THEN 50
24 IF Z$ = “S” THEN 60
26 IF Z$ = “E” THEN 96
28 GOTO 12
30 INPUT “A = “; A
32 GOTO 12
40 INPUT “B = “; B
42 GOTO 12
50 INPUT “C = “; C
52 GOTO 12
60 IF A=0 AND B AND C THEN 62 ELSE 70
62 S=SQR(C^2-B^2) @ Z$ = “A”
64 GOTO 90
70 IF A AND B=0 AND C THEN 72 ELSE 80
72 S=SQR(C^2-A^2) @ Z$ = “B”
74 GOTO 90
80 IF A AND B AND C=0 THEN 82 ELSE 86
82 S=SQR(A^2+B^2) @ Z$ = “C”
84 GOTO 90
86 DISP “MUST HAVE ONE 0” @ BEEP @ WAIT .5
88 GOTO 10
90 DISP Z$; “ = “; S @ PAUSE
92 GOTO 10
94 DISP “DONE”

Notes:

The line of IF A=0 AND B AND C… tests whether A is zero, B and C are non-zero.  You can test whether a variable is non-zero by just typing the variable in an IF condition.

The function SQR is the square root function of the HP 71B.

Anything following an exclamation point (!) is a comment.


Basic Bridged-T Notch Filter

The program NOTCH calculates the required capacitor and resistor to null out an undesired frequency.

Inputs:  inductance (H), frequency to nullified (Hz), resistance of the required coil (Ω)



Source:  Rosenstein, Morton.  Computing With the Scientific Calculator Casio: Tokyo, Japan.  1986.  ISBN-10: 1124161430

Program NOTCH
244 Bytes, 12/22/2016

10 DESTROY L,F,I,C,R
20 DISP “INDUCTANCE” @ WAIT 1
22 INPUT “in H: “; L
24 DISP “FREQUENCY” @ WAIT 1
26 INPUT “in Hz: “; F
28 DISP “COIL’S RESISTENCE” @ WAIT 1
30 INPUT “in Ω: “; I
40 C = 1/(2 * PI^2 * F^2 * L)
42 R = (PI * F * L)^2 / I
44 DISP “NOTCH CAPACITOR” @ WAIT 1
46 DISP C; “ F” @ PAUSE
48 DISP “NOTCH RESISTENCE” @ WAIT 1
50 DISP R; “ Ω”

Notes:
Capital Omega Character (Ω):  [ g ], [RUN] (CTRL), [ Q ]

Example:   L = 0.12 H, F = 1170 Hz, Coil Resistance = 30 Ω
Output: 
NOTCH CAPACITOR ≈ 3.08402 * 10^-7 F
NOTCH RESISTENCE ≈ 6485.04070 Ω


Differential Equations and Half-Increment Solution, Numerical Methods

The program HALFDIFF solves the numerical differential equation

d^2y/dt^2 = f(dy/dt, y, t)  given the initial conditions y(t0) = y0 and dy/dt (t0) = dy0

In this notation, y is the independent variable and t is the dependent variable.

The Method

Let C = f(dy/dt, y, t).  Give the change of t as Δt.

First Step:

With t = t0:
h_1/2 = dy0 + C * Δt/2
y1 = y0 + dy0 * Δt

Loop:

t = t0 + Δt
h_I+1/2 = h_I-1/2 + C * Δt
y_I+1 = y_I +h_I+1/2 * Δt

Repeat as many steps as desired.


Source:  Eiseberg, Robert M.  Applied Mathematical Physics with Programmable Pocket Calculators  McGraw-Hill, Inc:  New York.  1976.  ISBN 0-07-019109-3

Program HALFDIFF
250+ bytes, 12/22/2016

Edit f(A,Y,T) at line 5 where
A = y’(t), Y = y(t),  T = t

5 DEF FNF(A,Y,T) = insert function y’(t), y(t), and t here
10 DESTROY A,Y,D,N,H,T
12 RADIANS
20 INPUT “dY/dX (0) = “, “0”; A
22 INPUT “Y(0) = “, “0”; Y
24 INPUT “DELTA T = “,”1”; D
26 INPUT “TMAX = “,”5”; N
28 T = D
44 H = A + FNF(A,Y,T) * D/2
48 Y = Y + H * D
50 DISP D; “, “;  Y @ PAUSE
70 FOR I = 2 * D TO N STEP D
72 T = I @ A = H
76 H = H + FNF(A,Y,T) * D
78 Y = Y + H * D
80 DISP I; “, “; Y @ PAUSE
82 NEXT I

Example:  y’’(t) = y’(t) + 2 *  t with y’(0) = 0, y(0) = 1, Delta t = 1, Max t = 6
FNF(A,Y,T) = A + 2 * T

Results:

T
Y
1
2
2
8
3
26
4
70
5
168
6
376


  
This blog is property of Edward Shore, 2016.





Numworks (Python): Parallelograms Described by Vectors

Numworks (Python): Parallelograms Described by Vectors Introduction The script drawpgram.py draws a parallelogram constructed by ...