Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added basic register and type construts and marshallers.

  • Loading branch information...
commit 752d94dca31c7c7efb20af6c990e480838584dbe 1 parent dd1813d
@gregjurman authored
Showing with 122 additions and 0 deletions.
  1. +68 −0 pyfaze/registers.py
  2. +21 −0 pyfaze/types.py
  3. +33 −0 pyfaze/util.py
View
68 pyfaze/registers.py
@@ -0,0 +1,68 @@
+from pyfaze.types import *
+from collections import namedtuple
+
+def slicer(iterable, width):
+ """
+ Slices iterables into chunks. How does itertools not have this function.
+ """
+ if len(iterable) % width is not 0:
+ raise Exception("Iterable length must be divisable by the width")
+
+ for start in range(0, len(iterable), width):
+ yield iterable[start:start+width]
+
+
+class AnafazeRegister(object):
+ """{0}{4}:
+ {1}
+
+ Data Type: {2}
+ Size: {3}
+ """
+ pass
+
+
+def make_anafaze_register(cls_name, name, register_location, _type, read_only=False, long_description=None):
+ def from_python(cls, value):
+ out_list = []
+ for v in value:
+ out_list.append(_type.from_python(v))
+
+ return "".join(out_list)
+
+ def to_python(cls, value):
+ in_list = []
+ for v in slicer(str(value), _type.byte_size):
+ data = "".join(v)
+ in_list.append(_type.to_python(data)[0])
+
+ if len(in_list) == 1:
+ return in_list[0]
+
+ return in_list
+
+ reg = type(cls_name, (AnafazeRegister,),
+ dict(
+ __doc__ = AnafazeRegister.__doc__.format(
+ name, str(long_description),
+ _type.__class__.__name__, _type.byte_size,
+ " (Read Only)" if read_only else ""),
+ to_python = classmethod(to_python),
+ from_python = classmethod(from_python) if not read_only else classmethod(lambda x,v=None: None),
+ byte_size = _type.byte_size,
+ name = name,
+ type = _type,
+ location = register_location,
+ byte_location = ANA_REG.from_python(register_location),
+ )
+ )
+
+ return reg
+
+
+PropBandGain = make_anafaze_register("PropBandGain", "Proportional Band/Gain", 0x0020, ANA_UC)
+DerivativeTerm = make_anafaze_register("DerivativeTerm", "Derivative Term", 0x0060, ANA_UC)
+IntegralTerm = make_anafaze_register("IntegralTerm", "Integral Term", 0x00A0, ANA_UI)
+EEPROMVersion = make_anafaze_register("EEPROMVersion", "EEPROM Version Code", 0x0BF0, ANA_UC, True)
+AmbientSensorReadings = make_anafaze_register("AmbientSensorReadings", "Ambient Sensor Readings", 0x0720, ANA_SI, True)
+ControllerType = make_anafaze_register("ControllerType", "Controller Type", 0x47F0, ANA_UC, True)
View
21 pyfaze/types.py
@@ -0,0 +1,21 @@
+from struct import pack, unpack, calcsize
+from functools import partial
+from collections import namedtuple
+
+__all__ = ["ANA_REG", "ANA_UI", "ANA_SI", "ANA_UC", "ANA_SC"]
+
+
+def make_anafaze_type(struct_def, name):
+ return type(name, (object,),
+ dict(
+ to_python = partial(unpack, *[struct_def]),
+ from_python = partial(pack, *[struct_def]),
+ byte_size = calcsize(struct_def),
+ )
+ )()
+
+
+ANA_REG = ANA_UI = make_anafaze_type("H", "ANA_UI") # Unsigned Integer (16-bit unsigned int)
+ANA_SI = make_anafaze_type("h", "ANA_SI") # Signed Integer (16-bit signed int)
+ANA_UC = make_anafaze_type("B", "ANA_UC") # Unsigned Char (8-bit unsigned char)
+ANA_SC = make_anafaze_type("b", "ANA_SC") # Signed Char (8-bit signed char)
View
33 pyfaze/util.py
@@ -0,0 +1,33 @@
+import re
+
+def calc_bcc(data):
+ """
+ Calculates a checksum using the Block Character Check method.
+ """
+ bc = 0x00
+ for byte in str(data):
+ b = ord(byte)
+ if b is 0x10:
+ if last is 0x10:
+ bc = bc + b
+ else:
+ bc = bc + b
+ last = b
+ bcc = (~bc & 0xff) + 1
+
+ return chr(bcc & 0xff)
+
+
+def build_packet_bcc(message):
+"""
+ Builds a ANA/AB packet with a BCC checksum.
+"""
+ bcc = calc_bcc(message)
+ out = "\x10\x02%s\x10\x03%s" % (message, bcc)
+
+ return out
+
+
+def decaser(name):
+ s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
+ return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
Please sign in to comment.
Something went wrong with that request. Please try again.