Commit 9ad9be77 authored by James Pallister's avatar James Pallister

Merge pull request #4 from AWhetter/python-prep

Extensions to host receiver
parents 89b51b0c 670a2ae0
bin_PROGRAMS=host_receiver
lib_LTLIBRARIES=pyenergy.la
host_receiver_SOURCES=helper.cpp dataprocessor.cpp libusbinterface.cpp host_receiver.cpp
pyenergy_la_SOURCES=$(host_receiver_SOURCES) pymodule.cpp
host_receiver_CPPFLAGS = $(BOOST_CPPFLAGS)
host_receiver_LDADD = $(BOOST_REGEX_LIBS) $(BOOST_THREAD_LIBS) $(BOOST_SYSTEM_LIBS)
host_receiver_LDFLAGS = $(BOOST_REGEX_LDFLAGS) $(BOOST_THREAD_LDFLAGS) $(BOOST_SYSTEM_LDFLAGS)
pyenergy_la_CPPFLAGS = $(PYTHON_CPPFLAGS)
pyenergy_la_LIBADD = $(BOOST_PYTHON_LIBS) $(BOOST_PYTHON3_LIBS) $(PYTHON_LIBS) $(host_receiver_LDADD)
pyenergy_la_LDFLAGS = $(BOOST_PYTHON_LDFLAGS) $(BOOST_PYTHON3_LDFLAGS) -module -shared -avoid-version $(host_receiver_LDFLAGS)
ACLOCAL_AMFLAGS=-Im4
This diff is collapsed.
......@@ -3,6 +3,9 @@
/* Defined if the requested minimum BOOST version is satisfied */
#undef HAVE_BOOST
/* Define to 1 if you have <boost/python.hpp> */
#undef HAVE_BOOST_PYTHON_HPP
/* Define to 1 if you have <boost/regex.hpp> */
#undef HAVE_BOOST_REGEX_HPP
......
This diff is collapsed.
......@@ -6,7 +6,7 @@ AC_INIT([stm32-host-receiver], [0.0.1], [BUG-REPORT-ADDRESS])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE
AC_PROG_LIBTOOL
LT_INIT
AC_CONFIG_SRCDIR([host_receiver.cpp])
AC_CONFIG_HEADERS([config.h])
......@@ -28,6 +28,87 @@ BOOST_SYSTEM([n])
AC_CHECK_LIB([readline], [main],, [AC_MSG_ERROR([Cannot find readline library. Please install it.])])
AC_CHECK_LIB([usb-1.0], [main],, [AC_MSG_ERROR([Cannot find usb-1.0 library. Please install it.])])
# Python checking adapted from gdb's configure.ac
# It's license, and original code can be obtained from
# http://ftp.gnu.org/gnu/gdb/gdb-7.6.tar.bz2
AC_DEFUN([AC_TRY_LIBPYTHON],
[
version=$1
define([have_libpython_var],$2)
new_CPPFLAGS=$3
new_LIBS=$4
save_CPPFLAGS=$CPPFLAGS
save_LIBS=$LIBS
CPPFLAGS="$CPPFLAGS $new_CPPFLAGS"
LIBS="$LIBS $new_LIBS"
case $version in
python3*)
BOOST_FIND_LIB([python3], [n], [boost/python.hpp],
[], [BOOST_PYTHON_MODULE(empty) {}])
;;
*)
BOOST_PYTHON([n])
;;
esac
found_usable_python=no
AC_MSG_CHECKING([for ${version}])
AC_LINK_IFELSE(AC_LANG_PROGRAM([[#include "Python.h"]],
[[Py_Initialize ();]]),
[have_libpython_var=${version}
found_usable_python=yes
PYTHON_CPPFLAGS=$new_CPPFLAGS
PYTHON_LIBS=$new_LIBS])
CPPFLAGS=$save_CPPFLAGS
LIBS=$save_LIBS
AC_MSG_RESULT([${found_usable_python}])
])
AC_ARG_WITH(python,
AS_HELP_STRING([--with-python=PYTHON], [also build host receiver as a python module (/path/to/python)]),
[],
[with_python=no])
AC_MSG_CHECKING([whether to use python])
AC_MSG_RESULT([$with_python])
if test "${with_python}" = no; then
AC_MSG_WARN([python module will not be compiled])
else
python_bin="${with_python}"
if test ! -f "${python_bin}"; then
AC_MSG_ERROR(unable to find python program ${python_bin})
else
python_includes=`${python_bin}-config --includes`
if test $? != 0; then
AC_MSG_ERROR(failure running python-config --includes)
fi
python_libs=`${python_bin}-config --ldflags`
if test $? != 0; then
AC_MSG_ERROR(failure running python-config --ldflags)
fi
have_libpython=no
python_version=`echo " ${python_libs} " \
| sed -e 's,^.* -l\(python[[0-9]]*[[\.]]*[[0-9]]*\).*$,\1,'`
case "${python_version}" in
python*)
AC_TRY_LIBPYTHON(${python_version}, have_libpython,
${python_includes}, ${python_libs})
;;
*)
AC_MSG_ERROR([unable to determine python version from ${python_libs}])
;;
esac
if test "${have_libpython}" = no; then
AC_MSG_ERROR([no usable python found at ${with_python}])
fi
fi
fi
AC_SUBST(PYTHON_CPPFLAGS)
AC_SUBST(PYTHON_LIBS)
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
......
......@@ -6,6 +6,8 @@
#include "helper.h"
#include <math.h>
#define DEFAULT_OUTPUT "output_results"
using namespace std;
using namespace boost;
using namespace boost::accumulators;
......@@ -15,7 +17,6 @@ DataProcessor::DataProcessor(boost::mutex *m, std::queue<boost::shared_array<uns
mQueue = m;
dQueue = d;
status = 0;
output = fopen("output_results", "w");
cur_time = 0;
doAccumulation = false;
last_tick = 0;
......@@ -24,15 +25,13 @@ DataProcessor::DataProcessor(boost::mutex *m, std::queue<boost::shared_array<uns
resistor = 1.0;
gain = 50.0;
referenceVoltage = 3.0;
/* Don't set output. This should be done with openOutput. */
output = NULL;
}
DataProcessor::~DataProcessor()
{
if(output)
{
fclose(output);
output = NULL;
}
DataProcessor::closeOutput();
}
void DataProcessor::operator()()
......@@ -138,12 +137,18 @@ void DataProcessor::processData()
b2 |= (data[i]&0xF0) << 4;
power = convertToPower(b1);
fprintf(output, "%f %lu\n", power, cur_time);
if (DataProcessor::openedFile())
{
fprintf(output, "%f %lu\n", power, cur_time);
}
addDataItem(power, cur_time);
cur_time += rate;
power = convertToPower(b2);
fprintf(output, "%f %lu\n", power, cur_time);
if (DataProcessor::openedFile())
{
fprintf(output, "%f %lu\n", power, cur_time);
}
addDataItem(power, cur_time);
cur_time += rate;
}
......@@ -181,3 +186,46 @@ float DataProcessor::convertToPower(float v)
return double(v) * 4096. / 4095. / 4095. * double(referenceVoltage) * double(referenceVoltage)
* 2. / double(gain) / double(resistor);
}
int DataProcessor::closeOutput()
{
if (status == 1)
{
return 1;
}
int to_return = 0;
if (output)
{
to_return = fclose(output);
output = NULL;
}
return to_return;
}
int DataProcessor::openOutput()
{
openOutput(DEFAULT_OUTPUT);
}
int DataProcessor::openOutput(std::string output_loc)
{
int to_return = 0;
if (!output)
{
output = fopen(output_loc.c_str(), "w");
to_return = (output == NULL);
}
else
{
to_return = -1;
}
return to_return;
}
int DataProcessor::openedFile()
{
return (output != NULL);
}
......@@ -27,6 +27,11 @@ public:
void setResistor(float);
void setReferenceVoltage(float);
void setGain(float);
int closeOutput();
int openOutput();
int openOutput(std::string output_loc);
int openedFile();
private:
boost::mutex *mQueue;
std::queue<boost::shared_array<unsigned char> > *dQueue;
......
This diff is collapsed.
#include <python.h>
#include <boost/python.hpp>
#include <libusb-1.0/libusb.h>
#include <readline/readline.h>
#include "host_receiver.h"
void processCommand(std::string input);
using namespace boost::python;
int r;
BOOST_PYTHON_MODULE(pyenergy)
{
using namespace boost::python;
int r;
r = libusb_init(NULL);
if (r < 0)
{
......@@ -20,5 +18,21 @@ BOOST_PYTHON_MODULE(pyenergy)
rl_set_prompt("");
def("processCommand", processCommand);
def("connect", cmd_connect);
def("connect_to", cmd_connect_to);
def("getserial", cmd_getserial);
def("setserial", cmd_setserial);
def("setresistor", cmd_setresistor);
def("setrefvoltage", cmd_setrefvoltage);
def("setgain", cmd_setgain);
def("trigger", cmd_trigger);
def("leds", cmd_leds);
def("start", cmd_start);
def("start_with_file", cmd_start_with_file);
def("stop", cmd_stop);
def("power", cmd_power);
def("mode", cmd_mode);
def("help", cmd_help);
def("exit", cmd_exit);
def("quit", cmd_exit);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment