knx/examples/knxPython/knxmodule.cpp

199 lines
4.4 KiB
C++
Raw Permalink 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>
2024-09-14 11:55:51 +02:00
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
#include "linux_platform.h"
#include "knx/bau57B0.h"
#include "knx/group_object_table_object.h"
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
{
2024-09-14 11:55:51 +02:00
const char* operator() (const std::string& str)
{
return str.c_str();
}
2019-06-29 10:30:58 +02:00
};
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;
2024-09-14 11:55:51 +02:00
for (int i = 0; i < args.size(); i++)
2019-06-29 16:40:29 +02:00
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;
2024-09-14 11:55:51 +02:00
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;
2024-09-14 11:55:51 +02:00
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;
2024-09-14 11:55:51 +02:00
2019-01-21 23:31:53 +01:00
running = false;
2019-06-29 00:48:57 +02:00
bau->writeMemory();
bau->enabled(false);
2024-09-14 11:55:51 +02:00
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
2024-09-14 11:55:51 +02: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.");
2024-09-14 11:55:51 +02:00
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.");
2024-09-14 11:55:51 +02: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");
2024-09-14 11:55:51 +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)
{
if (!bau || goNr > bau->groupObjectTable().entryCount())
return (GroupObject*)nullptr;
return &bau->groupObjectTable().get(goNr);
}, py::return_value_policy::reference);
py::class_<GroupObject>(m, "GroupObject", py::dynamic_attr())
2024-09-14 11:55:51 +02:00
.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());
go.objectWritten();
})
.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
}