Example #1
0
 def exit(self):
     # Reinitialize all rows and columns as input at exit
     for i in range(len(self.ROWS)):
         DigitalInputDevice(self.ROWS[i], pull_up=True)
         # GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
     for j in range(len(self.COLUMNS)):
         DigitalInputDevice(self.COLUMNS[j], pull_up=True)
Example #2
0
    def get_pressed(self):
        # don't want to reactivate this function, within this function:
        for obj in self._col_objects:
            obj.when_activated = None

        # have to close outputs to make them tristates
        for obj in self._row_objects:
            obj.close()
        row_tristates = [
            DigitalInputDevice(pin, pull_up=None, active_state=True)
            for pin in self.row_pins
        ]

        # store the keypresses in pressed_keys
        pressed_keys = []
        for row, pin in enumerate(self.row_pins):
            row_tristates[row].close()
            output = DigitalOutputDevice(pin)
            for col, obj in enumerate(self._col_objects):
                if obj.is_active:
                    pressed_keys.append(self.keymap[row][col])
            output.close()
            row_tristates[row] = DigitalInputDevice(pin,
                                                    pull_up=None,
                                                    active_state=True)

        # cleanup
        for obj in row_tristates:
            obj.close()
        self._reset_internal_objects()

        return pressed_keys
Example #3
0
 def __init__(self, clockPin, dataPin):
     for pin in (clockPin, dataPin):
         assert isinstance(pin, int), 'Must be int'
     self.alive = True
     self.clock = DigitalInputDevice(clockPin)
     self.data = DigitalInputDevice(dataPin)
     self.last = None
Example #4
0
    def __init__(self,
                 clockPin,
                 dataPin,
                 switchPin=None,
                 rotaryCallback=None,
                 switchCallback=None,
                 rotaryBouncetime=0.050,
                 switchBouncetime=0.050):
        # persist values
        self.clockPin = DigitalInputDevice(clockPin,
                                           pull_up=False,
                                           bounce_time=rotaryBouncetime)
        self.dataPin = DigitalInputDevice(dataPin,
                                          pull_up=False,
                                          bounce_time=rotaryBouncetime)
        if None != switchPin:
            self.switchPin = DigitalInputDevice(switchPin,
                                                pull_up=False,
                                                bounce_time=switchBouncetime)
        else:
            self.switchPin = None
        self.rotaryCallback = rotaryCallback
        self.switchCallback = switchCallback
        self.rotaryBouncetime = rotaryBouncetime
        self.switchBouncetime = switchBouncetime

        # handle falling edge is a change to closed
        # http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/keyes-ky-040-arduino-rotary-encoder-user-manual/
        self.clockPin.pin.edges = "falling"
        self.dataPin.pin.edges = "falling"
        self.switchPin.pin.edges = "falling"
Example #5
0
    def __init__(self, pin_a, pin_b, pull_up=False):
        """
        Uses for detect rotary encoder changes (set when_rotated attribute)
        It takes one parameter which is +1 or +2 for clockwise and -1 or -2 for counterclockwise.

        :param int pin_a:
            Pin number of first (left) pin.
        :param int pin_b:
            Pin number of last (right) pin.
        :param bool pull_up:
            The common contact (middle) should be NOT connected to ground?
        """
        self.gpio_a = DigitalInputDevice(pin=pin_a, pull_up=pull_up)
        self.gpio_b = DigitalInputDevice(pin=pin_b, pull_up=pull_up)

        self.gpio_a.when_activated = self.pulse
        self.gpio_a.when_deactivated = self.pulse

        self.gpio_b.when_activated = self.pulse
        self.gpio_b.when_deactivated = self.pulse

        self.old_a_value = self.gpio_a.is_active
        self.old_b_value = self.gpio_b.is_active

        self.table_values = TableValues()
Example #6
0
 def __init__(self, motor_forward, motor_backward, open_sensor,
              close_sensor, max_time_open=60, max_time_close=45):
     self.max_time_open = max_time_open
     self.max_time_close = max_time_close
     self.motor = Motor(motor_forward, motor_backward)
     self.open_sensor = DigitalInputDevice(open_sensor)
     self.close_sensor = DigitalInputDevice(close_sensor)
     self.determine_state()
Example #7
0
 def __init__(self):
     self._value1 = 0
     self._value2 = 0
     self._pump = DigitalOutputDevice(17)  # GPIO 17
     self._sensor1 = DigitalInputDevice(27)  # GPIO 27
     self._sensor2 = DigitalInputDevice(22)  # GPIO 22
     self._verification_delay = VERIFICATION_DELAY_PUMP_OFF  # default pump off
     self._last_pump_on_time = None
     self._last_pump_off_time = None
Example #8
0
 def limit_refresh(self):
     left = DigitalInputDevice(6)
     right = DigitalInputDevice(13)
     up = DigitalInputDevice(26)
     down = DigitalInputDevice(19)
     while True:
         self.left_limit = left.value
         self.right_limit = right.value
         self.up_limit = up.value
         self.down_limit = down.value
         sleep(0.01)
Example #9
0
    def get_key(self):
        # Set all columns as output low
        for j in range(len(self.COLUMNS)):
            DigitalOutputDevice(self.COLUMNS[j], active_high=False)
            # GPIO.setup(self.COLUMNS[j], GPIO.OUT)
            # GPIO.output(self.COLUMNS[j], GPIO.LOW)

        # Set all rows as input
        for i in range(len(self.ROWS)):
            DigitalInputDevice(self.ROWS[i], pull_up=True)
            # GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)

        # Scan rows for pushed key/button
        # A valid key press should set "row_val" between 0 and 3.
        row_val = -1
        for i in range(len(self.ROWS)):
            # tmpRead = GPIO.input(self.ROWS[i])
            tmp_read = DigitalInputDevice(self.ROWS[i]).is_active
            if tmp_read == 0:
                row_val = i

        # if row_val is not 0 through 3 then no button was pressed and we can exit
        if row_val < 0 or row_val > 3:
            self.exit()
            return

        # Convert columns to input
        for j in range(len(self.COLUMNS)):
            # GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
            DigitalInputDevice(self.COLUMNS[j], pull_up=True)

        # Switch the i-th row found from scan to output
        DigitalOutputDevice(self.ROWS[row_val], active_high=True)
        # GPIO.setup(self.ROW[row_val], GPIO.OUT)
        # GPIO.output(self.ROW[row_val], GPIO.HIGH)

        # Scan columns for still-pushed key/button
        # A valid key press should set "col_val"  between 0 and 2.
        col_val = -1
        for j in range(len(self.COLUMNS)):
            tmp_read = DigitalOutputDevice(self.COLUMNS[j]).is_active
            # tmpRead = GPIO.input(self.COLUMN[j])
            if tmp_read == 1:
                col_val = j

        # if col_val is not 0 thru 2 then no button was pressed and we can exit
        if col_val < 0 or col_val > 2:
            self.exit()
            return

        # Return the value of the key pressed
        self.exit()
        return self.KEYPAD[row_val][col_val]
Example #10
0
    def __init__(self, pin_a, pin_b, pull_up=True):
        self.when_rotated = lambda *args: None

        self.pin_a = DigitalInputDevice(pin=pin_a, pull_up=pull_up)
        self.pin_b = DigitalInputDevice(pin=pin_b, pull_up=pull_up)

        self.pin_a.when_activated = self._pulse
        self.pin_a.when_deactivated = self._pulse

        self.pin_b.when_activated = self._pulse
        self.pin_b.when_deactivated = self._pulse

        self._old_a_value = self.pin_a.is_active
        self._old_b_value = self.pin_b.is_active
Example #11
0
    def __init__(self):

        #set default
        self.left = DigitalInputDevice(17)
        self.mid = DigitalInputDevice(27)
        self.right = DigitalInputDevice(22)

        #ROS publisher
        self.linePub = rospy.Publisher('lines', lines, queue_size=10)

        rospy.init_node('lines', anonymous=True)

        #this rate will drive the serial read rate
        self.rate = rospy.Rate(20)
Example #12
0
    def wait_dup_ready(self):
        """
        Function waits for DUP to indicate that it is ready. The DUP will
        pulls DD line low when it is ready. Requires DD to be input when
        function is called.

        Return:
            Returns 0 if function timed out waiting for DD line to go low
            Returns 1 when DUP has indicated it is ready.
        """
        count = 0

        dev_dd = DigitalOutputDevice(pin=self.pin_dd)
        dev_dd.on()
        dev_dd.close()
        self._wait()

        dev_dd = DigitalInputDevice(pin=self.pin_dd)

        while ((dev_dd.value == 1) and count < 16):
            # Clock out 8 bits before checking if DD is low again
            for _ in range(8):
                self.dev_dc.on()
                self._wait()
                self.dev_dc.off()
                self._wait()
            count += 1
        
        dev_dd.close()

        if count == 16:
            raise FlashError("Timeout for wait dup read")
Example #13
0
    def _read_debug_byte(self) -> int:
        """
        Reads a byte from the debug interface. Requires DD to be
        input when function is called.
        
        Return
            Returns the byte read.
        """
        data = 0

        dev_dd = DigitalInputDevice(pin=self.pin_dd)

        for _ in range(8):
            self.dev_dc.on()

            data = data << 1
            if dev_dd.value == 1:
                data |= 0x01

            self.dev_dc.off()
            self._wait()
        
        dev_dd.close()

        return data
 def __init__(self):
     super().__init__()
     self.logger = logging.getLogger(
         'rpiplatesrecognition_client.RPIMotionSensor')
     self.input_device = DigitalInputDevice(self.current_gpio_pin_number,
                                            pull_up=False,
                                            bounce_time=1)
Example #15
0
    def __init__(self):
        path = pathlib.Path(__file__).parent

        with open(path / 'event_multicast.yaml') as file:
            self.config_data = yaml.load(file, Loader=yaml.FullLoader)

        # self.auth=(self.config_data['user'],self.config_data['password'])
        signal_pin = int(self.config_data.get('gpio_pin'))
        self.button = DigitalInputDevice(signal_pin)

        self.button.when_activated = self.prepare_action
        self.button.when_deactivated = self.release_action

        self.bounce_ts = 0

        self.bounce_avoid = 1000

        self.bounce_last = None

        self.state = 0

        self.addrinfo = socket.getaddrinfo(self.config_data.get('mgroup'),
                                           None)[0]

        self.s = socket.socket(self.addrinfo[0], socket.SOCK_DGRAM)

        # Set Time-to-live (optional)
        ttl_bin = struct.pack('@i', self.config_data.get('mttl'))

        self.s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin)
Example #16
0
def setup():
    encoder_a = DigitalInputDevice(12)
    while True:
        encoder_a.wait_for_active()
        print('ON!')
        encoder_a.wait_for_inactive()
        print('OFF!')
Example #17
0
 def __init__(self, in1, in2, in3):
     self.direction = OutputDevice(in1, initial_value=True)  # direction Pin
     self.speed = PWMOutputDevice(in2, initial_value=1,
                                  frequency=980)  # speed Pin
     self.encoder = DigitalInputDevice(in3)
     #self.encoder = SmoothedInputDevice(in3, threshold=0.01, partial=True)
     self.total_time = 1.0 / (self.speed.frequency / 2.0)
Example #18
0
 def __init__(self, pin, label, level, activated_callback,
              deactivated_callback):
     '''
         The parameters include two optional callback functions, one when 
         the bumper is activated, another when deactivated. 
     '''
     self._log = Logger("bumper:" + label, level)
     self._log.debug('initialising bumper:{} on pin {}...'.format(
         label, pin))
     self._enabled = False
     if activated_callback is None:
         self._log.error("no activated_callback argument provided.")
     if deactivated_callback is None:
         self._log.error("no deactivated_callback argument provided.")
     self._pin = pin
     self._label = label
     self._sensor = DigitalInputDevice(pin,
                                       bounce_time=BOUNCE_TIME_SEC,
                                       pull_up=True)
     self._activated_callback = activated_callback
     self._deactivated_callback = deactivated_callback
     self._sensor.when_activated = self._activated
     self._sensor.when_deactivated = self._deactivated
     self._wait_for_inactive = True
     self._log.info('bumper on pin {} ready.'.format(label, pin))
Example #19
0
    def __init__(self, address=0x5b, bus_index=1):

        # Initialisation definitions
        self.bus = self.bus_init(bus_index)
        self.address = address

        # Register definitions
        self.STATUS = 0x00
        self.MODE = 0x01
        self.RESULT = 0x02
        self.ERROR = 0xE0
        self.APP_VERIFY = 0xF3
        self.APP_START = 0xF4
        self.RESET = 0xFF
        self.RESET_SEQUENCE = [0x11, 0xE5, 0x72, 0x8A]

        # Mode definitions
        self.MODE_0 = 0x00
        self.MODE_1 = 0x10
        self.MODE_2 = 0x20
        self.MODE_3 = 0x30
        self.MODE_4 = 0x40
        self.INTERRUPT = 0x08
        self.THRESHOLD = 0x04

        # Valid range definitions
        self.co2_min = 400
        self.co2_max = 8192
        self.tvoc_min = 0
        self.tvoc_max = 1187

        # Other definitions
        self.max_retrial_num = 5
        self.interrupt = DigitalInputDevice(18)
Example #20
0
    def __init__(self, pin):
        self._value = 0

        # setup gpiozero to call increment on each when_activated
        self.encoder = DigitalInputDevice(pin)
        self.encoder.when_activated = self._increment
        self.encoder.when_deactivated = self._increment
Example #21
0
 def __init__(self, pin):
     self._value = 0
     #GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
     #GPIO.add_event_detect(pin, GPIO.FALLING, callback=self._increment, bouncetime=10)
     encoder = DigitalInputDevice(pin, pull_up=True)
     encoder.when_activated = self._increment
     encoder.when_deactivated = self._increment
Example #22
0
    async def parent():
        alert_ready = DigitalInputDevice(ALERT_READY_PIN, pull_up=True)
        bus = smbus.SMBus(DEVICE_BUS)
        adc = ADS1015(bus, DEVICE_ADDRESS, alert_ready, channel=0)

        async with trio.open_nursery() as nursery:
            current_system = CurrentMeasure(nursery, adc, channel=2, notification_callbacks=[process_data])
            nursery.start_soon(current_system.a_run_notification_loop)
Example #23
0
 def __init__(self):
     self.rstPin = DigitalOutputDevice(self.PIN_NODE_RST)
     self.btnPin = DigitalInputDevice(self.PIN_START_BTN)
     self.outPin = DigitalOutputDevice(self.PIN_EXIT_EN, active_high=False)
     self.snakeOnPin = DigitalOutputDevice(self.PIN_SNAKE_EN,
                                           active_high=False)
     #self.outPin.source = self.btnPin.values
     return
Example #24
0
    def button_refresh(self):
        b_out = DigitalInputDevice(27)
        b_in = DigitalInputDevice(17)
        bin_pre = 0
        bout_pre = 0
        while True:
            bin_now = b_in.value
            bout_now = b_out.value
            sleep(0.01)
            bin_now2 = b_in.value
            bout_now2 = b_out.value
            #sleep(0.01)
            #bin_now3 = b_in.value
            #bout_now3 = b_out.value
            #sleep(0.01)
            #bin_now4 = b_in.value
            #bout_now4 = b_out.value
            #sleep(0.01)
            #bin_now5 = b_in.value
            #bout_now5 = b_out.value

            #if bin_now and bin_now2 and bin_now3 and bin_now4 and bin_now5:
            if bin_now and bin_now2:
                bin_now = 1
            else:
                bin_now = 0

            if bout_now and bout_now2:
                bout_now = 1
            else:
                bout_now = 0
            if bin_now and not bin_pre:
                print('power on ppp')
                poweron_p()
            elif bout_now and not bout_pre:
                print('power on nnn')
                poweron_n()
            elif not any([bin_now, bout_now]) and any([bin_pre, bout_pre]):
                sleep(0.1)
                print('poweroff')
                poweroff()
            else:
                pass
            bin_pre = bin_now
            bout_pre = bout_now
            sleep(0.3)
Example #25
0
 def returnResults(self):
     radar = DigitalInputDevice(self.__pin, pull_up=False, bounce_time=2.0)
     if(radar.when_activated):
         r = 1
         return r 
     else:
         r = 0
         return r
Example #26
0
    def __init__(self):
        self.adc = spidev.SpiDev()

        self.pin_reset = DigitalOutputDevice(18, active_high=False)
        self.pin_drdy = DigitalInputDevice(17)
        self.pin_cs = DigitalOutputDevice(22, active_high=False)

        self.exit_event = Event()
Example #27
0
 def __init__(self, pinA, pinB, rate = 1):
     ''' Construtor da classe. Entradas: pinA, pinB - canais do encoder
                                         rate [= 1] - 1 / (N * PPR).'''        
     # Entradas da GPIO:
     self.encA = DigitalInputDevice (pinA, pull_up = True)
     self.encB = DigitalInputDevice (pinB, pull_up = True)
     # Dados do encoder:
     self.value = 0
     self.rate = rate
     # Estados dos pinos:
     self.AState = False
     self.BState = False
     # Callbacks:
     self.encA.when_actived = self.changeA
     self.encB.when_actived = self.changeB
     self.encA.when_deactived = self.changeA
     self.encB.when_deactived = self.changeB
Example #28
0
def get_reading():
    d0_input = DigitalInputDevice(21)
    if not d0_input.value:
        print("moisture threshold reached")
        return 0
    else:
        print("needs water")
        return 1
Example #29
0
 def __init__(self):
     self.snakeOnPin = DigitalOutputDevice(self.PIN_SNAKE_EN,
                                           active_high=False)
     self.snakeDonePin = DigitalInputDevice(self.PIN_SNAKE_DONE,
                                            pull_up=False)
     self.snakeDonePin.when_activated = self.onDone
     self.done = False
     self.enabled = False
     return
Example #30
0
 def __init__(self):
     self.logger = logging.getLogger(__name__)
     super(SoundSensor, self).__init__()
     self._stopper = Event()  
     self.logger.info("Starting sound sensor thread...")
     # In case you want to use Remote GPIO
     factory = PiGPIOFactory(host=settings.GPIOD_HOST)
     self.do = DigitalInputDevice(self.pin, pin_factory=factory)
     self.piBot = PiTelegramBot()