Merge pull request #60 from helgibbons/main

Fixes the example in the MicroPython Display readme and adds a couple of new examples
pull/65/head
Philip Howard 2021-02-15 19:27:02 +00:00 zatwierdzone przez GitHub
commit f83aa6bf48
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
5 zmienionych plików z 234 dodań i 14 usunięć

Wyświetl plik

@ -0,0 +1,45 @@
## This example borrows a CircuitPython hsv_to_rgb function to cycle through some rainbows on Pico Display's screen and RGB LED . If you're into rainbows, HSV (Hue, Saturation, Value) is very useful!
import utime
import picodisplay as display
# Set up and initialise Pico Display
buf = bytearray(display.get_width() * display.get_height() * 2)
display.init(buf)
display.set_backlight(0.8)
# From CPython Lib/colorsys.py
def hsv_to_rgb(h, s, v):
if s == 0.0:
return v, v, v
i = int(h*6.0)
f = (h*6.0) - i
p = v*(1.0 - s)
q = v*(1.0 - s*f)
t = v*(1.0 - s*(1.0-f))
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_led(r, g, b) # Set LED to a converted HSV value
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!", 10, 10, 240, 6) # Add some text
display.update() # Update the display
utime.sleep(1.0 / 60)

Wyświetl plik

@ -0,0 +1,66 @@
# This example takes the temperature from the Pico's onboard temperature sensor, and displays it on Pico Display Pack, along with a little pixelly graph.
# It's based on the thermometer example in the "Getting Started with MicroPython on the Raspberry Pi Pico" book, which is a great read if you're a beginner!
import machine
import utime
# Pico Display boilerplate
import picodisplay as display
width = display.get_width()
height = display.get_height()
display_buffer = bytearray(width * height * 2)
display.init(display_buffer)
# reads from Pico's temp sensor and converts it into a more manageable number
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)
# Set the display backlight to 50%
display.set_backlight(0.5)
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)
# heck lets also set the LED to match
display.set_led(0, 255, 0)
if temperature > 20:
display.set_led(255, 0, 0)
if temperature < 13:
display.set_led(0, 0, 255)
# draws the reading as a tall, thin rectangle
display.rectangle(i, height - (temperature * 4), 5, height)
# draws a white background for the text
display.set_pen(255, 255, 255)
display.rectangle(1, 1, 50, 25)
# writes the reading as text in the white rectangle
display.set_pen(0, 0, 0)
display.text("{:.0f}".format(temperature) + "c", 3, 3, 0, 3)
# time to update the display
display.update()
# waits for 5 seconds
utime.sleep(5)
# the next tall thin rectangle needs to be drawn 5 pixels to the right of the last one
i += 5

Wyświetl plik

@ -0,0 +1,45 @@
## This example borrows a CircuitPython hsv_to_rgb function to cycle through some rainbows on Pico Explorer's screen and RGB LED . If you're into rainbows, HSV (Hue, Saturation, Value) is very useful!
import utime
import picoexplorer as display
# Set up and initialise Pico Explorer
buf = bytearray(display.get_width() * display.get_height() * 2)
display.init(buf)
# From CPython Lib/colorsys.py
def hsv_to_rgb(h, s, v):
if s == 0.0:
return v, v, v
i = int(h*6.0)
f = (h*6.0) - i
p = v*(1.0 - s)
q = v*(1.0 - s*f)
t = v*(1.0 - s*(1.0-f))
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)

Wyświetl plik

@ -0,0 +1,56 @@
# This example takes the temperature from the Pico's onboard temperature sensor, and displays it on Pico Explorer, along with a little pixelly graph.
# It's based on the thermometer example in the "Getting Started with MicroPython on the Raspberry Pi Pico" book, which is a great read if you're a beginner!
import machine
import utime
# Pico Explorer boilerplate
import picoexplorer as display
width = display.get_width()
height = display.get_height()
display_buffer = bytearray(width * height * 2)
display.init(display_buffer)
# 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)
# 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

Wyświetl plik

@ -1,6 +1,6 @@
# Pico Display Pack - MicroPython <!-- omit in toc -->
Our Pico Display Pack offers a vibrant 1.14" (240x135) IPS LCD screen for your Raspberry Pi Pico it also includes four switches and and an RGB LED!
Pico Display Pack is a vibrant 1.14", 240 x 135 pixel IPS LCD screen for your Raspberry Pi Pico, with four useful buttons and a RGB LED. [Click here](https://shop.pimoroni.com/products/pico-display-pack) to find out more!
We've included helper functions to handle every aspect of drawing to the screen and interfacing with the buttons and LED. See the [function reference](#function-reference) for details.
@ -25,25 +25,33 @@ We've included helper functions to handle every aspect of drawing to the screen
## Example Program
The following example sets up Pico Display, displays some basic demo text and graphics and will illuminate the RGB LED green if the A button is presse
The following example sets up Pico Display, displays some basic demo text and illuminates the RGB LED green when the A button is pressed.
```python
import utime
import picodisplay
# Initialise Picodisplay with a bytearray display buffer
buf = bytearray(picodisplay.get_width() * picodisplay.get_height() * 2)
picodisplay.init(buf)
picodisplay.set_backlight(1.0)
picodisplay.set_pen(255, 0, 0) # Set a red pen
picodisplay.clear() # Clear the display buffer
picodisplay.update() # Update the display with our changes
picodisplay.set_pen(255, 0, 0) # Set a red pen
picodisplay.clear() # Clear the display buffer
picodisplay.set_pen(255, 255, 255) # Set a white pen
picodisplay.text("pico display", 10, 10, 240, 6) # Add some text
picodisplay.update() # Update the display with our changes
picodisplay.set_led(255, 0, 0) # Set the RGB LED to Red
picodisplay.set_led(0, 255, 0) # Set the RGB LED to Green
picodisplay.set_led(0, 0, 255) # Set the RGB LED to Blue
picodisplay.set_led(255, 0, 0) # Set the RGB LED to red
utime.sleep(1) # Wait for a second
picodisplay.set_led(0, 255, 0) # Set the RGB LED to green
utime.sleep(1) # Wait for a second
picodisplay.set_led(0, 0, 255) # Set the RGB LED to blue
while not picodisplay.is_pressed(picodisplay.BUTTON_A): # Wait for Button A to be pressed
while picodisplay.is_pressed(picodisplay.BUTTON_A) == False:
pass
picodisplay.set_led(0, 255, 0) # Set the RGB LED to green
```
## Function Reference
@ -59,7 +67,7 @@ picodisplay.init(buf)
### set_backlight
Set the display backlight from 0.0 to 1.0
Sets the display backlight from 0.0 to 1.0.
```python
picodisplay.set_backlight(brightness)
@ -69,7 +77,7 @@ Uses hardware PWM to dim the display backlight, dimming values are gamma-correct
### set_led
Sets the RGB LED on Pico Display with an RGB triplet:
Sets the RGB LED on Pico Display with an RGB triplet.
```python
picodisplay.set_led(r, g, b)
@ -85,7 +93,7 @@ Reads the GPIO pin connected to one of Pico Display's buttons, returning `True`
picodisplay.is_pressed(button)
```
The button vaule should be a number denoting a pin, and constants `picodisplay.BUTTON_A`, `picodisplay.BUTTON_B`, `picodisplay.BUTTON_X` and `picodisplay.BUTTON_Y` are supplied to make it easier. e:
The button value should be a number denoting a pin, and constants `picodisplay.BUTTON_A`, `picodisplay.BUTTON_B`, `picodisplay.BUTTON_X` and `picodisplay.BUTTON_Y` are supplied to make it easier. e:
```python
is_a_button_pressed = picodisplay.is_pressed(picodisplay.BUTTON_A)
@ -93,7 +101,7 @@ is_a_button_pressed = picodisplay.is_pressed(picodisplay.BUTTON_A)
### update
To display your changes on Pico Display's screen you need to call `update`:
To display your changes on Pico Display's screen you need to call `update`.
```python
picodisplay.update()
@ -155,7 +163,7 @@ picodisplay.rectangle(x, y, w, h)
Draws a circle filled with the current pen colour to the buffer. The `x` and `y` parameters specify the centre of the circle, `r` specifies the radius in pixels.
```python
picodisplay.rectangle(x, y, w, h)
picodisplay.circle(x, y, r)
```
![Circle function explanation image](/micropython/modules/pico_display/images/circle.png)