Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(
self, pin=None, initial_angle=0.0,
min_angle=-90, max_angle=90,
min_pulse_width=1/1000, max_pulse_width=2/1000,
frame_width=20/1000, pin_factory=None):
self._min_angle = min_angle
self._angular_range = max_angle - min_angle
if initial_angle is None:
initial_value = None
elif ((min_angle <= initial_angle <= max_angle) or
(max_angle <= initial_angle <= min_angle)):
initial_value = 2 * ((initial_angle - min_angle) / self._angular_range) - 1
else:
raise OutputDeviceBadValue(
"AngularServo angle must be between %s and %s, or None" %
(min_angle, max_angle))
super(AngularServo, self).__init__(
pin, initial_value, min_pulse_width, max_pulse_width, frame_width,
pin_factory=pin_factory
)
def _write(self, value):
if not 0 <= value <= 1:
raise OutputDeviceBadValue("PWM value must be between 0 and 1")
super(PWMOutputDevice, self)._write(value)
def value(self, value):
if value is None:
self.pwm_device.pin.frequency = None
elif -1 <= value <= 1:
freq = self.mid_tone.frequency * 2 ** (self.octaves * value)
self.pwm_device.pin.frequency = freq
self.pwm_device.value = 0.5
else:
raise OutputDeviceBadValue(
'TonalBuzzer value must be between -1 and 1, or None')
def value(self, value):
for component in value:
if not 0 <= component <= 1:
raise OutputDeviceBadValue(
'each RGB color component must be between 0 and 1')
if isinstance(self._leds[0], LED):
if component not in (0, 1):
raise OutputDeviceBadValue(
'each RGB color component must be 0 or 1 with non-PWM '
'RGBLEDs')
self._stop_blink()
for led, v in zip(self._leds, value):
led.value = v
def value(self, value):
if not -1 <= value <= 1:
raise OutputDeviceBadValue("Motor value must be between -1 and 1")
if value > 0:
self.forward(value)
elif value < 0:
self.backward(-value)
else:
self.stop()
def value(self, value):
if not -1 <= value <= 1:
raise OutputDeviceBadValue("Motor value must be between -1 and 1")
if value > 0:
try:
self.forward(value)
except ValueError as e:
raise OutputDeviceBadValue(e)
elif value < 0:
try:
self.backward(-value)
except ValueError as e:
raise OutputDeviceBadValue(e)
else:
self.stop()
def angle(self, angle):
if angle is None:
self.value = None
elif ((self.min_angle <= angle <= self.max_angle) or
(self.max_angle <= angle <= self.min_angle)):
self.value = (
self._value_range *
((angle - self._min_angle) / self._angular_range) +
self._min_value)
else:
raise OutputDeviceBadValue(
"AngularServo angle must be between %s and %s, or None" %
(self.min_angle, self.max_angle))
def value(self, value):
for component in value:
if not 0 <= component <= 1:
raise OutputDeviceBadValue(
'each RGB color component must be between 0 and 1')
if isinstance(self._leds[0], LED):
if component not in (0, 1):
raise OutputDeviceBadValue(
'each RGB color component must be 0 or 1 with non-PWM '
'RGBLEDs')
self._stop_blink()
for led, v in zip(self._leds, value):
led.value = v