From e380f3a9d4ef25c32ac875c5add1828adbf22104 Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Mon, 1 Sep 2025 11:38:36 -0400 Subject: [PATCH] Add beginnings of custom arduino devices. --- src/monocoque/devices/serial/arduino.c | 44 ++++++++++++++++++++++---- src/monocoque/devices/serial/arduino.h | 3 +- src/monocoque/devices/serialdevice.c | 27 ++++++++++++++-- src/monocoque/devices/serialdevice.h | 13 ++++---- src/monocoque/helper/confighelper.c | 5 +++ src/monocoque/helper/confighelper.h | 3 +- 6 files changed, 79 insertions(+), 16 deletions(-) diff --git a/src/monocoque/devices/serial/arduino.c b/src/monocoque/devices/serial/arduino.c index 0d05cc6..ce83549 100644 --- a/src/monocoque/devices/serial/arduino.c +++ b/src/monocoque/devices/serial/arduino.c @@ -134,7 +134,7 @@ int arduino_init(SerialDevice* serialdevice, const char* portdev) -int arduino_customled_init(SerialDevice* serialdevice, const char* portdev, const char* luafile) +int arduino_custom_init(SerialDevice* serialdevice, const char* portdev, const char* luafile, bool uselights) { serialdevice->id = monocoque_serial_open(serialdevice, portdev); @@ -142,11 +142,14 @@ int arduino_customled_init(SerialDevice* serialdevice, const char* portdev, cons int numlights = 0; monocoque_serial_device serialdev = monocoque_serial_devices[serialdevice->id]; - int error = GetNumberOfLeds(serialdevice, &numlights); - - slogi("numlights is %i\n", numlights); - // close, error and free if numlights is 0 - serialdevice->numleds = numlights; + int error = 0; + if(uselights == true) + { + error = GetNumberOfLeds(serialdevice, &numlights); + slogi("numlights is %i\n", numlights); + // close, error and free if numlights is 0 + serialdevice->numleds = numlights; + } if(luafile == NULL) { @@ -394,3 +397,32 @@ int arduino_customled_free(SerialDevice* serialdevice, bool lua) return result; } + +int arduino_custom_update(SerialDevice* serialdevice, SimData* simdata) +{ + + size_t bufsize = 14; + char bytes[bufsize]; + lua_State* L = serialdevice->m.L; + + lua_pushstring(L, "buff"); + lua_settable(L, LUA_REGISTRYINDEX); + + simdata_to_lua(L, simdata); + lua_setglobal(L, "simdata"); + + + lua_getglobal(L,"myFunc"); + if (lua_pcall(L, 0, 0, 0) != LUA_OK) + { + fprintf(stderr, "Error calling Lua script: %s\n", lua_tostring(L, -1)); + } + + + size_t size = sizeof(bytes); + int result = arduino_update(serialdevice, &bytes, size); + + slogt("custom led wrote %i bytes", result); + + return result; +} diff --git a/src/monocoque/devices/serial/arduino.h b/src/monocoque/devices/serial/arduino.h index 33c99b2..8f408a1 100644 --- a/src/monocoque/devices/serial/arduino.h +++ b/src/monocoque/devices/serial/arduino.h @@ -7,8 +7,9 @@ int arduino_simled_update(SerialDevice* serialdevice, SimData* simdata); int arduino_update(SerialDevice* serialdevice, void* data, size_t size); -int arduino_customled_init(SerialDevice* serialdevice, const char* portdev, const char* luafile); +int arduino_custom_init(SerialDevice* serialdevice, const char* portdev, const char* luafile, bool useleds); int arduino_customled_update(SerialDevice* serialdevice, SimData* simdata); +int arduino_custom_update(SerialDevice* serialdevice, SimData* simdata); int arduino_init(SerialDevice* serialdevice, const char* portdev); int arduino_free(SerialDevice* serialdevice); int arduino_customled_free(SerialDevice* serialdevice, bool lua); diff --git a/src/monocoque/devices/serialdevice.c b/src/monocoque/devices/serialdevice.c index 1594bc3..85947b4 100644 --- a/src/monocoque/devices/serialdevice.c +++ b/src/monocoque/devices/serialdevice.c @@ -34,6 +34,15 @@ int serialdev_update(SimDevice* this, SimData* simdata) return 0; } +int arduino_custom_updater(SimDevice* this, SimData* simdata) +{ + SerialDevice* serialdevice = (void *) this->derived; + + arduino_custom_update(serialdevice, simdata); + + return 0; +} + int arduino_customled_updater(SimDevice* this, SimData* simdata) { SerialDevice* serialdevice = (void *) this->derived; @@ -219,14 +228,22 @@ int serialdev_init(SerialDevice* serialdevice, DeviceSettings* ds) break; case ARDUINODEV__SIMLED__CUSTOM: serialdevice->m.device_specific_config_file = strdup(ds->specific_config_file); - error = arduino_customled_init(serialdevice, ds->serialdevsettings.portdev, serialdevice->m.device_specific_config_file); + error = arduino_custom_init(serialdevice, ds->serialdevsettings.portdev, serialdevice->m.device_specific_config_file, true); + if(error < 0) + { + free(serialdevice->m.device_specific_config_file); + } + break; + case ARDUINODEV__CUSTOM: + serialdevice->m.device_specific_config_file = strdup(ds->specific_config_file); + error = arduino_custom_init(serialdevice, ds->serialdevsettings.portdev, serialdevice->m.device_specific_config_file, false); if(error < 0) { free(serialdevice->m.device_specific_config_file); } break; case ARDUINODEV__SIMLED: - error = arduino_customled_init(serialdevice, ds->serialdevsettings.portdev, NULL); + error = arduino_custom_init(serialdevice, ds->serialdevsettings.portdev, NULL, false); break; default: error = arduino_init(serialdevice, ds->serialdevsettings.portdev); @@ -240,6 +257,7 @@ int serialdev_init(SerialDevice* serialdevice, DeviceSettings* ds) static const vtable serial_simdevice_vtable = { &serialdev_update, &serialdev_free }; static const vtable arduino_shiftlights_vtable = { &arduino_shiftlights_update, &serialdev_free }; static const vtable arduino_simled_vtable = { &arduino_simled_updater, &serialdev_free }; +static const vtable arduino_custom_vtable = { &arduino_custom_updater, &serialdev_free }; static const vtable arduino_simled_custom_vtable = { &arduino_customled_updater, &serialdev_free }; static const vtable arduino_simwind_vtable = { &arduino_simwind_update, &serialdev_free }; static const vtable arduino_simhaptic_vtable = { &arduino_simhaptic_update, &serialdev_free }; @@ -288,6 +306,11 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms) { this->m.vtable = &arduino_simwind_vtable; slogi("Initializing arduino devices for sim wind."); break; + case (SIMDEVTYPE_ARDUINOCUSTOM): + this->devicetype = ARDUINODEV__CUSTOM; + this->m.vtable = &arduino_simwind_vtable; + slogi("Initializing custom arduino device."); + break; case (SIMDEVTYPE_SERIALHAPTIC): this->devicetype = ARDUINODEV__HAPTIC; this->m.vtable = &arduino_simhaptic_vtable; diff --git a/src/monocoque/devices/serialdevice.h b/src/monocoque/devices/serialdevice.h index e77ac27..c88cb73 100644 --- a/src/monocoque/devices/serialdevice.h +++ b/src/monocoque/devices/serialdevice.h @@ -6,12 +6,13 @@ typedef enum { - ARDUINODEV__SHIFTLIGHTS = 0, - ARDUINODEV__SIMWIND = 1, - ARDUINODEV__HAPTIC = 2, - SERIALDEV__MOZAR5 = 3, - ARDUINODEV__SIMLED = 4, - ARDUINODEV__SIMLED__CUSTOM = 5 + ARDUINODEV__SHIFTLIGHTS = 0, + ARDUINODEV__SIMWIND = 1, + ARDUINODEV__HAPTIC = 2, + SERIALDEV__MOZAR5 = 3, + ARDUINODEV__SIMLED = 4, + ARDUINODEV__SIMLED__CUSTOM = 5, + ARDUINODEV__CUSTOM = 6 } SerialDeviceType; diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index 07ecefe..5e8c4e6 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -160,6 +160,11 @@ int strtodevsubtype(const char* device_subtype, DeviceSettings* ds, int simdev) ds->dev_subtype = SIMDEVTYPE_SIMLED; break; } + if (strcicmp(device_subtype, "ArduinoCustom") == 0) + { + ds->dev_subtype = SIMDEVTYPE_ARDUINOCUSTOM; + break; + } if (strcicmp(device_subtype, "SimWind") == 0) { ds->dev_subtype = SIMDEVTYPE_SIMWIND; diff --git a/src/monocoque/helper/confighelper.h b/src/monocoque/helper/confighelper.h index c235445..f33b3cf 100644 --- a/src/monocoque/helper/confighelper.h +++ b/src/monocoque/helper/confighelper.h @@ -30,7 +30,8 @@ typedef enum SIMDEVTYPE_SERIALHAPTIC = 5, SIMDEVTYPE_USBWHEEL = 6, SIMDEVTYPE_SERIALWHEEL = 7, - SIMDEVTYPE_SIMLED = 8 + SIMDEVTYPE_SIMLED = 8, + SIMDEVTYPE_ARDUINOCUSTOM = 9 } DeviceSubType;