Skip to content

Commit e6727fb

Browse files
committed
updated CircuitPython slider code
-Changed file name -Took out mem error exception -Took out TFT setup handled by MiniTFT FeatherWing helper (thanks Melissa!) -Tested on slider, all seems well
1 parent ab253a8 commit e6727fb

File tree

2 files changed

+322
-347
lines changed

2 files changed

+322
-347
lines changed

CircuitPython_Slider/code.py

Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,322 @@
1+
import time
2+
import displayio
3+
import terminalio
4+
import adafruit_imageload
5+
from adafruit_display_text.label import Label
6+
from adafruit_featherwing import minitft_featherwing
7+
from adafruit_motorkit import MotorKit
8+
from adafruit_motor import stepper
9+
10+
#setup stepper motor
11+
kit = MotorKit()
12+
13+
#setup minitft featherwing
14+
minitft = minitft_featherwing.MiniTFTFeatherWing()
15+
16+
#setup bitmap file locations
17+
five_minBMP = "/5min_bmp.bmp"
18+
ten_minBMP = "/10min_bmp.bmp"
19+
twenty_minBMP = "/20min_bmp.bmp"
20+
hourBMP = "/60min_bmp.bmp"
21+
runningBMP = "/camSlide_bmp.bmp"
22+
reverseqBMP = "/reverseQ_bmp.bmp"
23+
backingUpBMP = "/backingup_bmp.bmp"
24+
stopBMP = "/stopping_bmp.bmp"
25+
26+
#variables for state machines in loop
27+
mode = 0
28+
onOff = 0
29+
pause = 0
30+
stop = 0
31+
z = 0
32+
33+
#image groups
34+
five_minGroup = displayio.Group(max_size=20)
35+
ten_minGroup = displayio.Group(max_size=20)
36+
twenty_minGroup = displayio.Group(max_size=20)
37+
hourGroup = displayio.Group(max_size=20)
38+
reverseqGroup = displayio.Group(max_size=20)
39+
backingUpGroup = displayio.Group(max_size=20)
40+
stopGroup = displayio.Group(max_size=20)
41+
progBarGroup = displayio.Group(max_size=20)
42+
43+
#bitmap setup for all of the menu screens
44+
five_minBG, five_minPal = adafruit_imageload.load(five_minBMP,
45+
bitmap=displayio.Bitmap,
46+
palette=displayio.Palette)
47+
five_minDis = displayio.TileGrid(five_minBG, pixel_shader=five_minPal)
48+
ten_minBG, ten_minPal = adafruit_imageload.load(ten_minBMP,
49+
bitmap=displayio.Bitmap,
50+
palette=displayio.Palette)
51+
ten_minDis = displayio.TileGrid(ten_minBG, pixel_shader=ten_minPal)
52+
twenty_minBG, twenty_minPal = adafruit_imageload.load(twenty_minBMP,
53+
bitmap=displayio.Bitmap,
54+
palette=displayio.Palette)
55+
twenty_minDis = displayio.TileGrid(twenty_minBG, pixel_shader=twenty_minPal)
56+
hourBG, hourPal = adafruit_imageload.load(hourBMP,
57+
bitmap=displayio.Bitmap,
58+
palette=displayio.Palette)
59+
hourDis = displayio.TileGrid(hourBG, pixel_shader=hourPal)
60+
runningBG, runningPal = adafruit_imageload.load(runningBMP,
61+
bitmap=displayio.Bitmap,
62+
palette=displayio.Palette)
63+
runningDis = displayio.TileGrid(runningBG, pixel_shader=runningPal)
64+
reverseqBG, reverseqPal = adafruit_imageload.load(reverseqBMP,
65+
bitmap=displayio.Bitmap,
66+
palette=displayio.Palette)
67+
reverseqDis = displayio.TileGrid(reverseqBG, pixel_shader=reverseqPal)
68+
backingUpBG, backingUpPal = adafruit_imageload.load(backingUpBMP,
69+
bitmap=displayio.Bitmap,
70+
palette=displayio.Palette)
71+
backingUpDis = displayio.TileGrid(backingUpBG, pixel_shader=backingUpPal)
72+
stopBG, stopPal = adafruit_imageload.load(stopBMP,
73+
bitmap=displayio.Bitmap,
74+
palette=displayio.Palette)
75+
stopDis = displayio.TileGrid(stopBG, pixel_shader=stopPal)
76+
77+
#setup for timer display when camera is sliding
78+
text_area = Label(terminalio.FONT, text=' ')
79+
text_area.x = 55
80+
text_area.y = 65
81+
82+
#adding the bitmaps to the image groups so they can be displayed
83+
five_minGroup.append(five_minDis)
84+
ten_minGroup.append(ten_minDis)
85+
twenty_minGroup.append(twenty_minDis)
86+
hourGroup.append(hourDis)
87+
progBarGroup.append(runningDis)
88+
progBarGroup.append(text_area)
89+
reverseqGroup.append(reverseqDis)
90+
backingUpGroup.append(backingUpDis)
91+
stopGroup.append(stopDis)
92+
93+
#setting button states on minitft featherwing to None
94+
down_state = None
95+
up_state = None
96+
a_state = None
97+
b_state = None
98+
select_state = None
99+
100+
#arrays to match up with the different slide speeds
101+
#graphics menu array
102+
graphics = [five_minGroup, ten_minGroup, twenty_minGroup, hourGroup]
103+
#delay for the stepper motor
104+
speed = [0.0154, 0.034, 0.0688, 0.2062]
105+
#time duration for the camera slide
106+
slide_duration = [300, 600, 1200, 3600]
107+
#beginning timer display
108+
slide_begin = ["5:00", "10:00", "20:00", "60:00"]
109+
#stepper motor steps that corresponds with the timer display
110+
slide_checkin = [860, 1720, 2580, 3440, 4300, 5160,
111+
6020, 6880, 7740, 8600, 9460, 10320,
112+
11180, 12040, 12900, 13760, 14620, 15480,
113+
16340, 17195]
114+
#variable that counts up through the slide_checkin array
115+
check = 0
116+
117+
#start time
118+
begin = time.monotonic()
119+
print(begin)
120+
#when feather is powered up it shows the initial graphic splash
121+
minitft.display.show(graphics[mode])
122+
123+
while True:
124+
#setup minitft featherwing buttons
125+
buttons = minitft.buttons
126+
#define the buttons' state changes
127+
if not buttons.down and down_state is None:
128+
down_state = "pressed"
129+
if not buttons.up and up_state is None:
130+
up_state = "pressed"
131+
if not buttons.select and select_state is None:
132+
select_state = "pressed"
133+
if not buttons.a and a_state is None:
134+
a_state = "pressed"
135+
if not buttons.b and b_state is None:
136+
b_state = "pressed"
137+
#scroll down to change slide duration and graphic
138+
if buttons.down and down_state == "pressed":
139+
#blocks the button if the slider is sliding or
140+
#in an inbetween state
141+
if pause == 1 or onOff == 1:
142+
mode = mode
143+
down_state = None
144+
else:
145+
mode += 1
146+
down_state = None
147+
if mode > 3:
148+
mode = 0
149+
print("Mode:,", mode)
150+
minitft.display.show(graphics[mode])
151+
#scroll up to change slide duration and graphic
152+
if buttons.up and up_state == "pressed":
153+
#blocks the button if the slider is sliding or
154+
#in an inbetween state
155+
if pause == 1 or onOff == 1:
156+
mode = mode
157+
up_state = None
158+
else:
159+
mode -= 1
160+
up_state = None
161+
if mode < 0:
162+
mode = 3
163+
print("Mode: ", mode)
164+
minitft.display.show(graphics[mode])
165+
#workaround so that the menu graphics show after a slide is finished
166+
if mode == mode and pause == 0 and onOff == 0:
167+
minitft.display.show(graphics[mode])
168+
#starts slide
169+
if buttons.select and select_state == "pressed" or z == 2:
170+
#blocks the button if the slider is sliding or
171+
#in an inbetween state
172+
if pause == 1 or onOff == 1:
173+
#print("null")
174+
select_state = None
175+
else:
176+
#shows the slider is sliding graphic
177+
minitft.display.show(progBarGroup)
178+
#gets time of button press
179+
press = time.monotonic()
180+
print(press)
181+
#displays initial timer
182+
text_area.text = slide_begin[mode]
183+
#resets button
184+
select_state = None
185+
#changes onOff state
186+
onOff += 1
187+
#changes z state
188+
z = 0
189+
if onOff > 1:
190+
onOff = 0
191+
#number of steps for the length of the aluminum extrusions
192+
for i in range(17200):
193+
#for loop start time
194+
start = time.monotonic()
195+
#gets actual duration time
196+
real_time = start - press
197+
#creates a countdown from the slide's length
198+
end = slide_duration[mode] - real_time
199+
# /60 since time is in seconds
200+
mins_remaining = end / 60
201+
if mins_remaining < 0:
202+
mins_remaining += 60
203+
#gets second(s) count
204+
total_sec_remaining = mins_remaining * 60
205+
#formats to clock time
206+
mins_remaining, total_sec_remaining = divmod(end, 60)
207+
#microstep for the stepper
208+
kit.stepper1.onestep(style=stepper.MICROSTEP)
209+
#delay determines speed of the slide
210+
time.sleep(speed[mode])
211+
if i == slide_checkin[check]:
212+
#check-in for time remaining based on motor steps
213+
print("0%d:%d" %
214+
(mins_remaining, total_sec_remaining))
215+
print(check)
216+
if total_sec_remaining < 10:
217+
text_area.text = "%d:0%d" % (mins_remaining, total_sec_remaining)
218+
else:
219+
text_area.text = "%d:%d" % (mins_remaining, total_sec_remaining)
220+
check = check + 1
221+
if check > 19:
222+
check = 0
223+
if end < 10:
224+
#displays the stopping graphic for the last 10 secs.
225+
minitft.display.show(stopGroup)
226+
#changes states after slide has completed
227+
kit.stepper1.release()
228+
pause = 1
229+
onOff = 0
230+
stop = 1
231+
check = 0
232+
#delay for safety
233+
time.sleep(2)
234+
#shows choice menu
235+
minitft.display.show(reverseqGroup)
236+
#b is defined to stop the slider
237+
#only active if the slider is in the 'stopped' state
238+
if buttons.b and b_state == "pressed" and stop == 1:
239+
#z defines location of the camera on the slider
240+
#0 means that it is opposite the motor
241+
if z == 0:
242+
b_state = None
243+
time.sleep(1)
244+
minitft.display.show(backingUpGroup)
245+
#delay for safety
246+
time.sleep(2)
247+
#brings camera back to 'home' at double speed
248+
for i in range(1145):
249+
kit.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)
250+
time.sleep(1)
251+
kit.stepper1.release()
252+
#changes states
253+
pause = 0
254+
stop = 0
255+
#1 means that the camera is next to the motor
256+
if z == 1:
257+
b_state = None
258+
time.sleep(2)
259+
#changes states
260+
pause = 0
261+
stop = 0
262+
z = 0
263+
#a is defined to slide in reverse of the prev. slide
264+
#only active if the slider is in the 'stopped' state
265+
if buttons.a and a_state == "pressed" and stop == 1:
266+
#z defines location of the camera on the slider
267+
#1 means that the camera is next to the motor
268+
if z == 1:
269+
a_state = None
270+
time.sleep(2)
271+
stop = 0
272+
pause = 0
273+
#2 allows the 'regular' slide loop to run
274+
#as if the 'select' button has been pressed
275+
z = 2
276+
#0 means that the camera is opposite the motor
277+
if z == 0:
278+
a_state = None
279+
#same script as the 'regular' slide loop
280+
time.sleep(2)
281+
minitft.display.show(progBarGroup)
282+
press = time.monotonic()
283+
print(press)
284+
text_area.text = slide_begin[mode]
285+
onOff += 1
286+
pause = 0
287+
stop = 0
288+
if onOff > 1:
289+
onOff = 0
290+
for i in range(17200):
291+
start = time.monotonic()
292+
real_time = start - press
293+
end = slide_duration[mode] - real_time
294+
mins_remaining = end / 60
295+
if mins_remaining < 0:
296+
mins_remaining += 60
297+
total_sec_remaining = mins_remaining * 60
298+
mins_remaining, total_sec_remaining = divmod(end, 60)
299+
#only difference is that the motor is stepping backwards
300+
kit.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.MICROSTEP)
301+
time.sleep(speed[mode])
302+
if i == slide_checkin[check]:
303+
print("0%d:%d" %
304+
(mins_remaining, total_sec_remaining))
305+
if total_sec_remaining < 10:
306+
text_area.text = "%d:0%d" % (mins_remaining, total_sec_remaining)
307+
else:
308+
text_area.text = "%d:%d" % (mins_remaining, total_sec_remaining)
309+
check = check + 1
310+
if check > 19:
311+
check = 0
312+
if end < 10:
313+
minitft.display.show(stopGroup)
314+
#state changes
315+
kit.stepper1.release()
316+
pause = 1
317+
onOff = 0
318+
stop = 1
319+
z = 1
320+
check = 0
321+
time.sleep(2)
322+
minitft.display.show(reverseqGroup)

0 commit comments

Comments
 (0)