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