Skip to content

Commit

Permalink
Added support for SWIG
Browse files Browse the repository at this point in the history
  • Loading branch information
pbosetti committed Jun 14, 2021
1 parent 8337fbc commit 3250a49
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ configure_file(
)
file(GLOB LIB_SOURCES "${SOURCE_DIR}/*.c")
file(GLOB HEADERS "${SOURCE_DIR}/*.h")
file(GLOB HEADERS "${SOURCE_DIR}/*.hh")
if(WIN32) # On windows, getopt is missing, provide local implementation
file(GLOB WIN_LIB_SOURCES "${CMAKE_CURRENT_LIST_DIR}/win/src/*.c")
file(GLOB WIN_HEADERS "${CMAKE_CURRENT_LIST_DIR}/win/include/*.h")
Expand Down
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
15 changes: 14 additions & 1 deletion README_binary.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,26 @@ cd examples
gcc grab.c -I../include -L../lib ../lib/libusb-1.0.a ../lib/libmightex_static.a -pthread -o grab
```

If you prefer dynamic linking:
```sh
clang grab.c -I../include -L../lib -pthread -lmightex -pthread -o grab
```
then move `libmightex.so` in the executable directory, or change `LD_LIBRARY_PATH` suitably.

### MacOS

```sh
cd examples
clang grab.c -I../include -L../lib ../lib/libusb-1.0.a ../lib/libmightex_static.a -pthread -framework IOKit -framework CoreFoundation -o grab
clang grab.c -I../include -L../lib ../lib/libusb-1.0.a ../lib/libmightex_static.a -framework IOKit -framework CoreFoundation -o grab
```

If you prefer dynamic linking:
```sh
clang grab.c -I../include -L../lib -lmightex -pthread -o grab -rpath @executable_path/../lib
```

Note that the file `libmightex.dylib` is not signed, so you will have to enable it in System Preferences > Security and Privacy.

### Windows

```sh
Expand Down
4 changes: 4 additions & 0 deletions matlab/mightex.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ function delete(obj)
disp("Mightex connection closed. Remember to call 'unloadlibrary libmightex'!");
end

function cleanBuffer(obj)
calllib('libmightex', 'mightex_set_mode', obj.Mtx, 0);
end

function setExposureTime(obj, value)
%setExposureTime Set exposure time to a value in ms (minimum: 0.1 ms)
obj.ExposureTime = value;
Expand Down
Binary file modified matlab/starter.mlx
Binary file not shown.
85 changes: 85 additions & 0 deletions src/mightex.hh
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
};
15 changes: 14 additions & 1 deletion src/mightex1304.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#endif // _WIN32
#include "defines.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Number of standard pixels
*/
Expand Down Expand Up @@ -47,6 +51,8 @@ typedef enum { MTX_NORMAL_MODE = 0, MTX_TRIGGER_MODE = 1 } mtx_mode_t;
*/
typedef enum { MTX_FAIL = 0, MTX_OK = 1 } mtx_result_t;

#ifndef SWIG

/**
* @brief Opaque structure encapsulating the driver
*/
Expand Down Expand Up @@ -363,4 +369,11 @@ uint16_t mightex_pixel_count(mightex_t *m);
DLLEXPORT
uint16_t mightex_dark_pixel_count(mightex_t *m);
/**@}*/
#endif

#endif //SWIG

#ifdef __cplusplus
}
#endif

#endif // double inclusion guard

0 comments on commit 3250a49

Please sign in to comment.