class RPi: PIN_NODE_RST = 27 PIN_START_BTN = 23 PIN_EXIT_EN = 24 def __init__(self, onStart=None, onStop=None): if not RPI_OK: return self.rstPin = DigitalOutputDevice(self.PIN_NODE_RST) #self.btnPin = DigitalInputDevice(self.PIN_START_BTN) self.btnPin = Button(self.PIN_START_BTN) self.exitPin = DigitalOutputDevice(self.PIN_EXIT_EN, active_high=True) #self.outPin.source = self.btnPin.values if onStart: self.btnPin.when_pressed = onStart if onStop: self.btnPin.when_released = onStop return def resetNetwork(self): if not RPI_OK: return self.rstPin.blink(n=1, on_time=0.1, off_time=0.1, background=True) return def openDoors(self, on): if not RPI_OK: return if on: self.exitPin.on() else: self.exitPin.off() return def gameEnabled(self): if not RPI_OK: return False return self.btnPin.is_active
class Ringer: def __init__(self,gpio): self.output=DigitalOutputDevice(gpio) def start_ringing(self): print("start ringing") self.output.blink(on_time=1.5,off_time=.5) def stop_ringing(self): print("stop ringing") self.output.off()
class CountDisplay: def __init__(self, reset_pin, increment_pin, change_time=0, background=True): self._reset_pin = OutputDevice(reset_pin) self._increment_pin = DigitalOutputDevice(increment_pin) self.change_time = change_time self.background = background self.reset() def reset(self): self._reset_pin.on() self._reset_pin.off() self._value = 0 @property def value(self): return self._value @value.setter def value(self, newval): if newval < 0: raise ValueError("CountDisplay cannot show negative numbers.") if newval > self.value: self._increment_pin.blink( on_time=self.change_time / 2, off_time=self.change_time / 2, n=newval - self.value, background=self.background, ) elif newval < self.value: self.reset() self._increment_pin.blink( on_time=self.change_time / 2, off_time=self.change_time / 2, n=newval, background=self.background, ) self._value = newval
class RemotePowerModule(BaseServer): def __init__(self, pwr_pin, res_pin, led_pin): super().__init__() self._pwr_sw = DigitalOutputDevice(pwr_pin) self._res_sw = DigitalOutputDevice(res_pin) self._pwr_led = DigitalInputDevice(led_pin) @property def power_status(self) -> Dict: return {"power": "on" if self._pwr_led.value == 1 else "off"} def power_on(self): if self.power_status["power"] == "off": self._pwr_sw.blink(on_time=0.5, n=1) def power_off(self): if self.power_status["power"] == "on": self._pwr_sw.blink(on_time=0.5, n=1) def reset(self): self._res_sw.blink(on_time=0.5, n=1)
from gpiozero import DigitalOutputDevice from signal import pause print("blink.py start") led_pin = DigitalOutputDevice(7) led_pin.blink(on_time=1, off_time=1) while True: print("Hello World") pause()
really_close_to_target = target - 5 if not power: background = make_blank_background("black") relay.off() elif value == target: background = make_blank_background("green") relay.off() elif value >= target: background = make_blank_background("red") relay.off() elif value >= really_close_to_target: background = make_blank_background("turquoise") relay.blink(on_time=2, off_time=4, n=1, background=False) elif value >= close_to_target: background = make_blank_background("aqua") relay.blink(on_time=2, off_time=2, n=1, background=False) else: background = make_blank_background("blue") relay.on() image = write_white_text(background, (str(value)) + u"\u00B0", 0, big_font) image = write_gray_text(background, (str(target)) + u"\u00B0", 150, small_font) lcd.display(image)
from gpiozero import DigitalOutputDevice from signal import pause pin_led_yellow = DigitalOutputDevice(25) pin_led_yellow.blink(on_time=1, off_time=1) pause()
def main(): """object detection camera inference example.""" parser = argparse.ArgumentParser() countdown = 20 parser.add_argument( '--num_frames', '-n', type=int, dest='num_frames', default=None, help='Sets the number of frames to run for, otherwise runs forever.') args = parser.parse_args() servoX = AngularServo(PIN_B) servoY = AngularServo(PIN_A) relay = DigitalOutputDevice(PIN_C, active_high=True, initial_value=True) #relay.blink(n=1) relay.blink(on_time=0.05, n=1) # Forced sensor mode, 1640x1232, full FoV. See: # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes # This is the resolution inference run on. with PiCamera(sensor_mode=4, resolution=(1640, 1232), framerate=10) as camera: camera.start_preview() # Annotator renders in software so use a smaller size and scale results # for increased performace. annotator = Annotator(camera, dimensions=(320, 240)) scale_x = 320 / 1640 scale_y = 240 / 1232 # Incoming boxes are of the form (x, y, width, height). Scale and # transform to the form (x1, y1, x2, y2). def transform(bounding_box): x, y, width, height = bounding_box return (scale_x * x, scale_y * y, scale_x * (x + width), scale_y * (y + height)) def textXYTransform(bounding_box): x, y, width, height = bounding_box return (scale_x * x, scale_y * y) with CameraInference(object_detection.model()) as inference: for result in inference.run(): objs = object_detection.get_objects(result, 0.3) annotator.clear() for obj in objs: # blue for person, green for cat, purple for dog, red for anything else outlineColor = "blue" if obj.kind == 1 else "green" if obj.kind == 2 else "purple" if obj.kind == 3 else "red" print(obj.kind) tBoundingBox = transform(obj.bounding_box) annotator.bounding_box(tBoundingBox, fill=0, outline=outlineColor) annotator.text( textXYTransform(obj.bounding_box), "person" if obj.kind == 1 else "cat" if obj.kind == 2 else "dog" if obj.kind == 3 else "other", color=outlineColor) if len(objs) == 1: x1, y1, x2, y2 = transform(obj.bounding_box) midX = ((x2 - x1) / 2) + x1 midY = ((y2 - y1) / 2) + y1 servoPosX = remap(midX, 0, 320, 75, -75) servoPosY = remap(midY, 0, 240, -90, 80) # 90 is low, -90 is high servoPosX = min(90, servoPosX) servoPosX = max(-90, servoPosX) servoPosY = min(90, servoPosY) servoPosY = max(-90, servoPosY) print("x", midX, servoPosX) print("y", midY, servoPosY) servoX.angle = servoPosX servoY.angle = servoPosY countdown -= 1 if countdown == -1: # squirt annotator.text((midX, midY), "Squirt!!", color=outlineColor) relay.blink(on_time=0.5, n=1) countdown = 20 else: annotator.text((midX, midY), str(countdown), color=outlineColor) if len(objs) == 0: countdown = 20 annotator.update() camera.stop_preview()
service_classes=[uuid, SERIAL_PORT_CLASS], profiles=[SERIAL_PORT_PROFILE], # protocols = [ OBEX_UUID ] ) print "Waiting for connection on RFCOMM channel %d" % port client_sock, client_info = server_sock.accept() print "Accepted connection from ", client_info try: while True: data = client_sock.recv(1024) if (data == "UP"): print("UP Key") frontward.blink(on_time=0.5, n=1) elif (data == "DOWN"): print("DOWN Key") backward.blink(on_time=0.5, n=1) elif (data == "MOTOR"): motor.on() print(motor.value) motor.off() elif len(data) == 0: break print "received [%s]" % data except IOError: pass print "disconnected"
class iodevice: def __init__(self, pin: int, mode: iostate, pin_factory = None, pull_up_in: bool = False, active_state_in = None, bounce_time_in = None, active_high_out : bool = True, initial_value_out = False): self.pin = pin self.mode = mode self.device = None self.current_value = initial_value_out self.pin_factory = pin_factory self.active_state_in = active_state_in self.bounce_time_in = None self.active_high_out = active_high_out self.pull_up_in = pull_up_in if (mode == iostate.Input): self.device = DigitalInputDevice(self.pin, pull_up = self.pull_up_in, pin_factory = self.pin_factory) elif (mode == iostate.Output): self.device = DigitalOutputDevice(self.pin, active_high = True, initial_value = self.current_value, pin_factory = self.pin_factory) def state(self): value = self.value() mode = 'O' if self.mode == iostate.Output else 'I' if self.mode == iostate.Input else 'F' val = '-' if self.mode == iostate.Float else '?' if value is None else '1' if value else '0' resistor = ' ' if self.mode == iostate.Float or self.mode == iostate.Output \ else '-' if self.device.pull_up is None \ else 'u' if self.device.pull_up else 'd' return f'({self.pin:2})={mode}{val}{resistor}' def value(self): if (self.mode == iostate.Output): return self.current_value if (self.mode == iostate.Input): return self.device.value return None def high(self): if (self.mode == iostate.Output): self.current_value = True self.device.on() else: print("Output failed. Not in Output state") def low(self): if (self.mode == iostate.Output): self.current_value = False self.device.off() else: print("Output failed. Not in Output state") def set(self, value: bool): if (value): self.high() else: self.low() def inputMode(self): if (self.mode == iostate.Input): return if (self.mode == iostate.Output): self.device.close() self.device = None self.device = DigitalInputDevice(self.pin, pull_up = self.pull_up_in, active_state = self.active_state_in, bounce_time = self.bounce_time_in, pin_factory = self.pin_factory) self.current_value = self.device.value self.mode = iostate.Input def outputMode(self, initial_value: bool = None): if (self.mode == iostate.Output): if (initial_value != None): self.current_value = initial_value self.set(self.current_value) return if (self.mode == iostate.Input): self.device.close() self.device = None self.device = DigitalOutputDevice(self.pin, active_high = self.active_high_out, initial_value = initial_value, pin_factory = self.pin_factory) self.current_value = initial_value self.mode = iostate.Output def close(self): if (self.mode == iostate.Input or self.mode == iostate.Output): self.device.close() self.device = None self.mode = iostate.Float self.current_value = None def blink(self, on_time=1, off_time=0, n=None, background=True): self.outputMode() value = self.current_value self.device.blink(on_time,off_time,n,background) set(value) def wait_for_active(self, timeout = None): self.inputMode() self.device.wait_for_active(timeout) def wait_for_inactive(self, timeout = None): self.inputMode() self.device.wait_for_inactive(timeout) @property def active_time(self): self.inputMode() return self.device.active_time @property def inactive_time(self): self.inputMode() return self.device.inactive_time @property def when_activated(self): self.inputMode() return self.device.when_activated @when_activated.setter def when_activated(self, x): self.inputMode() self.device.when_activated = x @property def when_deactivated(self): self.inputMode() return self.device.when_deactivated @when_deactivated.setter def when_deactivated(self, x): self.inputMode() self.device.when_deactivated = x