-
Notifications
You must be signed in to change notification settings - Fork 45
/
CalculatorApp.tspa
333 lines (292 loc) · 12.7 KB
/
CalculatorApp.tspa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
loadscript CalcApp
-- $Title: TSP Calculator
-- $Product: DMM6500, DAQ6510, 2450, 2460, 2461, 2470
-- $Description: A simple calculator that utilizes the TTI Interface
-- $Icon: calc_icon
-- $Version: 1.0
-- Screens
local loading_screen = 1
local main_screen = 1
-- States
local DIO_port = false
-- Global Values
local valOne
local valTwo
local finVal
local valFunc
local output
local historyList = {}
-- Buttons
local num1
local num2
local num3
local num4
local num5
local num6
local num7
local num8
local num9
local num0
local numDec
local NumpadPlus
local NumpadMinus
local NumpadMultiply
local NumpadDivide
local NumpadPower
local NumpadSqrt
local NumpadEquals
local NumpadClear
-- Checks if a DIO port is avaliable
local function numpad_available()
if available(digio) then
DIO_port = true
end
end
-- Toggles math function buttons
local function toggleFunctions(buttonState)
-- Enable = 0 (true), Disable = 1 (false)
display.setstate(NumpadPlus, buttonState)
display.setstate(NumpadMinus, buttonState)
display.setstate(NumpadDivide, buttonState)
display.setstate(NumpadMultiply, buttonState)
display.setstate(NumpadPower, buttonState)
display.setstate(NumpadSqrt, buttonState)
end
-- Toggles numpad buttons
local function toggleNumpad(buttonState)
-- Enable = 0 (true), Disable = 1 (false)
display.setstate(num1, buttonState)
display.setstate(num2, buttonState)
display.setstate(num3, buttonState)
display.setstate(num4, buttonState)
display.setstate(num5, buttonState)
display.setstate(num6, buttonState)
display.setstate(num7, buttonState)
display.setstate(num8, buttonState)
display.setstate(num9, buttonState)
display.setstate(num0, buttonState)
display.setstate(numDec, buttonState)
end
-- Prints a running history of stream outputs
local function printHistory(newVal)
-- Generates List
local history = display.create(main_screen, display.OBJ_LIST, 580, 100, 200, 275, display.LIST_SELECT_CELL);
display.setcell(history, display.LIST_ROW_HEADER, 1, "History")
-- Generates table rows
table.insert(historyList, newVal);
for i = 1, table.getn(historyList) do
display.setcell(history, i, 1, tostring(historyList[table.getn(historyList) - i + 1]))
end
end
functionNames = {
plus = "+",
minus = "-",
divide = "/",
multiply = "*",
pow = "^",
sqrt = "sqrt",
decimal = ".",
}
calculator = {
-- Resets values
clearVals = function()
-- Clears values and printline
valOne = ""
valTwo = ""
valFunc = ""
finVal = ""
display.settext(output, "0")
-- Disables functions/equals and enables numpad
toggleFunctions(1)
toggleNumpad(0)
display.setstate(NumpadEquals, display.STATE_DISABLE)
end,
-- Updates the output stream
updateNums = function(val)
-- Checks if it is the 1st or 2nd input
if valFunc != "" then
valTwo = valTwo..tostring(val)
display.settext(output, valTwo)
-- Disables functions and enables equals
toggleFunctions(1)
display.setstate(NumpadEquals, display.STATE_ENABLE)
else
valOne = valOne..tostring(val)
display.settext(output, tostring(valOne))
-- Enables functions
toggleFunctions(0)
end
-- If the input was a decimal
if val == "." then
-- Disable decimal and functions/equals
display.setstate(numDec, display.STATE_DISABLE)
toggleFunctions(1)
display.setstate(NumpadEquals, display.STATE_DISABLE)
end
end,
-- Sets the calculator's function
setFunction = function(funcType)
-- Sets global variable equal to input
valFunc = funcType;
-- If the function is a square root, calculate immediately
if funcType == "sqrt" then
calculator.calculate()
else
toggleNumpad(0)
end
end,
-- Calculates the output
calculate = function()
-- Determines what function to apply
if valFunc == "+" then
finVal = tostring(tonumber(valOne) + tonumber(valTwo))
elseif valFunc == "-" then
finVal = tostring(tonumber(valOne) - tonumber(valTwo))
elseif valFunc == "*" then
finVal = tostring(tonumber(valOne) * tonumber(valTwo))
elseif valFunc == "/" then
if tonumber(valTwo) != 0 then
finVal = tostring(tonumber(valOne) / tonumber(valTwo))
else
finVal = "nan"
end
elseif valFunc == "^" then
finVal = tostring(tonumber(valOne) ^ tonumber(valTwo))
elseif valFunc == "sqrt" then
finVal = math.sqrt(tonumber(valOne))
else
-- If a math operation fails to be toggled
finVal = "nan"
end
-- Updates values for additional calculations
valOne = finVal
valTwo = ""
-- Disables equals/numpad and enables functions
display.setstate(NumpadEquals, display.STATE_DISABLE)
toggleFunctions(0)
toggleNumpad(1)
-- Prints final value
display.settext(output, tostring(finVal))
-- Adds value to running history
printHistory(finVal)
end
}
-- Sets up screen
local function init()
-- Generates Main Screen
main_screen = display.create(display.ROOT, display.OBJ_SCREEN_HOME, "Calculator")
output = display.create(main_screen, display.OBJ_TEXT, 695, 70, "0", 0xFFFFFF, display.FONT_7, display.JUST_RIGHT)
-- Generates Loading Screen
loading_screen = display.create(display.ROOT, display.OBJ_SCREEN, "Loading, please wait...")
-- Draws outline for output stream
local outline_outer_id = display.create(main_screen, display.OBJ_RECT, 10, 10, 700, 80)
display.setcolor(outline_outer_id , 0x6495ED)
display.setthickness(outline_outer_id, 4)
local outline_inner_id = display.create(main_screen, display.OBJ_RECT, 15, 15, 690, 70)
display.setcolor(outline_inner_id , 0xD3D3D3)
display.setthickness(outline_inner_id, 4)
-- Checks for Numpad
numpad_available()
DIO_state = display.create(main_screen, display.OBJ_TEXT, 10, 410, "DIO Port: ", 0xadd8e6, display.FONT_LARGE, display.JUST_LEFT)
if DIO_port == true then
display.settext(DIO_state, "DIO Port: Available")
display.setcolor(DIO_state, 0x90ee90)
else
display.settext(DIO_state, "DIO Port: Not Available")
display.setcolor(DIO_state, 0xFF7F7F)
end
-- Generate Numpad
num1 = display.create(main_screen, display.OBJ_BUTTON, 10, 100, "1", 60)
num2 = display.create(main_screen, display.OBJ_BUTTON, 80, 100, "2", 60)
num3 = display.create(main_screen, display.OBJ_BUTTON, 150, 100, "3", 60)
num4 = display.create(main_screen, display.OBJ_BUTTON, 10, 170, "4", 60)
num5 = display.create(main_screen, display.OBJ_BUTTON, 80, 170, "5", 60)
num6 = display.create(main_screen, display.OBJ_BUTTON, 150, 170, "6", 60)
num7 = display.create(main_screen, display.OBJ_BUTTON, 10, 240, "7", 60)
num8 = display.create(main_screen, display.OBJ_BUTTON, 80, 240, "8", 60)
num9 = display.create(main_screen, display.OBJ_BUTTON, 150, 240, "9", 60)
num0 = display.create(main_screen, display.OBJ_BUTTON, 10, 310, "0", 130)
numDec = display.create(main_screen, display.OBJ_BUTTON, 150, 310, ".", 60)
-- Generate Functions
NumpadPlus = display.create(main_screen, display.OBJ_BUTTON, 260, 100, "+", 120)
NumpadMinus = display.create(main_screen, display.OBJ_BUTTON, 410, 100, "-", 120)
NumpadMultiply = display.create(main_screen, display.OBJ_BUTTON, 260, 170, "*", 120)
NumpadDivide = display.create(main_screen, display.OBJ_BUTTON, 410, 170, "/", 120)
NumpadPower = display.create(main_screen, display.OBJ_BUTTON, 260, 240, "^", 120)
NumpadSqrt = display.create(main_screen, display.OBJ_BUTTON, 410, 240, "sqrt", 120)
-- Generate Misc Buttons
NumpadEquals = display.create(main_screen, display.OBJ_BUTTON, 260, 310, "=", 270)
NumpadClear = display.create(main_screen, display.OBJ_BUTTON, 725, 20, "AC", 60)
-- Switches screens and deletes loading screen
display.waitevent(1)
display.changescreen(main_screen)
display.delete(loading_screen)
loading_screen = nil
end
local function main()
init()
calculator.clearVals()
-- Number Buttons
display.setevent(num1, display.EVENT_PRESS, "calculator.updateNums(tostring(1))")
display.setevent(num2, display.EVENT_PRESS, "calculator.updateNums(tostring(2))")
display.setevent(num3, display.EVENT_PRESS, "calculator.updateNums(tostring(3))")
display.setevent(num4, display.EVENT_PRESS, "calculator.updateNums(tostring(4))")
display.setevent(num5, display.EVENT_PRESS, "calculator.updateNums(tostring(5))")
display.setevent(num6, display.EVENT_PRESS, "calculator.updateNums(tostring(6))")
display.setevent(num7, display.EVENT_PRESS, "calculator.updateNums(tostring(7))")
display.setevent(num8, display.EVENT_PRESS, "calculator.updateNums(tostring(8))")
display.setevent(num9, display.EVENT_PRESS, "calculator.updateNums(tostring(9))")
display.setevent(num0, display.EVENT_PRESS, "calculator.updateNums(tostring(0))")
display.setevent(numDec, display.EVENT_PRESS, "calculator.updateNums(functionNames.decimal)")
-- Function Buttons
display.setevent(NumpadPlus, display.EVENT_PRESS, "calculator.setFunction(functionNames.plus)")
display.setevent(NumpadMinus, display.EVENT_PRESS, "calculator.setFunction(functionNames.minus)")
display.setevent(NumpadMultiply, display.EVENT_PRESS, "calculator.setFunction(functionNames.multiply)")
display.setevent(NumpadDivide, display.EVENT_PRESS, "calculator.setFunction(functionNames.divide)")
display.setevent(NumpadPower, display.EVENT_PRESS, "calculator.setFunction(functionNames.pow)")
display.setevent(NumpadSqrt, display.EVENT_PRESS, "calculator.setFunction(functionNames.sqrt)")
-- Calculation/Clear Buttons
display.setevent(NumpadEquals, display.EVENT_PRESS, "calculator.calculate()")
display.setevent(NumpadClear, display.EVENT_PRESS, "calculator.clearVals()")
end
main()
endscript
loadimage calc_icon CalcApp
iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAYAAAA6/NlyAAAHYE
lEQVRoQ+WbaXAURRTHuzdH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