from math import pi, asin KEY_NONE, KEY_LEFT, KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_ENTER, KEY_ESC, KEY_7, KEY_8, KEY_9, KEY_LEFT_PARENTHESIS, KEY_RIGHT_PARENTHESIS, KEY_4, KEY_5, KEY_6, KEY_1, KEY_2, KEY_3, KEY_0 = 0, 1, 2, 3, 4, 53, 6, 30, 31, 32, 33, 34, 36, 37, 38, 42, 43, 44, 48 def col3_2_rgb(c, bits, bgr): return c[2*bgr]//2**(8 - bits[0]) + c[1]//2**(8 - bits[1])*2**bits[0] + c[2*(not(bgr))]//2**(8-bits[2])*2**(bits[0] + bits[1]) tcanvas = None def get_infos(keys, sdl_width=640, sdl_height=480, sdl_scale=1): global tcanvas fnop = lambda : None ffalse = lambda : False screen_w, screen_h, screen_y0, color_bits, font_w, font_num_w, font_h, poly_has_color, poly_get_pixel, poly_set_pixel, poly_show, poly_draw_line, poly_fill_rect, poly_draw_circle, poly_fill_circle, poly_draw_ellipse, poly_fill_ellipse, poly_monotonic, poly_sleep, poly_draw_string, poly_clear_screen, poly_pause, poly_set_buffer, poly_get_key, poly_wait_key, poly_wait_release, poly_test_key, poly_esc_key, show_need_pause_on_exit, screen_need_clear_on_init, need_clear, need_line, need_rect, need_ellipse, need_string, need_monotonic, need_sleep, has_keys, poly_ignore_events = 0, 0, 0, [5, 6, 5], 0, 0, 0, 1, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, fnop, ffalse, ffalse, ffalse, ffalse, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, fnop if not (screen_w): try: import sys try: t_ns = sys.platform == "nspire" t_nw = sys.platform == "numworks" t_prizm = sys.platform == "prizm" if t_ns or t_nw or t_prizm: try: # KhiCAS Micropython import graphic, nsp, cas, time screen_w, screen_h, font_w, font_h, poly_get_pixel, poly_set_pixel, poly_show, poly_pause, need_clear, need_line, need_rect, need_ellipse, need_string, need_monotonic, screen_need_clear_on_init = t_prizm and 384 or 320, t_prizm and 192 or 222, 11 + 4*(t_ns or t_prizm), 18, graphic.get_pixel, graphic.set_pixel, graphic.show_screen, nsp.waitKeypress, 1, 0, 0, 0, 0, 0, 1 def poly_draw_line(x1, y1, x2, y2, c): graphic.draw_line(int(x1), int(y1), int(x2), int(y2), col3_2_rgb(c, color_bits, 1)) def poly_fill_rect(x1, y1, x2, y2, c): graphic.draw_filled_rectangle(int(x1), int(y1), int(x2), int(y2), c) def poly_draw_circle(x, y, r, c): graphic.draw_circle(int(x), int(y), int(r), c) def poly_fill_circle(x, y, r, c): graphic.draw_filled_circle(int(x), int(y), int(r), c) def poly_draw_ellipse(x, y, rx, ry, c): graphic.draw_arc(int(x), int(y), int(rx), int(ry), 0, 360, c) def poly_fill_ellipse(x, y, rx, ry, c): graphic.draw_filled_arc(int(x), int(y), int(rx), int(ry), 0, 360, c) def poly_draw_string(s, x, y, cf, cb=(255,255,255)): if cb: graphic.draw_string(int(x), int(y), s, cf, cb) else: graphic.draw_string(int(x), int(y), s, cf) def poly_wait_key(): d_key = {KEY_LEFT:0, KEY_UP:1, KEY_DOWN:2, KEY_RIGHT:3, KEY_ENTER:4, KEY_ESC:5, KEY_LEFT_PARENTHESIS:40, KEY_RIGHT_PARENTHESIS:41, KEY_0:48, KEY_1:49, KEY_2:50, KEY_3:51, KEY_4:52, KEY_5:53, KEY_6:54, KEY_7:55, KEY_8:56, KEY_9:57} key = int(cas.caseval("get_key()")) return key in d_key.values() and list(d_key.keys())[list(d_key.values()).index(key)] def poly_monotonic(): return time.monotonic() / (1 + (t_nw or t_prizm)*999) font_num_w = font_w except: # Nspire MicroPython import nsp screen_w, screen_h, need_clear, show_need_pause_on_exit = 320, 240, 1, 1 tcanvas = nsp.Texture(screen_w, screen_h, 0) poly_get_pixel = tcanvas.getPx def poly_set_pixel(x, y, c): if x>=0 and x<=screen_w-1 and y>=0 and y <=screen_h-1: tcanvas.setPx(x, y, col3_2_rgb(c, color_bits, 1)) poly_show, poly_pause = tcanvas.display, nsp.waitKeypress elif sys.platform == "TI-Nspire": # CX II import time screen_w, screen_h, font_w, font_h, need_monotonic, need_sleep, poly_sleep = 318, 212, 12, 12, 0, 0, time.sleep font_num_w = font_w def test(d,f): t=f() time.sleep(d) return round((f()-t)/d,0) if test(0.1, time.clock) == 1: poly_monotonic = time.clock else: poly_monotonic = time.time try: import ti_system d_key = {KEY_LEFT:'left', KEY_UP:'up', KEY_DOWN:'down', KEY_RIGHT:'right', KEY_ENTER:'enter', KEY_ESC:'esc', KEY_LEFT_PARENTHESIS:'(', KEY_RIGHT_PARENTHESIS:')', KEY_0:'0', KEY_1:'1', KEY_2:'2', KEY_3:'3', KEY_4:'4', KEY_5:'5', KEY_6:'6', KEY_7:'7', KEY_8:'8', KEY_9:'9'} def poly_get_key(): key = ti_system.get_key() return key in d_key.values() and list(d_key.keys())[list(d_key.values()).index(key)] def poly_wait_key(): key = "" while key == "": key = ti_system.get_key() return key in d_key.values() and list(d_key.keys())[list(d_key.values()).index(key)] def poly_test_key(v): return poly_get_key() == d_key[v] def poly_esc_key(): return poly_test_key(KEY_ESC) except: has_keys = 0 if "gp" in keys: import ti_image need_line, need_rect, need_ellipse, need_string = 1, 1, 1, 1 tcanvas = ti_image.new_image(screen_w, screen_h, (255,255,255)) poly_get_pixel, poly_set_pixel = tcanvas.get_pixel, tcanvas.set_pixel def poly_show(): tcanvas.show_image(0,0) else: import ti_draw ti_draw.use_buffer() ti_draw.set_pen("thin","solid") need_line, need_rect, need_ellipse, need_string, poly_show, set_buffer = 0, 0, 0, 0, ti_draw.paint_buffer, ti_draw.use_buffer def poly_set_pixel(x, y, c): ti_draw.set_color(tuple(c)) ti_draw.plot_xy(x, y, 7) def poly_draw_line(x1, y1, x2, y2, c): ti_draw.set_color(tuple(c)) ti_draw.draw_line(x1, y1, x2, y2) def poly_fill_rect(x, y, w, h, c): ti_draw.set_color(tuple(c)) ti_draw.fill_rect(x, y, w, h) def poly_draw_circle(x, y, r, c): ti_draw.set_color(tuple(c)) ti_draw.draw_circle(x, y, r) def poly_fill_circle(x, y, r, c): ti_draw.set_color(tuple(c)) ti_draw.fill_circle(x, y, r) def poly_draw_ellipse(x, y, rx, ry, c): ti_draw.set_color(tuple(c)) x0, y0 = x - rx, y - ry for dy in range(1 + (y0 > int(y0))): for dx in range(1 + (x0 > int(x0))): ti_draw.draw_arc(x0 + dx, y0 + dy, 2 * rx, 2 * ry, 0, 360) def poly_fill_ellipse(x, y, rx, ry, c): ti_draw.set_color(tuple(c)) ti_draw.fill_arc(x - rx, y - ry, 2 * rx, 2 * ry, 0, 360) def poly_draw_string(s, x, y, cf, cb=None): if cb: poly_fill_rect(x, y, font_w * len(s), font_h, cb) ti_draw.set_color(tuple(cf)) ti_draw.draw_text(x, y + font_h, s) elif sys.platform.startswith('TI-Python'): import ti_graphics, ti_system, time screen_w, screen_h, font_w, font_h, screen_y0, need_line, need_rect, need_ellipse, need_string, need_monotonic, need_sleep, poly_pause, poly_wait_key, poly_sleep, poly_monotonic, poly_esc_key, need_clear, show_need_pause_on_exit, need_sleep, screen_need_clear_on_init = 320, 210, 10, 15, 30, 0, 0, 0, 0, 0, ti_system.disp_wait, ti_system.wait_key, time.sleep, time.monotonic, ti_system.escape, 1, 1, 0, 1 font_num_w = font_w def poly_draw_line(x1, y1, x2, y2, c): ti_graphics.setColor(c) ti_graphics.drawLine(x1, y1 + screen_y0, x2, y2 + screen_y0) def poly_fill_rect(x, y, w, h, c): ti_graphics.setColor(c) ti_graphics.fillRect(x, y + screen_y0, w, h) def poly_get_pixel(x, y): return ti_graphics.getPixel(x, y + screen_y0) def poly_set_pixel(x, y, c): ti_graphics.setPixel(x, y + screen_y0, c) def poly_draw_ellipse(x, y, rx, ry, c): ti_graphics.setColor(c) x0, y0 = x - rx, y - ry for dy in range(1 + (y0 > int(y0))): for dx in range(1 + (x0 > int(x0))): ti_graphics.drawArc(x0 + dx, y0 + dy + screen_y0, 2 * rx, 2 * ry, 0, 3600) def poly_fill_ellipse(x, y, rx, ry, c): ti_graphics.setColor(c) ti_graphics.fillArc(x - rx, y - ry + screen_y0, 2 * rx, 2 * ry, 0, 3600) def poly_draw_circle(x, y, r, c): poly_draw_ellipse(x, y, r, r, c) def poly_fill_circle(x, y, r, c): ti_graphics.setColor(c) ti_graphics.fillCircle(x, y + screen_y0, r) def poly_draw_string(s, x, y, cf, cb=None): if cb: poly_fill_rect(x, y, font_w * len(s), font_h, cb) ti_graphics.setColor(cf) ti_graphics.drawString(s, x, y + screen_y0) def poly_wait_key(): d_key = {KEY_LEFT:2, KEY_UP:3, KEY_DOWN:4, KEY_RIGHT:1, KEY_ENTER:5, KEY_ESC:9, KEY_LEFT_PARENTHESIS:133, KEY_RIGHT_PARENTHESIS:134, KEY_0:142, KEY_1:143, KEY_2:144, KEY_3:145, KEY_4:146, KEY_5:147, KEY_6:148, KEY_7:149, KEY_8:150, KEY_9:151} key = ti_system.wait_key() return key in d_key.values() and list(d_key.keys())[list(d_key.values()).index(key)] except: pass except: pass if not (screen_w): # Casio Graph 90/35+E II try: import casioplot casioplot.set_pixel(0, 0, (0, 0, 255)) col = casioplot.get_pixel(0, 0) t = col[0] == col[2] screen_w, screen_h, poly_has_color, font_w, font_h, poly_get_pixel, poly_set_pixel, poly_show, poly_clear_screen, need_string, has_keys = t and 128 or 384, t and 64 or 192, not (t), t and 6 or 10, t and 5 or 10, casioplot.get_pixel, casioplot.set_pixel, casioplot.show_screen, casioplot.clear_screen, 0, 0 font_num_w = t and 4 or 8 def poly_draw_string(s, x, y, cf, cb=None): if cb: poly_fill_rect(x, y, font_w * len(s), font_h, cb) casioplot.draw_string(x, y, s, cf, "small") def poly_pause(): try: while(1): pass except KeyboardInterrupt: pass except: try: # NumWorks import kandinsky, ion, time screen_w, screen_h, font_w, font_h, poly_get_pixel, poly_set_pixel, poly_draw_string, poly_sleep, poly_monotonic, need_rect, need_string, need_monotonic, need_sleep = 320, 222, 11, 18, kandinsky.get_pixel, kandinsky.set_pixel, kandinsky.draw_string, time.sleep, time.monotonic, 0, 0, 0, 0 font_num_w = font_w def poly_fill_rect(x, y, w, h, c): kandinsky.fill_rect(int(x), int(y), int(w), int(h), c) def poly_pause(): def key_down(): for k in range(53): if ion.keydown(k): return 1 return 0 while key_down(): pass while not (key_down()): pass def poly_draw_string(s, x, y, cf, cb=(255,255,255)): if cb: kandinsky.draw_string(s, x, y, cf, cb) else: kandinsky.draw_string(s, x, y, cf) d_key = {KEY_LEFT:0, KEY_UP:1, KEY_RIGHT:2, KEY_DOWN:3, KEY_ENTER:52, KEY_ESC:5, KEY_LEFT_PARENTHESIS:33, KEY_RIGHT_PARENTHESIS:34, KEY_0:48, KEY_1:42, KEY_2:43, KEY_3:44, KEY_4:36, KEY_5:37, KEY_6:38, KEY_7:30, KEY_8:31, KEY_9:32} def poly_get_key(): for k in d_key: if poly_test_key(d_key[k]): return k return 0 def poly_wait_key(): k = 0 while not k: k = poly_get_key() return k def poly_test_key(v): return ion.keydown(d_key[v]) def poly_esc_key(): return poly_test_key(KEY_ESC) except: try: # HP Prime import hpprime screen_w, screen_h, font_h, color_bits, need_clear, show_need_pause_on_exit, screen_need_clear_on_init, need_line, need_rect, need_string = 320, 240, 15, (8, 8, 8), 1, 1, 1, 0, 0, 0 hpprime.dimgrob(1, screen_w, screen_h, 0) def poly_set_pixel(x, y, c): hpprime.pixon(1, x, y, col3_2_rgb(c, color_bits, 1)) def poly_draw_line(x1, y1, x2, y2, c): hpprime.line(1, x1, y1, x2, y2, col3_2_rgb(c, color_bits, 1)) def poly_fill_rect(x, y, w, h, c): hpprime.fillrect(1, x, y, w, h, col3_2_rgb(c, color_bits, 1), col3_2_rgb(c, color_bits, 1)) def poly_show(): hpprime.strblit(0, 0, 0, screen_w, screen_h, 1) def poly_pause(): while hpprime.keyboard(): pass while not(hpprime.keyboard()): pass def poly_draw_string(s, x, y, cf, cb=None): if cb: sz = hpprime.eval('TEXTSIZE("'+s+'")') poly_fill_rect(x, y, sz[0], sz[1], cb) hpprime.textout(1, x, y, s, col3_2_rgb(cf, color_bits, 1)) d_key = {KEY_LEFT:7, KEY_UP:2, KEY_DOWN:12, KEY_RIGHT:8, KEY_ENTER:30, KEY_ESC:4, KEY_LEFT_PARENTHESIS:28, KEY_RIGHT_PARENTHESIS:28, KEY_0:47, KEY_1:42, KEY_2:43, KEY_3:44, KEY_4:37, KEY_5:38, KEY_6:39, KEY_7:32, KEY_8:33, KEY_9:34 } def poly_get_key(): for k in d_key: if poly_test_key(k): return k return 0 def poly_wait_key(): while not hpprime.keyboard(): pass return poly_get_key() def poly_wait_release(): while hpprime.keyboard(): pass def poly_test_key(v): return hpprime.keyboard() & (1 << d_key[v]) def poly_esc_key(): return poly_test_key(KEY_ESC) except: # computer import polycalc_sdl2 polycalc_sdl2.polycalc_pc_init(sdl_width, sdl_height, sdl_scale) screen_w, screen_h, poly_set_pixel, poly_fill_rect, poly_clean_screen, poly_show, poly_pause, poly_wait_key, poly_get_key, poly_test_key, need_line, need_string, poly_ignore_events, poly_esc_key, poly_wait_release = polycalc_sdl2.WIDTH, polycalc_sdl2.HEIGHT, polycalc_sdl2.poly_set_pixel, polycalc_sdl2.poly_fill_rect, polycalc_sdl2.poly_clean_screen, polycalc_sdl2.poly_show, polycalc_sdl2.poly_pause, polycalc_sdl2.poly_wait_key, polycalc_sdl2.poly_get_key, polycalc_sdl2.poly_test_key, 1, 1, polycalc_sdl2.poly_ignore_events, polycalc_sdl2.poly_esc_key, polycalc_sdl2.poly_wait_release color_bits = [8, 8, 8] poly_has_color = 1 show_need_pause_on_exit = 0 if screen_w: if need_line: def poly_draw_line(x1, y1, x2, y2, c): m, a1, b1, a2, b2 = 0, int(x1), int(y1), int(x2), int(y2) if (x2 - x1) ** 2 < (y2 - y1) ** 2: m, a1, a2, b1, b2 = 1, b1, b2, a1, a2 if min(a1, a2) != a1: a1, b1, a2, b2 = a2, b2, a1, b1 for a in range(a1, a2 + 1): b = int(b1 + (b2 - b1) * (a - a1) / ((a2 - a1) or 1)) poly_set_pixel((a, b)[m], (b, a)[m], c) if need_rect: def poly_fill_rect(x, y, w, h, c): for k in range(h): poly_draw_line(x, y + k, x + w - 1, y + k, c) if need_ellipse: from math import sqrt def poly_draw_ellipse(x, y, rx, ry, c): for h in range(-int(ry), int(ry)+1): w = sqrt(max(0, rx*rx*(1-h*h/ry/ry))) x1, x2 = int(x - w), int(x + w) yc = int(y + h) poly_set_pixel(x1, yc, c) poly_set_pixel(x2, yc, c) for w in range(-int(rx), int(rx)+1): h = sqrt(max(0, ry*ry*(1-w*w/rx/rx))) y1, y2 = int(y - h), int(y + h) xc = int(x + w) poly_set_pixel(xc, y1, c) poly_set_pixel(xc, y2, c) def poly_fill_ellipse(x, y, rx, ry, c): for h in range(-int(ry), int(ry)+1): w = sqrt(max(0, rx*rx*(1-h*h/ry/ry))) poly_fill_rect(int(x - w), int(y + h), int(2 * w), 1, c) def poly_draw_circle(x, y, r, c): poly_draw_ellipse(x, y, r, r, c) def poly_fill_circle(x, y, r, c): poly_fill_ellipse(x, y, r, r, c) if need_string: from polyfont import poly_font font_w, font_h = 11, 10 font_num_w = 9 def poly_draw_string(s, x, y, cf, cb=None): for c in s: c = ord(c) >= 33 and ord(c) <= 127 and ord(c) - 32 or 0 if cb: poly_fill_rect(x, y, font_w, font_h, cb) for v in (0,) + poly_font[c] + (0,): ty = y while v: if v & 1: poly_set_pixel(x, ty, cf) ty += 1 v //= 2 x += 1 if need_clear: def poly_clear_screen(): poly_fill_rect(0, 0, screen_w, screen_h, [255, 255, 255]) if screen_need_clear_on_init: poly_clear_screen_init = poly_clear_screen else: poly_clear_screen_init = fnop if need_sleep: def poly_sleep(d): if need_monotonic: for k in range(int(d*250000)): pass else: s = poly_monotonic() + d while poly_monotonic() < s: pass if show_need_pause_on_exit: def poly_show_exit(): poly_show() poly_pause() else: poly_show_exit = poly_show l = [] for k in keys: if k=="w": l.append(screen_w) if k=="h": l.append(screen_h) if k=="hc": l.append(poly_has_color) if k=="fw": l.append(font_w) if k=="fwn": l.append(font_num_w) if k=="fh": l.append(font_h) if k=="gp": l.append(poly_get_pixel) if k=="sp": l.append(poly_set_pixel) if k=="dl": l.append(poly_draw_line) if k=="fr": l.append(poly_fill_rect) if k=="dc": l.append(poly_draw_circle) if k=="fc": l.append(poly_fill_circle) if k=="de": l.append(poly_draw_ellipse) if k=="fe": l.append(poly_fill_ellipse) if k=="ds": l.append(poly_draw_string) if k=="cl": l.append(poly_clear_screen) if k=="cli": l.append(poly_clear_screen_init) if k=="sh": l.append(poly_show) if k=="she": l.append(poly_show_exit) if k=="p": l.append(poly_pause) if k=="sb": l.append(poly_set_buffer) if k=="m": l.append(poly_monotonic) if k=="s": l.append(poly_sleep) if k=="hk": l.append(has_keys) if k=="ek": l.append(poly_esc_key) if k=="gk": l.append(poly_get_key) if k=="wk": l.append(poly_wait_key) if k=="wr": l.append(poly_wait_release) if k=="tk": l.append(poly_test_key) if k=="ie": l.append(poly_ignore_events) return l