From 0f64f03d67c6cee46f7da23fd8b6aaceb823bb68 Mon Sep 17 00:00:00 2001 From: Brandon Kinman Date: Mon, 5 Feb 2018 20:31:41 -0800 Subject: [PATCH 1/2] Added compatibility for python 2.7 --- tools/demo.py | 2 ++ tools/odrive/core.py | 2 +- tools/odrive/protocol.py | 29 ++++++++++++++++++++++------- tools/odrive/usbbulk_transport.py | 4 +++- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/tools/demo.py b/tools/demo.py index ab2fbe2a..4a06c260 100755 --- a/tools/demo.py +++ b/tools/demo.py @@ -3,6 +3,8 @@ Example usage of the ODrive python library to monitor and control ODrive devices """ +from __future__ import print_function + import odrive.core import time import math diff --git a/tools/odrive/core.py b/tools/odrive/core.py index c4888de7..bda8b58e 100644 --- a/tools/odrive/core.py +++ b/tools/odrive/core.py @@ -174,7 +174,7 @@ def create_object(name, json_data, namespace, channel, printer=noprint): attributes[member_name] = attribute # Create a type from the property list and instantiate it - jit_type = type(namespace, (object,), attributes) + jit_type = type(str(namespace), (object,), attributes) new_object = jit_type() return new_object diff --git a/tools/odrive/protocol.py b/tools/odrive/protocol.py index 1f8c9114..a8586667 100644 --- a/tools/odrive/protocol.py +++ b/tools/odrive/protocol.py @@ -2,7 +2,18 @@ import time import struct -from abc import ABC, abstractmethod +import sys + +import abc + +if sys.version_info >= (3, 4): + ABC = abc.ABC +else: + ABC = abc.ABCMeta('ABC', (), {}) + +if sys.version_info <= (3,4): + from monotonic import monotonic + time.monotonic = monotonic SYNC_BYTE = 0xAA CRC8_INIT = 0x42 @@ -30,6 +41,8 @@ def calc_crc(remainder, value, polynomial, bitwidth): def calc_crc8(remainder, value): if isinstance(value, bytearray) or isinstance(value, bytes) or isinstance(value, list): for byte in value: + if not isinstance(byte,int): + byte = ord(byte) remainder = calc_crc(remainder, byte, CRC8_DEFAULT, 8) else: remainder = calc_crc(remainder, byte, CRC8_DEFAULT, 8) @@ -38,6 +51,8 @@ def calc_crc8(remainder, value): def calc_crc16(remainder, value): if isinstance(value, bytearray) or isinstance(value, bytes) or isinstance(value, list): for byte in value: + if not isinstance(byte, int): + byte = ord(byte) remainder = calc_crc(remainder, byte, CRC16_DEFAULT, 16) else: remainder = calc_crc(remainder, value, CRC16_DEFAULT, 16) @@ -59,22 +74,22 @@ class DeviceInitException(Exception): class StreamSource(ABC): - @abstractmethod + @abc.abstractmethod def get_bytes(self, deadline): pass class StreamSink(ABC): - @abstractmethod + @abc.abstractmethod def process_bytes(self, bytes): pass class PacketSource(ABC): - @abstractmethod + @abc.abstractmethod def get_packet(self, deadline): pass class PacketSink(ABC): - @abstractmethod + @abc.abstractmethod def process_packet(self, packet): pass @@ -272,7 +287,7 @@ class Channel(PacketSink): self._expected_acks[seq_no] = packet[2:] else: - #if (calc_crc16(crc16, struct.pack(' Date: Sun, 11 Feb 2018 13:34:21 -0800 Subject: [PATCH 2/2] Update protocol.py --- tools/odrive/protocol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/odrive/protocol.py b/tools/odrive/protocol.py index a8586667..9d69aa08 100644 --- a/tools/odrive/protocol.py +++ b/tools/odrive/protocol.py @@ -11,7 +11,7 @@ if sys.version_info >= (3, 4): else: ABC = abc.ABCMeta('ABC', (), {}) -if sys.version_info <= (3,4): +if sys.version_info <= (3, 3): from monotonic import monotonic time.monotonic = monotonic