Simple Calculator
One of my math classes had a test recently, and the Professor only
allowed "simple" calculators. I forgot all about that particular
requirement, and was planning on taking my TI-89 until the test day
rolls around and I was reminded. So I rushed out this little
application in half an hour that does simple integer math (+,-,*,/,^).
I think it shows the power of the Propeller that I was able to build
this system so quickly and easily, and that it works the way it's
supposed to. Another 15 minutes today (after the test) made the output
and input a little nicer and more efficient.
|
The system has a keyboard
and a LCD tv screen taped to a demo board and
a 7.2V NiMH RC car battery. It uses the standard library objects for
those, plus FDXP for the strToDec routine. |
In the end there weren't
any numbers on the test that would have
required a calculator, so I didn't get to use it. Also, I was told that a
simple calculator can't have a full QWERTY keyboard or a multi-line
display. I took comfort, though, in the fact that the Propeller was
always ready and able to do the job. |
|
|
|
The Code:
{{
This is a fun little calculator program for the Demo Board.
March 2010, SRLM
}}
CON
_xinfreq = 5_000_000
_clkmode = xtal1 + pll16x
OBJ
keyboard : "keyboard"
tv : "Tv_Text"
serial : "FullDuplexSerialPlus"
VAR
byte num[20]
byte input[20]
long done
long key
long size
long num1, num2
long counter
long answer
long operator
PUB Main
keyboard.start(26, 27)
tv.start(12)
serial.start(0,1,0, 57600)
waitcnt(clkfreq + cnt)
tv.str(string("SRLM Calc!", 13))
done := size := 0
repeat
'Get input
repeat counter from 0 to 20
input[counter] := keyboard.getKey
if(input[counter] == 13) 'Enter key
input[counter] := 0
quit
tv.out(input[counter])
'Parse first number
repeat counter from 0 to 20
if(input[counter] => "0" AND
input[counter] =< "9")
num[counter] :=
input[counter]
else
num[counter] := 0
quit
if(counter <> 0)
num1 := serial.StrtoDec(@num)
else
num1 := answer
'Parse operator
operator := input[counter]
counter++
bytemove(@input, @input+counter, 20)
'Parse second number
repeat counter from 0 to 20
if(input[counter] => "0" AND
input[counter] =< "9")
num[counter] :=
input[counter]
else
num[counter] := 0
quit
num2 := serial.StrtoDec(@num)
if operator == 42
answer := num1 * num2
elseif operator == 47
answer := num1 / num2
elseif operator == 43
answer := num1 + num2
elseif operator == "-"
answer := num1 - num2
elseif operator == "^"
answer := 1
repeat counter from 1 to num2
answer *= num1
else
answer := 0
tv.str(string(" = "))
tv.dec(answer)
tv.out(13)
|
Attachments
(Right click and "Save Link As...")
Calculator.spin
|