Add beginnings of custom arduino devices.

This commit is contained in:
Paul Dino Jones 2025-09-01 11:38:36 -04:00
parent 433e2423e8
commit e380f3a9d4
6 changed files with 79 additions and 16 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;