def readline(self): console = self.console hi = 0 buf = b'' while console.isconnected(): if console.any(): c = console.read(1) if c == b'\x04': raise GotEOT elif c == b'\x0e': # ^N buf = self.cmd_hist[hi][:] hi = max(hi-1, 0) console.write(b'\r' + self.prompt + buf + b'\x1b[K') elif c == b'\x10': # ^P buf = self.cmd_hist[hi][:] hi = min(hi+1, len(self.cmd_hist)-1) console.write(b'\r' + self.prompt + buf + b'\x1b[K') elif c == b'\x7f' or c == b'\x08': if buf: console.write(b'\x08 \x08') buf = buf[:-1] elif c == b'\r' or c == b'\n': if buf and buf != self.cmd_hist[0]: self.cmd_hist.insert(0, buf) if len(self.cmd_hist) > 16: self.cmd_hist.pop() hi = 0 console.write(b'\r\n') return buf else: buf += c console.write(c) else: yield from sleep(50)
def play(self): random = self.random self.play_on = True self.perk_quit = 0 while self.play_on: delay = random.randrange(30,100) color = random.choice(((8,0,0), (0,8,0), (0,0,8))) yield self.perk(delay, color) yield from sleep(random.randrange(200, 300))
def fuzzle(self, quit_name): # For testing while not getattr(self, quit_name): #yield from self.flash_LED(self.err_led) # More queue-costly but let's us proceed without waiting for photons yield self.flash_LED(self.err_led) # let a coro do it so we can proceed yield from sleep(100) assert getattr(self, quit_name) setattr(self, quit_name, max(getattr(self, quit_name) - 1, 0))
def manage_brightness(self): a = pyb.ADC(pyb.Pin('Y12')) amb = a.read()/4096 while True: v = a.read() amb = 0.95*amb + 0.05*v/4096 bv = 254*amb + 1 self.set_brightness(bv) yield from sleep(123)
def play(self): random = self.random self.play_on = True self.perk_quit = 0 while self.play_on: delay = random.randrange(30, 100) color = random.choice(((8, 0, 0), (0, 8, 0), (0, 0, 8))) yield self.perk(delay, color) yield from sleep(random.randrange(200, 300))
def manage_brightness(self): a = pyb.ADC(pyb.Pin('Y12')) amb = a.read() / 4096 while True: v = a.read() amb = 0.95 * amb + 0.05 * v / 4096 bv = 254 * amb + 1 self.set_brightness(bv) yield from sleep(123)
def fuzzle(self, quit_name): # For testing while not getattr(self, quit_name): #yield from self.flash_LED(self.err_led) # More queue-costly but let's us proceed without waiting for photons yield self.flash_LED( self.err_led) # let a coro do it so we can proceed yield from sleep(100) assert getattr(self, quit_name) setattr(self, quit_name, max(getattr(self, quit_name) - 1, 0))
def bingo(self): stars = list(range(7, 63, 7)) p = self.percolator lattice = p.lattice rand.shuffle(stars) for i in stars: yield from sleep(200) color = tuple(lattice[i]) p.set_color_of(i, (0,0,0)) i = self.percolator.down(i, rng()&1) yield self.perk_and_roll(100, color, i)
def bingo(self): stars = list(range(7, 63, 7)) p = self.percolator lattice = p.lattice rand.shuffle(stars) for i in stars: yield from sleep(200) color = tuple(lattice[i]) p.set_color_of(i, (0, 0, 0)) i = self.percolator.down(i, rng() & 1) yield self.perk_and_roll(100, color, i)
def master(self): self.loop = yield GetRunningLoop(None) yield self.manage_brightness() yield self.percolator.keep_leds_current(10) for i in range(7, 63, 7): self.percolator.set_color_of(i, self.percolator.stoichiometric) yield self.percolator.bingo() yield self.rr.integrate_continuously() yield self.spin_feed_rollers() while True: yield from self.flash_LED(self.act_led) yield from sleep(1000)
def integrate_continuously(self, nap=10): #print("integrating continuously, napping %d" % nap) tscale = 1 / 1000000 then = pyb.micros() #should_be_less_than = (nap + 30) * 1000 while True: dt = pyb.elapsed_micros(then) #if dt >= should_be_less_than: # print("integration dt was", dt) then = pyb.micros() self.integrate(dt * tscale) self.show_balls() yield from sleep(nap)
def spin_feed_rollers(self): lower, upper = self.feed_rollers lower.center[2] = upper.center[2] = 1 v = 1.0 for i in range(len(lower.gear)): lower.gear[i] = [v, 0, 0] upper.gear[(3 - i) % len(lower.gear)] = [0, v, 0] v *= 0.3 while True: #time = self.loop.time #then = time() lower.gear.cw() upper.gear.ccw() lower.render() upper.render() #now = time() #t = 20 - (now - then) #then = now #yield from sleep(t) yield from sleep(20)
def spin_feed_rollers(self): lower, upper = self.feed_rollers lower.center[2] = upper.center[2] = 1 v = 1.0 for i in range(len(lower.gear)): lower.gear[i] = [v, 0, 0] upper.gear[(3-i)%len(lower.gear)] = [0, v, 0] v *= 0.3 while True: #time = self.loop.time #then = time() lower.gear.cw() upper.gear.ccw() lower.render() upper.render() #now = time() #t = 20 - (now - then) #then = now #yield from sleep(t) yield from sleep(20)
def show_for(self, duration): self.leds_need_sync = True yield from sleep(duration)
def run(cli, lightshow, config): yield lightshow.master() while True: yield from cli.repl() # Exits when console not connected yield from sleep(400)
def flash_LED(self, led, dur=1): led.on() yield from sleep(dur) led.off()