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 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
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 playsong(song): # this function plays your song a = 0 # this variable keeps track of the visualiser bars for i in range(len(song)): if (song[i] == "P"): bequiet() else: playtone(tones[song[i]]) explorer.set_pen(0, 255, 0) # switch to green pen explorer.rectangle( a, 240 - (int((tones[song[i]]) / 21)), 5, 240 ) # draw a green bar corresponding to the frequency of the note a += 7 if a >= 240: # clears the screen if the green bars reach the right hand edge clear() a = 0 explorer.update() utime.sleep( 0.15 ) # change this number if you want to alter how long the notes play for bequiet()
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
if 40 < humidity < 60: description = "good" else: description = "bad" return description while True: # read the sensors temperature, pressure, humidity, gas_resistance, status, gas_index, meas_index = bme.read() # pressure comes in pascals which is a reight long number, lets convert it to the more manageable hPa pressurehpa = pressure / 100 # draw a thermometer/barometer thingy display.set_pen(125, 125, 125) 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)
import picoexplorer as explorer import utime from machine import Pin from dht import DHT11, InvalidChecksum width = explorer.get_width() height = explorer.get_height() display_buffer = bytearray(width * height * 2) explorer.init(display_buffer) explorer.set_backlight(1.0) explorer.set_pen(0, 0, 0) explorer.clear() explorer.update() 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)
def blk(self): display.set_pen(0, 0, 0) display.clear() display.update()
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)
def clear(): # this function clears Pico Explorer's screen to black display.set_pen(0, 0, 0) display.clear() display.update()
# Simple oscilloscope for a Raspberry Pi Pico # Press the buttons to toggle the traces # 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
# # 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()
# Initialise display with a bytearray display buffer 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)
balls = [] for i in range(0, 100): r = random.randint(0, 10) + 3 balls.append( Ball( random.randint(r, r + (width - 2 * r)), random.randint(r, r + (height - 2 * r)), r, (14 - r) / 2, (14 - r) / 2, display.create_pen(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), )) while True: display.set_pen(40, 40, 40) display.clear() for ball in balls: ball.x += ball.dx ball.y += ball.dy xmax = width - ball.r xmin = ball.r ymax = height - ball.r ymin = ball.r if ball.x < xmin or ball.x > xmax: ball.dx *= -1 if ball.y < ymin or ball.y > ymax:
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)
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
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)
button = anykey() if button == buttons[colours[i] - 1]: lightup(colours[i], True) say("Your Turn") i += 1 else: exp.set_tone(150) wrong.off() say("Wrong!") playing = False i = difficulty + 1 time.sleep(1) all(False) wrong.on() say("Your turn") shhh() if playing: difficulty = difficulty + 1 exp.set_pen(black) exp.clear() exp.update() all(False) exp.update() while True: attract() game()
for i in range(n): y3 = y + int(grad * i) display.pixel(x + i, y3) # One dot at a time #Calls the line procedure above but uses start and end points def pointline(p1, p2): line(p1.X, p1.Y, p2.X, p2.Y) def update(): display.update() # Clears screen to black and sets pen for the rest of the drawing display.set_pen(155, 155, 155) display.clear() display.set_pen(purple) # Now lets play with the built in objects display.circle(100, 100, 50) display.rectangle(0, 00, 25, 25) display.rectangle(0, 0, 25, 25) display.pixel_span(0, 200, 120) #Use this for vertical line display.rectangle(120, 0, 1, 25) #should be diagonal across the screen line(0, 0, 240, 240) # Create and print a point
def fillTopFlatTriangle(p1,p2,p3): #filled triangle routines ported From http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html slope1 = float(p3.X - p1.X) / float(p3.Y - p1.Y) slope2 = float(p3.X - p2.X) / float(p3.Y - p2.Y) x1 = p3.X x2 = p3.X + 0.5 for scanlineY in range (p3.Y,p1.Y-1,-1): display.pixel_span(int(x1), scanlineY, int(x2)-int(x1)) x1 -= slope1 x2 -= slope2 display.set_pen(0,0,0) display.clear() display.set_pen(purple) t=Triangle(Point(30,60),Point(0,110),Point(120,200)) fillBottomFlatTriangle(Point(60,0),Point(0,110),Point(110,110)) fillTopFlatTriangle(Point(0,120),Point(110,120),Point(110,240)) display.update() display.set_pen(0,0,0) display.clear() display.set_pen(purple) t.fillTri()
def clear(): display.set_pen(0, 0, 0) display.clear() display.update()
RealStart = -2 * zoom RealEnd= 1 * zoom ImaginaryStart = -1 * zoom ImaginaryEnd = 1 * zoom display.clear() #One of the issues on the video was it was displaying the old images whilst generating the new one #So added the update below to send a clear screen to the display display.update() go = True while go: for x in range(0, width): for y in range(0, height): c = complex(RealStart + (x / width) * (RealEnd - RealStart),ImaginaryStart + (y / height) * (ImaginaryEnd - ImaginaryStart)) m = mandelbrot(c) colour = 255 - int(m * 255 / iterations) display.set_pen(colour,0,155) display.pixel(x, y) #uncomment this line if you want to see the progress the plot has made if you are not updating the screen every x #print(x) #if you put the display update here it will update every x line display.update() #if you put the display update here it will update at the end of processing (maybe 2 to 3 minutes in) #display.update() go = False
def clear(): # this function clears Pico Explorer's screen to black explorer.set_pen(0, 0, 0) explorer.clear() explorer.update()
display.init(display_buffer) 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("")
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()
import time import picoexplorer as explorer 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_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)
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()
# reads from Pico's temp sensor and converts it into a more manageable number sensor_temp = machine.ADC(4) conversion_factor = 3.3 / (65535) i = 0 while True: # the following two lines do some maths to convert the number from the temp sensor into celsius reading = sensor_temp.read_u16() * conversion_factor temperature = round(27 - (reading - 0.706) / 0.001721) # this if statement clears the display once the graph reaches the right hand side of the display if i >= (width + 1): 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)
def set_pen(self, color): display.set_pen(*color)