-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from pimoroni/examples/new-launcher-examples
Examples: replaced examples with no longer active API
- Loading branch information
Showing
6 changed files
with
194 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
""" | ||
This example connects to the Carbon Intensity API to give you a regional | ||
forecast of how your (UK) electricity is being generated and its carbon impact. | ||
Carbon Intensity API only reports data from the UK National Grid. | ||
Find out more about what the numbers mean at: | ||
https://carbonintensity.org.uk/ | ||
Make sure to uncomment the correct size for your display! | ||
""" | ||
|
||
import urequests | ||
import inky_frame | ||
|
||
graphics = None | ||
WIDTH = None | ||
HEIGHT = None | ||
|
||
# Length of time between updates in minutes. | ||
# Frequent updates will reduce battery life! | ||
UPDATE_INTERVAL = 240 | ||
|
||
# Set (the first half) of your UK postcode here | ||
POSTCODE = "S9" | ||
|
||
URL = "https://api.carbonintensity.org.uk/regional/postcode/" + str(POSTCODE) | ||
|
||
|
||
def update(): | ||
global region, forecast, index, power_list, datetime_to, datetime_from | ||
print(f"Requesting URL: {URL}") | ||
r = urequests.get(URL) | ||
# open the json data | ||
j = r.json() | ||
print("Data obtained!") | ||
print(j) | ||
|
||
# parse the json data | ||
region = j["data"][0]["shortname"] | ||
|
||
forecast = j["data"][0]["data"][0]["intensity"]["forecast"] | ||
index = j["data"][0]["data"][0]["intensity"]["index"] | ||
|
||
power_list = [] | ||
for power in j["data"][0]["data"][0]["generationmix"]: | ||
power_list.append(power['perc']) | ||
|
||
datetime_to = j["data"][0]["data"][0]["to"].split("T") | ||
datetime_from = j["data"][0]["data"][0]["from"].split("T") | ||
|
||
# close the socket | ||
r.close() | ||
|
||
|
||
def draw(): | ||
# we're setting up our PicoGraphics buffer after we've made our RAM intensive https request | ||
w, h = WIDTH, HEIGHT | ||
graphics.set_pen(inky_frame.WHITE) | ||
graphics.clear() | ||
|
||
# draw lines | ||
graphics.set_pen(inky_frame.BLACK) | ||
graphics.line(0, int((h / 100) * 0), w, int((h / 100) * 0)) | ||
graphics.line(0, int((h / 100) * 50), w, int((h / 100) * 50)) | ||
graphics.set_font("bitmap8") | ||
graphics.text('100%', w - 40, 10, scale=2) | ||
graphics.text('50%', w - 40, int((h / 100) * 50 + 10), scale=2) | ||
|
||
# draw bars | ||
bar_colours = [ | ||
inky_frame.ORANGE, | ||
inky_frame.RED, | ||
inky_frame.ORANGE, | ||
inky_frame.RED, | ||
inky_frame.BLUE, | ||
inky_frame.ORANGE, | ||
inky_frame.GREEN, | ||
inky_frame.GREEN, | ||
inky_frame.GREEN | ||
] | ||
for p in power_list: | ||
graphics.set_pen(bar_colours[power_list.index(p)]) | ||
graphics.rectangle(int(power_list.index(p) * w / 9), int(h - p * (h / 100)), | ||
int(w / 9), int(h / 100 * p)) | ||
|
||
# draw labels | ||
graphics.set_font('sans') | ||
# once in white for a background | ||
graphics.set_pen(inky_frame.WHITE) | ||
labels = ['biomass', 'coal', 'imports', 'gas', 'nuclear', 'other', 'hydro', 'solar', 'wind'] | ||
graphics.set_thickness(4) | ||
for label in labels: | ||
graphics.text(f'{label}', int((labels.index(label) * w / 9) + (w / 9) / 2), h - 10, angle=270, scale=1) | ||
# again in black | ||
graphics.set_pen(inky_frame.BLACK) | ||
labels = ['biomass', 'coal', 'imports', 'gas', 'nuclear', 'other', 'hydro', 'solar', 'wind'] | ||
graphics.set_thickness(2) | ||
for label in labels: | ||
graphics.text(f'{label}', int((labels.index(label) * w / 9) + (w / 9) / 2), h - 10, angle=270, scale=1) | ||
|
||
# draw header | ||
graphics.set_thickness(3) | ||
graphics.set_pen(inky_frame.GREEN) | ||
if index in ['high', 'very high']: | ||
graphics.set_pen(inky_frame.RED) | ||
if index in ['moderate']: | ||
graphics.set_pen(inky_frame.ORANGE) | ||
graphics.set_font("sans") | ||
graphics.text('Carbon Intensity', 10, 35, scale=1.2, angle=0) | ||
|
||
# draw small text | ||
graphics.set_pen(inky_frame.BLACK) | ||
graphics.set_font("bitmap8") | ||
graphics.text(f'Region: {region}', int((w / 2) + 30), 10, scale=2) | ||
graphics.text(f'{forecast} gCO2/kWh ({index})', int((w / 2) + 30), 30, scale=2) | ||
graphics.text(f'{datetime_from[0]} {datetime_from[1]} to {datetime_to[1]}', int((w / 2) + 30), 50, scale=2) | ||
|
||
graphics.update() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import gc | ||
import uos | ||
import machine | ||
import jpegdec | ||
import sdcard | ||
from urllib import urequest | ||
|
||
""" | ||
xkcd daily | ||
You *must* insert an SD card into Inky Frame! | ||
We need somewhere to save the jpg for display. | ||
Fetches a pre-processed XKCD daily image from: | ||
https://pimoroni.github.io/feed2image/xkcd-daily.jpg | ||
See https://xkcd.com/ for more webcomics! | ||
""" | ||
|
||
graphics = None | ||
WIDTH = None | ||
HEIGHT = None | ||
|
||
UPDATE_INTERVAL = 240 | ||
|
||
gc.collect() # We're really gonna need that RAM! | ||
|
||
FILENAME = "/sd/xkcd-daily.jpg" | ||
ENDPOINT = "https://pimoroni.github.io/feed2image/xkcd-daily.jpg" | ||
|
||
sd_spi = machine.SPI(0, sck=machine.Pin(18, machine.Pin.OUT), mosi=machine.Pin(19, machine.Pin.OUT), miso=machine.Pin(16, machine.Pin.OUT)) | ||
sd = sdcard.SDCard(sd_spi, machine.Pin(22)) | ||
uos.mount(sd, "/sd") | ||
gc.collect() # Claw back some RAM! | ||
|
||
|
||
def update(): | ||
url = ENDPOINT | ||
|
||
if (WIDTH, HEIGHT) != (600, 448): | ||
url = url.replace("xkcd-", f"xkcd-{WIDTH}x{HEIGHT}-") | ||
|
||
socket = urequest.urlopen(url) | ||
|
||
# Stream the image data from the socket onto disk in 1024 byte chunks | ||
# the 600x448-ish jpeg will be roughly ~24k, we really don't have the RAM! | ||
data = bytearray(1024) | ||
with open(FILENAME, "wb") as f: | ||
while True: | ||
if socket.readinto(data) == 0: | ||
break | ||
f.write(data) | ||
socket.close() | ||
gc.collect() # We really are tight on RAM! | ||
|
||
|
||
def draw(): | ||
|
||
gc.collect() # For good measure... | ||
|
||
jpeg = jpegdec.JPEG(graphics) | ||
|
||
graphics.set_pen(1) | ||
graphics.clear() | ||
|
||
jpeg.open_file(FILENAME) | ||
jpeg.decode() | ||
|
||
graphics.update() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.