Skip to content

Commit

Permalink
working with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
walchko committed Dec 13, 2023
1 parent 958020b commit ae91357
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 3 deletions.
8 changes: 8 additions & 0 deletions python/tests/messages/cal.yivo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
float[12] cals
uint8 sensor

<enum types uint8_t
accels = 1
gyros = 2
mags = 4
enum>
18 changes: 18 additions & 0 deletions python/tests/messages/imu.yivo
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
include vec

<enum error uint32_t
none = 0
accels = 1
gyros = 2
mags = 4
pressure = 8
temperature = 16
enum>

vec accel
vec gyro
vec mag
float pressure
float temperature
uint32 error
uint32 timestamp
26 changes: 26 additions & 0 deletions python/tests/messages/quat.yivo
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
float w
float x
float y
float z

fru fru

<c
float magnitude() {
return sqrtf(w*w + x*x + y*y + z*z);
}

vec_t normalize() {
float mag = magnitude();
return quat_t{w/mag,x/mag,y/mag,z/mag};
}
c>

<p
def magnitude(self):
return sqrt(self.x*self.x + self.y*self.y + self.z*self.z)

def normalize(self):
mag = self.magnitude()
return quat_t(self.w/mag, self.x/mag, self.y/mag, self.z/mag)
p>
19 changes: 19 additions & 0 deletions python/tests/messages/vec.yivo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
float x
float y
float z

<c
float magnitude() {
return sqrtf(x*x + y*y + z*z);
}

vec_t normalize() {
float mag = magnitude();
return vec_t{x/mag,y/mag,z/mag};
}
c>

<p
def normalize(self):
return sqrt(self.x*self.x + self.y*self.y + self.z*self.z)
p>
37 changes: 37 additions & 0 deletions python/tests/test_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from yivo import *
from collections import namedtuple
import pytest
from numpy import pi, allclose

from yivo.generator.yivo_gen import main

def test_generator():
info = {
"namespace": "foobar",
"license": "MIT Kevin Walchko (c) 2023",
"output": "tmp",
1: "tests/messages/vec.yivo",
2: "tests/messages/quat.yivo",
4: "tests/messages/imu.yivo",
5: "tests/messages/cal.yivo"
}

main(info)

from tmp.python.vec_t import vec_t
from tmp.python.base import fmt, cls, sizeof, id2str, msg_id

v = vec_t(1,2,3)

assert fmt(v) == "fff"
assert sizeof(v) == 12
assert cls(v) == vec_t
assert id2str(msg_id(v)) == "vec_t"

from tmp.python.quat_t import quat_t

q = quat_t(1,2,3,4)
assert fmt(q) == "ffff"
assert sizeof(q) == 16
assert cls(q) == quat_t
assert id2str(msg_id(q)) == "quat_t"
1 change: 0 additions & 1 deletion python/yivo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from .packet import Errors
from .packet import checksum
from .packet import num_fields
# from .packet import make_Struct # done internally now
from .packet import MsgInfo

__copyright__ = 'Copyright (c) 2020 Kevin Walchko'
Expand Down
2 changes: 2 additions & 0 deletions python/yivo/generator/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def create_python(msg_parts, out_path):
if c: line += f" {c}"
vars.append(line)

# print(">>", msg_parts.file.stem)

tmpl = env.get_template("msg.py.jinja")
info = {
"name": msg_parts.file.stem,
Expand Down
3 changes: 2 additions & 1 deletion python/yivo/generator/templates/base.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import dataclasses

def id2str(id):
{% for mid, name in ids.items() -%}
if id == {{ mid }}: return "{{ name|upper }}"
if id == {{ mid }}: return "{{ name|lower }}_t"
{% endfor -%}
return "UNKNOWN"

Expand All @@ -31,6 +31,7 @@ def sizeof(a):

def cls(a):
return a.__yivo__()[2]
{# return a.__class__ #}

def msg_id(a):
return a.__yivo__()[3]
2 changes: 2 additions & 0 deletions python/yivo/generator/templates/msg.py.jinja
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Do not edit, this is autogenerated
# {{ license_notice }}
###############################################################################
from dataclasses import dataclass
from enum import IntEnum
from .base import Base
{% for inc in includes %}
{{ inc }}
{% endfor %}
Expand Down
15 changes: 14 additions & 1 deletion python/yivo/generator/yivo_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@

Field = namedtuple("Field", "type array_size var comment")

py_init = """
# this file is auto generated
from .base import fmt
from .base import sizeof
from .base import cls
from .base import msg_id
from .base import id2str
"""

class Enums:
def __init__(self, name, size=None):
self.name = name
Expand Down Expand Up @@ -163,7 +172,8 @@ def main(files):
# create the base headers for messages ------------
ids = {}
for k,v in files.items():
ids[k] = v.split('.')[0]
f = pathlib.Path(v)
ids[k] = f.stem

baseinfo = [
("base.py.jinja", ids, "py"),
Expand All @@ -188,6 +198,9 @@ def main(files):

# create messages from each template ---------------
path = pathlib.Path(".").absolute()

write_file(pydir/"__init__.py", py_init)

for msgid, file in files.items():
file = path/file
msg_parts = tokenize(file)
Expand Down
7 changes: 7 additions & 0 deletions python/yivo/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

def make_Struct(payload):
"""
Wraps the payload format inside the header/footer format
yivo uses.
[ 0, 1, 2, 3,4, 5:-2, -1]
[h0,h1,LN,HN,T, payload, CS]
Header: h0, h1
Expand Down Expand Up @@ -199,6 +202,10 @@ def __unpack(self):
return Errors.NONE, val

def valid_msg(self, msg):
"""
Checks message to make sure it is valid, returns
True if the message is correct, False otherwise.
"""
size, msgid, payload, cs = chunk(msg)

a = ord(self.header[0])
Expand Down

0 comments on commit ae91357

Please sign in to comment.