Skip to content

Commit

Permalink
Python wrapper for the Wirepas C Mesh API library
Browse files Browse the repository at this point in the history
This commit implements a Python wrapper for the Wirepas C Mesh API
library, done using the Cython compiler: https://cython.org. Tested
with Cython v0.29.14. Build script is very rudimentary at this point.
  • Loading branch information
jy-wirepas committed Jan 23, 2020
1 parent 38a5e89 commit 96b4f0f
Show file tree
Hide file tree
Showing 6 changed files with 1,428 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
recursive-include wirepas *.pyx *.pxd *.py *.c *.h
48 changes: 48 additions & 0 deletions python/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

import sys
from setuptools import Extension, setup
from Cython.Distutils import build_ext

PACKAGE = "wirepas.mesh_api"
DIST_NAME = PACKAGE.replace(".", "-").replace("_", "-")
PACKAGE_DIR = PACKAGE.replace(".", "/")
LIB_DIR = "../lib"

if sys.platform == "win32":
LIB_EXT = ".lib"
else:
LIB_EXT = ".a"

ext_1 = Extension(
PACKAGE + "._mesh_api",
sources=[PACKAGE_DIR + "/_mesh_api.pyx"],
include_dirs=[PACKAGE_DIR, LIB_DIR + "/api"],
extra_objects=[LIB_DIR + "/build/mesh_api_lib" + LIB_EXT],
)

setup(
name=DIST_NAME,
version="0.0",
author="Wirepas Ltd",
author_email="[email protected]",
description="Wirepas Mesh API for Python",
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX :: Linux",
"Programming Language :: C",
"Programming Language :: Cython",
"Programming Language :: Python :: 3 :: Only",
"Topic :: Communications",
],
url="https://github.com/wirepas/c-mesh-api",
packages=[PACKAGE],
ext_modules=[ext_1],
python_requires=">=3.4",
cmdclass={"build_ext": build_ext},
zip_safe=False,
)
94 changes: 94 additions & 0 deletions python/testbench.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env python3

import sys
import time

import mesh_api as wpc

c = wpc.Connection(b"/dev/ttyACM0", 125000)

try:
addr = "%d" % c.get_node_address()
except wpc.MeshAPIError:
addr = "<not set>"

try:
nw_addr = c.get_network_address()
nw_addr = "%d (0x%06x)" % (nw_addr, nw_addr)
except wpc.MeshAPIError:
nw_addr = "<not set>"

try:
nw_channel = "%d" % c.get_network_channel()
except wpc.MeshAPIError:
nw_channel = "<not set>"

try:
role = "%s" % c.get_role()
except wpc.MeshAPIError:
role = "<not set>"

auth_key_set = c.is_authentication_key_set() and "<set>" or "<not set>"
cipher_key_set = c.is_cipher_key_set() and "<set>" or "<not set>"

try:
diag_interval = c.get_app_config_data().interval
except wpc.MeshAPIError:
diag_interval = "<not set>"

stack_status = c.get_stack_status()

print("node address: %s" % addr)
print("network address: %s" % nw_addr)
print("network channel: %s" % nw_channel)
print("role: %s" % role)
print("authentication key: %s" % auth_key_set)
print("encryption key: %s" % cipher_key_set)
print("diagnostic interval: %s" % diag_interval)
print("stack status: %s" % stack_status)


def app_config_data_callback(app_config_data):
sys.stdout.write(
"\nIn app_config_data_callback(app_config_data = %s)\n" % repr(app_config_data)
)
sys.stdout.flush()


c.register_for_app_config_data(app_config_data_callback)


def reception_callback(data):
sys.stdout.write("\nIn reception_callback(data = %s)\n" % repr(data))
sys.stdout.flush()


for n in range(256):
c.register_for_data(n, reception_callback)


def scan_callback(scan_ready):
sys.stdout.write("\nIn scan_callback(scan_ready = %s)\n" % repr(scan_ready))
sys.stdout.flush()


c.register_for_scan_neighbors_done(scan_callback)


def other_callback(*args, **kwargs):
sys.stdout.write("\nIn other_callback(%s, %s)\n" % (args, kwargs))
sys.stdout.flush()


c.register_for_remote_status(other_callback)
c.register_from_stack_status(other_callback)


def wait():
while True:
time.sleep(1)
sys.stdout.write(".")
sys.stdout.flush()


wait()
1 change: 1 addition & 0 deletions python/wirepas/mesh_api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ._mesh_api import * # noqa
Loading

0 comments on commit 96b4f0f

Please sign in to comment.