code.py
  1  # SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  """
  6  PyPortal Smart Lighting Controller
  7  -------------------------------------------------------------
  8  https://learn.adafruit.com/pyportal-smart-lighting-controller
  9  
 10  Brent Rubell for Adafruit Industries, 2019
 11  """
 12  import board
 13  import displayio
 14  from adafruit_bitmap_font import bitmap_font
 15  from adafruit_button import Button
 16  import adafruit_touchscreen
 17  from digitalio import DigitalInOut
 18  
 19  import busio
 20  import neopixel
 21  from adafruit_esp32spi import adafruit_esp32spi
 22  from adafruit_esp32spi import adafruit_esp32spi_wifimanager
 23  
 24  # import lifx library
 25  import adafruit_lifx
 26  
 27  # Get wifi details and more from a secrets.py file
 28  try:
 29      from secrets import secrets
 30  except ImportError:
 31      print("WiFi secrets are kept in secrets.py, please add them there!")
 32      raise
 33  
 34  # ESP32 SPI
 35  esp32_cs = DigitalInOut(board.ESP_CS)
 36  esp32_ready = DigitalInOut(board.ESP_BUSY)
 37  esp32_reset = DigitalInOut(board.ESP_RESET)
 38  spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
 39  esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
 40  status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
 41  wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
 42  
 43  # These pins are used as both analog and digital! XL, XR and YU must be analog
 44  # and digital capable. YD just need to be digital
 45  ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
 46                                        board.TOUCH_YD, board.TOUCH_YU,
 47                                        calibration=((5200, 59000), (5800, 57000)),
 48                                        size=(320, 240))
 49  
 50  # Set this to your LIFX personal access token in secrets.py
 51  # (to obtain a token, visit: https://cloud.lifx.com/settings)
 52  lifx_token = secrets['lifx_token']
 53  
 54  # Initialize the LIFX API Helper
 55  lifx = adafruit_lifx.LIFX(wifi, lifx_token)
 56  
 57  # Set these to your LIFX light selector (https://api.developer.lifx.com/docs/selectors)
 58  lifx_lights = ['label:Lamp', 'label:Bedroom']
 59  # set default light properties
 60  current_light = lifx_lights[0]
 61  light_brightness = 1.0
 62  
 63  # Make the display context
 64  button_group = displayio.Group()
 65  board.DISPLAY.show(button_group)
 66  # preload the font
 67  print('loading font...')
 68  font = bitmap_font.load_font("/fonts/Arial-12.bdf")
 69  glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: '
 70  font.load_glyphs(glyphs)
 71  # button properties
 72  BUTTON_WIDTH = 60
 73  BUTTON_HEIGHT = 60
 74  buttons = []
 75  
 76  # button fill colors (from https://api.developer.lifx.com/docs/colors)
 77  button_colors = {'red':0xFF0000, 'white':0xFFFFFF,
 78                   'orange':0xFF9900, 'yellow':0xFFFF00,
 79                   'green':0x00FF00, 'blue':0x0000FF,
 80                   'purple':0x9900FF, 'pink': 0xFF00FF}
 81  
 82  print('loading buttons...')
 83  
 84  # list of color buttons and their properties
 85  color_btn = [
 86      {'name':'red', 'pos':(15, 80), 'color':button_colors['red']},
 87      {'name':'white', 'pos':(85, 80), 'color':button_colors['white']},
 88      {'name':'orange', 'pos':(155, 80), 'color':button_colors['orange']},
 89      {'name':'yellow', 'pos':(225, 80), 'color':button_colors['yellow']},
 90      {'name':'pink', 'pos':(15, 155), 'color':button_colors['pink']},
 91      {'name':'green', 'pos':(85, 155), 'color':button_colors['green']},
 92      {'name':'blue', 'pos':(155, 155), 'color':button_colors['blue']},
 93      {'name':'purple', 'pos':(225, 155), 'color':button_colors['purple']}
 94  ]
 95  
 96  # generate color buttons from color_btn list
 97  for i in color_btn:
 98      button = Button(x=i['pos'][0], y=i['pos'][1],
 99                      width=BUTTON_WIDTH, height=BUTTON_HEIGHT, name=i['name'],
100                      fill_color=i['color'], style=Button.ROUNDRECT)
101      buttons.append(button)
102  
103  # light property buttons and their properties
104  prop_btn = [
105      {'name':'onoff', 'pos':(15, 15), 'label':'on/off'},
106      {'name':'up', 'pos':(75, 15), 'label':'+'},
107      {'name':'down', 'pos':(135, 15), 'label':'-'},
108      {'name':'lamp', 'pos':(195, 15), 'label':'lamp'},
109      {'name':'room', 'pos':(245, 15), 'label':'room'}
110  ]
111  
112  # generate property buttons from prop_btn list
113  for i in prop_btn:
114      button = Button(name=i['name'], x=i['pos'][0], y=i['pos'][1],
115                      width=40, height=40, label=i['label'],
116                      label_font=font, style=Button.SHADOWROUNDRECT)
117      buttons.append(button)
118  
119  # add buttons to the group
120  for b in buttons:
121      button_group.append(b.group)
122  
123  while True:
124      touch = ts.touch_point
125      if touch:
126          for i, button in enumerate(buttons):
127              if button.contains(touch):
128                  button.selected = True
129                  if button.name == 'lamp':
130                      current_light = lifx_lights[0]
131                      print('Switching to ', current_light)
132                  elif button.name == 'room':
133                      current_light = lifx_lights[1]
134                      print('Switching to ', current_light)
135                  elif button.name == 'onoff':
136                      print('Toggling {0}...'.format(current_light))
137                      lifx.toggle_light(current_light)
138                  elif button.name == 'up':
139                      light_brightness += 0.25
140                      print('Setting {0} brightness to {1}'.format(current_light, light_brightness))
141                      lifx.set_brightness(current_light, light_brightness)
142                  elif button.name == 'down':
143                      light_brightness -= 0.25
144                      print('Setting {0} brightness to {1}'.format(current_light, light_brightness))
145                      lifx.set_brightness(current_light, light_brightness)
146                  else:
147                      print('Setting {0} color to {1}'.format(current_light, button.name))
148                      lifx.set_color(current_light, 'on', button.name, light_brightness)
149                  button.selected = False
150              else:
151                  button.selected = False