Skip to content

Added gestures to the code #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
.static_storage/
.media/
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ Relies on [Adafruit Raspberry Pi Python Code](https://github.com/adafruit/Adafru

## Run Instructions

1. wire up to Pi's I2C
1. wire up to Pi's I2C and DR to GPIO17

2. navigate to /zxsensor/
2. navigate to /examples/

3. then run the following terminal command:
```bash
python zx_sensor_demo.py
python i2c_zx_demo.py
```

## Notes
* only basic 'x' & 'z' value retrieval so far, no gestures yet
* now with gestures - try examples/i2c_gesture_interrupt.py

84 changes: 84 additions & 0 deletions examples/i2c_gesture_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python

"""
i2c_gesture_Interrupt.py
XYZ Interactive ZX Sensor
Shawn Hymel @ SparkFun Electronics
May 6, 2015
Ported by: Christian Erhardt
Jan 6, 2018
https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library
Tests the ZX sensor's ability to read gesture data over I2C using
an interrupt pin. This program configures I2C and sets up an
interrupt to occur whenever the ZX Sensor throws its DR pin high.
The gesture is displayed along with its "speed" (how long it takes
to complete the gesture). Note that higher numbers of "speed"
indicate a slower speed.
Hardware Connections:

Raspberry Pin ZX Sensor Board Function
---------------------------------------
5V VCC Power
GND GND Ground
GPIO1 DA I2C Data
GPIO0 CL I2C Clock
Development environment specifics:
Written in python 2.7
Tested with a SparkFun RedBoard
Distributed as-is; no warranty is given.
"""

# standard
import time, sys, os
import logging
# project
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from zxsensor import *

logging.basicConfig(level=logging.WARN)

# Initialise the ZxSensor device using the default address
zx_sensor = ZxSensor()

# Read the model version number and ensure the library will work
ver = zx_sensor.get_model_version()
if (ver == ZX_ERROR):
print("Error reading model version!")
exit()
else:
print("Model version {}".format(ver))

if (not ver == ZX_MODEL_VER):
print("Model version needs to be {} to work with this library. Stopping".format(ZX_MODEL_VER))
exit()

# Read the register map version and ensure the library will work
ver = zx_sensor.get_reg_map_version()
if (ver == ZX_ERROR):
print("Error reading register map version number")
exit()
else:
print("Register map version {}".format(ver))

if (not ver == ZX_REG_MAP_VER):
print("Register map version needs to be {} to work with this library. Stopping".format(ZX_REG_MAP_VER))
exit()

# Endless loop
while (True):
if (zx_sensor.gesture_available()):
gesture = zx_sensor.read_gesture()
gesture_speed = zx_sensor.read_gesture_speed()

if (gesture == gesture_type.NO_GESTURE):
print("No Gesture")
elif (gesture == gesture_type.RIGHT_SWIPE):
print("Right Swipe. Speed: {}".format(gesture_speed))
elif (gesture == gesture_type.LEFT_SWIPE):
print("Left Swipe. Speed: {}".format(gesture_speed))
elif (gesture == gesture_type.UP_SWIPE):
print("Up Swipe. Speed: {}".format(gesture_speed))

time.sleep(.1)

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
106 changes: 106 additions & 0 deletions examples/i2c_gesture_interrupt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env python

"""
i2c_gesture_Interrupt.py
XYZ Interactive ZX Sensor
Shawn Hymel @ SparkFun Electronics
May 6, 2015
Ported by: Christian Erhardt
Jan 6, 2018
https://github.com/sparkfun/SparkFun_ZX_Distance_and_Gesture_Sensor_Arduino_Library
Tests the ZX sensor's ability to read gesture data over I2C using
an interrupt pin. This program configures I2C and sets up an
interrupt to occur whenever the ZX Sensor throws its DR pin high.
The gesture is displayed along with its "speed" (how long it takes
to complete the gesture). Note that higher numbers of "speed"
indicate a slower speed.
Hardware Connections:

Raspberry Pin ZX Sensor Board Function
---------------------------------------
5V VCC Power
GND GND Ground
GPIO1 DA I2C Data
GPIO0 CL I2C Clock
GPIO17 DR Data Ready
Development environment specifics:
Written in python 2.7
Tested with a SparkFun RedBoard
Distributed as-is; no warranty is given.
"""

# standard
import time, sys, os
import logging
# project
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from zxsensor import *
# external
import RPi.GPIO as GPIO

logging.basicConfig(level=logging.DEBUG)

# Set the DA pin to GPIO 17 of the Raspberry Pi
channel = 17
# Initialise the ZxSensor device using the default address
zx_sensor = ZxSensor(interrupts=interrupt_type.GESTURE_INTERRUPTS)

# Read the model version number and ensure the library will work
ver = zx_sensor.get_model_version()
if (ver == ZX_ERROR):
print("Error reading model version!")
exit()
else:
print("Model version {}".format(ver))

if (not ver == ZX_MODEL_VER):
print("Model version needs to be {} to work with this library. Stopping".format(ZX_MODEL_VER))
exit()

# Read the register map version and ensure the library will work
ver = zx_sensor.get_reg_map_version()
if (ver == ZX_ERROR):
print("Error reading register map version number")
exit()
else:
print("Register map version {}".format(ver))

if (not ver == ZX_REG_MAP_VER):
print("Register map version needs to be {} to work with this library. Stopping".format(ZX_REG_MAP_VER))
exit()

def interrupt_callback(channel):
# You MUST read the STATUS register to clear the interrupt
zx_sensor.clear_interrupts()

gesture = zx_sensor.read_gesture()
gesture_speed = zx_sensor.read_gesture_speed()

print(gesture)

if (gesture == gesture_type.NO_GESTURE):
print("No Gesture")
elif (gesture == gesture_type.RIGHT_SWIPE):
print("Right Swipe. Speed: {}".format(gesture_speed))
elif (gesture == gesture_type.LEFT_SWIPE):
print("Left Swipe. Speed: {}".format(gesture_speed))
elif (gesture == gesture_type.UP_SWIPE):
print("Up Swipe. Speed: {}".format(gesture_speed))

GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)
GPIO.add_event_detect(channel, GPIO.RISING, callback=interrupt_callback)

try:
# Clear Sensor interrupt and wait for pin to go low
zx_sensor.clear_interrupts()
time.sleep(.10)

# Endless loop
while (True):
time.sleep(1)

except KeyboardInterrupt:
GPIO.cleanup()

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
5 changes: 3 additions & 2 deletions zxsensor/zx_sensor_demo.py → examples/i2c_zx_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
a Rasberry Pi. Tested with Pi2. When run, prints 'x' & 'z' to console
"""

import time
import time, sys, os
# project
from zx_sensor import ZxSensor
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from zxsensor import ZxSensor

# Initialise the ZxSensor device using the default address
zx_sensor = ZxSensor(0x10)
Expand Down
1 change: 1 addition & 0 deletions zxsensor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
Binary file removed zxsensor/Adafruit_I2C.pyc
Binary file not shown.
5 changes: 5 additions & 0 deletions zxsensor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

from Adafruit_I2C import Adafruit_I2C
# project
from i2c_registers import *
from zx_sensor import ZxSensor
23 changes: 11 additions & 12 deletions zxsensor/i2c_registers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

from enum import Enum
""" Register map and other constaints relating the zx_sensor I2C interface
"""

Expand Down Expand Up @@ -52,17 +53,15 @@
SET_ALL_DRE = 0b00111111

# Enumeration for possible gestures
gesture_type = {
'RIGHT_SWIPE': 0x01,
'LEFT_SWIPE': 0x02,
'UP_SWIPE': 0x03,
'NO_GESTURE': 0xFF,
}
class gesture_type(Enum):
RIGHT_SWIPE = 0x01
LEFT_SWIPE = 0x02
UP_SWIPE = 0x08
NO_GESTURE = 0x00

# Enumeration for possible interrupt enables
interrupt_type = {
'NO_INTERRUPTS': 0x00,
'POSITION_INTERRUPTS': 0x01,
'GESTURE_INTERRUPTS': 0x02,
'ALL_INTERRUPTS': 0x03
}
class interrupt_type(Enum):
NO_INTERRUPTS = 0x00
POSITION_INTERRUPTS = 0x01
GESTURE_INTERRUPTS = 0x02
ALL_INTERRUPTS = 0x03
Binary file removed zxsensor/i2c_registers.pyc
Binary file not shown.
Loading