38 if not os.path.isfile(
'/proc/cpuinfo'):
44 with open(
'/proc/cpuinfo',
'r') as infile: 45 cpuinfo = infile.read() 47 match = re.search(
'^Hardware\s+:\s+(\w+)$', cpuinfo,
48 flags=re.MULTILINE | re.IGNORECASE)
52 if match.group(1) ==
'BCM2708':
55 elif match.group(1) ==
'BCM2709':
70 import RPi.GPIO
as GPIO
72 if ahioDriverInfo.AVAILABLE:
73 print(
"RPi.GPIO not installed: sudo apt-get install python3-rpi.gpio")
74 ahioDriverInfo.AVAILABLE = false
76 ahioDriverInfo.AVAILABLE = false
77 print(
"You probably need superuser privileges. Try running with sudo")
84 'D3 D5 D7 D8 D10 D12 D13 D15 D16 D18 D19 D21 D22 D23 D24 D26')
90 if ahioDriverInfo.AVAILABLE:
91 GPIO.setmode(GPIO.BOARD)
92 for pin
in Driver.Pins:
98 def __exit__(self, exc_type, exc_value, traceback):
101 def __create_pin_info(self, pid):
118 def __pin_to_int(self, pin):
122 return int(pin.name.replace(
'D',
''))
124 def __clamp(self, value, min, max):
125 return sorted((min, value, max))[1]
132 def _set_pin_direction(self, pin, direction):
134 if direction == ahio.Direction.Input:
135 GPIO.setup(pin, GPIO.IN)
137 GPIO.setup(pin, GPIO.OUT, initial=GPIO.LOW)
138 self.
__pwm.pop(pin,
None)
140 def _pin_direction(self, pin):
142 function = GPIO.gpio_function(pin)
143 if function == GPIO.IN:
144 return ahio.Direction.Input
145 elif function == GPIO.OUT
or function == GPIO.PWM:
146 return ahio.Direction.Output
150 def _set_pin_type(self, pin, ptype):
151 if ptype != ahio.PortType.Digital:
152 raise RuntimeError(
'Raspberry Pi pins can only be used as Digital')
154 def _pin_type(self, pin):
155 return ahio.PortType.Digital
157 def _write(self, pin, value, pwm):
162 if type(value)
is int
or type(value)
is float:
163 value = int(100 * self.
__clamp(float(value), 0.0, 1.0))
164 p = self.
__pwm.get(pin,
None)
169 p = GPIO.PWM(pin, freq)
173 'Value should be a float or int between 0 and 1')
175 if type(value)
is ahio.LogicValue:
177 value = GPIO.HIGH
if value == lv.High
else GPIO.LOW
178 GPIO.output(pin, value)
180 raise TypeError(
'Value should be of type ahio.LogicValue')
182 def _read(self, pin):
184 return GPIO.input(pin)
189 def _set_analog_reference(self, reference, pin):
190 raise RuntimeError(
'Raspberry Pi does not have analog pins')
192 def _analog_reference(self, pin):
193 raise RuntimeError(
'Raspberry Pi does not have analog pins')
195 def _set_pwm_frequency(self, frequency, pin):
199 pwm = self.
__pwm.get(pin,
None)
201 pwm.ChangeFrequency(frequency)
203 for pin
in Driver.Pins:
def __clamp(self, value, min, max)
def __create_pin_info(self, pid)
def pi_version()
Detect the version of the Raspberry Pi.
def __pin_to_int(self, pin)
Abstract class containing information about the driver.
def _pin_direction(self, pin)
dictionary __pwm_frequency
def __exit__(self, exc_type, exc_value, traceback)
def _set_pin_direction(self, pin, direction)
Contains abstract classes that should be implemented by drivers.
def _set_pwm_frequency(self, frequency, pin)
def analog_references(self)