Skip to content

Instantly share code, notes, and snippets.

@zoeleu
Last active March 30, 2022 18:54
Show Gist options
  • Save zoeleu/24a98f687d5150e21c0177e039ad37af to your computer and use it in GitHub Desktop.
Save zoeleu/24a98f687d5150e21c0177e039ad37af to your computer and use it in GitHub Desktop.
converter.py
from math import *
import kandinsky
import ion
from ion import *
LCD_HEIGHT = 320
LCD_WIDTH = 222
def convert(i, modifier, exponent):
return i * (modifier**exponent)
UNITS = [["meters", "m"], ["liters", "L"], ["grams", "g"]]
MODIFIERS = [["kilo", "k", 1_000], ["hecto", "h", 100], ["deca", "da", 10], ["", "", 1], ["deci", "d", 0.1], ["centi", "c", 0.01], ["milli", "m", 0.001]]
def input_number():
number = 1
done = False
while not done:
kandinsky.draw_string(str(number), LCD_HEIGHT / 2, LCD_WIDTH / 2)
one = ion.keydown(KEY_ONE)
two = ion.keydown(KEY_TWO)
three = ion.keydown(KEY_THREE)
four = ion.keydown(KEY_FOUR)
five = ion.keydown(KEY_FIVE)
six = ion.keydown(KEY_SIX)
seven = ion.keydown(KEY_SEVEN)
eight = ion.keydown(KEY_EIGHT)
nine = ion.keydown(KEY_NINE)
zero = ion.keydown(KEY_ZERO)
bsp = ion.keydown(KEY_BACKSPACE)
ok = ion.keydown(KEY_OK)
if ok:
done = True
break
if one:
number *= 10
number += 1
if two:
number *= 10
number += 2
if three:
number *= 10
number += 3
if four:
number *= 10
number += 4
if five:
number *= 10
number += 5
if six:
number *= 10
number += 6
if seven:
number *= 10
number += 7
if eight:
number *= 10
number += 8
if nine:
number *= 10
number += 9
if zero:
number *= 10
if bsp:
number = floor(number / 10)
return number
def input_select(array, message):
index = 0
while not done:
kandinsky.fill_rect(0, 0, 320, 222, kandinsky.color(255, 255, 255))
kandinsky.draw_string("Select destination modifier:", 0, 0)
kandinsky.draw_string("Selected: {}".format(array[index][0]), LCD_HEIGHT / 2, LCD_WIDTH / 2)
up = ion.keydown(KEY_DOWN)
down = ion.keydown(KEY_UP)
ok = ion.keydown(KEY_OK)
if ok:
done = True
break
if up:
index += 1
if down:
index -= 1
if index > len(UNITS):
index = 0
if index < 0:
index = len(UNITS)
return index
kandinsky.fill_rect(0, 0, 320, 222, kandinsky.color(255, 255, 255))
kandinsky.draw_string("Type what to convert:", 0, 0)
convert = input_number()
kandinsky.fill_rect(0, 0, 320, 222, kandinsky.color(255, 255, 255))
kandinsky.draw_string("Type exponent:", 0, 0)
exponent = input_number()
origin_unit = input_select(UNITS, "Select origin unit")
origin_modifier = input_select(MODIFIERS, "Select origin modifier")
destination_modifier = input_select(MODIFIERS, "Select destination modifier")
kandinsky.fill_rect(0, 0, 320, 222, kandinsky.color(255, 255, 255))
convert_normal = convert * MODIFIERS[origin_modifier][2]
converted = convert(convert_normal, MODIFIERS[destination_modifier][2], exponent)
kandinsky.fill_rect(0, 0, 320, 222, kandinsky.color(255, 255, 255))
kandinsky.draw_string("Converted: {} {}{}".format(str(converted), UNITS[origin_unit][1], MODIFIERS[destination_modifier][1]), LCD_WIDTH / 2, LCD_HEIGHT / 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment