X-Git-Url: https://git.ayoreis.com/hackapet/Adafruit_Blinka.git/blobdiff_plain/9a6f975a4505c02859d1c57ef8cb3dc1195db23f..173411fa58ff885d28703c69125a0e90db6cf0bf:/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py diff --git a/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py b/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py index c9c15a3..de6e181 100644 --- a/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py +++ b/src/adafruit_blinka/microcontroller/generic_agnostic_board/pin.py @@ -4,8 +4,10 @@ """generic_agnostic_board pin interface""" import random + class Pin: """A basic Pin class for use with generic_agnostic_board""" + # pin modes OUT = 0 IN = 1 @@ -16,42 +18,47 @@ class Pin: HIGH = 1 def return_toggle(self): - """Returns the pin's expected value, toggling between True and False""" - toggle_state = not self.previous_value - return toggle_state + """Returns the pin's expected value, toggling between True and False""" + toggle_state = not self.previous_value + return toggle_state def return_false(self): - """Returns the pin's expected value, False""" - return False + """Returns the pin's expected value, False""" + return False def return_true(self): - """Returns the pin's expected value, True""" - return True + """Returns the pin's expected value, True""" + return True def return_random_int(self): - """Returns a random integer""" - return random.randint(0, 65535) + """Returns a random integer""" + return random.randint(0, 65535) def return_fixed_int_pi(self): - """Returns the first five digits of Pi, 31415""" - return 31415 - - expected_pin_behavior = { - 'Dx_INPUT_TRUE': return_true, - 'Dx_INPUT_FALSE': return_false, - 'Dx_INPUT_TRUE_THEN_FALSE': return_toggle, - 'Dx_INPUT_TRUE_PULL_UP': return_true, - 'Dx_INPUT_TRUE_PULL_DOWN': return_true, - 'Dx_OUTPUT_TRUE': return_true, - 'Dx_OUTPUT_FALSE': return_true, - 'Ax_INPUT_RAND_INT': return_random_int - } + """Returns the first five digits of Pi, 31415""" + return 31415 def __init__(self, pin_id=None): - self.id = pin_id - self._mode = None - self.previous_value = None - self.current_value = None + self.id = pin_id + self._mode = None + self.previous_value = None + self.current_value = None + + # mapping of pin definition names to expected behavior + self.pin_behavior = { + 0: self.return_true, # Dx_INPUT_TRUE + 1: self.return_false, # Dx_INPUT_FALSE + 2: self.return_true, # Dx_INPUT_TRUE_PULL_UP + 3: self.return_true, # Dx_INPUT_TRUE_PULL_DOWN + 4: self.return_true, # Dx_OUTPUT_TRUE + 5: self.return_false, # Dx_OUTPUT_FALSE + 6: self.return_true, # NEOPIXEL + 7: self.return_random_int, # Ax_INPUT_RAND_INT + 8: self.return_fixed_int_pi, # Ax_INPUT_FIXED_INT_PI + 9: self.return_true, # Ax_OUTPUT_WAVE_SINE + 10: self.return_true, # Ax_OUTPUT_WAVE_SAWTOOTH + # Add other mappings as needed + } def init(self, mode=IN, pull=None): """Initialize the Pin""" @@ -62,15 +69,16 @@ class Pin: self._mode = mode def write(self, new_value): - """Saves the new_value to the pin for subsequent calls to .value""" - self.previous_value = self.current_value - self.current_value = new_value + """Saves the new_value to the pin for subsequent calls to .value""" + self.previous_value = self.current_value + self.current_value = new_value def read(self): - """Returns the pin's expected value.""" - self.previous_value = self.current_value - self.current_value = self.expected_pin_behavior.get(self.pin_id) - return self.current_value + """Returns the pin's expected value.""" + self.previous_value = self.current_value + # perform a lookup on the pin_behavior dict to get the value + self.current_value = self.pin_behavior.get(self.id)() + return self.current_value def value(self, val=None): """Set or return the Pin Value""" @@ -101,6 +109,7 @@ class Pin: "No action for mode {} with value {}".format(self._mode, val) ) + # create pin instances for each pin D0 = Pin(0) D1 = Pin(1)