forked from Hack-a-Day/Vectorscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.py
224 lines (196 loc) · 6.25 KB
/
sketch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import screennorm
import keyboardcb
import keyleds
import joystick
import vectoros
from vos_state import vos_state
import gc9a01
import gc # need to read files so we can garbage collect between
# 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)]
def save_model(filename):
global model
try:
with open(filename, 'w') as f:
gc.collect()
for row in model:
f.write(' '.join(map(str, row)) + '\n')
gc.collect()
return True
except:
return False
def load_model(filename):
global model
model=[]
gc.collect()
try:
with open(filename, 'r') as f:
for line in f:
numbers=line.split()
gc.collect()
model.append([int(x) for x in numbers])
cursor()
return True
except:
return False
# 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)
keyboardcb.KeyboardCB.leds=(keyboardcb.KeyboardCB.leds and not keyleds.LED_SCOPE) or keyleds.LED_SIG
else:
keyboardcb.KeyboardCB.leds=(keyboardcb.KeyboardCB.leds and not keyleds.LED_SIG) or keyleds.LED_SCOPE
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<SIZE-1:
cursor_x=newx
newy=cursor_y+y
if newy>=0 and newy<SIZE-1:
cursor_y=newy
if pendown:
model[cursor_x][cursor_y]=color
cursor(False)
# command keys (A=Black, B=Red, C=Green, D=Blue, User=Clear)
def red(key):
global color
if keyleds.KEY_SAVE in keyboardcb.KeyboardCB.current_keys:
save_model("B.sketch")
elif keyleds.KEY_USER in keyboardcb.KeyboardCB.current_keys:
load_model("B.sketch")
else:
color=gc9a01.RED
keyboardcb.KeyboardCB.leds=(keyboardcb.KeyboardCB.leds&03) or keyleds.LED_SQ
def green(key):
global color
if keyleds.KEY_SAVE in keyboardcb.KeyboardCB.current_keys:
save_model("C.sketch")
elif keyleds.KEY_USER in keyboardcb.KeyboardCB.current_keys:
load_model("C.sketch")
else:
color=gc9a01.GREEN
keyboardcb.KeyboardCB.leds=(keyboardcb.KeyboardCB.leds&03) or keyleds.LED_SAW
def blue(key):
global color
if keyleds.KEY_SAVE in keyboardcb.KeyboardCB.current_keys:
save_model("D.sketch")
elif keyleds.KEY_USER in keyboardcb.KeyboardCB.current_keys:
load_model("D.sketch")
else:
color=gc9a01.BLUE
keyboardcb.KeyboardCB.leds=(keyboardcb.KeyboardCB.leds&03) or keyleds.LED_TRI
def black(key):
global color,model
if keyleds.KEY_SAVE in keyboardcb.KeyboardCB.current_keys:
save_model("A.sketch")
elif keyleds.KEY_USER in keyboardcb.KeyboardCB.current_keys:
load_model("A.sketch")
else:
color=gc9a01.BLACK
keyboardcb.KeyboardCB.leds=(keyboardcb.KeyboardCB.leds&03) or keyleds.LED_SINE
def white(key): # erase
global color
color=gc9a01.WHITE
def cls(key):
fill_model(gc9a01.WHITE)
cursor()
def menu(key): # exit and return to menu
global stopflag
print("menu")
if vos_state.active:
stopflag=True
joy.detach()
btn.detach()
csel.detach()
# create our screen and keyboard/joystick
screen=screennorm.ScreenNorm()
joy=joystick.Joystick(joycmd,attach=False)
btn=keyboardcb.KeyboardCB(joybtn,keyleds.JOY_PRESS,attach=False) # no repeat
csel=keyboardcb.KeyboardCB({ keyleds.KEY_LEVEL: white, keyleds.KEY_A: black,
keyleds.KEY_B: red, keyleds.KEY_C: green, keyleds.KEY_D: blue, keyleds.KEY_USER: cls,
keyleds.KEY_MENU: menu},
attach=False)
import asyncio
import gc
async def vos_main():
global stopflag
if keyboardcb.KeyboardCB.task==None:
keyboardcb.KeyboardCB.run(100) # start keyboard service
cls(None) # zero out model and draw
joy.attach()
btn.attach()
csel.attach()
while stopflag==False:
await asyncio.sleep(5)
if vos_state.active==False:
gc.collect()
stopflag=False # ready for next time
vos_state.show_menu=True
vectoros.remove_task('sketch')
print("Exiting")
def main():
asyncio.run(vos_main())
if __name__ == "__main__":
main() # need gc if you try to run stand alone