generated from pbosetti/xtemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ The project compiles a static and a dynamic library: the documentation for the l | |
|
||
The library is designed to make the life easier to you if you want to use it within an interpreted language via FFI. As an example, the `matlab` folder contains a Matlab class that wraps the library. I plan to add FFI examples for Python and Ruby too. | ||
|
||
The project also enables quick creation of binary language extensions for scripting languages via [SWIG](http://swig.org). See later on. | ||
|
||
## Supported platforms | ||
|
||
Linux, OS X, and Windows. For the latter, see the dedicated section in this document. | ||
|
@@ -56,6 +58,45 @@ need to download a release version of the library (in zip format) and also be su | |
|
||
At the moment, **the Docker-based cross compilation is not enabled on Windows**. | ||
|
||
## SWIG support | ||
|
||
[SWIG](http://swig.org) is a commandline utility that simplifies the creation of C/C++ wrappers to be compiled into binary libraries for many common scripting languages. | ||
|
||
This project enables that with a special C++ header that can also be parsed by SWIG: `src/mightex.hh`. | ||
|
||
This file also plays as a header only C++ interface to the Mightex1304 library, so if you want to use the librari in a C++ fashion, just `#include "mightex.hh"` and properly link to the libraries, and you are set. | ||
|
||
The file also contains some conditional code that enables SWIG to parse it and generate the proper code. Let's see how it works for lua: | ||
|
||
```sh | ||
$ cd products_host/include | ||
$ swig -lua -c++ -o mightex_lua.cpp mightex.hpp | ||
``` | ||
|
||
this generates the source file `mightex_lua.cpp`, which can be compiled into a shared object run-time loadable by the lua interpreter: | ||
|
||
```sh | ||
$ clang++ mightex_lua.cpp -shared -I /usr/local/opt/[email protected]/include/lua -L /usr/local/opt/[email protected]/lib/ -llua -framework IOKit -framework CoreFoundation ../lib/libusb-1.0.a ../lib/libmightex_static.a -omightex.so | ||
# mightex.so is created... | ||
$ lua -l mightex # this starts the interpreter loading the mightex library | ||
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio | ||
> mightex.version() | ||
Mightex1304 v.0.2.2-9-g8337fbc for x86_64-apple-darwin19.6.0, Release build. | ||
> m = mightex.Mightex1304() | ||
> Found device: Mightex - USB-TCD1304-1 | ||
> Version: 1.3.0 | ||
> SerialNo.: 13-201114-002 | ||
> m:read_frame() | ||
1.0 | ||
> v = m:frame() | ||
> v[0] | ||
2039.0 | ||
``` | ||
|
||
which shall be clear considering that: `mightex` is the name of the module and `Mightex1304` is the name of the class, so `mightex.version()` is a module function and `m:read_frame()` is an object method. | ||
|
||
A Python interface can be rather analogously generated and used with `swig -python -c++ -o mightex_lua.cpp mightex.hpp`. | ||
|
||
## Author | ||
|
||
Paolo Bosetti, University of Trento. |
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
Binary file not shown.
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,85 @@ | ||
#ifdef SWIG | ||
// Generate libraries for scripting languages with SWIG | ||
// e.g., for Lua, generate with swig -lua -c++ -o mightex_lua.cpp mightex.hpp | ||
// then build with: | ||
// clang++ mightex_lua.cpp ../lib/libusb-1.0.a ../lib/libmightex_static.a -I /usr/local/opt/[email protected]/include/lua -L /usr/local/opt/[email protected]/lib/ -llua -shared -framework IOKit -framework CoreFoundation -omightex.so | ||
%module mightex | ||
%{ | ||
#include "mightex.hpp" | ||
%} | ||
%include "mightex1304.h" | ||
%include "std_string.i" | ||
%include "std_vector.i" | ||
namespace std { | ||
%template(vectori) vector<int>; | ||
}; | ||
#endif | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "mightex1304.h" | ||
|
||
std::string version() { return mightex_sw_version(); } | ||
|
||
class Mightex1304 { | ||
private: | ||
mightex_t *m; | ||
std::string _serial; | ||
std::string _version; | ||
uint16_t *_frame_p, *_raw_frame_p; | ||
|
||
public: | ||
Mightex1304() { | ||
m = mightex_new(); | ||
_frame_p = mightex_frame_p(m); | ||
_raw_frame_p = mightex_raw_frame_p(m); | ||
_serial = mightex_serial_no(m); | ||
_version = mightex_version(m); | ||
} | ||
~Mightex1304() { mightex_close(m); } | ||
|
||
std::string serial_no() { return _serial; } | ||
std::string version() { return _version; } | ||
uint16_t pixel_count() { return mightex_pixel_count(m); } | ||
int dark_pixel_count() { return (int)mightex_dark_pixel_count(m); } | ||
|
||
mtx_result_t set_exptime(float t) { | ||
return mightex_set_exptime(m, t); | ||
} | ||
mtx_result_t set_mode(mtx_mode_t mode) { | ||
return mightex_set_mode(m, mode); | ||
} | ||
|
||
mtx_result_t read_frame() { return mightex_read_frame(m); } | ||
|
||
int dark_mean() { return (int)mightex_dark_mean(m); } | ||
|
||
int frame_timestamp() { return (int)mightex_frame_timestamp(m); } | ||
|
||
std::vector<int> frame() { | ||
std::vector<int> data(_frame_p, _frame_p + mightex_pixel_count(m)); | ||
return data; | ||
} | ||
|
||
std::vector<int> raw_frame() { | ||
std::vector<int> data(_raw_frame_p, _raw_frame_p + mightex_pixel_count(m)); | ||
return data; | ||
} | ||
|
||
unsigned int timestamp() { return (unsigned int)mightex_frame_timestamp(m); } | ||
|
||
void apply_filter() { mightex_apply_filter(m, NULL); } | ||
double apply_estimator() { return mightex_apply_estimator(m, NULL); } | ||
|
||
// these are not really usable in scripting languages | ||
#ifndef SWIG | ||
void apply_filter(void *ud) { mightex_apply_filter(m, ud); } | ||
void set_filter(mightex_filter_t *f) { mightex_set_filter(m, f); } | ||
void reset_filter() { mightex_reset_filter(m); } | ||
|
||
double apply_estimator(void *ud) { return mightex_apply_estimator(m, ud); } | ||
void set_estimator(mightex_estimator_t *e) { mightex_set_estimator(m, e); } | ||
void reset_estimator() { mightex_reset_estimator(m); } | ||
#endif | ||
}; |
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