77from adafruit_esp32spi import adafruit_esp32spi
88import adafruit_requests as requests
99import digitalio
10- import displayio
1110import analogio
1211from adafruit_pyportal import PyPortal
1312from adafruit_display_shapes .circle import Circle
6261 print ("MAC addr:" , [hex (i ) for i in esp .MAC_address ])
6362
6463pyportal = PyPortal (esp = esp , external_spi = spi )
65- #light = analogio.AnalogIn(board.LIGHT)
6664
6765for ap in esp .scan_networks ():
6866 print ("\t %s\t \t RSSI: %d" % (str (ap ['ssid' ], 'utf-8' ), ap ['rssi' ]))
@@ -102,15 +100,15 @@ def create_text_areas(configs):
102100 return text_areas
103101
104102class Switch (object ):
105- def __init__ (self , pin , pyportal ):
103+ def __init__ (self , pin , my_pyportal ):
106104 self .switch = digitalio .DigitalInOut (pin )
107105 self .switch .direction = digitalio .Direction .OUTPUT
108106 rect = RoundRect (SWITCHX , SWITCHY , 31 , 60 , 16 , fill = 0x0 , outline = DISPLAY_COLOR , stroke = 3 )
109- pyportal .splash .append (rect )
107+ my_pyportal .splash .append (rect )
110108 self .circle_on = Circle (SWITCHX + 15 , SWITCHY + 16 , 10 , fill = 0x0 )
111- pyportal .splash .append (self .circle_on )
109+ my_pyportal .splash .append (self .circle_on )
112110 self .circle_off = Circle (SWITCHX + 15 , SWITCHY + 42 , 10 , fill = DISPLAY_COLOR )
113- pyportal .splash .append (self .circle_off )
111+ my_pyportal .splash .append (self .circle_off )
114112
115113 # turn switch on or off
116114 def enable (self , enable ):
@@ -124,7 +122,7 @@ def enable(self, enable):
124122 self .circle_off .fill = DISPLAY_COLOR
125123
126124 def toggle (self ):
127- if self .switch .value == True :
125+ if self .switch .value :
128126 self .enable (False )
129127 else :
130128 self .enable (True )
@@ -137,26 +135,28 @@ def status(self):
137135# See https://apidock.com/ruby/DateTime/strftime for full options
138136TIME_SERVICE_TIMESTAMP = '&fmt=%25s+%25z'
139137
140- class Clock (object ):
138+ class Clock (object , my_pyportal ):
141139 def __init__ (self ):
142140 self .low_light = False
143141 self .update_time = None
144142 self .snapshot_time = None
143+ self .pyportal = my_pyportal
144+ self .current_time = 0
145145 self .light = analogio .AnalogIn (board .LIGHT )
146146 text_area_configs = [dict (x = 0 , y = 110 , size = 72 , color = DISPLAY_COLOR , font = time_font ),
147147 dict (x = 260 , y = 165 , size = 18 , color = DISPLAY_COLOR , font = ampm_font ),
148148 dict (x = 10 , y = 40 , size = 18 , color = DISPLAY_COLOR , font = date_font )]
149149 self .text_areas = create_text_areas (text_area_configs )
150150 for ta in self .text_areas :
151- pyportal .splash .append (ta )
151+ self . pyportal .splash .append (ta )
152152
153153 def adjust_backlight (self , force = False ):
154154 """Check light level. Adjust the backlight and background image if it's dark."""
155155 if force or (self .light .value >= 1500 and self .low_light ):
156- pyportal .set_backlight (1.00 )
156+ self . pyportal .set_backlight (1.00 )
157157 self .low_light = False
158158 elif self .light .value <= 1000 and not self .low_light :
159- pyportal .set_backlight (0.1 )
159+ self . pyportal .set_backlight (0.1 )
160160 self .low_light = True
161161
162162 def tick (self , now ):
@@ -228,6 +228,8 @@ def get_local_timestamp(self, location=None):
228228 gc .collect ()
229229 return int (seconds + tzseconds )
230230
231+ # Define callback methods which are called when events occur
232+ # pylint: disable=unused-argument, redefined-outer-name
231233def connected (client , userdata , flags , rc ):
232234 # This function will be called when the client is connected
233235 # successfully to the broker.
@@ -263,7 +265,7 @@ def message(client, topic, message):
263265mqtt_client .on_message = message
264266
265267mqtt_client .connect ()
266- clock = Clock ()
268+ clock = Clock (pyportal )
267269switch = Switch (board .D4 , pyportal )
268270
269271second_timer = time .monotonic ()
0 commit comments