def circleSimple(): tx = "CIRCLE" header(tx, True) x = maxx // 2 y = (maxy - miny) // 2 + (miny // 2) if x > y: r = y - miny else: r = x - miny while r > 0: color = machine.random(0xFFFFFF) fill = machine.random(0xFFFFFF) lcd.circle(x, y, r, color, fill) r -= 10 x += 10
def lineDemo(sec=5): header("LINE DEMO", True) n = time.time() + sec while time.time() < n: x1 = machine.random(maxx - 4) y1 = machine.random(miny, maxy - 4) x2 = machine.random(maxx - 1) y2 = machine.random(miny, maxy - 1) color = machine.random(0xFFFFFF) lcd.line(x1, y1, x2, y2, color) if btnA.wasPressed(): speaker.tone(346, 120, 1) break lcd.resetwin()
def lineDemo(sec=5): header("LINE DEMO", True) n = time.time() + sec while time.time() < n: x1 = machine.random(maxx - 4) y1 = machine.random(miny, maxy - 4) x2 = machine.random(maxx - 1) y2 = machine.random(miny, maxy - 1) color = machine.random(0xFFFFFF) tft.line(x1, y1, x2, y2, color) if touched(): break tft.resetwin()
def fontDemo(sec=5, rot=False): tx = "FONTS" if rot: tx = "ROTATED " + tx header(tx, True) tx = "ESP32-MicrpPython" n = time.time() + sec while time.time() < n: frot = 0 if rot: frot = math.floor(machine.random(359) / 5) * 5 for font in fontnames: if (not rot) or (font != lcd.FONT_7seg): x = machine.random(maxx - 8) if font != lcd.FONT_7seg: lcd.font(font, rotate=frot) _, fsz = lcd.fontSize() y = machine.random(miny, maxy - fsz) lcd.text(x, y, tx, machine.random(0xFFFFFF)) else: l = machine.random(6, 12) w = machine.random(1, l // 3) lcd.font(font, rotate=frot, dist=l, width=w) _, fsz = lcd.fontSize() y = machine.random(miny, maxy - fsz) lcd.text(x, y, "-12.45/", machine.random(0xFFFFFF)) if buttonA.wasPressed(): break lcd.resetwin()
def ellipseDemo(sec=5, dofill=False): tx = "ELLIPSE" if dofill: tx = "FILLED " + tx header(tx, True) n = time.time() + sec while time.time() < n: x = machine.random(4, maxx - 2) y = machine.random(miny + 2, maxy - 2) if x < y: rx = machine.random(2, x) else: rx = machine.random(2, y) if x < y: ry = machine.random(2, x) else: ry = machine.random(2, y) color = machine.random(0xFFFFFF) if dofill: fill = machine.random(0xFFFFFF) lcd.ellipse(x, y, rx, ry, 15, color, fill) else: lcd.ellipse(x, y, rx, ry, 15, color) if buttonA.wasPressed(): break lcd.resetwin()
def roundrectDemo(sec=5, dofill=False): tx = "ROUND RECT" if dofill: tx = "FILLED " + tx header(tx, True) n = time.time() + sec while time.time() < n: x = machine.random(2, maxx - 18) y = machine.random(miny, maxy - 18) w = machine.random(12, maxx - x) h = machine.random(12, maxy - y) if w > h: r = machine.random(2, h // 2) else: r = machine.random(2, w // 2) color = machine.random(0xFFFFFF) if dofill: fill = machine.random(0xFFFFFF) lcd.roundrect(x, y, w, h, r, color, fill) else: lcd.roundrect(x, y, w, h, r, color) if buttonA.wasPressed(): break lcd.resetwin()
def circleSimple(): tx = "CIRCLE" header(tx, True) x = 110 y = 160 r = 110 z = 0 while z < 12: color = machine.random(0xFFFFFF) fill = machine.random(0xFFFFFF) tft.circle(x, y, r, color, fill) r -= 10 x += 10 z += 1
def on_enter(self, rnd=0, did_lose=False, multiplayer_info=None): self.unbind_buttons() # buttons do nothing in this state self.rnd = rnd self.did_lose = did_lose self.multiplayer_info = multiplayer_info if self.multiplayer_info: mac, seed = self.multiplayer_info if rnd == 0: # first round, seed the random number generator random.seed(seed) else: self.state_machine.timer.init( period=250, mode=machine.Timer.ONE_SHOT, callback=self.turn_on_waiting_lights, ) # send state to opponent -- if they aren't listening yet, # we'll send it again when they send us their state return self.send_game_state() # TODO -- expire this state if we lose contact with the opponent somehow else: # single player if rnd == 0: # first round, seed the random number generator with an actual random number random.seed(machine.random(0, 99999)) return self.handle_round()
def on_wifi_message(self, mac, msg): if msg == b"anyone there?": # challenge them! seed = machine.random(0, 999999) self.state_machine.wifi.send_message(mac, "challenge: %s" % seed) return # wait for a response message elif msg.startswith( b"challenge: "): # accept the challenge by echoing back seed_str = msg.split(b" ")[1] seed = int(seed_str) self.state_machine.wifi.send_message( mac, "challenge_accepted: %s" % seed_str.decode("utf-8")) self.clear_wifi_message_callback( ) # just to make sure this isnt triggered again self.state_machine.timer.deinit() self.unbind_buttons() self.state_machine.quiet_lights.opponent_found() return self.state_machine.go_to_state("simon_says_round_sync", multiplayer_info=(mac, seed)) elif msg.startswith( b"challenge_accepted: "): # they accepted our challenge self.clear_wifi_message_callback( ) # just to make sure this isnt triggered again seed = int(msg.split(b" ")[1]) self.state_machine.timer.deinit() self.unbind_buttons() self.state_machine.quiet_lights.opponent_found() return self.state_machine.go_to_state("simon_says_round_sync", multiplayer_info=(mac, seed))
def confetti(self, times=50): last_led = None while times: times -= 1 # randomly pick an led that is different from the last one that flashed all_leds_except_last = [led for led in self.leds if led != last_led] last_led = all_leds_except_last[random(0, len(all_leds_except_last) - 1)] last_led.blink()
def do_an_eye_thing(self, *args): lights = self.state_machine.lights thing = machine.random(0, 5) if thing <= 3: # blink lights.fade_out([lights.LED_TL, lights.LED_TR]) lights.fade_in([lights.LED_TL, lights.LED_TR], speed=2) elif thing == 4: # wink left eye lights.fade_out([lights.LED_TL]) lights.fade_in([lights.LED_TL], speed=2) elif thing == 5: lights.fade_out([lights.LED_TR]) lights.fade_in([lights.LED_TR], speed=2) self.state_machine.timer.init( period=machine.random(20000, 45000), mode=machine.Timer.ONE_SHOT, callback=self.do_an_eye_thing, )
def on_enter(self): self.state_machine.quiet_lights.all_off() lights = self.state_machine.lights lights.fade_in([lights.LED_TL, lights.LED_TR]) self.state_machine.timer.init( period=machine.random(20000, 45000), mode=machine.Timer.ONE_SHOT, callback=self.do_an_eye_thing, )
def dispFont(sec=5): header("DISPLAY FONTS", False) if maxx < 240: tx = "MicroPython" else: tx = "Hi from MicroPython" starty = miny + 4 n = time.time() + sec while time.time() < n: y = starty x = 0 i = 0 while y < maxy: if i == 0: x = 0 elif i == 1: x = lcd.CENTER elif i == 2: x = lcd.RIGHT i = i + 1 if i > 2: i = 0 for font in fontnames: if font == lcd.FONT_7seg: lcd.font(font) lcd.text(x, y, "-12.45/", machine.random(0xFFFFFF)) else: lcd.font(font) lcd.text(x, y, tx, machine.random(0xFFFFFF)) _, fsz = lcd.fontSize() y = y + 2 + fsz if y > (maxy - fsz): y = maxy if buttonA.wasPressed(): break
def circleDemo(sec=5, dofill=False): tx = "CIRCLE" if dofill: tx = "FILLED " + tx header(tx, True) n = time.time() + sec while time.time() < n: color = machine.random(0xFFFFFF) fill = machine.random(0xFFFFFF) x = machine.random(4, maxx - 2) y = machine.random(miny + 2, maxy - 2) if x < y: r = machine.random(2, x) else: r = machine.random(2, y) if dofill: lcd.circle(x, y, r, color, fill) else: lcd.circle(x, y, r, color) if btnA.wasPressed(): speaker.tone(346, 120, 1) break lcd.resetwin()
def rectDemo(sec=5, dofill=False): tx = "RECTANGLE" if dofill: tx = "FILLED " + tx header(tx, True) n = time.time() + sec while time.time() < n: x = machine.random(4, maxx - 2) y = machine.random(miny, maxy - 2) w = machine.random(2, maxx - x) h = machine.random(2, maxy - y) color = machine.random(0xFFFFFF) if dofill: fill = machine.random(0xFFFFFF) lcd.rect(x, y, w, h, color, fill) else: lcd.rect(x, y, w, h, color) if buttonA.wasPressed(): break lcd.resetwin()
def circleDemo(sec=5, dofill=False): tx = "CIRCLE" if dofill: tx = "FILLED " + tx header(tx, True) n = time.time() + sec while time.time() < n: color = machine.random(0xFFFFFF) fill = machine.random(0xFFFFFF) x = machine.random(4, maxx - 2) y = machine.random(miny + 2, maxy - 2) if x < y: r = machine.random(2, x) else: r = machine.random(2, y) if dofill: tft.circle(x, y, r, color, fill) else: tft.circle(x, y, r, color) if touched(): break tft.resetwin()
def random_song(): return SONGS[machine.random(0, len(SONGS) - 1)]
print("subscription topic =", sub_topic) tft = display.TFT() if display_type == 'WROVER': # ST7789V used by v3 esp-wrover kit (I think default is 240 x 320) tft.init(tft.ST7789, rst_pin=18, backl_pin=5, miso=25, mosi=23, clk=19, cs=22, dc=21) else: # ILI9341 tft.init(tft.ILI9341, width=width, height=height, miso=19, mosi=18, clk=5, cs=15, dc=33, bgr=True) font_num = getattr(tft, font) tft.font(font_num) #utime.sleep(1) tft.clear() tft.text(10, 10, "Hello Steve", random(0xFFFFFF)) #pin15 = Pin(15, Pin.OUT) #will need to find another pin since this is cs pin regex= re.compile('{(.*?)}') #s = "jkdsfl{RED}fkjsdflds{GREEN}jlklfjsl{PINK}lkdsjflkdsjfl" def display_text(s, n, tag=None, h=0): # the following two things can only happen the first time a string is processed if s and s[0] != '{': # deal with strings with no pos 0 tag (they may be tags elsewhere in the string) s = '{WHITE}' + s if tag is None: z = regex.search(s) tag = z.group(0)
def on_enter(self): self.state_machine.timer.init( period=machine.random(500, 2000), mode=machine.Timer.PERIODIC, callback=self.broadcast, )
from fri3d import Badge import utime import machine b = Badge() while True: b.eyes.pupil(machine.random(5), machine.random(3)) # -- sleep for 100 millis utime.sleep_ms(3000) b.eyes.blink() utime.sleep_ms(3000)
def random_song(): return SONGS[random(0, len(SONGS) - 1)]