knx/knxPython/knxmodule.cpp

113 lines
2.9 KiB
C++
Raw Normal View History

2018-12-22 01:55:08 +01:00
#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>
#include <pybind11/functional.h>
2018-12-22 01:55:08 +01:00
namespace py = pybind11;
2018-12-11 22:42:13 +01:00
#include <Python.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
2018-12-22 01:55:08 +01:00
#include <thread>
#include <stdint.h>
#include <vector>
2018-12-11 22:42:13 +01:00
#include "linux_platform.h"
#include "knx/bau57B0.h"
#include "knx/group_object_table_object.h"
2018-12-22 01:55:08 +01:00
LinuxPlatform platform;
Bau57B0 bau(platform);
2018-12-11 22:42:13 +01:00
2019-01-21 23:31:53 +01:00
bool running = false;
2018-12-22 01:55:08 +01:00
static void loop()
2018-12-11 22:42:13 +01:00
{
2019-01-21 23:31:53 +01:00
while (running)
2018-12-11 22:42:13 +01:00
{
bau.loop();
2018-12-22 01:55:08 +01:00
platform.mdelay(100);
2018-12-11 22:42:13 +01:00
}
}
2018-12-22 01:55:08 +01:00
static std::thread workerThread;
2018-12-11 22:42:13 +01:00
2019-01-21 23:31:53 +01:00
2018-12-22 01:55:08 +01:00
static void Start()
2018-12-11 22:42:13 +01:00
{
2019-01-21 23:31:53 +01:00
if (running)
2018-12-22 01:55:08 +01:00
return;
2019-01-21 23:31:53 +01:00
running = true;
bau.readMemory();
bau.enabled(true);
2018-12-11 22:42:13 +01:00
2018-12-22 01:55:08 +01:00
workerThread = std::thread(loop);
workerThread.detach();
2018-12-11 22:42:13 +01:00
}
2019-01-21 23:31:53 +01:00
static void Stop()
{
if (!running)
return;
running = false;
bau.writeMemory();
bau.enabled(false);
workerThread.join();
}
2018-12-22 01:55:08 +01:00
static bool ProgramMode(bool value)
2018-12-11 22:42:13 +01:00
{
bau.deviceObject().progMode(value);
2018-12-22 01:55:08 +01:00
return bau.deviceObject().progMode();
2018-12-11 22:42:13 +01:00
}
2018-12-22 01:55:08 +01:00
static bool ProgramMode()
2018-12-11 22:42:13 +01:00
{
2018-12-22 01:55:08 +01:00
return bau.deviceObject().progMode();
}
2019-01-17 21:36:45 +01:00
static bool Configured()
{
return bau.configured();
}
2018-12-22 01:55:08 +01:00
PYBIND11_MAKE_OPAQUE(std::vector<GroupObject>);
PYBIND11_MODULE(knx, m)
{
m.doc() = "wrapper for knx device lib"; // optional module docstring
2018-12-22 01:55:08 +01:00
py::bind_vector<std::vector<GroupObject>>(m, "GroupObjectList");
m.def("Start", &Start, "Start knx handling thread.");
2019-01-21 23:31:53 +01:00
m.def("Stop", &Start, "Stop knx handling thread.");
m.def("ProgramMode", (bool(*)())&ProgramMode, "get programing mode active.");
2018-12-22 01:55:08 +01:00
m.def("ProgramMode", (bool(*)(bool))&ProgramMode, "Activate / deactivate programing mode.");
2019-01-17 21:38:02 +01:00
m.def("Configured", (bool(*)())&Configured, "get configured status.");
2019-01-21 21:29:00 +01:00
m.def("FlashFilePath", []() { return platform.flashFilePath(); });
m.def("FlashFilePath", [](std::string path) { platform.flashFilePath(path); });
m.def("GetGroupObject", [](uint16_t goNr) { return bau.groupObjectTable().get(goNr); });
2018-12-22 01:55:08 +01:00
py::class_<GroupObject>(m, "GroupObject", py::dynamic_attr())
.def(py::init())
.def("objectWrite", (void(GroupObject::*)(float))&GroupObject::objectWrite)
.def("asap", &GroupObject::asap)
.def("size", &GroupObject::valueSize)
.def_property("value",
[](GroupObject& go) { return py::bytes((const char*)go.valueRef(), go.valueSize()); },
[](GroupObject& go, py::bytes bytesValue)
{
const auto value = static_cast<std::string>(bytesValue);
if (value.length() != go.valueSize())
throw std::length_error("bytesValue");
auto valueRef = go.valueRef();
memcpy(valueRef, value.c_str(), go.valueSize());
2019-01-17 21:36:45 +01:00
go.objectWritten();
})
.def("callBack", (void(GroupObject::*)(GroupObjectUpdatedHandler))&GroupObject::callback);
2018-12-22 01:55:08 +01:00
}