def test_shift(): assert eq(TEST, TEST.shift_left(0)) assert eq(TEST, TEST.shift_up(0)) for n in range(-6, 7): assert eq(TEST.shift_left(n), TEST.shift_right(-n)) assert eq(TEST.shift_up(n), TEST.shift_down(-n)) assert eq(TEST.shift_left(1), Image('44440:55540:56540:55540:44440')) assert eq(TEST.shift_down(1), Image('00000:44444:45554:45654:45554'))
def wake(self): self.comm.send_command("start_fight", 1) display.show(Image("00900:00900:00900:00000:00900")) sleep(500) for x in range(11): display.show(Image.ANGRY.shift_right(x - 5)) sleep(200)
def display_health(self): lights = self.health * 10 board = [["0" for x in range(5)] for y in range(5)] for i in range(5): if lights >= i * 20 + 20: board[0][i] = "9" return Image(':'.join([''.join(vals) for vals in board]))
def count_down(delay): """Count down and show progress""" on_image = Image('99999:99999:99999:99999:99999') display.show(on_image) for y_coord in range(5): for x_coord in range(5): wait = delay while wait >= 300: sleep(300) wait -= 300 if abandon_talk() is True: return # Flash the last row, so making it clear that # the talk is coming to an end if y_coord == 4: for i in range(x_coord, 5): brightness = display.get_pixel(i, y_coord) if brightness == 0: brightness = 9 else: brightness = 0 display.set_pixel(i, y_coord, brightness) sleep(wait) display.set_pixel(x_coord, y_coord, 0) start_up_screen() return
def heartbeat(): status['last_heartbeat'] = running_time() throb_base = Image('90000:00000:00000:00000:00000') throb = [throb_base * (i / 9) for i in range(1, 9)] throb += [throb_base * (i / 9) for i in range(8, -1, -1)] display.show(throb, delay=100, wait=False, loop=False, clear=False)
def display_health(self): lights = int((self.health / float(self.maxHealth)) * 100) board = [["0" for x in range(5)] for y in range(5)] for i in range(5): if lights >= i * 20: board[0][i] = "9" return Image(':'.join([''.join(vals) for vals in board]))
def anim(): while 1: with open('anim0.txt', 'r') as _f: for _ in range(60): content = _f.readline() display.show(Image(content.strip())) sleep(100) if button_a.was_pressed() or button_b.was_pressed(): return
def render_cells(coff, poff): global g_w global g_h screen = Image(5, 5) def render_cell(x, y, b): i = Image(3, 3) for i_x in range(3): for i_y in range(3): i.set_pixel(i_x, i_y, 6) dirs = get_passages(x, y) if len(dirs) > 0: i.set_pixel(1, 1, 9 if b else 0) if DIR_UP in dirs: i.set_pixel(1, 0, 0) if DIR_LEFT in dirs: i.set_pixel(0, 1, 0) if DIR_DOWN in dirs: i.set_pixel(1, 2, 0) if DIR_RIGHT in dirs: i.set_pixel(2, 1, 0) return i for cbx in range(3): for cby in range(3): cx = cbx + coff[0] cy = cby + coff[1] cimage = render_cell(cx, cy, cx == g_w - 1 and cy == g_h - 1) for scx in range(3): for scy in range(3): sx = cbx * 2 + scx - 1 + poff[0] sy = cby * 2 + scy - 1 + poff[1] x_oob = sx < 0 or sx >= 5 y_oob = sy < 0 or sy >= 5 if x_oob or y_oob: continue pixel = cimage.get_pixel(scx, scy) screen.set_pixel(sx, sy, pixel) return screen
def _message2images(message): if type(message) is str: message = (message) for gliph in message: if type(gliph) is str: for char in gliph: yield Image(char) else: yield gliph
def oled_add_text(x, y, text): global oled_screen for i in range(0, min(len(text), 12 - x)): for c in range(0, 5): col = 0 for r in range(1, 6): p = Image(text[i]).get_pixel(c, r - 1) col = col | (1 << r) if (p != 0) else col ind = x * 10 + y * 128 + i * 10 + c * 2 + 1 oled_screen[ind], oled_screen[ind + 1] = col, col oled_set_pos(x * 5, y) ind0 = x * 10 + y * 128 + 1 i2c.write(OLED_ADDR, b'\x40' + oled_screen[ind0:(ind + 1)])
def add_text(x, y, text, draw=1): for i in range(0, min(len(text), 12 - x)): for c in range(0, 5): col = 0 for r in range(1, 6): p = Image(text[i]).get_pixel(c, r - 1) col = col | (1 << r) if (p != 0) else col ind = x * 10 + y * 128 + i * 10 + c * 2 + 1 screen[ind], screen[ind + 1] = col, col if draw == 1: set_zoom(1) set_pos(x * 5, y) ind0 = x * 10 + y * 128 + 1 i2c.write(ADDR, b'\x40' + screen[ind0:ind + 1])
def display_as_text(self, inputData, x, row): row = 7 if row > 7 else row x = 23 if x > 23 else x displayString = str(inputData) for i in range(0, min(len(displayString), 12 - x)): for c in range(0, 5): col = 0 for r in range(1, 6): p = Image(displayString[i]).get_pixel(c, r - 1) col = col | (1 << r) if (p != 0) else col ind = x * 5 + row * 128 + i * 5 + c + 1 self.screen[ind] = col self.set_pos(x * 5, row) ind0 = x * 5 + row * 128 + 1 i2c.write(self.ADDR, b'\x40' + self.screen[ind0:ind])
def _reflect_image(image, in_x=True): new = Image(5, 5) for y in range(5): for x in range(5): if in_x: new.set_pixel(x, 4 - y, image.get_pixel(x, y)) else: new.set_pixel(4 - x, y, image.get_pixel(x, y)) return new
def _rotate_image(image, clockwise=True): new = Image(5, 5) for y in range(5): for x in range(5): if clockwise: new.set_pixel(4 - y, x, image.get_pixel(x, y)) else: new.set_pixel(y, 4 - x, image.get_pixel(x, y)) return new
def add_text(self, x, y, text, s=1): for i in range(0, min(len(text), 25 // s - x)): for c in range(0, 5): col = 0 for r in range(1, 6): p = Image(text[i]).get_pixel(c, r - 1) col = col | (1 << r * s) if (p != 0) else col if s == 2: col = col | (1 << (r * s) - 1) if (p != 0) else col ind = x * 5 * s + y * 128 + i * 5 * s + c * s + 1 if s == 2: self.screen[ind + 128], self.screen[ind] = divmod( col, 0x100) self.screen[ind + 1] = self.screen[ind] self.screen[ind + 129] = self.screen[ind + 128] else: self.screen[ind] = col self.draw_screen()
class DiceStandard(): def __init__(self): self.roll() def roll(self): self.num = random.choice(list(self.sides.keys())) return self.sides[self.num] def melody(self): return self.melodies[self.num] sides = { '1': Image("00000:" "00000:" "00900:" "00000:" "00000"), '2': Image("90000:" "00000:" "00000:" "00000:" "00009"), '3': Image("90000:" "00000:" "00900:" "00000:" "00009"), '4': Image("90009:" "00000:" "00000:" "00000:" "90009"), '5': Image("90009:" "00000:" "00900:" "00000:" "90009"), '6': Image("90009:" "00000:" "90009:" "00000:" "90009") } melodies = { '1': ["C4:2"], '2': ["C4:2", "D4:2"], '3': ["C4:2", "D4:2", "E4:2"], '4': ["C4:2", "D4:2", "E4:2", "F4:2"], '5': ["C4:2", "D4:2", "E4:2", "F4:2", "G4:2"], '6': ["C4:2", "D4:2", "E4:2", "F4:2", "G4:2", "A4:2"] }
def write(text, offset_col=0, offset_row=0, brightness=255): image = None for letter in text: image = None letter_width = char_len(letter) if letter != " " and (offset_col + letter_width) >= 1: if ord(letter) > 127: image = _icons[ord(letter) - 128] else: try: image = Image(letter) except: pass if image is not None: if not draw_icon(offset_col, offset_row, image, brightness): return offset_col += letter_width + 1 del image
def _analyse_image(image): # handle single quote as special case otherwise '' looks like " if str(image) == str(Image("'")): return (0, 3) left = None right = None skip = 0 for x in range(5): for y in range(5): if image.get_pixel(x, y) != 0: left = skip break if left is not None: break skip += 1 if left is None: left = 5 skip = 0 for x in reversed(range(5)): for y in range(5): if image.get_pixel(x, y) != 0: right = skip break if right is not None: break skip += 1 if right is None: right = 5 return (left, right)
class Player: SHIELD = Image("99999:99999:90909:09090:00900") DEAD = Image("90009:09090:00900:09090:90009") CRIT_ANIMATION = [ Image("00900:00900:00900:09990:00900"), Image("90909:00900:00900:09990:90909"), Image("00900:00900:00900:09990:00900"), Image("90909:00900:00900:09990:90909") ] def __init__(self, comm): self.comm = comm self.level = 3 self.maxHealth = 20 self.health = self.maxHealth self.hitProb = 0.6 self.damageReduction = 1 self.classDamage = 1 def start(self): while self.health > 0: display.show(self.display_health()) sleep(100) display.show(Player.DEAD) def attack(self): self.comm.send_command("phys_attack", self.level * self.classDamage) def take_damage(self, damage): if (random.randint(0, 10) / 10.0) <= self.hitProb: self.health -= (int(damage)) else: pass display.show(Player.SHIELD) sleep(500) def display_health(self): lights = int((self.health / float(self.maxHealth)) * 100) board = [["0" for x in range(5)] for y in range(5)] for i in range(5): if lights >= i * 20: board[0][i] = "9" return Image(':'.join([''.join(vals) for vals in board]))
def test_blit(): assert eq(TEST, TEST) i = Image(4, 4) i.blit(TEST, 1, 1, 5, 5) assert eq(i, Image("5554:5654:5554:4444")) i.fill(2) i.blit(TEST, -2, -2, 3, 3) assert eq(i, Image('0002:0002:0042:2222')) i.fill(2) i.blit(TEST, 2, 2, 3, 3) assert eq(i, Image('6542:5542:4442:2222')) i.fill(2) i.blit(TEST, 0, 0, 5, 5, -3, -3) assert eq(i, Image('5422:4422:2222:2222')) i.fill(2) i.blit(TEST, 2, 2, 2, 2, 1, 1) assert eq(i, Image('2222:2652:2552:2222')) i = TEST.copy() i.blit(i, 2, 2, 3, 3) assert eq(i, Image('65444:55454:44454:45554:44444')) i = TEST.copy() i.blit(i, -2, -2, 5, 5) assert eq(i, Image('00000:00000:00444:00455:00456')) i = TEST.copy() i.blit(i, 0, 0, 3, 3, 2, 2) assert eq(i, Image('44444:45554:45444:45455:44456')) i = Image(2, 7) i.fill(2) i.blit(TEST, -100, -100, 50, 50, 1, 1) assert eq(i, Image('22:20:20:20:20:20:20'))
def scroll(message, delay=150, upside_down=False, monospace=False): if not display.is_on(): return None err = False try: _validate_message(message) except TypeError as err_txt: save = err_txt err = True if err: raise TypeError(save) blank = Image(5, 5) current = blank for pic in _message2images(message): if monospace: left = 0 right = 0 else: left, right = _analyse_image(pic) if left == 5: left = 0 right = 4 if upside_down: pic = _rotate_image(_rotate_image(pic)) for col in range(left, 5 - right): if upside_down: current = current.shift_right(1) current.blit(pic, 4 - col, 0, 1, 5, 0, 0) else: current = current.shift_left(1) current.blit(pic, col, 0, 1, 5, 4, 0) _show(current, delay) if upside_down: current = current.shift_right(1) current.blit(blank, 0, 0, 1, 5, 0, 0) else: current = current.shift_left(1) current.blit(blank, 0, 0, 1, 5, 4, 0) _show(current, delay) for col in range(5): if upside_down: current = current.shift_right(1) current.blit(blank, 0, 0, 1, 5, 0, 0) else: current = current.shift_left(1) current.blit(blank, 0, 0, 1, 5, 4, 0) _show(current, delay)
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 scroll_up(message, delay=150, scroll_down=False, monospace=False): if not display.is_on(): return None err = False try: _validate_message(message) except TypeError as err_txt: save = err_txt err = True if err: raise TypeError(save) blank = Image(5, 5) current = blank for pic in _message2images(message): if monospace: top = 0 bottom = 0 else: top, bottom = _analyse_image(pic) if top == 5: top = 0 bottom = 4 pic = _rotate_image(pic, not scroll_down) if scroll_down: pic = _reflect_image(pic) for row in range(top, 5 - bottom): if scroll_down: current = current.shift_down(1) current.blit(pic, 0, row, 5, 1, 0, 0) else: current = current.shift_up(1) current.blit(pic, 0, row, 5, 1, 0, 4) _show(current, delay) if scroll_down: current = current.shift_down(1) current.blit(blank, 0, row, 5, 1, 0, 0) else: current = current.shift_up(1) current.blit(blank, 0, row, 5, 1, 0, 4) _show(current, delay) for row in range(5): if scroll_down: current = current.shift_down(1) current.blit(blank, 0, 0, 5, 1, 0, 0) else: current = current.shift_up(1) current.blit(blank, 0, 0, 5, 1, 0, 4) _show(current, delay)
import microbit import radio import random from microbit import compass, accelerometer, button_a, button_b, display, Image, sleep, pin0 slave = "1" lightValue = 5 contValue = True flash = [Image().invert()*(i/9) for i in range(9, -1, -1)] def lightOn(x): microbit.display.clear() if x == 1: microbit.display.set_pixel(0, 2, lightValue) elif x == 2: microbit.display.set_pixel(2, 2, lightValue) elif x == 3: microbit.display.set_pixel(4, 2, lightValue) elif x == (1, 2): microbit.display.set_pixel(0, 2, lightValue) microbit.display.set_pixel(2, 2, lightValue) elif x == (2, 3): microbit.display.set_pixel(2, 2, lightValue) microbit.display.set_pixel(4, 2, lightValue) else: pass sleep(500) # The radio won't work unless it's switched on. # A unique configuration for the master micro bit. radio.config(address = 0x1337b33f)
import radio # States READY = const(1) SAMPLE_DATA = const(2) SEND_DATA = const(3) EXIT = const(-1) # Global constants CHANNEL = const(25) MAX_ATTEMPT = const(1) SAMPLE_INTERVAL = const(10) # Sample period in ms SAMPLE_DURATION = const(1500) # Total sample time in ms ACK = const(b"0") NACK = const(b"1") # LED Numbers (3,2,1) rotated 90 degrees clockwise NUM_IMGS = const([ Image("90909:90909:90909:99999:99999"), Image("99909:90909:90909:90909:90999"), Image("00000:00090:99999:99999:00000") ]) def retry(attempts=MAX_ATTEMPT, retry_interval=100, valid_responses=None, invalid_responses=None, invalid_msg=None): """ Decorator to call a function for multiple times depending on its return value @param attempts : Maximum number of attempts @param retry_interval : Wait time (in milliseconds) between each retry @param valid_responses : Valid responses as list/tuple
###Feedback################### incoming = radio.receive() if incoming == 'Feedback': count += 1 ###Count Check################ oled_add_text(4, 1, str(count) + '/' + str(totalno_people)) ###Count Status################ if count == totalno_people: pin0.write_digital(1) pin1.write_digital(0) else: pin0.write_digital(0) pin1.write_digital(1) sleep(100) needle = ((15 - compass.heading()) // 30) display.show(Image.ALL_CLOCKS[needle]) #################SENDER######################## radio.on() while True: incoming = radio.receive() if incoming == 'Check': display.show(Image("00000:00000:00900:00000:00000")) sleep(1000) radio.send('Feedback') display.clear() sleep(100) needle = ((15 - compass.heading()) // 30) display.show(Image.ALL_CLOCKS[needle])
from microbit import display, Image buf = bytearray(x % 10 for x in range(100)) i = Image(10, 10, buf) display.show(i.crop(3, 4, 5, 5))
def draw_screen(): server_board = board[0:5] client_board = reversed(board[4:9]) display.show(Image(str(':'.join(server_board)))) radio.send(str(':'.join(client_board)))
from microbit import uart, button_b, display, Image uart.init(115200) # serial baudrate RED = b'R' YELLOW = b'Y' GREEN = b'G' TRIGGER = b'T' CLEAR = b'C' UNKNOWN = b'U' dim_value = 9 # value 1-9 for dimming the display NEUTRAL = Image('00000:09090:00000:99999:00000:') display.show(Image.GHOST / 9 * dim_value) while True: if uart.any(): data = uart.read() if RED in data: display.show(Image.SAD / 9 * dim_value) elif YELLOW in data: display.show(NEUTRAL / 9 * dim_value) elif GREEN in data: display.show(Image.HAPPY / 9 * dim_value) elif CLEAR in data: display.clear() elif UNKNOWN in data: display.show(Image("?") / 9 * dim_value)
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: break if value is None: display.show(Image("00000:03730:07970:03730:00000")) else: display.show(ARROWS[value]) rv = "move:{}".format(value) print(rv) radio.send(rv) sleep(SLEEP_TIME)
#Function for Changing a Key def update_press(key, value): global keyboard_keys if value != keyboard_keys.get(key, False): if value: PressKey(key) else: ReleaseKey(key) keyboard_keys[key] = value previous_values = microbit.accelerometer.get_values() keyboard_keys = {} stable = Image("00000:" "00900:" "09990:" "00900:" "00000:") init_img = Image("90009:" "09990:" "09990:" "09990:" "90009:") image_N = Image("00900:" "09990:" "99999:" "00000:" "00000:") image_NE = Image("00999:" "00099:" "00009:" "00000:" "00000:") image_NW = Image("99900:" "99000:" "90000:" "00000:" "00000:") image_SE = Image("00000:" "00000:" "00009:" "00099:" "00999:") image_SW = Image("00000:" "00000:" "90000:" "99000:" "99900:") image_E = Image("00990:" "00090:" "00009:" "00090:" "00990:") image_W = Image("09900:" "09000:" "90000:" "09000:" "09900:") image_S = Image("00000:" "00000:" "99999:" "09990:" "00900:") images = { "N": image_N, "NE": image_NE, "NW": image_NW, "SE": image_SE, "SW": image_SW,