convert.py

Created by cent20

Created on September 16, 2022

4.42 KB

Convert bin hex dec
Python Util for Numworks, all models.
By Ilyas RAHMOUN & Vincent ROBERT. September 2022.

Learn more about Convert on: nsi.xyz/convert (FR)

Changelog

Convert v1.0 - 16/09/2022:
- Initial util


from kandinsky import draw_string as txt, fill_rect as rec
from ion import keydown as k

# Convert 1.0 NumWorks, 16.09.2022
# Par Ilyas RAHMOUN & Vincent ROBERT
# https://nsi.xyz/convert <3

grey_c,dark_c,game_c = (196,196,196),(42,42,42),(148,113,222)
pos,val,mode = 0,42,1
characters, eggs = {"C":32355021942288319580, "o":16773148219294089216, "n":20231912733114630144, "v":4991731697172086784,
              "e":34625346773446557696, "r":1190113516208521216, "t":34624836154483246113, "0":16773148341215741486,
              "1":9520900167075899784, "2":34624836168961245711, "3":17888878827397464591, "4":9520900958181164065,
              "5":17888878826877912126, "6":16773148338514068542, "7":19041800347036697103,
              "8":16773148337994516014, "9":16735957322825254446, "A":20231912870068667716, "B":8414467146932340007,
              "D":8414467432816100647, "E":34624836160925500478, "F":1190112527326938174, "+":4568909873152,
              "-":33285996544, "":0}, {42:"The answer to life,"}
values = ["0","0","1","0","1","0","1","0","","","2","A","","+","0","4","2"]


def draw_letter(x, y, dico_key, c, loop=0):
    rec(x * 2 + 18 * loop, y, 14, 38, "white")
    for j in range(65):
        if characters[dico_key] >> j & 1 == 1:
            rec(x * 2 + 18 * loop + (j % 5) * 3, y + (j // 5) * 3, 2, 2, c)


def gui():
    for i, l in enumerate("Convert"):
        draw_letter(42, 5, l, game_c, i)
    txt("8 bits", 220, 30, dark_c)
    rec(139, 200, 174, 22, game_c)
    txt("Calculate by", 12, 202, grey_c)
    txt("nsi.xyz/convert", 152, 202, "white", game_c)
    txt("  BIN         HEX     DEC", 50, 155, dark_c)
    cursor()
    refresh(values, values, 1)


def refresh(old_values, new_values, init=0):
    global values, mode
    if init:
        txt("  integer  ", 8, 55, "white", game_c)
        txt(" Two's complement ", 130, 55, "white", grey_c)
        for i in range(len(values)):
            draw_letter(5, 98, values[i], game_c, i)
    else:
        for i in range(len(values)):
            if old_values[i] != new_values[i]:
                draw_letter(5 + 9 * i, 98, new_values[i], game_c)
        values = new_values
    rec(0, 178, 320, 19, "white")
    if val in eggs:
        txt(eggs[val], 160 - len(eggs[val]) * 5, 178, grey_c)


def switch():
    global mode, val
    txt("  integer  ", 8, 55, "white", grey_c if mode == 1 else game_c)
    txt(" Two's complement ", 130, 55, "white", game_c if mode == 1 else grey_c)
    mode = -1 if mode == 1 else 1
    val = (val*(-1 if val < 0 else -1 if mode != 1 else 1)) if val <= 128 else -128
    refresh(values, calcul(val))


def arrow(x, y, d):
    for i in range(6):
        if d == 0:
            rec((x - 2) + 6 - i, y + i, 2 + 2 * i, 1, game_c)
        if d == 1:
            rec((x - 1) + i, y + i, 12 - 2 * i, 1, game_c)


def d_p(p_x):
    return 18 * p_x + (12 if p_x < 8 else 48 if p_x < 10 else 66)


def cursor():
    global pos
    pos = 13 if pos == -1 else 0 if pos == 14 else pos
    rec(11, 85, 300, 6, "white")
    rec(11, 145, 300, 6, "white")
    arrow(d_p(pos), 85, 0)
    arrow(d_p(pos), 145, 1)


def update(p, signe):
    global val
    if p == 10 and val != 0:
        switch()
        return
    temp_val = val + signe * (2 ** abs(p - 7)) * (p < 8) + signe * (2 ** (4 * (p == 8)))*(8 <= p < 10) + signe * (int(
               "1" + "0" * (13 - p)))*(11 <= p < 14)
    if (val >= 0 and temp_val < 0) or (val < 0 and temp_val >= 0):
        switch()
    val = temp_val if -128 <= temp_val <= 255 else val
    refresh(values, calcul(val))


def calcul(v):
    return list("0" * (10 - len(bin(v + 256 * (v < 0)))) + bin(v + 256 * (v < 0))[2:]) + ["", ""] + list("0" * (4 - len(
            hex(v + 256 * (v < 0)))) + hex(v + 256 * (v < 0))[2:].upper()) + ["", "-" if val < 0 else "+"] + list(
            "0" * (3 - len(str(v)[1*(mode != 1):])) + str(v)[1*(mode != 1):])


def convert():
    global pos
    gui()
    last_key = None
    while not k(5):
        if k(0) and last_key != 0:
            pos -= 1
            cursor()
            last_key = 0
        if k(3) and last_key != 3:
            pos += 1
            cursor()
            last_key = 3
        if (k(1) and last_key != 1) or (k(2) and last_key != 2):
            update(pos, 1*(k(1)+-1*(k(2))))
            last_key = 1 if k(1) else 2
        if k(4) and last_key != 4 and val != 0:
            switch()
            last_key = 4
        if not (k(0) or k(3) or k(1) or k(2) or k(4)):
            last_key = None


convert()