Skip to content

Commit

Permalink
updating edit_eeprom function
Browse files Browse the repository at this point in the history
  • Loading branch information
CleaLCo committed Nov 5, 2024
1 parent 7cbe412 commit 2c10211
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions control/planktoscopehat/planktoscope/eeprom/eeprom.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,9 @@ def edit_eeprom(

for _, key in enumerate(keys):
if key in labels:
# Find index of label in labels list and corresponding address and length
label_index = labels.index(key)
current_addr = start_addr[label_index]
value = data[key]
data_to_write = [ord(char) for char in value] # Convert value to ASCII codes
current_addr = start_addr[labels.index(key)] # Find the index where key is in labels
data_to_write = self._prepare_data(labels, key, data_lengths, data)
remaining_data = data_to_write
data_length = data_lengths[label_index] # Maximum length allowed for this data

# Read current EEPROM data at this address for comparison
try:
Expand All @@ -121,10 +117,6 @@ def edit_eeprom(
print(f"Error during the reading process at address {current_addr:#04x}: {e}")
continue

# If new data is shorter than required length, pad with null bytes
if len(data_to_write) < data_length:
data_to_write.extend([0x00] * (data_length - len(data_to_write)))

# Begin writing process, ensuring page boundaries are respected
while remaining_data:
page_boundary = self.MAX_BLOCK_SIZE - (current_addr % self.MAX_BLOCK_SIZE)
Expand Down Expand Up @@ -154,3 +146,35 @@ def _get_memory_address_bytes(self, address: int) -> tuple[int, int]:
mem_addr_high = (address >> 8) & 0xFF # High byte of the address
mem_addr_low = address & 0xFF # Low byte of the address
return mem_addr_high, mem_addr_low

def _prepare_data(self, labels: list[str], key: str, data_lengths: list[int], data: dict[str, str]):
"""
Prepares the data for writing to the EEPROM by converting it to ASCII values
and padding it to match the expected length for the specified key.
Args:
key (str): The specific data label to prepare for writing.
labels (list): The list of all possible labels in the EEPROM data.
data_lengths (list): The list of expected data lengths for each label.
data (dict): The dictionary containing data values to be written to EEPROM.
Returns:
list: A list of ASCII integer values ready for writing to the EEPROM,
padded with 0x00 if shorter than the expected length.
"""

# Find the index of the key in labels to determine the correct data length
label_index = labels.index(key)
data_length = data_lengths[label_index] # Expected data length for this label

# Convert the value associated with the key to a list of ASCII values
value = data[key]
data_to_write = [ord(char) for char in value] # ASCII conversion for each character
print(data_to_write)

# If data is shorter than expected, pad it with 0x00 to match the required length
if len(data_to_write) < data_length:
data_to_write.extend([0x00] * (data_length - len(data_to_write)))

# Return the list of ASCII values, padded as necessary
return data_to_write

0 comments on commit 2c10211

Please sign in to comment.