def mode(self, p_mode): """Set the pin mode. :param int p_mode: The pin mode to set. :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("PiFaceGpioDigital") if p_mode is None: p_mode = p_mode.TRI PiFaceGPIO.mode.fset(self, p_mode) # determine A or B port based on pin address if self.inner_pin.value < self.GPIO_B_OFFSET: self.__set_mode_a(p_mode) else: self.__set_mode_b(p_mode) # if any pins are configured as input pins, then we need to start the # interrupt monitoring poll timer. if self.__currentDirectionA > 0 or self.__currentDirectionB > 0: self.poll() else: self.cancel_poll()
def get_terminal_config(self, channel=None): """Get the terminal configuration for the specified channel. :param DeviceControlChannel channel: The channel to get the terminal configuration for. :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. :raises: raspy.argument_null_exception.ArgumentNullException if the 'channel' param is None. :raises: raspy.io.io_exception.IOException if an I/O error occurred. The specified address is inaccessible or the I2C transaction failed. """ if self.is_disposed: return ObjectDisposedException("MCPDeviceController") if channel is None: raise ArgumentNullException("'channel' param cannot be None.") if not isinstance(channel, device_control_channel.DeviceControlChannel): msg = "'channel' param must be of type DeviceControlChannel." raise IllegalArgumentException(msg) # Read the current config. tcon = self._read(channel.term_control_address) # Build result chan_enabled = (tcon & channel.hardware_config_ctrl_bit) > 0 pin_a_enabled = (tcon & channel.term_a_connection_ctrl_bit) > 0 pin_w_enabled = (tcon & channel.wiper_connection_ctrl_bit) > 0 pin_b_enabled = (tcon & channel.term_b_connection_ctrl_bit) > 0 return DeviceControllerTermConfig(channel, chan_enabled, pin_a_enabled, pin_w_enabled, pin_b_enabled)
def state(self, pwr_state): """Set the state of the power component. :param int pwr_state: The state to set. :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. :raises: raspy.io.invalid_pin_mode_exception.InvalidPinModeException if the pin being used to control this device is not configure as an output. :raises: raspy.invalid_operation_exception.InvalidOperationException if an invalid state is specified. """ if self.is_disposed: raise ObjectDisposedException("GpioPowerComponent") if self.__output.mode != pin_mode.OUT: msg = "Pins in use by power components MUST be configured as " msg += "outputs." raise InvalidPinModeException(msg, self.__output) if pwr_state == power_state.OFF: self.__output.write(self.__offState) pwr.PowerInterface.state.fset(self, pwr_state) elif pwr_state == power_state.ON: self.__output.write(self.__onState) pwr.PowerInterface.state.fset(self, pwr_state) else: bad_state = power_utils.get_power_state_name(pwr_state) msg = "Cannot set power state: " + bad_state raise InvalidOperationException(msg)
def read_gyro(self): """Read the gyro and store the value internally. :raise: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. :raises: raspy.io.io_exception.IOException if unable to write to the gyro. """ if self.is_disposed: raise ObjectDisposedException("ADXL345") now = system_info.get_current_time_millis() self.__timeDelta = now - self.__lastRead self.__lastRead = now self.__device.write_byte(self.__address, 0x00) core_utils.sleep(10) data = self.__device.read_bytes(self.__address, 6) if len(data) != 6: msg = "Couldn't read compass data; Return buffer size: " msg += str(len(data)) raise IOException(msg) self.a_x.raw_value = ((data[0] & 0xff) << 8) + (data[1] & 0xff) self.a_y.raw_value = ((data[2] & 0xff) << 8) + (data[3] & 0xff) self.a_z.raw_value = ((data[3] & 0xff) << 8) + (data[5] & 0xff)
def open(self): """Open a connection to the I2C bus. :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. :raises: raspy.io.io_exception.IOException if unable to open the bus connection. """ if self.is_disposed: raise ObjectDisposedException("I2CBus") if self.__isOpen: return try: self.__bus = SMBus(self.__busID) except OSError or IOError: msg = "Error opening bus '" + str(self.__busID) + "'." raise IOException(msg) if self.__bus.fd is None: msg = "Error opening bus '" + str(self.__busID) + "'." raise IOException(msg) self.__isOpen = True
def level(self, lev): """Set the brightness level. :param int lev: The brightness level. :raises: IndexError if the specified level is less than min_level or greater than max_level. :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("DimmableLightComponent") if lev < self.__min: raise IndexError("Level cannot be less than min_level.") if lev > self.__max: raise IndexError("Level cannot be greater than max_level.") on_before_change = self.is_on self.__pin.pwm = lev on_after_change = self.is_on evt = LightLevelChangeEvent(lev) self.on_light_level_changed(evt) if on_before_change != on_after_change: evt2 = LightStateChangeEvent(on_after_change) self.on_light_state_changed(evt2)
def step(self, steps): """Step the motor the specified number of steps. :param int steps: The number or steps to rotate. :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("StepperMotorComponent") if steps == 0: self.state = motor_state.STOP return # Perform step in positive or negative direction from current position. StepperMotor.step(self, steps) evt = MotorRotateEvent(steps) self.on_rotation_started(evt) if steps > 0: for _i in range(0, steps): self._do_step(True) else: for _j in range(steps, 0): self._do_step(False) # Stop movement. self.stop() self.on_rotation_stopped()
def poll(self): """Start a pin poll cycle. This will monitor the pin and check for state changes. If a state change is detected, the raspy.io.Gpio.EVENT_GPIO_STATE_CHANGED event will be emitted. The poll cycle runs asynchronously until stopped by the cancel_poll() method or when this object instance is disposed. :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. :raises: raspy.invalid_operation_exception.InvalidOperationException if the poll thread is already running. """ if self.is_disposed: raise ObjectDisposedException("PiFaceGpioDigital") if self.__pollRunning: raise InvalidOperationException("Poll thread already running.") self.__stopEvent.clear() self.__pollThread = threading.Thread(target=self.__background_poll) self.__pollThread.name = "PiFaceGpioPoller" self.__pollThread.daemon = True self.__pollThread.start() self.__pollRunning = True
def increase(self, channel=None, steps=0): """Increment the volatile wiper for the given number of steps. :param DeviceControlChannel channel: The device channel the wiper is on. :param int steps: The number of steps. :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. :raises: raspy.argument_null_exception.ArgumentNullException if the 'channel' param is None. :raises: raspy.illegal_argument_exception.IllegalArgumentException if param 'channel' is not of type DeviceControlChannel. :raises: raspy.io.io_exception.IOException if an I/O error occurred. The specified address is inaccessible or the I2C transaction failed. """ if self.is_disposed: return ObjectDisposedException("MCPDeviceController") if channel is None: raise ArgumentNullException("'channel' param cannot be None.") if not isinstance(channel, device_control_channel.DeviceControlChannel): msg = "'channel' param must be of type DeviceControlChannel." raise IllegalArgumentException(msg) # Increase only works on volatile-wiper. if steps is None: steps = 0 mem_addr = channel.volatile_mem_address self._increase_or_decrease(mem_addr, True, steps)
def get_temperature(self): """Send commands to get the temperature from the sensor. :returns: The temperature with half-degree granularity. :rtype: long :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("DS1620") self.__reset.write(pin_state.LOW) self.__clock.write(pin_state.HIGH) self.__reset.write(pin_state.HIGH) self.__send_command(0x0c) # write config command. self.__send_command(0x02) # cpu mode self.__reset.write(pin_state.LOW) # wait until the configuration register is written. core_utils.sleep_microseconds(200000) self.__clock.write(pin_state.HIGH) self.__reset.write(pin_state.HIGH) self.__send_command(0xEE) # start conversion self.__reset.write(pin_state.LOW) core_utils.sleep_microseconds(200000) self.__clock.write(pin_state.HIGH) self.__reset.write(pin_state.HIGH) self.__send_command(0xAA) raw = self.__read_data() self.__reset.write(pin_state.LOW) return Decimal(raw).quantize(Decimal('1.00')) / 2.0
def _fire_wiper_action_event(self, wiper_evt): """Fire the EVENT_WIPER_ACTION event. :param WiperEvent wiper_evt: The event info. """ if self.is_disposed: raise ObjectDisposedException("MicrochipPotentiometer") self.emit(EVENT_WIPER_ACTION, wiper_evt)
def on_button_hold(self, btn_event): """Fire the buttons hold event. :param raspy.components.buttons.button_event.ButtonEvent btn_event: The buttons event info. """ if self.is_disposed: raise ObjectDisposedException("Button") self.emit(EVENT_HOLD, btn_event)
def provision(self): """Provision this pin. :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("Gpio") self.write(self.__initValue)
def state(self, rel_state): """Set the relay state. :param int rel_state: The relay state to set. :raises: ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("Relay") self.__state = rel_state
def state(self, mot_state): """Set the motor state. :param int mot_state: The motor state. :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("Motor") self.__state = mot_state
def write(self, ps): """Write a value to the pin. :param int ps: The pin state value to write to the pin. :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("Gpio") self.__state = ps
def disable(self): """Disable the gyro. :raise: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. :raises: raspy.io.io_exception.IOException if unable to write to the gyro. """ if self.is_disposed: raise ObjectDisposedException("ADXL345")
def on_button_released(self, btn_event): """Fire the buttons released event. :param raspy.components.buttons.button_event.ButtonEvent btn_event: The buttons event info. """ if self.is_disposed: raise ObjectDisposedException("Button") self.emit(EVENT_RELEASED, btn_event) self._stop_hold_timer()
def write(self, state): """Write a value to the pin. :param int state: The pin state value to write to the pin. :raises: raspy.ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("PiFaceGpioDigital") PiFaceGPIO.write(self, state) self.__set_state(state)
def emit(self, evt, args): """Emit the specified event to all registered listeners. :param str evt: The name of the event to emit. :param object args: The arguments to pass to the event handlers (listeners). :raises: ObjectDisposedException if this instance is disposed. """ if self.is_disposed: raise ObjectDisposedException("Fireplace") self.__emitter.emit(evt, args)
def on(self, evt, callback): """Register an event with a callback to handle it. :param str evt: The name of the event to register a handler for. :param function callback: The callback to execute when the event fires. :raises: ObjectDisposedException if this instance is disposed. """ if self.is_disposed: raise ObjectDisposedException("Fireplace") self.__emitter.on(evt, callback)
def read(self): """Read a value from the pin. :returns: The state (value) of the pin. :rtype: int :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("Gpio") return pin_state.LOW
def set_wiper_lock(self, enabled=False): """Enable or disable the wiper lock. :param bool enabled: Set True to enable the wiper lock. :raises: raspy.io.io_exception.IOException if communication with the device fails. :raises: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("MicrochipPotentiometer") chan = device_control_channel.value_of(self.__channel) self.__controller.set_wiper_lock(chan, enabled)
def on_pulse_stop(self): """Fire the pulse stop event. :raises: ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("Relay") _t = threading.Thread(target=self.emit, name=EVENT_PULSE_STOP, args=[EVENT_PULSE_STOP]) _t.daemon = True _t.start()
def on_capture_done(self, done_evt): """Fire the capture done event. :param CaptureDoneEvent done_evt: The event object. :raises: ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("PiCameraDevice") _t = threading.Thread(target=self.emit, name=EVENT_CAPTURE_DONE, args=(EVENT_CAPTURE_DONE, done_evt)) _t.daemon = True _t.start()
def on_pilot_light_state_change(self, evt): """Fire the pilot light state change event. :param raspy.devices.fireplaces.fireplace_pilot_light_event.FireplacePilotLightEvent evt: The event object. """ if self.is_disposed: raise ObjectDisposedException("Fireplace") _t = threading.Thread(target=self.emit, name=EVENT_PILOT_LIGHT_STATE_CHANGED, args=(EVENT_PILOT_LIGHT_STATE_CHANGED, evt)) _t.daemon = True _t.start()
def on_operation_timeout(self, evt): """Fire the operation timeout event. :param raspy.devices.fireplaces.fireplace_timeout_event.FireplaceTimeoutEvent evt: The event object. """ if self.is_disposed: raise ObjectDisposedException("Fireplace") _t = threading.Thread(target=self.emit, name=EVENT_OPERATION_TIMEOUT, args=(EVENT_OPERATION_TIMEOUT, evt)) _t.daemon = True _t.start()
def poll(self): """Poll the input pin status every 200ms.""" if self.is_disposed: raise ObjectDisposedException("TempSensorComponent") if self.__isPolling: return self.__stopEvent.clear() self.__isPolling = True self.__pollThread = threading.Thread(target=self._execute_poll) self.__pollThread.name = "TempSensorComponentPollThread" self.__pollThread.daemon = True self.__pollThread.start()
def enable(self): """Enable the gyro. :raise: raspy.object_disposed_exception.ObjectDisposedException if this instance has been disposed. :raises: raspy.io.io_exception.IOException if unable to write to the gyro. """ if self.is_disposed: raise ObjectDisposedException("ADXL345") packet = [0x31, 0x0B] self.__device.write_bytes(self.__address, packet)
def on_capture_output_received(self, out_evt): """Fire the capture output event. :param CaptureOutputEvent out_evt: The event object. :raises: ObjectDisposedException if this instance has been disposed. """ if self.is_disposed: raise ObjectDisposedException("PiCameraDevice") _t = threading.Thread(target=self.emit, name=EVENT_CAPTURE_OUTPUT, args=(EVENT_CAPTURE_OUTPUT, out_evt)) _t.daemon = True _t.start()