def send(msg): global morse y = LINE_SPACING * 2 for letter in msg: letter = letter.lower() print(letter) if letter in morse: symbols = morse[letter] explorer.text(letter + ": " + symbols, 0, y, 256, 3) y += LINE_SPACING if y >= HEIGHT: clear() y = 0 explorer.set_pen(255,0,0) explorer.update() for symbol in symbols: print(symbol, sep="") if symbol == ".": explorer.set_tone(TONE_FREQUENCY) utime.sleep(DURATION_DOT / SPEED) explorer.set_tone(-1) elif symbol == "-": explorer.set_tone(TONE_FREQUENCY) utime.sleep(DURATION_DASH / SPEED) explorer.set_tone(-1) utime.sleep(DURATION_SYMBOL / SPEED) utime.sleep(DURATION_CHARACTER / SPEED) else: print("/") utime.sleep(DURATION_WORD / SPEED)
def say(string): x = half_width // 2 y = height // 3 h = half_width w = height // 3 exp.set_pen(black) exp.rectangle(x, y, h, w) exp.set_pen(white) exp.text(string, x + 10, y + 10, 15) exp.update()
def say(string): x = int(half_width / 2) y = int(height / 3) h = half_width w = int(height / 3) explorer.set_pen(black) explorer.rectangle(x, y, h, w) explorer.set_pen(white) explorer.text(string, x + 10, y + 10, 15) explorer.update()
def __gameboard(self, seq: int = -1, user: bool = False) -> int: bt, tn = -1, -1 exp.set_pen(0) exp.clear() #draw squares ~ capture button presses (if avail) OR Simon ~ store button tone for n, d in enumerate([2093, 2249, 2637, 2794]): p = ((exp.is_pressed(n) and user) or (n == seq)) bt = n if p else bt tn = d if p else tn x, y = (n > 1) * 120, (n % 2) * 120 exp.set_pen(self.__pal(n + 8)) exp.rectangle(x + 2, y + 2, 116, 116) exp.set_pen(self.__pal(n + (4 * p))) exp.rectangle(x + 7, y + 7, 106, 106) #center circle for level display exp.set_pen(0) exp.circle(120, 120, 40) exp.set_pen(self.__pal(12)) exp.circle(120, 120, 36) exp.set_pen(self.__pal(13)) exp.circle(120, 120, 31) exp.set_pen(65535) #find x for center placement of level display if self.__l < 10: c = self.__c[self.__l] else: a = self.__l // 10 b = self.__l - int(a * 10) c = self.__c[a] + self.__c[b] #display level number exp.text("{}".format(self.__l), 121 - c, 108, 160, 4) exp.update() return bt, tn
display.circle(190, 190, 40) display.rectangle(180, 45, 20, 140) # switch to red to draw the 'mercury' display.set_pen(red) display.circle(190, 190, 30) thermometerheight = int(120 / 30 * temperature) if thermometerheight > 120: thermometerheight = 120 if thermometerheight < 1: thermometerheight = 1 display.rectangle(186, 50 + 120 - thermometerheight, 10, thermometerheight) # drawing the temperature text display.set_pen(white) display.text("temperature:", 10, 10, 240, 3) display.set_pen(tempcolour) display.text('{:.1f}'.format(temperature) + 'C', 10, 30, 240, 5) display.set_pen(white) display.text(describe_temperature(temperature), 10, 60, 240, 3) # and the pressure text display.text("pressure:", 10, 90, 240, 3) display.text('{:.0f}'.format(pressurehpa) + 'hPa', 10, 110, 240, 5) display.text(describe_pressure(pressurehpa), 10, 140, 240, 3) # and the humidity text display.text("humidity:", 10, 170, 240, 3) display.text('{:.0f}'.format(humidity) + '%', 10, 190, 240, 5) display.text(describe_humidity(humidity), 10, 220, 240, 3)
explorer.set_backlight(1.0) explorer.set_audio_pin(0) i = 1 while True: explorer.set_pen(120, 40, 60) explorer.clear() adc0 = int(explorer.get_adc(0) * 120) adc1 = int(explorer.get_adc(1) * 120) adc2 = int(explorer.get_adc(2) * 120) explorer.set_pen(255, 255, 255) explorer.text("ADC0:", 20, 20, 100) explorer.text("ADC1:", 20, 40, 100) explorer.text("ADC2:", 20, 60, 100) explorer.set_pen(adc0 * 2, 0, 0) explorer.circle(90 + adc0, 26, 10) explorer.set_pen(0, adc1 * 2, 0) explorer.circle(90 + adc1, 46, 10) explorer.set_pen(0, 0, adc2 * 2) explorer.circle(90 + adc2, 66, 10) explorer.set_pen(255, 255, 255) explorer.text("Plug a jumper wire from GP0 to AUDIO to hear noise!", 20, 110, 200)
explorer.set_pen(255, 255, 255) pin = Pin(0, Pin.OUT, Pin.PULL_DOWN) utime.sleep(1) sensor = DHT11(pin) valid_checks = 0 invalid_checks = 0 while True: explorer.set_pen(0, 0, 0) explorer.clear() explorer.set_pen(255, 255, 255) try: explorer.text("Temperature: {:.01f}C".format(sensor.temperature), 10, 10, 240) explorer.text("Humidity: {:.01f}%".format(sensor.humidity), 10, 30, 240) valid_checks += 1 except InvalidChecksum: invalid_checks += 1 explorer.set_pen(255, 0, 0) explorer.text("Checksum is invalid", 10, 10, 240) explorer.set_pen(255, 255, 255) explorer.text("Valid checks: {}".format(valid_checks), 10, 60, 240) explorer.text("Invalid checks: {}".format(invalid_checks), 10, 80, 240) explorer.text( "Ratio: {:.02f}%".format(valid_checks / (valid_checks + invalid_checks) * 100), 10,
def __gameloop(self): while self.__l <= self.__sl: #adjust not duration by level self.__d = max(150, SimonGame.DURATION - (self.__l * self.__sl)) #reset sequence position pos = 0 #Simon while not self.__u: #play sequence for s in range(self.__l): b, t = self.__gameboard(self.__s[s]) self.__playtone(t, self.__d) #switch user self.__u = SimonGame.PLAYER #Player while self.__u: #update gameboard b, t = self.__gameboard(user=True) #if a button was pressed if b > -1: #if the button matches the current sequence value if b == self.__s[pos]: #play tone til user releases the button exp.set_tone(t) if t > -1 else None while (b > -1) and exp.is_pressed(b): pass exp.set_tone(-1) self.__gameboard() #increment sequence position pos += 1 #if the spot matches the current level, increment the level and switch users if pos == self.__l: self.__l += 1 self.__u = SimonGame.SIMON sleep_ms(500) #if the button didn't match the current sequence value else: #play fart sound and increment try counter self.__playtone(self.__fart, 1000) self.__t += 1 #if all 3 tries are used up show loser message, play fart sound more and break master while condition if self.__t == 3: exp.text('You Lose!', 16, 30, 240, 5) exp.update() self.__playtone(self.__fart - 100, 1000) self.__l = self.__sl + 2 #switch users ~ doesn't do anything if you lost self.__u = SimonGame.SIMON #if the game has been won if self.__l == (self.__sl + 1): self.__l -= 1 #adjust for display #play winner animation for s in [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]: self.__gameboard(s) exp.set_pen(65535) exp.text('You Win!', 10, 30, 240, 6) exp.update() self.__playtone(t, 100) #go to start screen self.__startscreen()
i = i % 6 if i == 0: return v, t, p if i == 1: return q, v, p if i == 2: return p, v, t if i == 3: return p, q, v if i == 4: return t, p, v if i == 5: return v, p, q h = 0 while True: h += 1 r, g, b = [int(255 * c) for c in hsv_to_rgb(h / 360.0, 1.0, 1.0)] # rainbow magic display.set_pen(r, g, b) # Set pen to a converted HSV value display.clear() # Fill the screen with the colour display.set_pen(0, 0, 0) # Set pen to black display.text("pico disco!", 25, 20, 240, 6) # Add some text display.text("\o/ \o/ \o/ \o/ \o/ \o/ \o/ \o/ \o/", 25, 120, 240, 4) # and some more text display.text("oontz oontz oontz", 25, 220, 240, 2) # and a bit more tiny text display.update() # Update the display utime.sleep(1.0 / 60)
sda = Pin(20) # Pico Explorer scl = Pin(21) i2c = I2C(0, sda=sda, scl=scl, freq=400000) bme = BME280.BME280(i2c=i2c) def clear(): # this function clears Pico Explorer's screen to black display.set_pen(0, 0, 0) display.clear() display.update() clear() display.set_pen(0, 255, 0) display.text('Weather Station', 0, 100, width, 3) display.update() # Piezo.playsong(Piezo.song) sleep(2) while True: temp = bme.read_temperature() / 100 hum = bme.read_humidity() / 1000 pres = bme.read_pressure() / 25600 print('Temperature: {:.1f} °C'.format(temp)) print('Humidity: {:.1f} %rH'.format(hum)) print('Pressure: {:.0f} hPa'.format(pres)) print("") # writes the reading as text
# delete all items flagged for removal (e.g. an alien that's just been shot) def cleanup(original_list, items_to_remove): for item in items_to_remove: if item in original_list: original_list.remove(item) # setup screen display_buffer = bytearray(WIDTH * HEIGHT * 2) display.init(display_buffer) # show startup message display.set_pen(0,0,0) display.clear() display.set_pen(255, 0, 0) display.text("Space Invaders", 0, 0, 0, 4) display.update() utime.sleep(1) # setup joystick # plug X into ADC1 and Y into ADC0 # Make sure to also connect ground and 3v3 joystick = [machine.ADC(0), machine.ADC(1)] print("Width: {}, Height: {}".format(WIDTH, HEIGHT)) # store list of bullets from player going upwards # each bullet is a list of [x, y] shots = [] last_fired_player = 0 last_fired_alien = 0
def block(letter, x, y, colour): exp.set_pen(colour) exp.rectangle(x, y, half_width, half_height) exp.set_pen(black) exp.text(letter, x + 50, y + 50, 4)
y += LINE_SPACING if y >= HEIGHT: clear() y = 0 explorer.set_pen(255,0,0) explorer.update() for symbol in symbols: print(symbol, sep="") if symbol == ".": explorer.set_tone(TONE_FREQUENCY) utime.sleep(DURATION_DOT / SPEED) explorer.set_tone(-1) elif symbol == "-": explorer.set_tone(TONE_FREQUENCY) utime.sleep(DURATION_DASH / SPEED) explorer.set_tone(-1) utime.sleep(DURATION_SYMBOL / SPEED) utime.sleep(DURATION_CHARACTER / SPEED) else: print("/") utime.sleep(DURATION_WORD / SPEED) clear() explorer.set_pen(255,0,0) explorer.text("Morse Code", 0,0,256,4) explorer.update() msg = "Raspberry Pi Pico Morse Code" send(msg)
def text(self, x, y, text): display.text(text, round(x), round(y), 200) # 200 needs replacing
def __startscreen(self) -> None: self.__l = 1 #level self.__d = SimonGame.DURATION #initial note duration self.__u = SimonGame.SIMON #current user self.__t = 0 #tries self.__c = [12, 6, 10, 10, 12, 10, 12, 12, 12, 12] #character widths #draw start screen exp.set_pen(self.__pal(5)) exp.clear() exp.set_pen(self.__pal(2)) exp.text('DO SAME', 10, 20, 235, 6) exp.set_pen(0) exp.text('by: OneMadGypsy 2021', 10, 60, 240, 1) exp.text('A = Easy 10', 40, 100, 240, 2) exp.text('B = Medium 15', 40, 120, 240, 2) exp.text('X = Hard 25', 40, 140, 240, 2) exp.text('Y = Nightmare 50', 40, 160, 240, 2) exp.update() dfclt = [10, 15, 5, 50] #difficulties self.__sl = 0 #sequence length #wait for difficulty selection while not self.__sl: for n in range(4): if exp.is_pressed(n): self.__sl = dfclt[n] break self.__s = [randint(0, 3) for _ in range(self.__sl)] #sequence self.__gameboard() #draw gameboard sleep(1) #pause 1 second for player to prepare self.__gameloop() #start game
sun_set_seconds = ((sunset[0] * 60) + sunset[1]) * 60 day_length = sun_set_seconds - sun_rise_seconds day_progress = (day_seconds - sun_rise_seconds) / day_length if day_progress < 0: day_progress = 0 if day_progress > 1: day_progress = 1 # fill the screen yellow display.set_pen(255, 255, 0) display.clear() # draw title display.set_pen(0, 0, 0) display.text("Sunflower", 0, 0, 240, 5) # draw bar to show how far through the day we are display.set_pen(255, 0, 0) display.rectangle(0, 50, int(240 * day_progress), 10) display.set_pen(0, 0, 0) # manually control servo 1 if mode == 0: # move the servos if the button is pressed if display.is_pressed(display.BUTTON_A): display.text("A", 0, 50, 240, 8) if angle1 > 0: angle1 -= 2
# # This is the file that I saved as main.py to act as the beginnings of a boot menu, which will then call the bird game after a button click # width = explorer.get_width() height = explorer.get_height() display_buffer = bytearray(width * height * 2) # 2-bytes per pixel (RGB565) explorer.init(display_buffer) #explorer.set_backlight(1.0) explorer.set_audio_pin(0) clicked = False unclicked = True while unclicked: explorer.set_pen(0, 0, 0) explorer.clear() explorer.set_pen(155, 155, 155) explorer.text("Press B to flap the bird", 20, 110, 200) if explorer.is_pressed(explorer.BUTTON_B): unclicked = False explorer.update() time.sleep(0.01) explorer.set_pen(0, 0, 0) explorer.clear() explorer.update()
import picoexplorer as display import time import random buf = bytearray(display.get_width() * display.get_height() * 2) display.init(buf) while True: red = random.randint(0, 255) green = random.randint(0, 255) blue = random.randint(0, 255) display.set_pen(red, green, blue) display.clear() display.set_pen(0, 0, 0) display.text("STEM Club", 25, 20, 240, 6) display.text("\\o/ \\o/ \\o/ \\o/ \\o/ \\o/ \\o/ \\o/ \\o/", 25, 120, 240, 4) display.update() time.sleep(1)
# Designed for use with an pcio explorer board import picoexplorer as display import machine import utime # set up screen buffer and ADC WIDTH = display.get_width() HEIGHT = display.get_height() display_buffer = bytearray(WIDTH * HEIGHT * 2) display.init(display_buffer) # Clear screen and draw title display.set_pen(0, 0, 0) display.clear() display.set_pen(255, 0, 0) display.text("PicoScope", 0, 0, 0, 2) # Get 4 channels of Analogue to Digital converters adc = [] for i in range(4): adc.append(machine.ADC(i)) # Colours to display each ADC chanenl colors = [ (255, 0, 0), # Red, channel 0 (0, 255, 0), # Green, channel 1 (0, 0, 255), # Blue, channel 2 (0, 255, 255) ] # Cyan channel 3 # button label names to display on screen to toggle traces
import time, random import picoexplorer as explorer # set up screem WIDTH = explorer.get_width() HEIGHT = explorer.get_height() display_buffer = bytearray(WIDTH * HEIGHT * 2) # 2-bytes per pixel (RGB565) explorer.init(display_buffer) # main loop (forever) while True: # clear screen and display controle explorer.set_pen(0, 0, 0) explorer.rectangle(0, 0, WIDTH, HEIGHT) explorer.set_pen(0, 0, 255) explorer.text("A/B Motor 1", 0, 0, WIDTH, 4) explorer.text("X/Y Motor 2", 0, 50, WIDTH, 4) # Press A/B to control motor 1 if explorer.is_pressed(explorer.BUTTON_A): explorer.set_motor(0, 0, 1) explorer.text("Motor 1 Forwards", 0, 100, WIDTH, 2) elif explorer.is_pressed(explorer.BUTTON_B): explorer.set_motor(0, 1, 1) explorer.text("Motor 1 Backwards", 0, 100, WIDTH, 2) else: explorer.set_motor(0, 0, 0) # Press X/Y to control motor 2 if explorer.is_pressed(explorer.BUTTON_X): explorer.set_motor(1, 0, 1)
explorer.set_audio_pin(0) i = 1 while True: explorer.set_pen(120, 40, 60) explorer.clear() adc0 = int(explorer.get_adc(0) * 120) adc1 = int(explorer.get_adc(1) * 120) adc2 = int(explorer.get_adc(2) * 120) explorer.set_pen(255, 255, 255) explorer.text("ADC0:", 20, 20, 100) explorer.text("ADC1:", 20, 40, 100) explorer.text("ADC2:", 20, 60, 100) explorer.set_pen(adc0 * 2, 0, 0) explorer.circle(90 + adc0, 26, 10) explorer.set_pen(0, adc1 * 2, 0) explorer.circle(90 + adc1, 46, 10) explorer.set_pen(0, 0, adc2 * 2) explorer.circle(90 + adc2, 66, 10) # example for the on-board A/B/X/Y buttons if explorer.is_pressed(explorer.BUTTON_A): explorer.set_pen(255, 255, 255)
buf = bytearray(display.get_width() * display.get_height() * 2) display.init(buf) # sets up a handy function we can call to clear the screen def clear(): display.set_pen(0, 0, 0) display.clear() display.update() while True: if display.is_pressed(display.BUTTON_A): # if a button press is detected then... clear() # clear to black display.set_pen(255, 255, 255) # change the pen colour display.text("Button A pressed", 10, 10, 240, 4) # display some text on the screen display.update() # update the display utime.sleep(1) # pause for a sec clear() # clear to black again elif display.is_pressed(display.BUTTON_B): clear() display.set_pen(0, 255, 255) display.text("Button B pressed", 10, 10, 240, 4) display.update() utime.sleep(1) clear() elif display.is_pressed(display.BUTTON_X): clear() display.set_pen(255, 0, 255) display.text("Button X pressed", 10, 10, 240, 4) display.update()
i = 0 display.set_pen(0, 0, 0) display.clear() # chooses a pen colour based on the temperature display.set_pen(0, 255, 0) if temperature > 20: display.set_pen(255, 0, 0) if temperature < 13: display.set_pen(0, 0, 255) # draws the reading as a tall, thin rectangle display.rectangle(i, height - (temperature * 6), 6, height) # draws a white background for the text display.set_pen(255, 255, 255) display.rectangle(1, 1, 65, 33) # writes the reading as text in the white rectangle display.set_pen(0, 0, 0) display.text("{:.0f}".format(temperature) + "c", 3, 3, 0, 4) # time to update the display display.update() # waits for 5 seconds utime.sleep(5) # the next tall thin rectangle needs to be drawn 6 pixels to the right of the last one i += 6
def birdgame(width, height): #colours brightwhite = display.create_pen(255, 255, 255) white = display.create_pen(155, 155, 155) red = display.create_pen(155, 0, 0) green = display.create_pen(0, 155, 0) brown = display.create_pen(210, 105, 30) skyblue = display.create_pen(50, 50, 115) black = display.create_pen(0, 0, 0) birdgrid = [ "00000011111100000", "00001144441510000", "00014444415551000", "00014444415515100", "01111444415515100", "15555144415555100", "15555514441555100", "14555414444111110", "01444144441333331", "00111222213111110", "00001222221333100", "00000112222111100", "00000001111000000" ] def drawbird(x, y, zoom): #draw the bird row = 0 col = 0 for line in birdgrid: #print(line) for pixel in line: #print(pixel) colour = str(pixel) if colour != "0": if colour == "1": display.set_pen(black) if colour == "2": display.set_pen(green) if pixel == "3": display.set_pen(brown) if pixel == "4": display.set_pen(red) if pixel == "5": display.set_pen(white) display.rectangle(x + (col * zoom), y + (row * zoom), zoom, zoom) col += 1 row += 1 col = 0 class Bird: def __init__(self, x, y): self.x = x self.y = y self.flying = True self.crashed = False class Pillar: def __init__(self, x, hole): self.x = x self.holetop = hole self.holebottom = hole + 70 bird = Bird(5, 20) colonade = [] score = 0 # reset def reset(): x = 0 for i in range(0, 3): hole = random.randint(1, height - 100) colonade.append(Pillar(x + 100, hole)) x += int(width / 3) reset() while bird.flying: display.set_pen(skyblue) display.clear() #draw the pillars for pillar in colonade: display.set_pen(white) display.rectangle(pillar.x, 0, 10, height) display.set_pen(skyblue) display.rectangle(pillar.x, pillar.holetop, 10, 70) if not bird.crashed: if pillar.x > -10: pillar.x -= 1 else: pillar.x = width hole = random.randint(1, height - 100) pillar.holetop = hole pillar.holebottom = hole + 50 score += 1 if pillar.x < 39 and pillar.x > 6: if bird.y < pillar.holetop or bird.y + 28 > pillar.holebottom: bird.crashed = True #draw the bird drawbird(bird.x, bird.y, 2) #draw score display.set_pen(white) display.text(str(score), width - 70, 5, 1, 5) #move the bird if not bird.crashed: if display.is_pressed(display.BUTTON_A): #flap if bird.y > -22: bird.y -= 3 else: if bird.y < height - 25: bird.y += 3 if display.is_pressed(display.BUTTON_B): colonade = [] reset() bird.y = 20 bird.crashed = False display.update()