code.py
 1  # SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import time
 6  import board
 7  import displayio
 8  import adafruit_sgp30
 9  from adafruit_bitmap_font import bitmap_font
10  from adafruit_display_text import label
11  import adafruit_imageload
12  from adafruit_clue import clue
13  
14  # --| User Config |-------------------------
15  TVOC_LEVELS = (80, 120)  # set two TVOC levels
16  MESSAGES = ("GOOD", "SUS?", "BAD!")  # set three messages (4 char max)
17  # ------------------------------------------
18  
19  # setup UI
20  cow_bmp, cow_pal = adafruit_imageload.load("bmps/milk_bg.bmp")
21  background = displayio.TileGrid(cow_bmp, pixel_shader=cow_pal)
22  
23  mouth_bmp, mouth_pal = adafruit_imageload.load("bmps/mouth_sheet.bmp")
24  mouth = displayio.TileGrid(
25      mouth_bmp,
26      pixel_shader=mouth_pal,
27      tile_width=40,
28      tile_height=20,
29      width=1,
30      height=1,
31      x=35,
32      y=110,
33  )
34  
35  msg_font = bitmap_font.load_font("fonts/Alphakind_28.bdf")
36  msg_font.load_glyphs("".join(MESSAGES))
37  message = label.Label(msg_font, text="WAIT", color=0x000000)
38  message.anchor_point = (0.5, 0.5)
39  message.anchored_position = (172, 38)
40  
41  data_font = bitmap_font.load_font("fonts/F25_Bank_Printer_Bold_12.bdf")
42  data_font.load_glyphs("eTVOC=12345?")
43  tvoc = label.Label(data_font, text="TVOC=?????", color=0x000000)
44  tvoc.anchor_point = (0, 1)
45  tvoc.anchored_position = (5, 235)
46  
47  eco2 = label.Label(data_font, text="eCO2=?????", color=0x000000)
48  eco2.anchor_point = (0, 1)
49  eco2.anchored_position = (130, 235)
50  
51  splash = displayio.Group()
52  splash.append(background)
53  splash.append(mouth)
54  splash.append(message)
55  splash.append(tvoc)
56  splash.append(eco2)
57  clue.display.show(splash)
58  
59  # setup SGP30 and wait for initial warm up
60  sgp30 = adafruit_sgp30.Adafruit_SGP30(board.I2C())
61  time.sleep(15)
62  
63  # loop forever
64  while True:
65      eCO2, TVOC = sgp30.iaq_measure()
66  
67      tvoc.text = "TVOC={:5d}".format(TVOC)
68      eco2.text = "eCO2={:5d}".format(eCO2)
69  
70      level = 0
71      for thresh in TVOC_LEVELS:
72          if TVOC <= thresh:
73              break
74          level += 1
75  
76      if level <= len(TVOC_LEVELS):
77          message.text = MESSAGES[level]
78          mouth[0] = level
79      else:
80          message.text = "????"
81  
82      time.sleep(1)