def get_talk_time(minutes): """Setup talk time""" display.scroll('A -1, B +1') # clear press counters button_a.get_presses() button_b.get_presses() quit_flag = False while quit_flag is not True: minutes = minutes - button_a.get_presses() \ + button_b.get_presses() display_minutes(int(minutes)) # To exit press a for 2 seconds if button_a.is_pressed(): start = running_time() while button_a.is_pressed(): if running_time() - start > 2000: quit_flag = True display.scroll(str(int(minutes))) return minutes
def main(): global C display_init() D.repaint(60) D.clear() press_count = A.get_presses() + B.get_presses() while press_count == A.get_presses() + B.get_presses(): pulse(150) C = running_time() D.repaint(60) run_game() game_over()
def get_button_presses(): """ returns how many times button_a is pressed until button_b is pressed """ # getting the count now resets the counter num = button_a.get_presses() # sleep until button_b is pressed ... while not button_b.is_pressed(): sleep(100) # get the count again to get the number of # button_a presses since the last call num = button_a.get_presses() # return the result ... return num
def Speed_measurement(): pin14.write_digital(1) sleep(5000) IR_R = button_a.get_presses() IR_L = button_b.get_presses() RPM_R = IR_R RPM_L = IR_L print("\r" + "RPM_R:{:3.0f}r/min".format(RPM_R), "RPM_L:{:3.0f}r/min".format(RPM_L), end='')
def get_cast_spell(self): chain = [] for _ in range(self.level): if (button_a.get_presses() > 0): chain.append("A") display.show("A") elif (button_b.get_presses() > 0): chain.append("B") display.show("B") return chain
def get_direction(method, positive, negative): value = (method() + 300) // 700 if value == 0: return None value = value // abs(value) if value > 0: return positive return negative radio.on() while True: rv = None ct_a = button_a.get_presses() if ct_a > 0: rv = 'btn:a' else: ct_b = button_b.get_presses() if ct_b > 0: rv = 'btn:b' if rv is not None: print(rv) radio.send(rv) sleep(SLEEP_TIME) value = None for direction in DIRECTIONS: value = get_direction(*direction) if value is not None:
[0, 255, 0], # green [0, 0, 255], # blue [255, 0, 255], # purple [255, 80, 0] # orange ) color_i = 0 color = colors[color_i] rainbow = Rainbow() leds = range(24) i = 0 ii = 1 while True: while True: if button_a.get_presses(): break if button_b.get_presses(): color_i += 1 if color_i >= len(colors): color_i = 0 color = colors[color_i] heartbeat(l) for other_led in (0, 1, 2, 3, 4, 5): np[l - other_led] = [max(c - int(c * 0.2 * other_led) - 1, 0) for c in color] np.show() sleep(100) l += 1 if l >= 24:
FACES = [ Image("00000:09090:00000:99999:00000"), Image("00000:09090:03930:09990:03930"), ] INDEX = 0 display.scroll(SPEECH[INDEX][1]) def speak(to_say): global FACES words = to_say.split(" ") index = 0 for word in words: display.show(FACES[index]) speech.say(word) sleep(10) index = 1 - index while True: ct = button_a.get_presses() if ct > 0: INDEX += ct while INDEX >= len(SPEECH): INDEX = INDEX - len(SPEECH) display.scroll(SPEECH[INDEX][0]) if button_b.was_pressed(): speak(SPEECH[INDEX][1]) display.show(Image.HAPPY)
elif clicks == 5: #reload sample program for i, d in enumerate(prog): memory[i] = d memRead(pc) elif clicks == 0: #no button clear data databyte = "00000000" dataWrite(databyte) else: display.set_pixel(row, col, (7 * (display.get_pixel(row, col) < 1))) dataRead() elif button_b.is_pressed(): longpress = 0 clicks = button_a.get_presses() clicks = 0 while button_b.is_pressed(): sleep(100) longpress = longpress + 1 if longpress > 5: #options if button down >0.5second clicks = button_a.get_presses() if clicks == 1: memWrite(pc) pc = min(pc + 1, 255) memRead(pc) elif clicks == 2: pc = max(0, pc - 1) memRead(pc) elif clicks == 3: #goto mem addr from data entry dataRead()
from microbit import display, Image from microbit import sleep, button_a def set_leds(level: int): """Set all LEDs level to equal value.""" display.show(Image('{0}:{0}:{0}:{0}:{0}'.format(str(level) * 5))) def blink(): """Blink by all LEDs""" display.clear() sleep(500) set_leds(9) sleep(500) display.clear() blink() sleep(10000) display.scroll(str(button_a.get_presses())) # Number of presses
from microbit import display from microbit import sleep from microbit import button_a, button_b effects = 1 while True: display.clear() effects -= button_a.get_presses() effects += button_b.get_presses() if effects > 0: for i in range(10): display.set_pixel(2, 2, i) sleep(10) if effects > 1: for i in range(4, 10): display.set_pixel(2, 1, i) display.set_pixel(2, 3, i) display.set_pixel(1, 2, i) display.set_pixel(3, 2, i) sleep(20) if effects > 2: for i in range(2, 10): display.set_pixel(1, 1, i) display.set_pixel(3, 1, i) display.set_pixel(1, 3, i) display.set_pixel(3, 3, i) display.set_pixel(2, 2, 9 - i) sleep(20) if effects > 3: for i in range(9, -1, -1):
def generate(self, requested=0): particle_count = len(self.particles) self.max_particles = max(requested + self.max_particles, 0) if self.max_particles > 0: new_particle_count = self.max_particles - particle_count for i in range(new_particle_count): particle = FallingParticle( random.randint(MIN_SPEED, MAX_SPEED), random.randint(0, MAX_X)) self.particles.append(particle) def render(self): display.clear() for particle in self.particles: display.set_pixel(particle.x, particle.y, particle.brightness) def advance(self): for particle in self.particles[:]: particle.drop() if particle.y > MAX_Y: self.particles.remove(particle) animator = Animator() while True: animator.generate(requested=button_a.get_presses() - button_b.get_presses()) animator.render() animator.advance() sleep(100)