Calculator app - work in progress

Calculator app.

Navigation with UP, DOWN, LEFT, RIGHT arrows

ENTER selects the box

BACK clears the window and the ANS.

ESC will exit the code

Still work in progress. To do next:

  • error management
  • accept longer expressions. Right now, you have to type the full expression ex.: sqr(5)*sqr(5). In the future, we should use the function boxes as a shortcut.
  • adding new functions, as needed
' --------------------------
' Define colors
' --------------------------
bl=RGB(blue)
gr=RGB(green)
bk=RGB(black)
wt=RGB(white)
gy=RGB(gray)
rd=RGB(red)

' --------------------------
' UI box dimensions
' --------------------------
DIM x(3), y(2)
w=73
h=68
x(0)=10 : x(1)=85 : x(2)=160 : x(3)=235
y(0)=110 : y(1)=180 : y(2)=250

' --------------------------
' Define function labels
' --------------------------
DIM fun$(2,3)
fun$(0,0)="e"       : fun$(0,1)="pi"     : fun$(0,2)="%"     : fun$(0,3)="ANS"
fun$(1,0)="log"     : fun$(1,1)="ln"     : fun$(1,2)="x^y"   : fun$(1,3)="x!"
fun$(2,0)="sqrt"    : fun$(2,1)="sin(x)" : fun$(2,2)="cos(x)": fun$(2,3)="tg(x)"

' --------------------------
' Draw initial UI
' --------------------------
CLS
BOX 0,0,320,320,,bk,bk
BOX 10,10,298,80,2,rd,wt

FOR r=0 TO 2
  FOR c=0 TO 3
    BOX x(c),y(r),w,h,2,wt,gy
    TEXT x(c)+w/2,y(r)+h/2,fun$(r,c),"CM",,,wt,gy
  NEXT
NEXT

' --------------------------
' First input exactly 
' --------------------------
COLOR rd, wt
TEXT 20,60,">","LT",,,rd,wt
INPUT "", input$
ANS = EVAL(input$)

r=-1 : c=0 ' r=-1 means input box is selected
editing%=1

' --------------------------
' Helpers
' --------------------------
SUB HighlightHeader(sel%)
  IF sel% THEN
    BOX 10,10,298,80,2,bl,wt
    COLOR rd,wt
    TEXT 20,60,STR$(ANS),"LT",,,rd,wt
  ELSE
    BOX 10,10,298,80,2,rd,wt
    COLOR rd,wt
    TEXT 20,60,STR$(ANS),"LT",,,rd,wt
  ENDIF
END SUB


SUB EditInput()
  BOX 10,10,298,80,2,bl,wt
  COLOR rd,wt
  TEXT 20,60,">","LT",,,rd,wt
  INPUT "", input$
END SUB

' --------------------------
' Selection logic
' --------------------------
DO WHILE editing%
  IF r>=0 THEN
    BOX x(c),y(r),w,h,2,bl,gy
    TEXT x(c)+w/2,y(r)+h/2,fun$(r,c),"CM",,,bl,gy
  ELSE
    HighlightHeader(1)
  ENDIF

  a$=INKEY$
  IF a$<>"" THEN
    IF r>=0 THEN
      BOX x(c),y(r),w,h,2,wt,gy
      TEXT x(c)+w/2,y(r)+h/2,fun$(r,c),"CM",,,wt,gy
    ELSE
      HighlightHeader(0)
    ENDIF

    SELECT CASE a$
      CASE CHR$(130) : IF r>=0 AND c>0 THEN c=c-1 ' Left
      CASE CHR$(131) : IF r>=0 AND c<3 THEN c=c+1 ' Right
      CASE CHR$(128) ' Up
        IF r=0 THEN r=-1
        IF r>0 THEN r=r-1
      CASE CHR$(129) ' Down
        IF r=-1 THEN r=0 ELSE IF r<2 THEN r=r+1
      CASE CHR$(27)  : editing%=0 ' ESC
      CASE CHR$(8)   ' Backspace: clear window and reset ANS
        ANS=0
            BOX 10,10,298,80,2,rd,wt
            COLOR rd,wt
            TEXT 20,60,"0","LT",,,rd,wt
      CASE CHR$(13)  ' Enter
        IF r=-1 THEN
          ' Prompt for new input, update ANS, and display ANS
          EditInput
          IF LEN(input$) > 0 THEN
            ANS = EVAL(input$)
          ENDIF
          BOX 10,10,298,80,2,rd,wt
          COLOR rd,wt
          TEXT 20,60,STR$(ANS),"LT",,,rd,wt
        ELSE
          ON ERROR SKIP
          SELECT CASE fun$(r,c)
            CASE "e"      : ANS=EXP(1)
            CASE "pi"     : ANS=PI
            CASE "%"      : ANS=ANS/100
            CASE "ANS"    : ANS=ANS
            CASE "log"    : ANS=LOG(ANS) / LOG(10)
            CASE "ln"     : ANS=LOG(ANS)
            CASE "x^y"
              EditInput
              IF LEN(input$) > 0 THEN ANS=ANS^EVAL(input$)
            CASE "x!"     : ANS=Factorial(ANS)
            CASE "sqrt"   : ANS=SQR(ANS)
            CASE "sin(x)" : ANS=SIN(ANS)
            CASE "cos(x)" : ANS=COS(ANS)
            CASE "tg(x)"  : ANS=TAN(ANS)
          END SELECT
          IF MM.ERRNO <> 0 THEN
            BOX 10,10,298,80,2,rd,wt
            COLOR rd,wt
            TEXT 20,60,"Error: " + STR$(MM.ERRNO),"LT",,,rd,wt
            MM.ERRNO = 0
          ELSE
            BOX 10,10,298,80,2,rd,wt
            COLOR rd,wt
            TEXT 20,60,STR$(ANS),"LT",,,rd,wt
          ENDIF
        ENDIF
    END SELECT
  ENDIF

  PAUSE 20
LOOP

COLOR gr,bk
CLS bk

' --------------------------
' Factorial function
' --------------------------
FUNCTION Factorial(n)
  IF n < 0 THEN
    Factorial = 0
  ELSEIF n = 0 OR n = 1 THEN
    Factorial = 1
  ELSE
    Factorial = 1
    FOR i = 2 TO n
      Factorial = Factorial * i
    NEXT
  ENDIF
END FUNCTION

9 Likes

Maybe a larger font ?

Yeah, definitely! Right now I’m focused on making the logic error free and then I can add/remove stuff and make it more user-friendly.

1 Like

Absolutely. I have several projects on the go, software and hardware, flipping between them when I fancy, taking the time to think of better/faster/easier ways to do things etc. Enjoying big moves forward when they occasionally occur…

2 Likes

I’m taking the time, I have no deadlines to meet. I just get ideas and wonder how would they look like and then the question gets annoying and I need to put it in practice. I’m new to coding (except some Arduino stuff) and new to BASIC so I need to learn both at the same time. I love the PicoCalc because you can test your code on the go. Really awesome gadget!

Over 51 years in electronics and coding here. Started with machine code and moved on to basic with what many think was Sinclairs first machine. A little fortran77 and Vax command language too but my hearts always in the machine code camp (hate c and java).

1 Like

Might be easier to implement it as an RPN calculator. You won’t have to deal with lots of brackets in complex equations.

1 Like

Nice. I didn’t knew about it. Sound practical and I’m sure it is, but that might bring confusion for the general user. It might be like the metric system in USA :slight_smile:

1 Like

RPN might be a little confusion at first but after a day or so one gets the feel of it. After a week or so you begain to like it. After a month, you’ll never want to pick up another algebraic calculator. - Jim (IMHO)

1 Like

I’ve gotten very used to RPN from working in Forth myself; one’s its natural you won’t give it second thought.

I used to write a lot of Postscript, also stack based RPN. Really easy once you get used to it. As for calculators, I’d always lusted after the Texas SR52 advertised in Scientific American, so when I finally got to the age where I could afford such a machine I bought a Texas TI58. These were not RPN. However I had lots of access to the more expensive HP which was RPN, and learned to prefer the RPN method, although I still loved my Texas.

Like this ? RPN Calculator for the picocalc :slight_smile: