diff --git a/docs/api.rst b/docs/api.rst index 782d90c..5312c80 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -109,6 +109,14 @@ MotionSensor :inherited-members: :members: +TouchSensor +----------- + +.. autoclass:: TouchSensor + :show-inheritance: + :inherited-members: + :members: + Switch ------ diff --git a/docs/changelog.rst b/docs/changelog.rst index 8c397cc..bae99d6 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -7,6 +7,7 @@ Change log ----------- + Introduced ``MotionSensor`` class for PIR sensors ++ Introduced ``TouchSensor`` class for capacitive touch sensors 0.4.2 - 2023-05-12 ------------------ diff --git a/docs/examples/touch_sensor.py b/docs/examples/touch_sensor.py new file mode 100644 index 0000000..f5fb6e5 --- /dev/null +++ b/docs/examples/touch_sensor.py @@ -0,0 +1,12 @@ +from picozero import TouchSensor, pico_led +from time import sleep + +# Capacitive touch sensor output connected to pin 2 +touch = TouchSensor(2) + +while True: + if touch.is_touched: + pico_led.on() + else: + pico_led.off() + sleep(0.1) diff --git a/docs/examples/touch_sensor_callbacks.py b/docs/examples/touch_sensor_callbacks.py new file mode 100644 index 0000000..cdd640f --- /dev/null +++ b/docs/examples/touch_sensor_callbacks.py @@ -0,0 +1,15 @@ +from picozero import TouchSensor, pico_led +from time import sleep + +touch = TouchSensor(2) + +# Set up event callbacks +touch.when_touched = pico_led.on +touch.when_touch_ends = pico_led.off + +# Keep the program running +try: + while True: + sleep(1) +except KeyboardInterrupt: + pico_led.off() # Make sure LED is off when exiting diff --git a/docs/recipes.rst b/docs/recipes.rst index f0e29bf..e20b36a 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -145,6 +145,17 @@ Turn the :obj:`pico_led` on when a :class:`Button` is pressed and off when it is .. literalinclude:: examples/button_led.py +Touch sensor +------------ + +Detect touch using a capacitive touch sensor: + +.. literalinclude:: examples/touch_sensor.py + +Use callbacks to respond to touch events: + +.. literalinclude:: examples/touch_sensor_callbacks.py + Motion sensor ------------- diff --git a/picozero/__init__.py b/picozero/__init__.py index 83cb63c..d2e9e9c 100644 --- a/picozero/__init__.py +++ b/picozero/__init__.py @@ -1,14 +1,12 @@ __name__ = "picozero" __package__ = "picozero" -__version__ = '0.4.2' +__version__ = "0.4.2" __author__ = "Raspberry Pi Foundation" from .picozero import ( PWMChannelAlreadyInUse, EventFailedScheduleQueueFull, - pinout, - DigitalOutputDevice, DigitalLED, Buzzer, @@ -16,28 +14,23 @@ PWMLED, LED, pico_led, - PWMBuzzer, Speaker, - RGBLED, Motor, Robot, Servo, - DigitalInputDevice, Switch, Button, MotionSensor, - + TouchSensor, AnalogInputDevice, Potentiometer, Pot, - TemperatureSensor, pico_temp_sensor, TempSensor, Thermistor, - DistanceSensor, ) diff --git a/picozero/picozero.py b/picozero/picozero.py index 2836e0b..f0a4d59 100644 --- a/picozero/picozero.py +++ b/picozero/picozero.py @@ -2028,6 +2028,36 @@ def __init__(self, pin, pull_up=False, bounce_time=1.00): MotionSensor.when_no_motion = MotionSensor.when_deactivated +class TouchSensor(Button): + """ + Represents a capacitive touch sensor (e.g. TTP223) + + :param int pin: + The pin that the capacitive touch sensor is connected to. + + :param bool pull_up: + If :data:`True`, the device will be pulled up to + HIGH. If :data:`False` (the default), the device will be pulled down to LOW. + Most capacitive touch sensors work with pull_up=False. + + :param float bounce_time: + The bounce time for the device. If set, the device will ignore + any touch events that happen within the bounce time after a + touch event. This is useful to prevent false triggers from + electrical noise or multiple rapid touches. + Defaults to 0.02 seconds. + """ + + def __init__(self, pin, pull_up=False, bounce_time=0.02): + super().__init__(pin=pin, pull_up=pull_up, bounce_time=bounce_time) + + +TouchSensor.is_touched = TouchSensor.is_active +# Note: No alias for is_inactive - use 'not touch.is_touched' for clarity +TouchSensor.when_touched = TouchSensor.when_activated +TouchSensor.when_touch_ends = TouchSensor.when_deactivated + + class AnalogInputDevice(InputDevice, PinMixin): """ Represents a generic input device with analogue functionality, e.g.