Code Monkey home page Code Monkey logo

pjon-cython's Introduction

PJON-cython

Call the PJON C++ library directly from Python 2 or Python 3 (via Cython)

PJON (Github: PJON ) is an open-source, multi-master, multi-media (one-wire, two-wires, radio) communication protocol available for various platforms (Arduino/AVR, ESP8266, Teensy).

PJON is one of very few open-source implementations of multi-master communication protocols for microcontrollers.

PJON-cython vs PJON-python

PJON-cython allows you to use the C++ PJON library from Python via Cython (C++ wrappers for Python) while PJON-python is a re-implementation of the PJON protocol in Python

Current status:

Support for PJON 12 and the following strategies :-

  • LocalUDP
  • GlobalUDP
  • ThroughSerial
  • ThroughSerialAsync

Note

  • PJON-cython versions are aligned with PJON versions to indicate compatibility with C implementation for uC platforms.

Python support

Python 2.7, 3.4, 3.5, 3.6 and 3.7 are tested and considered supported

Platform support

Linux and Mac OS X are considered supported. Windows is not supported (sorry!).

Install from pip

Current version is 12.0.0

pip install pjon-cython

Testing

$(which python) setup.py nosetests --with-doctest --doctest-extension=md

GlobalUDP example

>>> import pjon_cython as PJON
>>> class GlobalUDP(PJON.GlobalUDP):
...     # you can overload __init__ if you want
...     def __init__(self, device_id):
...         PJON.GlobalUDP.__init__(self, device_id)
...         self.packets_received = 0
...     def receive(self, data, length, packet_info):
...         print ("Recv ({}): {}".format(length, data))
...         print (packet_info)
...         self.packets_received += 1
...         self.reply(b'P')

>>> g = GlobalUDP(44)
>>> idx = g.send(123, b'HELO')
>>> # calling loop calls the PJON bus.update() and bus.receive()
>>> # and the return is the results of those functions -
>>> packets_to_send, receive_status = g.loop()
>>> # packets_to_send is the Number of packets in the PJON buffer
>>> packets_to_send
1
>>> #PJON constants are available too
>>> receive_status == PJON.PJON_FAIL
True
>>> # When you're done with your PJON interface, you can cleanup the connection by deleting it
>>> del g

Through Serial example

>>> import pjon_cython as PJON
>>> #ThroughSerial Example
>>> # Make sure you set self.bus.set_synchronous_acknowledge(false) on the other side
>>> 
>>> class ThroughSerial(PJON.ThroughSerial):
...
...     def receive(self, data, length, packet_info):
...        if data.startswith(b'H'):
...            print ("Recv ({}): {} - REPLYING".format(length, data))
...            self.reply(b'BONZA')
...        else:
...            print ("Recv ({}): {}".format(length, data))
...        print ('')
...
>>> # Put your actual serial device in here...
>>> ts = ThroughSerial(44, b"/dev/null", 115200)
>>> # Send returns the packet's index in the packet buffer
>>> ts.send(100, b'PING 1')
0
>>> ts.send(100, b'PING 2')
1
>>> # Error handling happens through exceptions such as PJON.PJON_Connection_Lost
>>> while True:
...     packets_to_send, receive_status = ts.loop()
Traceback (most recent call last):
    ...
pjon_cython._pjon_cython.PJON_Connection_Lost

Setting configurable properties

>>> import pjon_cython as PJON
>>> class GlobalUDP(PJON.GlobalUDP):
...     def receive(self, data, length, packet_info):
...         print ("Recv ({}): {}".format(length, data))

>>> # GlobalUDP and LocalUDP both support set_port to configure their UDP listening port
>>> g = GlobalUDP(99, 8821)
>>> del g
>>> #They return the class object, so you can "chain them"
>>> pjon = GlobalUDP(100,8821).set_autoregistration(False)
>>> pjon                                                            # doctest: +ELLIPSIS
<GlobalUDP object at 0x...>
>>>
>>> # These options affect packet overhead (in bytes)
>>> pjon.packet_overhead()
6
>>> pjon.set_crc_32(True).packet_overhead()
9
>>> pjon.set_packet_id(True).packet_overhead()
11
>>> pjon.set_synchronous_acknowledge(True).packet_overhead()
11
>>> pjon.set_packet_id(False).set_asynchronous_acknowledge(False).packet_overhead()
9
>>> pjon.set_crc_32(False).include_sender_info(False).packet_overhead()
5

Use serial based strategies with pyserial_Asyncio

Instead of passing a serial port string, you can pass a file descriptor to the ThroughSerial and ThroughSerialAsync methods which allows other libraries to poll the serial port.

For examples, see the reticul8 project.

pjon-cython's People

Contributors

xlfe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

pjon-cython's Issues

PJON 13.0 support

Ciao @xlfe thank you very much for your support, I hope you are fine and things are going smooth there.
Have you planned to update PJON-cython to be compatible with PJON 13.0?
Some users have asked about it :)

PJON Linux serial support

Ciao @xlfe compliments for the development done on PJON-cython, its new release and the LINUX serial support. Would you open a pull-request to the PJON repo to add the LINUX serial support?
Keep up the good work and thank you again for your support.

python setup.py install failed with error D8021

In short:
cl : Command line error D8021 : invalid numeric argument '/Wno-unneeded-internal-declaration'

Platform: Win10 x64, Python 2.7.14

Full output:
f:\compile\PJON-cython>python setup.py install
running install
running bdist_egg
running egg_info
creating pjon_cython.egg-info
writing pjon_cython.egg-info\PKG-INFO
writing top-level names to pjon_cython.egg-info\top_level.txt
writing dependency_links to pjon_cython.egg-info\dependency_links.txt
writing manifest file 'pjon_cython.egg-info\SOURCES.txt'
package init file 'pjon_cython_init_.py' not found (or not a regular file)
reading manifest file 'pjon_cython.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'pjon_cython.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
running build_ext
building 'pjon_cython' extension
creating build
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
creating build\temp.win-amd64-2.7\Release\pjon_cython
C:\Users<username>\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IPJON/src -IC:\Python27\x64\include -IC:\Python27\x64\PC /Tppjon_cython/pjon_cython.cpp /Fobuild\temp.win-amd64-2.7\Release\pjon_cython/pjon_cython.obj -std=c++11 -DLINUX -std=c++11 -DLINUX -DPJON_INCLUDE_TS -DPJON_INCLUDE_LUDP -DPJON_INCLUDE_GUDP -Wno-unneeded-internal-declaration -Wno-unused-variable
cl : Command line error D8021 : invalid numeric argument '/Wno-unneeded-internal-declaration'
error: command 'C:\Users\\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe' failed with exit status 2

PJON v12.0 support

Ciao @xlfe how its going?
Do you plan to update PJON-cython to be compatible to the next release?
If so in master you find the code :)

bugfix; ImportError: cannot import name build_ext

I tried to install/compile PJON-python without success.

  1. If I try to clone it from git I get this:
osboxes@osboxes:~$ git clone https://github.com/xlfe/PJON-cython
Cloning into 'PJON-cython'...
remote: Counting objects: 99, done.
remote: Compressing objects: 100% (58/58), done.
remote: Total 99 (delta 33), reused 92 (delta 28), pack-reused 0
Unpacking objects: 100% (99/99), done.
Checking connectivity... done.
osboxes@osboxes:~$ cd PJON-cython/
osboxes@osboxes:~/PJON-cython$ python setup.py build_ext --inplace
Traceback (most recent call last):
  File "setup.py", line 2, in <module>
    from Cython.Build import build_ext
ImportError: cannot import name build_ext
osboxes@osboxes:~/PJON-cython$ python setup.py build
Traceback (most recent call last):
  File "setup.py", line 2, in <module>
    from Cython.Build import build_ext
ImportError: cannot import name build_ext
osboxes@osboxes:~/PJON-cython$ python setup.py install
Traceback (most recent call last):
  File "setup.py", line 2, in <module>
    from Cython.Build import build_ext
ImportError: cannot import name build_ext
  1. If I try it via pip:
osboxes@osboxes:~/PJON-cython$ sudo pip install pjon-cython
Downloading/unpacking pjon-cython
  Downloading pjon_cython-11.1.0.tar.gz (466kB): 466kB downloaded
  Running setup.py (path:/tmp/pip_build_root/pjon-cython/setup.py) egg_info for package pjon-cython
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/tmp/pip_build_root/pjon-cython/setup.py", line 2, in <module>
        from Cython.Build import build_ext
    ImportError: cannot import name build_ext
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/tmp/pip_build_root/pjon-cython/setup.py", line 2, in <module>

    from Cython.Build import build_ext

ImportError: cannot import name build_ext

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip_build_root/pjon-cython
Storing debug log for failure in /home/osboxes/.pip/pip.log

Further if I replace setup.py with this one:

from distutils.core import setup
from Cython.Build import cythonize

std_args =['-std=c++11', '-DLINUX']

setup(
    ext_modules = cythonize("pjon_cython/pjon_cython.pyx")
)

it starts complaining about c++: pjon_cython/pjon_cython.pyx:78:23: Operation only allowed in c++ etc.

This was all done using Python 2.7.6.

Support for other PJON config constants?

Is possible to add support for other PJON config constants? I'm using TSA strategy and it would be great to be able to configure eg. TSA_RESPONSE_TIME_OUT or TS_RESPONSE_TIME_OUT for TS strategy

Internal inclusion change in PJON

Ciao @xlfe thank you very much for PJON-cython ๐Ÿ‘
I have changed in PJON the inclusion of internal sources chaging from <> to "": gioblu/PJON@6fcfd75
This was required to ease the integration with MySensors, do this change break something here?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.