|
11 | 11 |
|
12 | 12 | All text above must be included in any redistribution. |
13 | 13 | """ |
| 14 | + |
| 15 | +import time |
| 16 | +import board |
| 17 | +import json |
| 18 | +import displayio |
| 19 | +from adafruit_pyportal import PyPortal |
| 20 | +from adafruit_display_shapes.rect import Rect |
| 21 | +from adafruit_display_text.Label import Label |
| 22 | +from adafruit_bitmap_font import bitmap_font |
| 23 | + |
| 24 | +try: |
| 25 | + from secrets import secrets |
| 26 | +except ImportError: |
| 27 | + print("""WiFi settings are kept in secrets.py, please add them there! |
| 28 | +the secrets dictionary must contain 'ssid' and 'password' at a minimum""") |
| 29 | + raise |
| 30 | + |
| 31 | +MAX_BAR_HEIGHT = 160 |
| 32 | +MARGIN = 10 |
| 33 | +SPACE_BETWEEN_BARS = 1 |
| 34 | + |
| 35 | +COLORS = [0x00FF00, 0x83C602, 0xa2CF02, |
| 36 | + 0xF7DE03, 0xF6B502, 0xF78802, |
| 37 | + 0xF65201, 0xEA2709, |
| 38 | + 0xDA0115, 0xFC019E, 0xB548FF, |
| 39 | + 0x988FFE, 0x7EA7FE, 0x66BFFD, 0x4BD9FF] |
| 40 | + |
| 41 | +cwd = ("/"+__file__).rsplit('/', 1)[0] |
| 42 | + |
| 43 | +CAPTION_FONT_FILE = cwd+'/fonts/Helvetica-Bold-16.bdf' |
| 44 | +TEXT_FONT_FILE = cwd+'/fonts/Helvetica-Bold-16.bdf' |
| 45 | +HOUR_FONT_FILE = cwd+'/fonts/Arial-12.bdf' |
| 46 | + |
| 47 | +def halt_and_catch_fire(message, *args): |
| 48 | + """Log a critical error and stall the system.""" |
| 49 | + print(message % args) |
| 50 | + while True: |
| 51 | + pass |
| 52 | + |
| 53 | +url = 'https://enviro.epa.gov/enviro/efservice/getEnvirofactsUVHOURLY/ZIP/{0}/JSON'.format(secrets['zip']) |
| 54 | + |
| 55 | + |
| 56 | +# Initialize the pyportal object and let us know what data to fetch and where |
| 57 | +# to display it |
| 58 | +pyportal = PyPortal(url=url, |
| 59 | + status_neopixel=board.NEOPIXEL, |
| 60 | + default_bg=0xFFFFFF, |
| 61 | + text_font=TEXT_FONT_FILE, |
| 62 | + text_position=(20, 60), |
| 63 | + text_color=0xFFFFFF, |
| 64 | + text_wrap=35, |
| 65 | + caption_text='UV Index', |
| 66 | + caption_font=CAPTION_FONT_FILE, |
| 67 | + caption_position=(5, 20), |
| 68 | + caption_color=0x000000) |
| 69 | + |
| 70 | +def extract_hour(date_time): |
| 71 | + return '\n'.join(date_time.split()[1:3]) |
| 72 | + |
| 73 | +data = [{'hour': extract_hour(d['DATE_TIME']), 'value': d['UV_VALUE']} for d in json.loads(pyportal.fetch()) if d['UV_VALUE'] > 0] |
| 74 | + |
| 75 | +number_of_readings = len(data) |
| 76 | + |
| 77 | +whitespace = (number_of_readings - 1) * SPACE_BETWEEN_BARS + 2 * MARGIN |
| 78 | +bar_width = (320 - whitespace) // number_of_readings |
| 79 | + |
| 80 | +max_reading = max([d['value'] for d in data]) |
| 81 | + |
| 82 | +canvas = displayio.Group(max_size=24) |
| 83 | +pyportal.splash.append(canvas) |
| 84 | +hour_font = bitmap_font.load_font(HOUR_FONT_FILE) |
| 85 | + |
| 86 | +for i, reading in enumerate(data): |
| 87 | + bar_height = (MAX_BAR_HEIGHT * reading['value']) // max_reading |
| 88 | + x = MARGIN + i * (bar_width + SPACE_BETWEEN_BARS) |
| 89 | + canvas.append(Rect(x, 200 - bar_height, bar_width, bar_height, fill=COLORS[reading['value']])) |
| 90 | + text = Label( hour_font, x=x, y=220, text=reading['hour'], color=0x000000, line_spacing=0.8) |
| 91 | + canvas.append(text) |
| 92 | + |
| 93 | +while True: |
| 94 | + pass |
0 commit comments