7
7
from adafruit_esp32spi import adafruit_esp32spi
8
8
import adafruit_requests as requests
9
9
import digitalio
10
- import displayio
11
10
import analogio
12
11
from adafruit_pyportal import PyPortal
13
12
from adafruit_display_shapes .circle import Circle
62
61
print ("MAC addr:" , [hex (i ) for i in esp .MAC_address ])
63
62
64
63
pyportal = PyPortal (esp = esp , external_spi = spi )
65
- #light = analogio.AnalogIn(board.LIGHT)
66
64
67
65
for ap in esp .scan_networks ():
68
66
print ("\t %s\t \t RSSI: %d" % (str (ap ['ssid' ], 'utf-8' ), ap ['rssi' ]))
@@ -102,15 +100,15 @@ def create_text_areas(configs):
102
100
return text_areas
103
101
104
102
class Switch (object ):
105
- def __init__ (self , pin , pyportal ):
103
+ def __init__ (self , pin , my_pyportal ):
106
104
self .switch = digitalio .DigitalInOut (pin )
107
105
self .switch .direction = digitalio .Direction .OUTPUT
108
106
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 )
110
108
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 )
112
110
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 )
114
112
115
113
# turn switch on or off
116
114
def enable (self , enable ):
@@ -124,7 +122,7 @@ def enable(self, enable):
124
122
self .circle_off .fill = DISPLAY_COLOR
125
123
126
124
def toggle (self ):
127
- if self .switch .value == True :
125
+ if self .switch .value :
128
126
self .enable (False )
129
127
else :
130
128
self .enable (True )
@@ -137,26 +135,28 @@ def status(self):
137
135
# See https://apidock.com/ruby/DateTime/strftime for full options
138
136
TIME_SERVICE_TIMESTAMP = '&fmt=%25s+%25z'
139
137
140
- class Clock (object ):
138
+ class Clock (object , my_pyportal ):
141
139
def __init__ (self ):
142
140
self .low_light = False
143
141
self .update_time = None
144
142
self .snapshot_time = None
143
+ self .pyportal = my_pyportal
144
+ self .current_time = 0
145
145
self .light = analogio .AnalogIn (board .LIGHT )
146
146
text_area_configs = [dict (x = 0 , y = 110 , size = 72 , color = DISPLAY_COLOR , font = time_font ),
147
147
dict (x = 260 , y = 165 , size = 18 , color = DISPLAY_COLOR , font = ampm_font ),
148
148
dict (x = 10 , y = 40 , size = 18 , color = DISPLAY_COLOR , font = date_font )]
149
149
self .text_areas = create_text_areas (text_area_configs )
150
150
for ta in self .text_areas :
151
- pyportal .splash .append (ta )
151
+ self . pyportal .splash .append (ta )
152
152
153
153
def adjust_backlight (self , force = False ):
154
154
"""Check light level. Adjust the backlight and background image if it's dark."""
155
155
if force or (self .light .value >= 1500 and self .low_light ):
156
- pyportal .set_backlight (1.00 )
156
+ self . pyportal .set_backlight (1.00 )
157
157
self .low_light = False
158
158
elif self .light .value <= 1000 and not self .low_light :
159
- pyportal .set_backlight (0.1 )
159
+ self . pyportal .set_backlight (0.1 )
160
160
self .low_light = True
161
161
162
162
def tick (self , now ):
@@ -228,6 +228,8 @@ def get_local_timestamp(self, location=None):
228
228
gc .collect ()
229
229
return int (seconds + tzseconds )
230
230
231
+ # Define callback methods which are called when events occur
232
+ # pylint: disable=unused-argument, redefined-outer-name
231
233
def connected (client , userdata , flags , rc ):
232
234
# This function will be called when the client is connected
233
235
# successfully to the broker.
@@ -263,7 +265,7 @@ def message(client, topic, message):
263
265
mqtt_client .on_message = message
264
266
265
267
mqtt_client .connect ()
266
- clock = Clock ()
268
+ clock = Clock (pyportal )
267
269
switch = Switch (board .D4 , pyportal )
268
270
269
271
second_timer = time .monotonic ()
0 commit comments