### Python PPL Wrapper Example Source: https://udel.edu/~mm/hp/primePython This snippet demonstrates how to wrap Python code within a PPL structure for execution on the HP Prime. The `PYTHON(PPLwrapper)` call executes the specified Python code block. ```python #PYTHON PPLwrapper print('Super complicated python functions go here!') #END EXPORT myFunc() BEGIN PYTHON(PPLwrapper); END; ``` -------------------------------- ### Access PPL Program Variable from Python Source: https://udel.edu/~mm/hp/primePython This example demonstrates how to access a variable (`myVar`) from a PPL program (`myProg`) within a Python environment. This allows Python code to read results computed by PPL programs. ```python # Assume myProg.myVar exists and holds a value # Accessing the value in Python would be similar to: # retrieved_value = myProg.myVar ``` -------------------------------- ### Access Prime Program Variable Source: https://udel.edu/~mm/hp/primePython/#:~:text=Since%20official%20Prime%20documentation%20for,App%20needs%20a%20PPL%20wrapper: This example illustrates how to access or set a variable within a Prime PPL program from Python. It uses the syntax 'programName.variableName' to interact with the calculator's environment. ```python myProg.myVar ``` -------------------------------- ### Get Mouse Coordinates Source: https://udel.edu/~mm/hp/primePython/spiro.ppl Waits for a mouse click and returns the coordinates and type of the click event. Used for menu selections. ```HP PPL mousePt() BEGIN LOCAL m,m1; WHILE MOUSE(1)≥0 DO END; REPEAT m := MOUSE; m1 := m(1); UNTIL SIZE(m1)>0; RETURN m1; // {x, y, xOrig, yOrig, type} END; ``` -------------------------------- ### HP Prime Pop-Up Menu Source: https://udel.edu/~mm/hp/primePython Displays a pop-up menu on the HP Prime screen. The `choose()` function presents a title and multiple choices, returning the selected choice's index (starting from 1). ```python import hpprime as h c = h.eval('X:=0;choose(X,"Title","Choice 1","Choice 2","Choice 3")') ``` -------------------------------- ### Python App Demo Program Source: https://udel.edu/~mm/hp/primePython/#:~:text=Since%20official%20Prime%20documentation%20for,App%20needs%20a%20PPL%20wrapper: This program demonstrates how to interact with the HP Prime's environment using the hpprime library. It shows how to evaluate PPL commands, retrieve variable types, handle keyboard and touch input, and exit the application. ```python import hpprime as h def main(): h.eval('AVars("MyVar"):=1234') h.eval('AVars("MyVar2"):=(3,4)') h.eval('AVars("MyVar3"):="Mike"') n=h.eval('AVars("MyVar")') c=h.eval('AVars("MyVar2")') s=h.eval('AVars("MyVar3")') h.eval('print') # Clear screen. print('n=1234 is now type %s' % str(type(n))) print('c=(3,4) is now type %s' % str(type(c))) print('s="Mike" is now type %s' % str(type(s))) print('Press + to end demo.') while True: h.eval('wait(0.2)') # Throttle i/o loop. # Retrieve mouse & keyboard events. b=h.eval('getkey') # Keyboard button press, -1 if none since last call. f1,f2=h.eval('mouse') # Finger 1, finger 2 touch data. if len(f1)>0: # Finger touch sensed. # Mouse data: [x,y,xOrig,yOrig,type]. print('You touched (%d,%d)' % (f1[0],f1[1]), end='') if len(f2)>0: # Two finger touch. print(' and (%d,%d)' % (f2[0],f2[1])) else: print() if b==-1: # No button pressed. continue print('You pressed button %d' % b) if b==50: # + button exits demo. print('Slán!') break main() ``` -------------------------------- ### Display Pop-Up Menu Source: https://udel.edu/~mm/hp/primePython/#:~:text=Since%20official%20Prime%20documentation%20for,App%20needs%20a%20PPL%20wrapper: This Python code snippet shows how to create and display a pop-up menu on the HP Prime using the hpprime.eval() function to call the PPL CHOOSE command. The first choice is indexed as 1. ```python import hpprime as h c = h.eval('X:=0;choose(X,"Title","Choice 1","Choice 2","Choice 3")') ``` -------------------------------- ### Share App Results with AVars and cas.caseval Source: https://udel.edu/~mm/hp/primePython/#:~:text=Since%20official%20Prime%20documentation%20for,App%20needs%20a%20PPL%20wrapper: This snippet demonstrates how to share data from a Python app to the HP Prime's environment using AVars and a workaround for a PPL parsing bug involving exponents with plus signs. ```python import cas def numToAvars(varName, val): cmd = 'AVars("%s"):=%s' % (varName, str(val)) cas.caseval(cmd) ``` -------------------------------- ### Initialize and Read Mouse Events Source: https://udel.edu/~mm/hp/primePython/#:~:text=Since%20official%20Prime%20documentation%20for,App%20needs%20a%20PPL%20wrapper: These Python subroutines use the hpprime library to manage screen tap events. mouseClear() clears old events, while mousePt() waits for and returns touch coordinates for up to two fingers. ```python import hpprime as h def mouseClear(): # Clear out old mouse events like button bounces. while h.eval('mouse(1)')>=0: pass def mousePt(): # Wait forever for a screen touch. while True: h.eval('wait(0.1)') # Throttle i/o loop. f1,f2 = h.eval('mouse') # Touch info for fingers 1 and 2. if len(f1) > 0: # Got a finger touch! return f1,f2 # [x,y,xOrig,yOrig,type], [x,y,xOrig,yOrig,type] ``` -------------------------------- ### Main Spirograph Function Source: https://udel.edu/~mm/hp/primePython/spiro.ppl The main function SPIRO() initializes the Spirograph application, displays a menu, and handles user selections to clear the screen, draw different Spirograph types, choose colors, or exit. ```HP PPL EXPORT SPIRO() BEGIN LOCAL b,c:=0,m; // Display splash screen. RECT; // Clear screen. PRINT; // Clear terminal. WHILE 1 DO DRAWMENU("Clear","Inner","Outer","Maurer","Color","Exit"); // Soft menu. m:=mousePt; // Wait for choice. b:=softPick(m); CASE IF b==1 THEN RECT; END; // Clear drawing. IF b==2 THEN inner(c); END; // Inner. IF b==3 THEN outer(c); END; // Outer. IF b==4 THEN maurer(c); END; // Maurer. IF b==5 THEN c:=pen(); RECT; END; // Choose pen color. IF b==6 THEN PRINT;RECT;KILL; END; // Exit. END; END; END; ``` -------------------------------- ### Draw and Handle Soft Menu Source: https://udel.edu/~mm/hp/primePython/#:~:text=Since%20official%20Prime%20documentation%20for,App%20needs%20a%20PPL%20wrapper: This snippet demonstrates how to draw a soft menu with multiple items and then determine which item the user selected based on their tap coordinates. It's useful for creating interactive menus in your Prime Python applications. ```python import hpprime as h def main(): while True: h.eval('drawmenu("Item0", "Item1", "Item2", "Item3", "Item4", "Item5")') m = mousePt() # Get user screen tap coordinates. b = softPick(m) # -1 if not a soft menu tap. if b == 0: doItem0() elif b == 1: doItem1() elif b == 2: doItem2() elif b == 3: doItem3() elif b == 4: doItem4() elif b == 5: doItem5() ``` -------------------------------- ### Choose Pen Color Source: https://udel.edu/~mm/hp/primePython/spiro.ppl Presents a color selection dialog to the user and returns the corresponding RGB color value. Defaults to black if no color is chosen. ```HP PPL pen() BEGIN LOCAL c; CHOOSE(c, "Choose Color", "Black","Blue","Green","Orange","Red","Yellow"); IF c==1 THEN RETURN RGB(0,0,0); END; IF c==2 THEN RETURN RGB(0,0,255); END; IF c==3 THEN RETURN RGB(0,255,0); END; IF c==4 THEN RETURN RGB(255,127,0); END; IF c==5 THEN RETURN RGB(255,0,0); END; IF c==6 THEN RETURN RGB(255,255,0); END; END; ``` -------------------------------- ### Python PPL Wrapper Source: https://udel.edu/~mm/hp/primePython/#:~:text=Since%20official%20Prime%20documentation%20for,App%20needs%20a%20PPL%20wrapper: This snippet demonstrates how to define a Python function that can be called from PPL. The Python code is enclosed within #PYTHON and #END directives, and the EXPORT keyword makes the function accessible from PPL. ```python #PYTHON PPLwrapper print('Super complicated python functions go here!') #END EXPORT myFunc() BEGIN PYTHON(PPLwrapper); END; ``` -------------------------------- ### Display Message Box Source: https://udel.edu/~mm/hp/primePython/#:~:text=Since%20official%20Prime%20documentation%20for,App%20needs%20a%20PPL%20wrapper: This snippet shows how to display a message box on the Prime calculator screen using a string variable. It constructs a PPL command and executes it via hpprime.eval(). ```python import hpprime as h cmd = 'msgbox("%s")' % s h.eval(cmd) ``` -------------------------------- ### Wait for Screen Touch Source: https://udel.edu/~mm/hp/primePython Waits indefinitely for a screen touch event. It throttles the I/O loop with `h.eval('wait(0.1)')` and returns touch information for up to two fingers. ```python import hpprime as h def mousePt(): # Wait forever for a screen touch. while True: h.eval('wait(0.1)') # Throttle i/o loop. f1,f2 = h.eval('mouse') # Touch info for fingers 1 and 2. if len(f1) > 0: # Got a finger touch! return f1,f2 # [x,y,xOrig,yOrig,type], [x,y,xOrig,yOrig,type] ``` -------------------------------- ### Draw Maurer Rose Source: https://udel.edu/~mm/hp/primePython/spiro.ppl Calculates and draws a Maurer rose pattern. Requires user input for the number of petals and the angular step. ```HP PPL maurer(c) // Calculate and draw a Maurer rose. // See https://en.wikipedia.org/wiki/Maurer_rose BEGIN LOCAL f:=1,r,t; LOCAL x0,y0,x1,y1; LOCAL a,p,s,x0p,y0p,x1p,y1p; INPUT({p,s}, "Maurer Rose", {"p=","s="}, {"Number of Petals", "Angular Step"}); RECT; // Clear screen. LOCAL pi=3.1415928, sInc=2*pi/360; FOR t FROM 0 TO 2*pi+sInc STEP sInc DO x0:=x1; y0:=y1; // Save previous point. r:=SIN(p*s*t); x1:=r*COS(s*t); y1:=r*SIN(s*t); IF f==1 THEN // First time through loop. f:=0; // Never first time again. ELSE a:=(110-10); // Scale to plotting area. x0p:=160+x0*a; x1p:=160+x1*a; // Center figure. y0p:=100+y0*a; y1p:=100+y1*a; LINE_P(x0p,y0p,x1p,y1p,c); END; END; FREEZE; END; ``` -------------------------------- ### Draw Epitrochoid Source: https://udel.edu/~mm/hp/primePython/spiro.ppl Calculates and draws an epitrochoid, where a small wheel rolls outside a fixed circle. Requires user input for the radii of the fixed and rolling circles, and the pen offset. ```HP PPL outer(c) // Calculate an epitrochoid where a small wheel rolls outside a fixed // circle. GUI inputs: // R: radius in pixels of fixed circle. // r: radius in pixels of rolling circle outside fixed circle. // d: offset in pixels of pen from center of rolling circle. // See https://en.wikipedia.org/wiki/Epitrochoid BEGIN LOCAL d,f:=1,R,r,t,tInc,tMax; LOCAL x0,y0,x1,y1; LOCAL s,x0p,y0p,x1p,y1p; INPUT({R,r,d}, "Epitrochoid", {"R=","r=","d="}, {"Radius (pixels) of Fixed Circle", "Radius (pixels) of Outer Rolling Circle", "Pen Distance (pixels) from Rolling Center"}); if R==0 THEN RETURN; END; tMax:=CAS.lcm(R, r); // theta maximum. tInc:=0.1; // theta increment. RECT; // Clear screen. FOR t FROM 0 TO tMax+tInc STEP tInc DO x0:=x1; y0:=y1; // Save previous point. x1:=(R+r)*COS(t) - d*COS(((R+r)/r)*t); y1:=(R+r)*SIN(t) - d*SIN(((R+r)/r)*t); IF f==1 THEN // First time through loop. f:=0; // Never first time again. ELSE s:=(110-1)/(R+2*r); // Scale to plotting area. x0p:=160+x0*s; x1p:=160+x1*s; // Center figure. y0p:=110+y0*s; y1p:=110+y1*s; LINE_P(x0p,y0p,x1p,y1p,c); END; END; FREEZE; END; ``` -------------------------------- ### Determine Soft Button Click Source: https://udel.edu/~mm/hp/primePython/spiro.ppl Analyzes mouse click coordinates to determine which soft menu button was pressed. Returns -1 if the click was not within the menu region. ```HP PPL softPick(m) // Determine which soft button was clicked on by user, -1 if none. BEGIN LOCAL x,y; x:=m(1); // X coord of mouse click. y:=m(2); // Y coord. IF y<220 THEN RETURN -1; END; // Not in soft menu region. IF x≤52 THEN RETURN 1; END; IF x≤105 THEN RETURN 2; END; IF x≤158 THEN RETURN 3; END; IF x≤211 THEN RETURN 4; END; IF x≤264 THEN RETURN 5; END; RETURN 6; END; ``` -------------------------------- ### Draw Hypotrochoid Source: https://udel.edu/~mm/hp/primePython/spiro.ppl Calculates and draws a hypotrochoid, where an inner wheel rolls within a fixed outer wheel. Requires user input for the radii of the fixed and rolling circles, and the pen offset. ```HP PPL inner(c) // Calculate a hypotrochoid where an inner wheel rolls within a fixed, outer // wheel. GUI inputs: // R: radius in pixels of fixed, larger circle. // r: radius in pixels of rolling, smaller circle inside fixed circle. // d: offset in pixels of pen from center of rolling circle. // See https://en.wikipedia.org/wiki/Hypotrochoid BEGIN LOCAL d,f:=1,R,r,t,tInc,tMax; LOCAL x0,y0,x1,y1; LOCAL s,x0p,y0p,x1p,y1p; INPUT({R,r,d}, "Hypotrochoid", {"R=","r=","d="}, {"Radius (pixels) of Fixed Circle", "Radius (pixels) of Inner Rolling Circle", "Pen Distance (pixels) from Rolling Center"}); if R==0 THEN RETURN; END; tMax:=CAS.lcm(R, r); // theta maximum. tInc:=0.1; // theta increment. RECT; // Clear screen. FOR t FROM 0 TO tMax+tInc STEP tInc DO x0:=x1; y0:=y1; // Save previous point. x1:=(R-r)*COS(t) + d*COS(((R-r)/r)*t); y1:=(R-r)*SIN(t) - d*SIN(((R-r)/r)*t); IF f==1 THEN // First time through loop. f:=0; // Never first time again. ELSE s:=(110-1)/R; // Scale to plotting area. x0p:=160+x0*s; x1p:=160+x1*s; // Center figure. y0p:=110+y0*s; y1p:=110+y1*s; LINE_P(x0p,y0p,x1p,y1p,c); END; END; FREEZE; END; ``` -------------------------------- ### Call numToAvars to Share Data Source: https://udel.edu/~mm/hp/primePython/#:~:text=Since%20official%20Prime%20documentation%20for,App%20needs%20a%20PPL%20wrapper: This shows how to call the numToAvars function to export a Python variable's value to a named variable accessible in the HP Prime's environment. ```python numToAvars('solution', myNumber) ``` -------------------------------- ### Determine Soft Button Tap Source: https://udel.edu/~mm/hp/primePython/#:~:text=Since%20official%20Prime%20documentation%20for,App%20needs%20a%20PPL%20wrapper: This function calculates which soft menu button was pressed given the tap coordinates. It assumes a specific screen layout and button size, returning the button index or -1 if no soft button was tapped. ```python def softPick(pt): # pt is [x, y, xOrig, yOrig, type] return -1 if pt[1]<220 else pt[0]//53 # Soft button is 53x20 pixels. ``` -------------------------------- ### Clear Mouse Events Source: https://udel.edu/~mm/hp/primePython Clears any pending mouse events, such as button bounces, before processing new input. It repeatedly calls `h.eval('mouse(1)')` until no events are detected. ```python import hpprime as h def mouseClear(): # Clear out old mouse events like button bounces. while h.eval('mouse(1)')>=0: pass ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.