diff options
| author | Daniel Thompson <daniel@redfelineninja.org.uk> | 2020-12-28 14:37:15 (GMT) |
|---|---|---|
| committer | Daniel Thompson <daniel@redfelineninja.org.uk> | 2020-12-29 08:59:53 (GMT) |
| commit | e570bf4f262180f09d8b6bdef393d529c567b238 (patch) | |
| tree | 77fd4069c6a21d80f70095843ab6d80c14a866f8 | |
| parent | 60fecc946934fc95ed20479e50cc3aacceb58b37 (diff) | |
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>
| -rw-r--r-- | wasp/apps/calc.py | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/wasp/apps/calc.py b/wasp/apps/calc.py index 35a2309..5860b31 100644 --- a/wasp/apps/calc.py +++ b/wasp/apps/calc.py @@ -39,6 +39,10 @@ calc = ( b'n3l5j7h9f<b?\x01^?\x05' b'XAA?\tV?\x0eP(') +fields = ( '789+(' + '456-)' + '123*^' + 'C0./=' ) class CalculatorApp(): NAME = 'Calc' @@ -46,11 +50,7 @@ class CalculatorApp(): def __init__(self): self.output = "" - self.fields = [["7","8","9","+","("], - ["4","5","6","-",")"], - ["1","2","3","*","^"], - ["C","0",".",":","="]] - + def foreground(self): self._draw() self.output = "" @@ -62,13 +62,15 @@ class CalculatorApp(): if (self.output != ""): self.output = self.output[:-1] 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 - if (indices[0]>3): - indices[0] = 3 - if (indices[1]>4): - indices[1] = 4 - button_pressed = self.fields[indices[0]][indices[1]] + if x > 4: + x = 4 + if y > 3: + y = 3 + button_pressed = fields[x + 5*y] if (button_pressed == "C"): self.output = "" elif (button_pressed == "="): @@ -96,7 +98,7 @@ class CalculatorApp(): if x == 3: draw.set_color(wasp.system.theme('accent-mid')) for y in range(4): - label = self.fields[y][x] + label = fields[x + 5*y] if (x == 0): draw.string(label, x*47+14, y*47+60) else: |
