apps: calc: Optimize the fields lookup structure
Currently the fields is a list of lists of strings. This will needlessly consume RAM so lets switch it over to a simple string (which is immutable and can be stored in flash). We also replace indices with simple x and y variables. In addition to avoiding a (temporary) memory allocation this is also easier to use when looking up in fields. Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
This commit is contained in:
parent
60fecc9469
commit
e570bf4f26
1 changed files with 14 additions and 12 deletions
|
@ -39,6 +39,10 @@ calc = (
|
||||||
b'n3l5j7h9f<b?\x01^?\x05'
|
b'n3l5j7h9f<b?\x01^?\x05'
|
||||||
b'XAA?\tV?\x0eP(')
|
b'XAA?\tV?\x0eP(')
|
||||||
|
|
||||||
|
fields = ( '789+('
|
||||||
|
'456-)'
|
||||||
|
'123*^'
|
||||||
|
'C0./=' )
|
||||||
|
|
||||||
class CalculatorApp():
|
class CalculatorApp():
|
||||||
NAME = 'Calc'
|
NAME = 'Calc'
|
||||||
|
@ -46,11 +50,7 @@ class CalculatorApp():
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.output = ""
|
self.output = ""
|
||||||
self.fields = [["7","8","9","+","("],
|
|
||||||
["4","5","6","-",")"],
|
|
||||||
["1","2","3","*","^"],
|
|
||||||
["C","0",".",":","="]]
|
|
||||||
|
|
||||||
def foreground(self):
|
def foreground(self):
|
||||||
self._draw()
|
self._draw()
|
||||||
self.output = ""
|
self.output = ""
|
||||||
|
@ -62,13 +62,15 @@ class CalculatorApp():
|
||||||
if (self.output != ""):
|
if (self.output != ""):
|
||||||
self.output = self.output[:-1]
|
self.output = self.output[:-1]
|
||||||
else:
|
else:
|
||||||
indices = [(event[2]// 48)-1,event[1]//47]
|
x = event[1] // 47
|
||||||
|
y = (event[2] // 48) - 1
|
||||||
|
|
||||||
# Error handling for touching at the border
|
# Error handling for touching at the border
|
||||||
if (indices[0]>3):
|
if x > 4:
|
||||||
indices[0] = 3
|
x = 4
|
||||||
if (indices[1]>4):
|
if y > 3:
|
||||||
indices[1] = 4
|
y = 3
|
||||||
button_pressed = self.fields[indices[0]][indices[1]]
|
button_pressed = fields[x + 5*y]
|
||||||
if (button_pressed == "C"):
|
if (button_pressed == "C"):
|
||||||
self.output = ""
|
self.output = ""
|
||||||
elif (button_pressed == "="):
|
elif (button_pressed == "="):
|
||||||
|
@ -96,7 +98,7 @@ class CalculatorApp():
|
||||||
if x == 3:
|
if x == 3:
|
||||||
draw.set_color(wasp.system.theme('accent-mid'))
|
draw.set_color(wasp.system.theme('accent-mid'))
|
||||||
for y in range(4):
|
for y in range(4):
|
||||||
label = self.fields[y][x]
|
label = fields[x + 5*y]
|
||||||
if (x == 0):
|
if (x == 0):
|
||||||
draw.string(label, x*47+14, y*47+60)
|
draw.string(label, x*47+14, y*47+60)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue