I am re-engineering the DJI Robomaster S1 App in Python based on code in Golang (https://git.bug-br.org.br/bga/robomasters1/src/master/app/internal/dji/unity/bridge/wrapper). In this code they used a unitybridge.dll that does most of the communication with the robot. The communication with this dll mostly works through (ansynchronous) callbacks. I used ctypes for accessing the code in C.
I used a C-library "libffcall" that returns a pointer to a callback (also used in Go). Since the code is very complex I will make a small example here that should work, but doesn't. Normally this should create, init the bridge and start a connection (by sending a specific event). These all seem to work. When setting a callback when a specific event (chosen by me since it occurs every few seconds) occurs in the bridge nothing happens. \
alloc_helper.c: makes the callback by using libffcall. To make things as simple as possible, the callback just prints something:
#include <stdio.h>
#include "alloc_helper.h"
#include "event_callback_windows.h"
#include <stdlib.h>
callback_t alloc_callback_python(void* data) {
return alloc_callback(&event_callback, data);
}
event_callback_windows.c: should be called by unitybridge.dll and will eventually call the python function (not done in this example)
void event_callback(void* context, va_alist alist) {
prinf("This should print");
}
example.py: the values in the arguments are the same as in Go
from ctypes import *
import time
# load the C-code
dll = CDLL("./unitybridge.dll")
alloc_helper = CDLL("./alloc_helper.so")
# create bridge args: str name, bool debuggable
dll.CreateUnityBridge(c_char_p(b"Robomaster", c_int(1))
# init bridge (returns True if successfull)
b = dll.UnityBridgeInitialize()
print("BOOL after init: {}".format(b))
# send event that starts connection
# args: event_code, data, sub_type
dll.UnitySendEvent(c_unint64(100 << 32), None, 0)
# set returntype
alloc_helper.alloc_callback_python.restype = POINTER(c_int)
# make callback
cb = alloc_helper.alloc_callback_python(None) # None to make is as simple as possible
# set event callback
# args: event_code (when this events occurs: call cb), callback
dll.UnitySetEventCallback(c_uint(304 << 32), cb)
time.sleep(3) # to give the bridge some time to make callbacks
# uninit and destroy bridge
dll.UnityBridgeUninitialze() # typo is not a mistake -> defined that way in dll
dll.DestroyUnityBridge()
time.sleep(5)
This is my first post, so feel free to ask more information since I probably forgot some. I tried (almost) everything and really want to fix this.
Thanks in advance <3