knx/examples/knxPython/knxmodule.cpp

192 lines
4.4 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>
2019-06-29 10:30:58 +02:00
#include <pybind11/stl.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>
2019-06-29 10:30:58 +02:00
#include <algorithm>
2018-12-11 22:42:13 +01:00
2024-08-19 18:07:27 +02:00
#include "knx/bits.h"
2024-08-16 22:19:48 +02:00
#include "knx/platform/linux_platform.h"
2024-08-20 01:37:35 +02:00
#include "knx/ip/bau57B0.h"
#include "knx/interface_object/group_object_table_object.h"
2018-12-11 22:42:13 +01:00
2019-06-29 00:48:57 +02:00
LinuxPlatform* platform = 0;
Bau57B0* bau = 0;
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
{
2019-06-29 00:48:57 +02:00
bau->loop();
delayMicroseconds(100);
2018-12-11 22:42:13 +01:00
}
}
2018-12-22 01:55:08 +01:00
static std::thread workerThread;
2019-06-29 10:37:33 +02:00
static std::vector<std::string> argsVector;
2019-06-29 10:30:58 +02:00
static std::vector<const char*> argv;
2018-12-11 22:42:13 +01:00
2019-06-29 10:30:58 +02:00
struct StdStringCStrFunctor
2019-06-29 00:50:08 +02:00
{
2019-06-29 10:30:58 +02:00
const char* operator() (const std::string& str) { return str.c_str(); }
};
static void Prepare(std::vector<std::string> args)
{
2019-06-29 16:40:29 +02:00
// copy args so we control the livetime of the char*
argsVector = args;
for(int i = 0; i < args.size(); i++)
printf("%s\n", args[i].c_str());
2019-06-29 10:30:58 +02:00
2019-06-29 16:40:29 +02:00
argv = std::vector<const char*>(argsVector.size());
std::transform(argsVector.begin(), argsVector.end(), argv.begin(), StdStringCStrFunctor());
2019-06-29 10:30:58 +02:00
2020-10-28 21:44:01 +01:00
platform = new LinuxPlatform();
platform->cmdLineArgs(argv.size(), const_cast<char**>(argv.data()));
2019-06-29 16:40:29 +02:00
bau = new Bau57B0(*platform);
2019-06-29 00:50:08 +02:00
}
static void Destroy()
{
2019-06-29 16:40:29 +02:00
delete platform;
delete bau;
platform = 0;
bau = 0;
}
static void ReadMemory()
{
if (!bau)
return;
bau->readMemory();
2019-06-29 00:50:08 +02: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-06-29 16:40:29 +02:00
if (!bau)
return;
2019-06-29 00:48:57 +02:00
2019-01-21 23:31:53 +01:00
running = true;
2019-06-29 00:48:57 +02:00
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;
2019-06-29 00:48:57 +02:00
bau->writeMemory();
bau->enabled(false);
2019-01-21 23:31:53 +01:00
workerThread.join();
}
2018-12-22 01:55:08 +01:00
static bool ProgramMode(bool value)
2018-12-11 22:42:13 +01:00
{
2019-06-29 16:40:29 +02:00
if (!bau)
return false;
2019-06-29 00:50:08 +02:00
2019-06-29 00:48:57 +02:00
bau->deviceObject().progMode(value);
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
{
2019-06-29 16:40:29 +02:00
if (!bau)
return false;
2019-06-29 00:50:08 +02:00
2019-06-29 00:48:57 +02:00
return bau->deviceObject().progMode();
2018-12-22 01:55:08 +01:00
}
2019-01-17 21:36:45 +01:00
static bool Configured()
{
2019-06-29 16:40:29 +02:00
if (!bau)
return false;
2019-06-29 00:50:08 +02:00
2019-06-29 00:48:57 +02:00
return bau->configured();
2019-01-17 21:36:45 +01:00
}
2018-12-22 01:55:08 +01:00
PYBIND11_MODULE(knx, m)
{
m.doc() = "wrapper for knx device lib"; // optional module docstring
2018-12-22 01:55:08 +01:00
m.def("Start", &Start, "Start knx handling thread.");
2019-06-29 10:30:58 +02:00
m.def("Stop", &Stop, "Stop knx handling thread.");
m.def("Prepare", &Prepare, "Allocated needed objects.");
m.def("Destroy", &Destroy, "Free object allocated by Prepare.");
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-06-29 16:40:29 +02:00
m.def("ReadMemory", &ReadMemory, "read memory from flash file");
2019-06-29 00:50:08 +02:00
m.def("FlashFilePath", []()
{
if(!platform)
return std::string("");
return platform->flashFilePath();
});
m.def("FlashFilePath", [](std::string path)
{
if(!platform)
return;
platform->flashFilePath(path);
});
m.def("GetGroupObject", [](uint16_t goNr)
{
2019-06-30 00:31:31 +02:00
if(!bau || goNr > bau->groupObjectTable().entryCount())
2019-06-29 16:40:29 +02:00
return (GroupObject*)nullptr;
2019-06-29 00:50:08 +02:00
2019-06-29 16:40:29 +02:00
return &bau->groupObjectTable().get(goNr);
}, py::return_value_policy::reference);
2018-12-22 01:55:08 +01:00
py::class_<GroupObject>(m, "GroupObject", py::dynamic_attr())
.def(py::init())
.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();
})
2019-06-29 16:40:29 +02:00
.def_property("callback",
[](GroupObject& go)
{
return go.callback();
},
[](GroupObject& go, GroupObjectUpdatedHandler handler)
{
go.callback(handler);
}
)
.def("callBack", (void(GroupObject::*)(GroupObjectUpdatedHandler))&GroupObject::callback);
2019-06-29 10:30:58 +02:00
}