|
18 | 18 | except ImportError: |
19 | 19 | import board |
20 | 20 |
|
| 21 | +try: |
| 22 | + from typing import Union |
| 23 | +except Exception: |
| 24 | + pass |
| 25 | + |
21 | 26 | import os |
22 | 27 |
|
23 | 28 | import lib.pysquared.functions as functions |
24 | 29 | import lib.pysquared.nvm.register as register |
| 30 | +from lib.adafruit_drv2605 import DRV2605 ### This is Hacky V5a Devel Stuff### |
25 | 31 | from lib.adafruit_mcp230xx.mcp23017 import ( |
26 | 32 | MCP23017, ### This is Hacky V5a Devel Stuff### |
27 | 33 | ) |
28 | 34 | from lib.adafruit_mcp9808 import MCP9808 ### This is Hacky V5a Devel Stuff### |
29 | 35 | 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### |
30 | 37 |
|
31 | 38 | # from lib.pysquared.Big_Data import AllFaces ### This is Hacky V5a Devel Stuff### |
32 | 39 | from lib.pysquared.cdh import CommandDataHandler |
@@ -258,9 +265,95 @@ def all_faces_on(): |
258 | 265 |
|
259 | 266 | tca = TCA9548A(i2c1, address=int(0x77)) |
260 | 267 |
|
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) |
262 | 357 |
|
263 | 358 | ## Onboard Temp Sensor ## |
264 | 359 | mcp = MCP9808(i2c1, address=30) # Not working for some reason |
265 | | - |
266 | | -### This is Hacky V5a Devel Stuff### |
|
0 commit comments