Gesture Event Sample

This sample demonstrates detecting hand gestures using GPIO interrupt.

To run the sample:

  1. Copy the sample code to a folder on the Raspberry Pi.

  2. Open terminal, and then go to the folder you copied the sample.

  3. Run the sample with the following command:

python3 Gesture_Event.py

Sample Code

Gesture_Event.py
#!/usr/bin/env python3

#This sample demonstrates detecting hand gestures using GPIO interrupt.
#Install IoT HAT 3 library with "pip3 install turta-iothat3"

from time import sleep
import RPi.GPIO as GPIO
from turta_iothat3 import Turta_Light
from turta_iothat3 import Turta_Buzzer

#Gesture Event
def gesture_event(ch):
    g = light.read_gesture()
    if g is not None:
        print("Gesture Detected: " + str(g))
        buzzer.beep()

#Interrupt Pin
gesture_int = 4

#Initialize
light = Turta_Light.LightSensor(Turta_Light.MODES.PROX_GES, True)
buzzer = Turta_Buzzer.BuzzerDriver()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(gesture_int, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(gesture_int, GPIO.FALLING, callback=gesture_event, bouncetime=1)

#Add your configuration code here

try:
    while True:
        #Add your code here

        #Wait
        sleep(1.0)

#Exit on CTRL+C
except KeyboardInterrupt:
    GPIO.cleanup()
    print('Bye.')

Result

When you run the sample, it waits for an interrupt signal from the light sensor. When interrupt is detected, the program reads the buffer from the light sensor and searches for gesture directions. If it finds a valid gesture, prints the direction. The example output should be like this:

Gesture Detected: GESTURES.LEFT
Gesture Detected: GESTURES.RIGHT
Gesture Detected: GESTURES.UP
Gesture Detected: GESTURES.DOWN

To stop the Python script, just press CTRL + C.

Last updated