Skip to content

Commit f2ee9e5

Browse files
committed
Temp all_faces Fix
This is a temporary reimplementation of all_faces in the `repl.py` to allow people to continue testing while we refactor these in PySquared
1 parent 1d751b9 commit f2ee9e5

File tree

1 file changed

+96
-3
lines changed

1 file changed

+96
-3
lines changed

repl.py

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,22 @@
1818
except ImportError:
1919
import board
2020

21+
try:
22+
from typing import Union
23+
except Exception:
24+
pass
25+
2126
import os
2227

2328
import lib.pysquared.functions as functions
2429
import lib.pysquared.nvm.register as register
30+
from lib.adafruit_drv2605 import DRV2605 ### This is Hacky V5a Devel Stuff###
2531
from lib.adafruit_mcp230xx.mcp23017 import (
2632
MCP23017, ### This is Hacky V5a Devel Stuff###
2733
)
2834
from lib.adafruit_mcp9808 import MCP9808 ### This is Hacky V5a Devel Stuff###
2935
from lib.adafruit_tca9548a import TCA9548A ### This is Hacky V5a Devel Stuff###
36+
from lib.adafruit_veml7700 import VEML7700 ### This is Hacky V5a Devel Stuff###
3037

3138
# from lib.pysquared.Big_Data import AllFaces ### This is Hacky V5a Devel Stuff###
3239
from lib.pysquared.cdh import CommandDataHandler
@@ -258,9 +265,95 @@ def all_faces_on():
258265

259266
tca = TCA9548A(i2c1, address=int(0x77))
260267

261-
# all_faces = AllFaces(tca, logger)
268+
269+
### This is Hacky V5a Devel Stuff###
270+
class Face:
271+
def __init__(self, add: int, pos: str, tca: TCA9548A, logger: Logger) -> None:
272+
self.tca: TCA9548A = tca
273+
self.address: int = add
274+
self.position: str = pos
275+
self.logger: Logger = logger
276+
277+
# Use tuple instead of list for immutable data
278+
self.senlist: tuple = ()
279+
# Define sensors based on position using a dictionary lookup instead of if-elif chain
280+
sensor_map: dict[str, tuple[str, ...]] = {
281+
"x+": ("MCP", "VEML", "DRV"),
282+
"x-": ("MCP", "VEML"),
283+
"y+": ("MCP", "VEML", "DRV"),
284+
"y-": ("MCP", "VEML"),
285+
"z-": ("MCP", "VEML", "DRV"),
286+
}
287+
self.senlist: tuple[str, ...] = sensor_map.get(pos, ())
288+
289+
# Initialize sensor states dict only with needed sensors
290+
self.sensors: dict[str, bool] = {sensor: False for sensor in self.senlist}
291+
292+
# Initialize sensor objects as None
293+
self.mcp = None
294+
self.veml = None
295+
self.drv = None
296+
297+
def sensor_init(self, senlist, address) -> None:
298+
if "MCP" in senlist:
299+
try:
300+
self.mcp: MCP9808 = MCP9808(self.tca[address], address=27)
301+
self.sensors["MCP"] = True
302+
except Exception as e:
303+
self.logger.error("Error Initializing Temperature Sensor", e)
304+
305+
if "VEML" in senlist:
306+
try:
307+
self.veml: VEML7700 = VEML7700(self.tca[address])
308+
self.sensors["VEML"] = True
309+
except Exception as e:
310+
self.logger.error("Error Initializing Light Sensor", e)
311+
312+
if "DRV" in senlist:
313+
try:
314+
self.drv: DRV2605 = DRV2605(self.tca[address])
315+
self.sensors["DRV"] = True
316+
except Exception as e:
317+
self.logger.error("Error Initializing Motor Driver", e)
318+
319+
320+
class AllFaces:
321+
def __init__(self, tca: TCA9548A, logger: Logger) -> None:
322+
self.tca: TCA9548A = tca
323+
self.faces: list[Face] = []
324+
self.logger: Logger = logger
325+
326+
# Create faces using a loop instead of individual variables
327+
positions: list[tuple[str, int]] = [
328+
("y+", 0),
329+
("y-", 1),
330+
("x+", 2),
331+
("x-", 3),
332+
("z-", 4),
333+
]
334+
for pos, addr in positions:
335+
face: Face = Face(addr, pos, tca, self.logger)
336+
face.sensor_init(face.senlist, face.address)
337+
self.faces.append(face)
338+
339+
def face_test_all(self) -> list[list[float]]:
340+
results: list[list[float]] = []
341+
for face in self.faces:
342+
if face:
343+
try:
344+
temp: Union[float, None] = (
345+
face.mcp.temperature if face.sensors.get("MCP") else None
346+
)
347+
light: Union[float, None] = (
348+
face.veml.lux if face.sensors.get("VEML") else None
349+
)
350+
results.append([temp, light])
351+
except Exception:
352+
results.append([None, None])
353+
return results
354+
355+
356+
all_faces = AllFaces(tca, logger)
262357

263358
## Onboard Temp Sensor ##
264359
mcp = MCP9808(i2c1, address=30) # Not working for some reason
265-
266-
### This is Hacky V5a Devel Stuff###

0 commit comments

Comments
 (0)