Helper Functions

src.pico_power_management.helpers.check_list(value, name, expected_value)

Check if the value is one of the expected values of a list.

Parameters
  • value – The value to check.

  • name – The variable name.

  • min_value – expected list of values

Raises

ValueError – If the value is outside the specified range.

src.pico_power_management.helpers.check_range(value, name, min_value, max_value)

Check if the value is within a specified range.

Parameters
  • value – The value to check.

  • name – The variable name.

  • min_value – The minimum allowed value.

  • max_value – The maximum allowed value.

Raises

ValueError – If the value is outside the specified range.

src.pico_power_management.helpers.check_str(value, name, expected_value)

Check if the value is one of the expected strings.

Parameters
  • value – The value to check.

  • name – The variable name.

  • min_value – expected string value or list of values

Raises

ValueError – If the value is outside the specified range.

src.pico_power_management.helpers.check_type(value, name, expected_type)

Check if the value is of the expected data type.

Parameters
  • value – The value to check.

  • name – The variable name.

  • expected_type – The expected data type.

Raises

ValueError – If the value is not of the expected type.

src.pico_power_management.helpers.read_modify(read_data, modify_data, bit_mask)

Helper method to write data without editing.

The first parantheses clears fields that are being edited while others are untouched. The second parantheses restricts the modifying data to the bits we want edited. performing a bitwise OR then allows for the RMW cycle to be complete.

Parameters
  • read_data (int) – current data from the device

  • modify_data (int) – Data to be edited

  • modify_data – field mask of the data being edited.

Returns

write_data

Return type

int

Code

  1# -----------------------------------------
  2#                 NOTES 
  3# -----------------------------------------
  4
  5# Dieter Steinhauser
  6# 10/2023
  7# Helper methods
  8
  9# -----------------------------------------
 10#               IMPORTS
 11# -----------------------------------------
 12
 13
 14# -----------------------------------------
 15#                Class:
 16# -----------------------------------------
 17
 18def check_type(value, name, expected_type):
 19    """
 20    Check if the value is of the expected data type.
 21
 22    :param value: The value to check.
 23    :param name: The variable name.
 24    :param expected_type: The expected data type.
 25    :raises ValueError: If the value is not of the expected type.
 26    """
 27    if value is None:
 28        return None
 29
 30    if not isinstance(value, expected_type):
 31
 32        expected_type = [val.__name__ for val in expected_type] if isinstance(expected_type, (tuple, list)) else expected_type.__name__
 33        raise ValueError(f"Incorrect {name}: '{value}'. {name} is not of type {expected_type}")
 34    
 35
 36def check_range(value, name, min_value, max_value):
 37    """
 38    Check if the value is within a specified range.
 39
 40    :param value: The value to check.
 41    :param name: The variable name.
 42    :param min_value: The minimum allowed value.
 43    :param max_value: The maximum allowed value.
 44    :raises ValueError: If the value is outside the specified range.
 45    """
 46    if value is None:
 47        return None
 48
 49    if not (min_value <= value <= max_value):
 50        raise ValueError(f"Incorrect {name}: '{value}'. {name} is not within the range [{min_value}, {max_value}]")
 51    
 52
 53def check_str(value, name, expected_value):
 54    """
 55    Check if the value is one of the expected strings.
 56
 57    :param value: The value to check.
 58    :param name: The variable name.
 59    :param min_value: expected string value or list of values
 60    :raises ValueError: If the value is outside the specified range.
 61    """
 62    if value is None:
 63        return None
 64
 65    if isinstance(expected_value, str):
 66        if not value == expected_value:
 67            raise ValueError(f"Incorrect {name}: '{value}'. {name} is not the same as the expected string {expected_value}")
 68        
 69    if isinstance(expected_value, (list, tuple)):
 70        if value not in expected_value:
 71           raise ValueError(f"Incorrect {name}: '{value}'. {name} is not one of the expected strings {expected_value}") 
 72        
 73
 74
 75def check_list(value, name, expected_value):
 76    """
 77    Check if the value is one of the expected values of a list.
 78
 79    :param value: The value to check.
 80    :param name: The variable name.
 81    :param min_value: expected list of values
 82    :raises ValueError: If the value is outside the specified range.
 83    """
 84    if value is None:
 85        return None
 86
 87    if value not in expected_value:
 88        raise ValueError(f"Incorrect {name}: '{value}'. {name} is not one of the expected values {expected_value}") 
 89
 90
 91
 92
 93def read_modify(read_data, modify_data, bit_mask):
 94    """
 95    Helper method to write data without editing.
 96
 97    The first parantheses clears fields that are being edited while others are untouched.
 98    The second parantheses restricts the modifying data to the bits we want edited.
 99    performing a bitwise OR then allows for the RMW cycle to be complete.
100
101    :param read_data: current data from the device
102    :type read_data: int
103    :param modify_data: Data to be edited
104    :type modify_data: int
105    :param modify_data: field mask of the data being edited.
106    :type modify_data: int
107    :return: write_data
108    :rtype: int
109    """
110    write_data = (read_data & ~bit_mask) | (modify_data & bit_mask) 
111    return write_data
112    
113# def check_enum(value, name, key_enum):
114#     """
115#     Check if the value is a valid enum value.
116# 
117#     :param value: The value to check.
118#     :param name: The variable name.
119#     :param key_enum: A dictionary or enum containing valid values.
120#     :raises ValueError: If the value is not a valid enum value.
121#     """
122#     key_names = [key.name for key in key_enum] 
123# 
124#     if value is None:
125#         return None
126# 
127#     if not isinstance(value, (str, key_enum)):
128#         raise ValueError(f"{name} '{value}' is not valid. Possible values are: {key_names}")
129# 
130#     if isinstance(value, str):
131#         if value not in key_names:
132#             raise ValueError(f"{name} '{value}' is not valid. Possible strings are: {key_names}")
133#     
134#         return key_enum[value]
135#         
136#     if not isinstance(value, key_enum):
137#         raise ValueError(f"{name} '{value}' is not valid. Possible Enum objects are: {key_names}")
138#     
139#     return value
140#