-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #863 from jpreiss/pybindings
Python bindings draft
- Loading branch information
Showing
8 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
%module cffirmware | ||
|
||
// ignore GNU specific compiler attributes | ||
#define __attribute__(x) | ||
|
||
%{ | ||
#define SWIG_FILE_WITH_INIT | ||
#include "math3d.h" | ||
%} | ||
|
||
%include "math3d.h" | ||
|
||
%inline %{ | ||
%} | ||
|
||
%pythoncode %{ | ||
import numpy as np | ||
%} | ||
|
||
#define COPY_CTOR(structname) \ | ||
structname(struct structname const *x) { \ | ||
struct structname *y = malloc(sizeof(struct structname)); \ | ||
*y = *x; \ | ||
return y; \ | ||
} \ | ||
~structname() { \ | ||
free($self); \ | ||
} \ | ||
|
||
%extend vec { | ||
COPY_CTOR(vec) | ||
|
||
%pythoncode %{ | ||
def __repr__(self): | ||
return "({}, {}, {})".format(self.x, self.y, self.z) | ||
|
||
def __array__(self): | ||
return np.array([self.x, self.y, self.z]) | ||
|
||
def __len__(self): | ||
return 3 | ||
|
||
def __getitem__(self, i): | ||
if 0 <= i and i < 3: | ||
return _cffirmware.vindex(self, i) | ||
else: | ||
raise IndexError("vec index must be in {0, 1, 2}.") | ||
|
||
# Unary operator overloads. | ||
def __neg__(self): | ||
return _cffirmware.vneg(self) | ||
|
||
# Vector-scalar binary operator overloads. | ||
def __rmul__(self, s): | ||
return _cffirmware.vscl(s, self) | ||
|
||
def __div__(self, s): | ||
return self.__truediv__(s) | ||
|
||
def __truediv__(self, s): | ||
return _cffirmware.vdiv(self, s) | ||
|
||
# Vector-vector binary operator overloads. | ||
def __add__(self, other): | ||
return _cffirmware.vadd(self, other) | ||
|
||
def __sub__(self, other): | ||
return _cffirmware.vsub(self, other) | ||
%} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Compiles the cffirmware C extension.""" | ||
|
||
import distutils.command.build | ||
from distutils.core import setup, Extension | ||
import os | ||
|
||
fw_dir = "." | ||
include = [ | ||
os.path.join(fw_dir, "src/modules/interface"), | ||
] | ||
|
||
modules = [ | ||
# list firmware c-files here | ||
] | ||
fw_sources = [os.path.join(fw_dir, "src/modules/src", mod) for mod in modules] | ||
|
||
cffirmware = Extension( | ||
"_cffirmware", | ||
include_dirs=include, | ||
sources=fw_sources + ["bin/cffirmware_wrap.c"], | ||
extra_compile_args=[ | ||
"-O3", | ||
], | ||
) | ||
|
||
# Override build command to specify custom "build" directory | ||
class BuildCommand(distutils.command.build.build): | ||
def initialize_options(self): | ||
distutils.command.build.build.initialize_options(self) | ||
self.build_base = "bin" | ||
|
||
setup( | ||
name="cffirmware", | ||
version="1.0", | ||
cmdclass={"build": BuildCommand}, | ||
ext_modules=[cffirmware] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env python | ||
|
||
import numpy as np | ||
import cffirmware | ||
|
||
def test_conversion_to_numpy(): | ||
v_cf = cffirmware.mkvec(1, 2, 3) | ||
v_np = np.array(v_cf) | ||
assert np.allclose(v_np, np.array([1,2,3])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
|
||
scriptDir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) | ||
|
||
make test_python "${@}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters