def receive(): """Starts a receiver program""" radio.on() channel_idx = 0 while True: # Handle the display switching # A for prev channel, B for next channel channel_selector = button_incdec() if channel_selector != 0: # Switch the channel channel_idx = (channel_idx + channel_selector) % MAX_DISPLAY_IDX radio.config(channel=BASE_CHANNEL + channel_idx, length=251) radio.on() # Give the user some feedback display.show(str(channel_idx)) sleep(750) display.clear() msg = radio.receive() if msg: # TODO: validate that we have received a valid frame try: display.show(eval(msg)) except Exception as e: display.show(Image.SAD) print(repr(e))
def plotBarGraph(value, max_value, b=9): order = ( 23, 21, 20, 22, 24, 18, 16, 15, 17, 19, 13, 11, 10, 12, 14, 8, 6, 5, 7, 9, 3, 1, 0, 2, 4, ) counter = 0 display.clear() for y in range(5): for x in range(5): if value / max_value > order[counter] / 25: display.set_pixel(x, y, b) counter += 1
def blink(): """Blink by all LEDs""" display.clear() sleep(500) set_leds(9) sleep(500) display.clear()
def __init__(self): """ Turn of both motors and clear the display """ self.motor_off(KMotor.MOTOR_1) self.motor_off(KMotor.MOTOR_2) display.clear()
def reset(): display.clear() status.update({ 'showing_error': False, 'last_reset': running_time(), 'last_heartbeat': 0, 'status': HEARTBEAT_MESSAGE, })
def start_countdown(count=3): """Play a countdown animation for the specified number of seconds.""" for i in range(count, 0, -1): display.show(str(i)) play('C:1') sleep(1000) play('A:3') display.clear()
def beat_box(sound): if sound: display.show(Image.HEART) sleep(10) display.clear() speech.pronounce(sound) else: sleep(gap)
def main(): data_sent = 0 state = GET_DATA_NUM while True: # State 0 if state == GET_DATA_NUM: data_num = 1 attempt = 0 while True: cy, cx = divmod(data_num-1, 5) # Cursor x and y depending on the data_num display.set_pixel(cx, cy, 9) if button_a.is_pressed() and button_b.is_pressed(): state = READY_TO_SAMPLE # TODO: Change state to some other state data_sent = 0 sleep(500) break elif button_a.is_pressed(): if data_num > 1: display.set_pixel(cx, cy, 0) # Clear LED pixel if data_num > 1 data_num = data_num - 1 if (data_num > 1) else 1 elif button_b.is_pressed(): data_num = data_num + 1 if (data_num < MAX_FILES_NUM) else MAX_FILES_NUM sleep(200) # State 1 elif state == READY_TO_SAMPLE: while True: if button_a.is_pressed(): state = SAMPLE_DATA break elif button_b.is_pressed(): display.clear() cy, cx = divmod(data_num-data_sent-1, 5) setPixelTill(cx, cy, 9) else: display.show(Image.ARROW_W) sleep(200) # State 2 elif state == SAMPLE_DATA: countdown(3) display.show(Image.TARGET) with open("file_{}.csv".format(data_sent), "w") as data_file: data_file.write("x,y,z\n") initial_time = running_time() while (running_time()-initial_time) < SAMPLE_DURATION: t0 = running_time() data_file.write("{},{},{}\n".format(*accelerometer.get_values())) t_diff = running_time()-t0 sleep(0 if (SAMPLE_PERIOD-t_diff)<0 else SAMPLE_PERIOD-t_diff) data_sent += 1 if (data_num-data_sent)>0: state = READY_TO_SAMPLE else: state = EXIT # State 3 elif state == EXIT: display.show(Image.HAPPY) break
def show_wave(name, frame, duration=1500): display.scroll(name + " wave", wait=False, delay=100) audio.play(repeated_frame(frame, duration), wait=False) for i in range(75): sleep(100) if button_a.is_pressed(): display.clear() audio.stop() break
def show_wave(name, frame, duration=1500): display.scroll(name + " wave", wait=False,delay=100) audio.play(repeated_frame(frame, duration),wait=False) for i in range(75): sleep(100) if button_a.is_pressed(): display.clear() audio.stop() break
def blowUpShip(self): self.hide() self.drawExplosion(self.properties[2], 1) sleep(200) display.clear() for brightness in range(self.properties[2], 0, -1): self.drawExplosion(brightness, 2) sleep(200) display.clear()
def imp_lista(lista: list, isDificil: bool) -> None: display.clear() if isDificil: fator = 6 else: fator = 1.5 for y, x in enumerate(lista): if (0 <= x < 5 and 0 <= y < 5): display.set_pixel(x, y, 9 - int(y * fator))
def startGame(self): self.score = 0 self.endGame = False display.clear() X = [2, 2, 2, 2, 3, 4, 4, 4, 3, 1, 0, 0, 0, 1] Y = [0, 1, 2, 4, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4] for i in range(4): display.set_pixel(X[i], Y[i], 9) sleep(200) for i in range(4, 14): display.set_pixel(X[i], Y[i], 9) sleep(50) sleep(1000) display.clear()
def AddPoint(): global points global pointsToWin for i in range(4): display.clear() time.sleep(0.5) display.show(points) time.sleep(0.5) if(i == 0): points += 1 display.clear() if(points >= pointsToWin): radio.send("l") EndScreen(True)
def show_id(): di.clear() for id in uids: if id > 0: uid = id - 1 di.set_pixel(uid % 5, int(uid / 5), 2) uid = lid - 1 di.set_pixel(uid % 5, int(uid / 5), 5) id = mid if id > 0: uid = id - 1 di.set_pixel(uid % 5, int(uid / 5), 9)
def game_shake(): if accelerometer.was_gesture("shake"): for w in shake_waits: display.clear() sleep(50) rolled = dice.roll() display.show(rolled) music.play(["C4:1"], wait=False) sleep(w) music.play(dice.melody(), wait=False) for i in range(4): display.clear() sleep(500) display.show(rolled) sleep(500)
def swish(direction): ostart = 0 if direction == RIGHT else 5 oend = 9 if direction == RIGHT else -5 step = 1 if direction == RIGHT else -1 istart = 1 if direction == RIGHT else 3 iend = 4 if direction == RIGHT else -1 for i in range(ostart, oend, step): display.clear() if -1 < i < 5: display.set_pixel(i, 2, 9) for j in range(istart, iend, step): k = i - j * step brightness = 9 - (2 * j) if 5 > k >= 0: display.set_pixel(k, 2, brightness) sleep(100)
def play(): display.clear() radio.on() radio.config(channel=90, queue=12) count = -1 def gen(): recvd = audio.AudioFrame() empty = audio.AudioFrame() while True: if radio.receive_bytes_into(recvd) == 32: yield recvd else: yield empty if button_a.is_pressed() and button_b.is_pressed(): return audio.play(gen())
def send(): display.clear() radio.on() radio.config(channel=90, power=4) if "sample.raw" in os.listdir(): gen = sample_generator("sample.raw") else: gen = sawtooth_generator() start = running_time() sent = 0 for f in gen: # One frame every 4ms = 8kHz while sent > ((running_time() - start) >> 2) + 3: sleep(1) radio.send_bytes(f) sent += 1 print(sent)
def main(): sb = ScrollBit() sd = ScrollDisplay(100) bd = BitDisplay(7) while True: sb.set_pixel(sd.x, sd.y, sd.b) sb.show() display.set_pixel(bd.x, bd.y, bd.b) sd.step() bd.step() sleep(200) sb.clear() display.clear()
def main(): state = READY while True: # Ready state if state == READY: while True: if button_a.is_pressed(): state = SAMPLE_DATA break elif button_b.is_pressed(): radio.on() radio.send("done") radio.off() else: display.show(Image.ARROW_W) sleep(100) # Sample Data state elif state == SAMPLE_DATA: data_sent = 0 # Reset data sent value countdown(3) # Show countdown on the Microbit LED display.show(Image.TARGET) radio.on() initial_time = running_time() while (running_time() - initial_time) < SAMPLE_DURATION: t0 = running_time() if data_sent == 0: # Turn off all Microbit LEDs display.clear() cx, cy = divmod( data_sent, 5) # Get current LED pixel coordinate of the BBC Microbit radio.send(str(accelerometer.get_values())) display.set_pixel(4 - cx, cy, 9) data_sent = 0 if data_sent >= 24 else data_sent + 1 # Increase and limit data_sent value within 0-24 range wait_t = SAMPLE_INTERVAL - (running_time() - t0 ) # Time till next sample if (wait_t > 0): sleep(wait_t) radio.send("done") radio.off() state = READY # Exit state elif state == EXIT: display.show(Image.HAPPY) return 0 sleep(100)
def setting_mode(threshold): while (button_a.is_pressed() or button_b.is_pressed()): display.show(Image.CLOCK12) sleep(300) display.show(Image.CLOCK3) sleep(300) display.show(Image.CLOCK6) sleep(300) display.show(Image.CLOCK9) sleep(300) display.clear() display.show(threshold) sleep(300) display.show(Image.ARROW_E) sleep(400) display.show(Image.ARROW_W) sleep(400) display.clear() last_click = running_time() while (running_time() < last_click + 5000): if button_b.was_pressed(): last_click = running_time() threshold = threshold + 1 display.show(Image.ARROW_N) sleep(300) display.show(threshold) sleep(300) display.clear() if button_a.was_pressed(): last_click = running_time() threshold = threshold - 1 display.show(Image.ARROW_S) sleep(300) display.show(threshold) sleep(300) display.clear() tfile = open("tstore", "w") tfile.write(str(threshold)) tfile.close() display.show(Image.YES) sleep(700) display.clear() return (threshold)
def StartCountdown(): global lookingForGame global gameStarted global ball global points display.clear() for i in range(5, -1, -1): display.show(i) time.sleep(1) display.clear() lookingForGame = False points = 0 gameStarted = True prevBallMove = time.ticks_ms() if(not ball is None): display.set_pixel(ball.x, ball.y, 9) CheckBounce()
def play(self): p1 = pin1.read_analog() p2 = pin2.read_analog() if p1 <= 300: if p2 < 300: display.show(Image.ARROW_SW) elif p2 >= 900: display.show(Image.ARROW_NW) else: display.show(Image.ARROW_W) elif p1 >= 900: if p2 < 300: display.show(Image.ARROW_SE) elif p2 >= 900: display.show(Image.ARROW_NE) else: display.show(Image.ARROW_E) elif 300 < p1 < 900: if p2 < 300: display.show(Image.ARROW_S) elif p2 >= 900: display.show(Image.ARROW_N) else: if not pin15.read_digital(): display.show(Image.HAPPY) music.play(music.JUMP_UP) elif not pin14.read_digital(): display.show(Image.SURPRISED) music.play(music.POWER_UP) elif not pin13.read_digital(): display.show(Image.SAD) music.play(music.JUMP_DOWN) elif not pin12.read_digital(): display.show(Image.ASLEEP) music.play(music.POWER_DOWN) elif not pin8.read_digital(): display.show(Image.HEART) music.play(["C1:1", "C#1:1"]) display.clear()
def main(): """ entry point to the app """ # loop forever while True: # generate a random number to represent the number of leds to show ... num = random.randint(0, 10) # clear the display from the previous game ... display.clear() # a list to keep track of which leds have been used ... used = [] # loop 'num' times ... for i in range(num): # generate a random number to represent which led to show ... led = random.randint(0, NUM_LEDS-1) # if the led has already been used, # generate another random number ... while led in used: led = random.randint(0, NUM_LEDS-1) used.append(led) # get the coords for the led to show ... x, y = get_coords(led) # show the led ... display.set_pixel(x, y, 9) # get the answer from the user ... input_num = get_button_presses() # if the answer is correct, display WIN otherwise LOSE ... if num == input_num: display.scroll("WIN!") else: display.scroll("LOSE!")
def wait_start(): global uids, lid, joystick, car_enabled go = 0 while True: if button_b.was_pressed(): joystick = 1 di.show("J") go = 1 if pin_logo.is_touched(): di.show("E") go = 1 if button_a.was_pressed(): go = 1 car_enabled = 1 move_motor_lr(0, 0) di.show("C") if go == 1: sleep(500) di.clear() uids = find_used_ids() gnid(uids) break
def main(): """ entry point to the app """ # loop forever while True: # generate a random number to represent the number of leds to show ... num = random.randint(0, 10) # clear the display from the previous game ... display.clear() # a list to keep track of which leds have been used ... used = [] # loop 'num' times ... for i in range(num): # generate a random number to represent which led to show ... led = random.randint(0, NUM_LEDS - 1) # if the led has already been used, # generate another random number ... while led in used: led = random.randint(0, NUM_LEDS - 1) used.append(led) # get the coords for the led to show ... x, y = get_coords(led) # show the led ... display.set_pixel(x, y, 9) # get the answer from the user ... input_num = get_button_presses() # if the answer is correct, display WIN otherwise LOSE ... if num == input_num: display.scroll("WIN!") else: display.scroll("LOSE!")
def logic(self): display.clear() p1 = pin1.read_analog() p2 = pin2.read_analog() if p1 <= 204: x = 0 elif 204 < p1 <= 408: x = 1 elif 408 < p1 <= 612: x = 2 elif 612 < p1 <= 816: x = 3 else: x = 4 if p2 <= 204: y = 4 elif 204 < p2 <= 408: y = 3 elif 408 < p2 <= 612: y = 2 elif 612 < p2 <= 816: y = 1 else: y = 0 display.set_pixel(*self.pix) display.set_pixel(x, y, 9) if [x, y, 9] == self.pix: self.pix = None display.show(Image.HAPPY) music.play(music.JUMP_UP) sleep(500) if self.btn_pressed(): sleep(150) self.set_random_pix()
def main(): data_received = 0 display.show(Image.HAPPY) while True: response = radio.receive() if response is not None: # Some data received uart.write(response+"\n") # Write received data with line break on serial bus if data_received == 0: display.clear() # Clear Microbit LEDs if "," in response: # "(x,y,z)" data received cy, cx = divmod(data_received, 5) display.set_pixel(cx, cy, 9) # Increase and limit data_received value within 0-24 range data_received = 0 if data_received >= 24 else data_received+1 elif response == "done": data_received = 0 display.show(Image.YES) elif response == "exit": display.show(Image.HAPPY) sleep(2000) display.clear() break else: sleep(100)
def calibrate(): t = running_time() lastrt = t prev = 0 calibrated = False while not calibrated: r = pin0.read_analog() - 11 r = abs(r) / 900 * 5 r = int(r) if r < 6 else 5 if (r == 5 and prev != 5) or (r == 0 and prev != 0): t = running_time() prev = r if running_time() - t > 3000: lastrt = running_time() if r == 0: # No sound for more than 3 seconds # Turn right display.show(Image.ARROW_E) else: # Saturated for more than 3 seconds # Turn left display.show(Image.ARROW_W) else: for y in range(0, 5): for x in range(0, 5): if 4 - y < r: display.set_pixel(x, y, 5) else: display.set_pixel(x, y, 0) if r > 0: # If there is sound, leave the leds on for .1 seconds sleep(100) if running_time() - lastrt > 5000: display.show(Image.YES) sleep(1000) calibrated = True display.clear() return calibrated
def show(self): # Completely untested, I had to write it blindly :( display.clear() for y, row in enumerate(self.rows): for x, pixel in enumerate(row): display.set_pixel(x, y, pixel)
def clear_screens(): display.clear() radio.send("CLEAR")
from microbit import display display.scroll("Hello, World!") # Images from microbit import display, Image display.show(Image.HAPPY) boat = Image("00710:00755:00700:99999:09990") display.show(boat) # Animation display.show(reversed([str(x) for x in range(10)]), loop=False, delay=500) display.show(Image.ALL_CLOCKS, loop=True, delay=100) display.clear()
def stop(self): self.km.motor_off(self.km.MOTOR_1) self.km.motor_off(self.km.MOTOR_2) display.clear()
assert os.listdir() == [ "test1.txt", "test2.txt" ] os.remove("test1.txt") os.remove("test2.txt") assert not os.listdir() def test_many_files(): for i in range(80): name = "%d.dat" % i write_data_to_file(name, i*3, 16, 4) verify_file(name, data_stream(i*3), 16, 4, 'b') for i in range(80): os.remove("%d.dat" % i) name = "_%d.dat" % i write_data_to_file(name, i*3, 16, 4) verify_file(name, data_stream(i*3), 16, 4, 'b') for i in range(80): os.remove("_%d.dat" % i) assert not os.listdir() display.clear() try: clear_files() test_small_files() test_many_files() test_text_file() print("File test: PASS") display.show(Image.HAPPY) except Exception as ae: display.show(Image.SAD) raise
def hdr_bb() : dp.clear()