Grinch Refactor

This commit is contained in:
2025-10-31 21:24:07 -06:00
parent d0a8df25f6
commit 4e131cd5e4
7 changed files with 202 additions and 197 deletions

View File

@@ -1,5 +1,5 @@
from enum import STRICT, IntEnum
from typing import NamedTuple, Optional
from typing import Optional
class UpdateMethod(IntEnum, boundary=STRICT):
@@ -9,7 +9,7 @@ class UpdateMethod(IntEnum, boundary=STRICT):
FREEZE = 4
class GrinchRamData(NamedTuple):
class GrinchRamData:
"""A Representation of an update to RAM data for The Grinch.
ram_address (int): The RAM address that we are updating, usually passed in with hex, representation (0x11111111)
@@ -23,22 +23,25 @@ class GrinchRamData(NamedTuple):
ram_address: int
value: Optional[int] = None # none is empty/null
# Either set or add either hex or unsigned values through Client.py
# Hex uses 0x00, unsigned are base whole numbers
binary_bit_pos: Optional[int] = None
byte_size: int = 1
endian = "little"
update_method: UpdateMethod = UpdateMethod.SET
min_count: int = 0
max_count: int = 255
ram_area = "MainRAM"
def __init__(
self,
ram_address: int,
value: Optional[int],
byte_size: int,
update_method: UpdateMethod,
min_count: int,
max_count: int,
value: Optional[int] = None,
binary_bit_pos: int = -1,
byte_size: int = 1,
update_method: UpdateMethod = UpdateMethod.SET,
min_count: int = -1,
max_count: int = -1,
endian: str = "little",
ram_area: str = "MainRAM",
):
self.ram_address = ram_address
@@ -47,7 +50,10 @@ class GrinchRamData(NamedTuple):
else:
self.value = 1
if byte_size:
if binary_bit_pos > -1:
self.binary_bit_pos = bin
if byte_size > 0:
self.byte_size = byte_size
if update_method:
@@ -59,8 +65,26 @@ class GrinchRamData(NamedTuple):
if max_count and max_count > -1:
self.max_count = max_count
elif max_count and max_count > ((2 ** (self.byte_size * 8)) - 1):
raise ValueError(
"max_count cannot be larger than the RAM addresses max possible value"
)
raise ValueError("max_count cannot be larger than the RAM addresses max possible value")
else:
self.max_count = (2 ** (self.byte_size * 8)) - 1
self.endian = endian
self.ram_area = ram_area
# Error Handling
if self.value and self.value > self.max_count:
raise ValueError(
f"Value passed in is greater than max_count.\n\nRAM Address: {self.ram_address}\nValue: {self.value}\nMax Count: {self.max_count}"
)
if self.value and self.value < self.min_count:
raise ValueError(
f"Value passed in is lower than min_count.\n\nRAM Address: {self.ram_address}\nValue: {self.value}\nMin Count: {self.max_count}"
)
if self.binary_bit_pos and self.update_method not in [UpdateMethod.SET, UpdateMethod.FREEZE]:
raise ValueError(f"binary_bit_position can only be passed in if update_method is SET or FREEZE")
if self.binary_bit_pos and self.value not in [0, 1]:
raise ValueError(f"value must be 0 or 1 if using binary_bit_position")