Skip to content

Commit

Permalink
more logging, chang knxPython
Browse files Browse the repository at this point in the history
  • Loading branch information
thelsing committed Aug 30, 2024
1 parent a6563b0 commit f9ba9ac
Show file tree
Hide file tree
Showing 33 changed files with 462 additions and 550 deletions.
3 changes: 0 additions & 3 deletions examples/knx-demo-smal-go/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ libdeps_dir = /tmp/libdeps
src_dir = .

;--- SAMD --------------------------------------------------
; SMALL_GROUPOBJECT just tested with TP on SAMD, but should work also in other environments
[env:zeroUSB]
platform = atmelsam
board = zeroUSB
Expand All @@ -25,7 +24,6 @@ lib_extra_dirs = ../../../

lib_deps =
SPI
https://github.com/thelsing/FlashStorage.git
knx

build_flags =
Expand All @@ -43,7 +41,6 @@ build_flags =

; lib_deps =
; SPI
; https://github.com/thelsing/FlashStorage.git
; knx

; build_flags =
Expand Down
2 changes: 1 addition & 1 deletion examples/knx-linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void setup()
GO_MAX.dataPointType(Dpt(9, 1));
GO_MAX.valueNoSend(-273.0);
GO_RESET.dataPointType(Dpt(1, 15));
GO_RESET.callback(resetCallback);
//GO_RESET.callback(resetCallback);
LOGGER.info("Timeout: %d", knx.paramWord(0));
LOGGER.info("Zykl. senden: %d", knx.paramByte(2));
LOGGER.info("Min/Max senden: %d", knx.paramByte(3));
Expand Down
8 changes: 4 additions & 4 deletions examples/knxPython/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
cmake_minimum_required(VERSION 3.16)
project(knx VERSION 1.5)
project(knxPython VERSION 1.5)

add_subdirectory(pybind11)

pybind11_add_module(knxPython
knxmodule.cpp
)
include_directories(src pybind11/include)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0")
#set(outdir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
#set_target_properties(knx PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${outdir})
set_target_properties(knxPython PROPERTIES OUTPUT_NAME knx)
set_property(TARGET knxPython PROPERTY CXX_STANDARD 11)
#target_compile_definitions(knxPython PUBLIC -DMASK_VERSION=0x57B0)
target_link_libraries(knxPython PRIVATE knx)
target_link_libraries(knxPython PUBLIC knx)
install(TARGETS knxPython LIBRARY DESTINATION .)
14 changes: 0 additions & 14 deletions examples/knxPython/Manifest.in

This file was deleted.

1 change: 0 additions & 1 deletion examples/knxPython/VERSION

This file was deleted.

160 changes: 90 additions & 70 deletions examples/knxPython/knxmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <pybind11/stl_bind.h>
#include <pybind11/functional.h>
#include <pybind11/stl.h>

namespace py = pybind11;

#include <Python.h>
Expand All @@ -18,6 +18,10 @@ namespace py = pybind11;
#include "knx/platform/linux_platform.h"
#include "knx/ip/bau57B0.h"
#include "knx/interface_object/group_object_table_object.h"
#include "knx/util/logger.h"

#define LOGGER Logger::logger("knxmodule")

using namespace Knx;

LinuxPlatform* platform = 0;
Expand All @@ -40,22 +44,33 @@ static std::vector<const char*> argv;

struct StdStringCStrFunctor
{
const char* operator() (const std::string& str) { return str.c_str(); }
const char* operator() (const std::string& str)
{
return str.c_str();
}
};

static void Prepare(std::vector<std::string> args)
static void init()
{
// 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());
Logger::logLevel("knxmodule", Logger::Info);
Logger::logLevel("ApplicationLayer", Logger::Info);
Logger::logLevel("BauSystemBDevice", Logger::Info);
Logger::logLevel("GroupObject", Logger::Info);

/*
// copy args so we control the livetime of the char*
argsVector = args;
argv = std::vector<const char*>(argsVector.size());
std::transform(argsVector.begin(), argsVector.end(), argv.begin(), StdStringCStrFunctor());
for (int i = 0; i < args.size(); i++)
printf("%s\n", args[i].c_str());
argv = std::vector<const char*>(argsVector.size());
std::transform(argsVector.begin(), argsVector.end(), argv.begin(), StdStringCStrFunctor());
*/
platform = new LinuxPlatform();
platform->cmdLineArgs(argv.size(), const_cast<char**>(argv.data()));
bau = new Bau57B0(*platform);

}

static void Destroy()
Expand All @@ -69,7 +84,7 @@ static void Destroy()
static void ReadMemory()
{
if (!bau)
return;
init();

bau->readMemory();
}
Expand All @@ -78,12 +93,12 @@ static void Start()
{
if (running)
return;

if (!bau)
return;
init();

running = true;

bau->enabled(true);

workerThread = std::thread(loop);
Expand All @@ -94,99 +109,104 @@ static void Stop()
{
if (!running)
return;

running = false;
bau->writeMemory();
bau->enabled(false);

workerThread.join();
}

static bool ProgramMode(bool value)
{
if (!bau)
return false;
init();

LOGGER.info("ProgramMode %d", value);
bau->deviceObject().progMode(value);
return bau->deviceObject().progMode();
}

static bool ProgramMode()
{
if (!bau)
return false;
init();

return bau->deviceObject().progMode();
}

static bool Configured()
{
if (!bau)
return false;
init();

return bau->configured();
}


PYBIND11_MODULE(knx, m)
PYBIND11_MODULE(knx, m)
{
m.doc() = "wrapper for knx device lib"; // optional module docstring

m.def("Start", &Start, "Start knx handling thread.");
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("Destroy", &Destroy, "Free object allocated objects.");
m.def("ProgramMode", (bool(*)())&ProgramMode, "get programing mode active.");
m.def("ProgramMode", (bool(*)(bool))&ProgramMode, "Activate / deactivate programing mode.");
m.def("Configured", (bool(*)())&Configured, "get configured status.");
m.def("Configured", (bool(*)())&Configured, "get configured status.");
m.def("ReadMemory", &ReadMemory, "read memory from flash file");
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);

m.def("FlashFilePath", []()
{
if (!platform)
init();

return platform->flashFilePath();
});
m.def("FlashFilePath", [](std::string path)
{
if (!platform)
init();

platform->flashFilePath(path);
});
m.def("GetGroupObject", [](uint16_t goNr)
{
LOGGER.info("GetGroupObject arg %d", goNr);
LOGGER.info("GetGroupObject entrycount %d", bau->groupObjectTable().entryCount());

if (!bau)
init();

if (goNr > bau->groupObjectTable().entryCount())
return (GroupObject*)nullptr;

return &bau->groupObjectTable().get(goNr);
}, py::return_value_policy::reference);
m.def("Callback", [](GroupObjectUpdatedHandler handler)
{
GroupObject::classCallback(handler);
});

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());
go.objectWritten();
})
.def_property("callback",
[](GroupObject& go)
{
return go.callback();
},
[](GroupObject& go, GroupObjectUpdatedHandler handler)
{
go.callback(handler);
}
)
.def("callBack", (void(GroupObject::*)(GroupObjectUpdatedHandler))&GroupObject::callback);
.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();
});

}
Loading

0 comments on commit f9ba9ac

Please sign in to comment.