import screennorm import keyboardcb import keyleds import joystick import vectoros from vos_state import vos_state import gc9a01 # little etch-a-sketch demo pendown = True # start off with pen down color = gc9a01.BLACK # start off black # To maintain the cursor we need a bit map which is slow # so less is more. Also pixels are tiny # so 40 is a good compromise (40x40 drawing area) SIZE=40 PIXSIZE=240//SIZE # size of each "pixel" # start cursor in the middle cursor_x = (SIZE+1)//2 cursor_y = (SIZE+1)//2 # This will be the backing store for the screen model=[] stopflag=False # stop when true # clear the mdoel with color c (does not draw; see cursor) def fill_model(c): global model model=[[c for i in range(SIZE)] for j in range(SIZE)] # update screen and overlay cursor (or just cursor area if blit==False) def cursor(blit=True): global model global cursor_x global cursor_y global PIXSIZE global pendown ccolor=gc9a01.color565(0xC0,0xFF,0x80) if pendown: ccolor=gc9a01.color565(0xFF,0x80,0x80) if blit: for x in range(SIZE): for y in range(SIZE): screen.tft.fill_rect(x*PIXSIZE,y*PIXSIZE,PIXSIZE,PIXSIZE,model[x][y]) else: # only draw around the cursor for x in range(-1,2): for y in range(-1,2): nx=cursor_x+x ny=cursor_y+y screen.tft.fill_rect(nx*PIXSIZE,ny*PIXSIZE,PIXSIZE,PIXSIZE,model[nx][ny]) screen.tft.fill_rect(cursor_x*PIXSIZE,cursor_y*PIXSIZE,PIXSIZE,PIXSIZE,ccolor) # flip pen/up down on joystick button (non-repeating) def joybtn(key): global pendown pendown=not pendown cursor(False) # update cursor color # normal joystick commands def joycmd(key): global pendown, cursor_x, cursor_y x=0 y=0 if key==keyleds.JOY_N: y=-1 elif key==keyleds.JOY_S: y=1 elif key==keyleds.JOY_E: x=1 elif key==keyleds.JOY_W: x=-1 elif key==keyleds.JOY_NW: y=-1 x=-1 elif key==keyleds.JOY_SW: y=1 x=-1 elif key==keyleds.JOY_NE: y=-1 x=1 elif key==keyleds.JOY_SE: y=1 x=1 if x!=0 or y!=0: newx=cursor_x+x if newx>=0 and newx=0 and newy