# SPDX-License-Identifier: LGPL-3.0-or-later # Copyright (C) 2020 Johannes Wache """Calculator application This is a simple calculator app that uses the build-in eval() function to compute the solution. """ import wasp, fonts # 2-bit RLE, generated from res/calc.png, 413 bytes calc = ( b'\x02' b'`@' b'(@\x03P?\x0eV?\x08[?\x04_?\x01b' b' 200): # undo button pressed if (self.output != ""): self.output = self.output[:-1] else: x = event[1] // 47 y = (event[2] // 48) - 1 # Error handling for touching at the border 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 == "="): try: self.output = str(eval(self.output.replace('^', '**')))[:12] except: wasp.watch.vibrator.pulse() else: self.output += button_pressed self._update() def _draw(self): draw = wasp.watch.drawable draw.fill() # Make grid: draw.set_color(wasp.system.theme('accent-lo')) for i in range(4): # horizontal lines draw.line(x0=0,y0=(i+1)*47,x1=239,y1=(i+1)*47) # vertical lines draw.line(x0=(i+1)*47,y0=47,x1=(i+1)*47,y1=239) draw.line(x0=0, y0=47, x1=0, y1=239) draw.line(x0=239, y0=47, x1=239, y1=239) draw.line(x0=0, y0=239, x1=239, y1=239) # Draw button label: draw.set_color(wasp.system.theme('accent-hi')) for x in range(5): if x == 3: draw.set_color(wasp.system.theme('accent-mid')) for y in range(4): label = fields[x + 5*y] if (x == 0): draw.string(label, x*47+14, y*47+60) else: draw.string(label, x*47+16, y*47+60) draw.string("<", 215, 10) draw.set_color(wasp.system.theme('accent-hi')) def _update(self): output = self.output if len(self.output) < 12 else self.output[len(self.output)-12:] wasp.watch.drawable.string(output, 0, 14, width=200, right=True)