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_imageload
  9  from adafruit_matrixportal.matrix import Matrix
 10  import adafruit_scd30
 11  
 12  # --| User Config |----
 13  CO2_CUTOFFS = (1000, 2000, 5000)
 14  UPDATE_RATE = 1
 15  # ---------------------
 16  
 17  # the sensor
 18  scd30 = adafruit_scd30.SCD30(board.I2C())
 19  
 20  # optional if known (pick one)
 21  # scd30.ambient_pressure = 1013.25
 22  # scd30.altitude = 0
 23  
 24  # the display
 25  matrix = Matrix(width=64, height=32, bit_depth=6)
 26  display = matrix.display
 27  display.rotation = 90  # matrixportal up
 28  # display.rotation = 270 # matrixportal down
 29  
 30  # current condition smiley face
 31  smileys_bmp, smileys_pal = adafruit_imageload.load("/bmps/smileys.bmp")
 32  smiley = displayio.TileGrid(
 33      smileys_bmp,
 34      pixel_shader=smileys_pal,
 35      x=0,
 36      y=0,
 37      width=1,
 38      height=1,
 39      tile_width=32,
 40      tile_height=32,
 41  )
 42  
 43  # current condition label
 44  tags_bmp, tags_pal = adafruit_imageload.load("/bmps/tags.bmp")
 45  label = displayio.TileGrid(
 46      tags_bmp,
 47      pixel_shader=tags_pal,
 48      x=0,
 49      y=32,
 50      width=1,
 51      height=1,
 52      tile_width=32,
 53      tile_height=16,
 54  )
 55  
 56  # current CO2 value
 57  digits_bmp, digits_pal = adafruit_imageload.load("/bmps/digits.bmp")
 58  co2_value = displayio.TileGrid(
 59      digits_bmp,
 60      pixel_shader=digits_pal,
 61      x=0,
 62      y=51,
 63      width=4,
 64      height=1,
 65      tile_width=8,
 66      tile_height=10,
 67  )
 68  
 69  # put em all together
 70  splash = displayio.Group()
 71  splash.append(smiley)
 72  splash.append(label)
 73  splash.append(co2_value)
 74  
 75  # and show em
 76  display.show(splash)
 77  
 78  
 79  def update_display(value):
 80  
 81      value = abs(round(value))
 82  
 83      # smiley and label
 84      if value < CO2_CUTOFFS[0]:
 85          smiley[0] = label[0] = 0
 86      elif value < CO2_CUTOFFS[1]:
 87          smiley[0] = label[0] = 1
 88      elif value < CO2_CUTOFFS[2]:
 89          smiley[0] = label[0] = 2
 90      else:
 91          smiley[0] = label[0] = 3
 92  
 93      # CO2 value
 94      # clear it
 95      for i in range(4):
 96          co2_value[i] = 10
 97      # update it
 98      i = 3
 99      while value:
100          co2_value[i] = value % 10
101          value = int(value / 10)
102          i -= 1
103  
104  
105  while True:
106      # protect against NaNs and Nones
107      try:
108          update_display(scd30.CO2)
109      except:
110          pass
111      time.sleep(UPDATE_RATE)