99
1010_logger = logging .getLogger (__name__ )
1111
12+ class PinNameNoneError (Exception ):
13+ """Raised when None is passed"""
14+
15+ class PinNameNotFound (Exception ):
16+ """"Raised when name doesn't exist"""
17+
18+
1219class EdgePiGPIO (EdgePiGPIOExpander , EdgePiGPIOChip ):
1320 '''
1421 A class used to represent the GPIO Expander configuration for an I2C Device.
@@ -20,8 +27,11 @@ def __init__(self):
2027 EdgePiGPIOExpander .__init__ (self )
2128 EdgePiGPIOChip .__init__ (self )
2229
23- # TODO: add generic functions read, write, set, clear methods. depending on the pin name,
24- # the proper methods will get called
30+ def __pin_name_check (self , pin_name : str = None ):
31+ if pin_name is None :
32+ raise PinNameNoneError (f'Missing Pin name: { pin_name } ' )
33+ if pin_name not in self .expander_pin_dict and pin_name not in self .gpiochip_pins_dict :
34+ raise PinNameNotFound (f'The following pin name: { pin_name } is not found' )
2535
2636 def read_pin_state (self , pin_name : str = None ):
2737 """
@@ -31,9 +41,97 @@ def read_pin_state(self, pin_name: str = None):
3141 return
3242 state (bool): True/False depending on High/Low
3343 """
44+ state = None
45+ self .__pin_name_check (pin_name )
46+ if pin_name in self .expander_pin_dict :
47+ state = self .read_expander_pin (pin_name )
48+ if pin_name in self .gpiochip_pins_dict :
49+ state = self .read_gpio_pin_state (pin_name )
50+ return state
51+
52+
53+ def set_pin_state (self , pin_name : str = None ):
54+ """
55+ Set corresponding pin state to high
56+ Args:
57+ pin_name(str): name of the pin will be passed as Enum
58+ return:
59+ N/A
60+ """
61+ self .__pin_name_check (pin_name )
62+ if pin_name in self .expander_pin_dict :
63+ self .set_expander_pin (pin_name )
64+ if pin_name in self .gpiochip_pins_dict :
65+ self .write_gpio_pin_state (pin_name , True )
66+
67+ def clear_pin_state (self , pin_name : str = None ):
68+ """
69+ Clearcorresponding pin state to high
70+ Args:
71+ pin_name(str): name of the pin will be passed as Enum
72+ return:
73+ N/A
74+ """
75+ self .__pin_name_check (pin_name )
76+ if pin_name in self .expander_pin_dict :
77+ self .clear_expander_pin (pin_name )
78+ if pin_name in self .gpiochip_pins_dict :
79+ self .write_gpio_pin_state (pin_name , False )
80+
81+ def get_pin_direction (self , pin_name : str = None ):
82+ """
83+ Get GPIO pin direction
84+ Args:
85+ pin_name(str): name of the pin
86+ Return:
87+ direction (bool): True if direction is input, False if direction is output
88+ """
89+ direction = None
90+ self .__pin_name_check (pin_name )
91+ if pin_name in self .expander_pin_dict :
92+ direction = self .get_expander_pin_direction (pin_name )
93+ if pin_name in self .gpiochip_pins_dict :
94+ direction = bool (self .gpiochip_pins_dict [pin_name ].dir == "in" )
95+ return direction
96+
97+ def set_pin_direction_in (self , pin_name : str = None ):
98+ """
99+ Set GPIO pin direction to input
100+ Args:
101+ pin_name(str): name of the pin
102+ Return:
103+ N/A
104+ """
105+ self .__pin_name_check (pin_name )
106+ if pin_name in self .expander_pin_dict :
107+ self .set_expander_pin_direction_in (pin_name )
108+ if pin_name in self .gpiochip_pins_dict :
109+ self .set_gpio_pin_dir (pin_name , True )
110+
111+ def set_pin_direction_out (self , pin_name : str = None ):
112+ """
113+ Set GPIO pin direction to output
114+ Args:
115+ pin_name(str): name of the pin
116+ Return:
117+ N/A
118+ """
119+ self .__pin_name_check (pin_name )
120+ if pin_name in self .expander_pin_dict :
121+ self .set_expander_pin_direction_out (pin_name )
122+ if pin_name in self .gpiochip_pins_dict :
123+ self .set_gpio_pin_dir (pin_name , False )
124+
125+ def toggle_pin (self , pin_name : str = None ):
126+ """
127+ Toggle GPIO pin
128+ Args:
129+ pin_name (str): name of the pin
130+ Return:
131+ N/A
132+ """
133+ self .__pin_name_check (pin_name )
34134 if pin_name in self .expander_pin_dict :
35- return self .read_expander_pin (pin_name )
135+ self .toggle_expander_pin (pin_name )
36136 if pin_name in self .gpiochip_pins_dict :
37- return self .read_gpio_pin_state (pin_name )
38- _logger .error ("pin_name doesn't exists" )
39- return None
137+ self .toggle_gpio_pin_state (pin_name )
0 commit comments