From 0dd6ce653923d9aac88ff5b8fbda897c192a4155 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Fri, 22 Jun 2018 11:17:14 +0200 Subject: [PATCH] Library and example --- .gitignore | 1 + Libraries/Example.py | 37 +++++++++++++++++++++++ Libraries/{RNodeInterface.py => RNode.py} | 31 +++++++++++-------- README.md | 4 +-- 4 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 Libraries/Example.py rename Libraries/{RNodeInterface.py => RNode.py} (95%) diff --git a/.gitignore b/.gitignore index 2e61a39..e16aab2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store *.hex +*.pyc TODO diff --git a/Libraries/Example.py b/Libraries/Example.py new file mode 100644 index 0000000..45d9ba5 --- /dev/null +++ b/Libraries/Example.py @@ -0,0 +1,37 @@ +# This is a short example program that +# demonstrates the bare minimum of using +# RNode in a Python program. First we'll +# import the RNodeInterface class. +from RNode import RNodeInterface + +# We'll also define which serial port the +# RNode is attached to. +serialPort = "/dev/ttyUSB0" + +# This function gets called every time a +# packet is received +def gotPacket(data, rnode): + print "Received a packet: "+data + +# Create an RNode instance. This configures +# and powers up the radio. +rnode = RNodeInterface( + callback = gotPacket, + name = "My RNode", + port = serialPort, + frequency = 868000000, + bandwidth = 125000, + txpower = 2, + sf = 7, + cr = 5, + loglevel = RNodeInterface.LOG_DEBUG) + +# Enter a loop waiting for user input. +try: + print "Waiting for packets, hit enter to send a packet, Ctrl-C to exit" + while True: + raw_input() + rnode.send("Hello World!") +except KeyboardInterrupt as e: + print "" + exit() \ No newline at end of file diff --git a/Libraries/RNodeInterface.py b/Libraries/RNode.py similarity index 95% rename from Libraries/RNodeInterface.py rename to Libraries/RNode.py index 72f227b..7a53da0 100644 --- a/Libraries/RNodeInterface.py +++ b/Libraries/RNode.py @@ -1,12 +1,10 @@ from __future__ import print_function -from Interface import Interface from time import sleep import sys import serial import threading import time import math -import RNS class KISS(): FEND = chr(0xC0) @@ -54,6 +52,7 @@ class KISS(): class RNodeInterface(): + MTU = 500 MAX_CHUNK = 32768 FREQ_MIN = 137000000 FREQ_MAX = 1020000000 @@ -248,7 +247,7 @@ class RNodeInterface(): return False def setPromiscuousMode(self, state): - if state == True + if state == True: kiss_command = KISS.FEND+KISS.CMD_PROMISC+chr(0x01)+KISS.FEND else: kiss_command = KISS.FEND+KISS.CMD_PROMISC+chr(0x00)+KISS.FEND @@ -270,7 +269,7 @@ class RNodeInterface(): self.callback(data, self) def send(self, data): - processOutgoing(data) + self.processOutgoing(data) def processOutgoing(self,data): if self.online: @@ -321,7 +320,7 @@ class RNodeInterface(): command = KISS.CMD_UNKNOWN data_buffer = "" command_buffer = "" - elif (in_frame and len(data_buffer) < RNS.Reticulum.MTU): + elif (in_frame and len(data_buffer) < RNodeInterface.MTU): if (len(data_buffer) == 0 and command == KISS.CMD_UNKNOWN): command = byte elif (command == KISS.CMD_DATA): @@ -416,11 +415,11 @@ class RNodeInterface(): self.r_random = ord(byte) elif (command == KISS.CMD_ERROR): if (byte == KISS.ERROR_INITRADIO): - self.log(str(self)+" hardware initialisation error (code "+RNS.hexrep(byte)+")", RNodeInterface.LOG_ERROR) + self.log(str(self)+" hardware initialisation error (code "+self.hexrep(byte)+")", RNodeInterface.LOG_ERROR) elif (byte == KISS.ERROR_INITRADIO): - self.log(str(self)+" hardware TX error (code "+RNS.hexrep(byte)+")", RNodeInterface.LOG_ERROR) + self.log(str(self)+" hardware TX error (code "+self.hexrep(byte)+")", RNodeInterface.LOG_ERROR) else: - self.log(str(self)+" hardware error (code "+RNS.hexrep(byte)+")", RNodeInterface.LOG_ERROR) + self.log(str(self)+" hardware error (code "+self.hexrep(byte)+")", RNodeInterface.LOG_ERROR) elif (command == KISS.CMD_READY): # TODO: add timeout and reset if ready # command never arrives @@ -441,15 +440,14 @@ class RNodeInterface(): self.log("A serial port error occurred, the contained exception was: "+str(e), RNodeInterface.LOG_ERROR) self.log("The interface "+str(self.name)+" is now offline.", RNodeInterface.LOG_ERROR) - def log(msg, level=3): + def log(self, msg, level=3): + logtimefmt = "%Y-%m-%d %H:%M:%S" if self.loglevel >= level: timestamp = time.time() logstring = "["+time.strftime(logtimefmt)+"] ["+self.loglevelname(level)+"] "+msg + print(logstring) - if (logdest == LOG_STDOUT): - print(logstring) - - def loglevelname(level): + def loglevelname(self, level): if (level == RNodeInterface.LOG_CRITICAL): return "Critical" if (level == RNodeInterface.LOG_ERROR): @@ -467,6 +465,13 @@ class RNodeInterface(): if (level == RNodeInterface.LOG_EXTREME): return "Extra" + def hexrep(data, delimit=True): + delimiter = ":" + if not delimit: + delimiter = "" + hexrep = delimiter.join("{:02x}".format(ord(c)) for c in data) + return hexrep + def __str__(self): return "RNodeInterface["+self.name+"]" diff --git a/README.md b/README.md index 067bde0..0e1fca4 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ Have a look in the "Libraries" folder for includes to let you easily use RNode i Here's a Python example: ```python -import RNodeInterface +from RNode import RNodeInterface def gotPacket(data, rnode): print "Received a packet: "+data @@ -76,7 +76,7 @@ rnode = RNodeInterface( txpower = 2, sf = 7, cr = 5, - loglevel = RnodeInterface.LOG_DEBUG) + loglevel = RNodeInterface.LOG_DEBUG) rnode.send("Hello World!") ```