diff --git a/src/monocoque/devices/serial/arduino.c b/src/monocoque/devices/serial/arduino.c index f134ab8..1138142 100644 --- a/src/monocoque/devices/serial/arduino.c +++ b/src/monocoque/devices/serial/arduino.c @@ -119,15 +119,14 @@ int arduino_customled_init(SerialDevice* serialdevice, const char* portdev, cons } lua_setglobal(L,"myFunc"); - serialdevice->L = L; + serialdevice->m.L = L; return serialdevice->id; } -int arduino_simled_update(SimDevice* this, SimData* simdata) +int arduino_simled_update(SerialDevice* serialdevice, SimData* simdata) { - SerialDevice* serialdevice = (void *) this->derived; int result = 1; int total_leds = serialdevice->numleds; @@ -211,9 +210,8 @@ int arduino_simled_update(SimDevice* this, SimData* simdata) return result; } -int arduino_customled_update(SimDevice* this, SimData* simdata) +int arduino_customled_update(SerialDevice* serialdevice, SimData* simdata) { - SerialDevice* serialdevice = (void *) this->derived; int result = 1; int total_leds = serialdevice->numleds; @@ -240,7 +238,7 @@ int arduino_customled_update(SimDevice* this, SimData* simdata) bytes[bufsize-3] = 0xff; - lua_State* L = serialdevice->L; + lua_State* L = serialdevice->m.L; lua_pushstring(L, "buff"); lua_pushlightuserdata(L, &bytes); @@ -325,7 +323,7 @@ int arduino_customled_free(SerialDevice* serialdevice, bool lua) if(lua == true) { - lua_close(serialdevice->L); + lua_close(serialdevice->m.L); } return result; } diff --git a/src/monocoque/devices/serial/arduino.h b/src/monocoque/devices/serial/arduino.h index 29c6484..33c99b2 100644 --- a/src/monocoque/devices/serial/arduino.h +++ b/src/monocoque/devices/serial/arduino.h @@ -5,10 +5,10 @@ #include "../serialdevice.h" -int arduino_simled_update(SimDevice* this, SimData* simdata); +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_customled_update(SimDevice* this, SimData* simdata); +int arduino_customled_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 1577c47..14521c2 100644 --- a/src/monocoque/devices/serialdevice.c +++ b/src/monocoque/devices/serialdevice.c @@ -34,6 +34,24 @@ int serialdev_update(SimDevice* this, SimData* simdata) return 0; } +int arduino_customled_updater(SimDevice* this, SimData* simdata) +{ + SerialDevice* serialdevice = (void *) this->derived; + + arduino_customled_update(serialdevice, simdata); + + return 0; +} + +int arduino_simled_updater(SimDevice* this, SimData* simdata) +{ + SerialDevice* serialdevice = (void *) this->derived; + + arduino_simled_update(serialdevice, simdata); + + return 0; +} + int arduino_shiftlights_update(SimDevice* this, SimData* simdata) { SerialDevice* serialdevice = (void *) this->derived; @@ -149,7 +167,7 @@ int serialdev_free(SimDevice* this) serialdevice->u.simhapticdata.effect4 = 0; serialdevice->u.simhapticdata.motor4 = 1; serialdevice->state = 0; - + size_t size = sizeof(SimHapticData); monocoque_serial_write_block(serialdevice->id, &serialdevice->u.simhapticdata, size, 9000); slogi("set zero to arduino device"); @@ -199,7 +217,7 @@ int serialdev_init(SerialDevice* serialdevice, DeviceSettings* ds) error = moza_init(serialdevice, ds->serialdevsettings.portdev); break; case ARDUINODEV__SIMLED__CUSTOM: - error = arduino_customled_init(serialdevice, ds->serialdevsettings.portdev, ds->serialdevsettings.config_file); + error = arduino_customled_init(serialdevice, ds->serialdevsettings.portdev, ds->specific_config_file); break; case ARDUINODEV__SIMLED: error = arduino_customled_init(serialdevice, ds->serialdevsettings.portdev, NULL); @@ -215,8 +233,8 @@ 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_update, &serialdev_free }; -static const vtable arduino_simled_custom_vtable = { &arduino_customled_update, &serialdev_free }; +static const vtable arduino_simled_vtable = { &arduino_simled_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 }; static const vtable serialwheel_vtable = { &serial_wheel_update, &serial_wheel_free }; diff --git a/src/monocoque/devices/simdevice.h b/src/monocoque/devices/simdevice.h index 1908bc1..a5fa53b 100644 --- a/src/monocoque/devices/simdevice.h +++ b/src/monocoque/devices/simdevice.h @@ -2,13 +2,16 @@ #define _SIMDEVICE_H #include +#include #include "lua.h" #include "usbdevice.h" #include "sounddevice.h" #include "serialdevice.h" + #include "hapticeffect.h" + #include "../helper/confighelper.h" #include "../simulatorapi/simapi/simapi/simdata.h" @@ -27,6 +30,7 @@ struct SimDevice int id; int fps; bool initialized; + lua_State* L; DeviceType type; HapticEffect hapticeffect; }; @@ -51,7 +55,7 @@ typedef struct int id; SerialType type; struct sp_port* port; - lua_State* L; + SerialDeviceType devicetype; // move these two they only apply to the haptic device int motorsposition; @@ -93,6 +97,7 @@ typedef struct { SimDevice m; int id; + hid_device* handle; USBType type; union { @@ -145,4 +150,16 @@ int devfree(SimDevice* simdevices, int numdevices); int simdevfree(SimDevice* this); + +/****** USB SubTypes ****************/ +int wheeldev_update(USBDevice* usbdevice, SimData* simdata); +int wheeldev_init(USBDevice* usbdevice, DeviceSettings* ds); +int wheeldev_free(USBDevice* usbdevice); + +int tachdev_update(USBDevice* tachdevice, SimData* simdata); +int tachdev_init(USBDevice* tachdevice, DeviceSettings* ds); +int tachdev_free(USBDevice* tachdevice); + + + #endif diff --git a/src/monocoque/devices/tachdevice.c b/src/monocoque/devices/tachdevice.c index c4ac26f..e50c502 100644 --- a/src/monocoque/devices/tachdevice.c +++ b/src/monocoque/devices/tachdevice.c @@ -2,14 +2,16 @@ #include #include -#include "tachdevice.h" -#include "revburner.h" -#include "../../helper/confighelper.h" -#include "../../simulatorapi/simapi/simapi/simdata.h" -#include "../../slog/slog.h" +//#include "tachdevice.h" +#include "simdevice.h" +#include "usb/revburner.h" +#include "../helper/confighelper.h" +#include "../simulatorapi/simapi/simapi/simdata.h" +#include "../slog/slog.h" -int tachdev_update(TachDevice* tachdevice, SimData* simdata) +int tachdev_update(USBDevice* usbdevice, SimData* simdata) { + TachDevice* tachdevice = &usbdevice->u.tachdevice; // current plan is to just use the revburner xml format for other possible tachometer devices // with that assumption this same logic is assumed the same for other tachometer devices // the only difference then being in communication to the physical device @@ -43,30 +45,33 @@ int tachdev_update(TachDevice* tachdevice, SimData* simdata) } } slogt("Settings tachometer pulses to %i", pulses); - revburner_update(tachdevice, pulses); + revburner_update(usbdevice, pulses); break; } return 0; } -int tachdev_free(TachDevice* tachdevice) +int tachdev_free(USBDevice* usbdevice) { + TachDevice* tachdevice = &usbdevice->u.tachdevice; switch ( tachdevice->type ) { case TACHDEV_UNKNOWN : case TACHDEV_REVBURNER : - revburner_update(tachdevice, 0); - revburner_free(tachdevice); + revburner_update(usbdevice, 0); + revburner_free(usbdevice); break; } return 0; } -int tachdev_init(TachDevice* tachdevice, DeviceSettings* ds) +int tachdev_init(USBDevice* usbdevice, DeviceSettings* ds) { slogi("initializing tachometer device..."); + + TachDevice* tachdevice = &usbdevice->u.tachdevice; int error = 0; // detection of tach device model tachdevice->type = TACHDEV_UNKNOWN; @@ -74,7 +79,7 @@ int tachdev_init(TachDevice* tachdevice, DeviceSettings* ds) tachdevice->tachsettings = ds->tachsettings; - error = revburner_init(tachdevice); + error = revburner_init(usbdevice); return error; } diff --git a/src/monocoque/devices/tachdevice.h b/src/monocoque/devices/tachdevice.h index 1d22a73..2333a5a 100644 --- a/src/monocoque/devices/tachdevice.h +++ b/src/monocoque/devices/tachdevice.h @@ -1,7 +1,7 @@ #ifndef _TACHDEVICE_H #define _TACHDEVICE_H -#include + #include "../helper/confighelper.h" #include "../simulatorapi/simapi/simapi/simdata.h" @@ -19,13 +19,10 @@ typedef struct int id; TachType type; bool use_pulses; - hid_device* handle; TachometerSettings tachsettings; } TachDevice; -int tachdev_update(TachDevice* tachdevice, SimData* simdata); -int tachdev_init(TachDevice* tachdevice, DeviceSettings* ds); -int tachdev_free(TachDevice* tachdevice); + #endif diff --git a/src/monocoque/devices/usb/revburner.c b/src/monocoque/devices/usb/revburner.c index d4b781e..332c520 100644 --- a/src/monocoque/devices/usb/revburner.c +++ b/src/monocoque/devices/usb/revburner.c @@ -2,13 +2,13 @@ #include -#include "tachdevice.h" -#include "../slog/slog.h" +#include "../simdevice.h" +#include "../../slog/slog.h" const size_t REVBURNER_BUFSIZE = 8; -int revburner_update(TachDevice* tachdevice, int pulses) +int revburner_update(USBDevice* tachdevice, int pulses) { int res = 0; @@ -37,7 +37,7 @@ int revburner_update(TachDevice* tachdevice, int pulses) return res; } -int revburner_free(TachDevice* tachdevice) +int revburner_free(USBDevice* tachdevice) { int res = 0; @@ -47,7 +47,7 @@ int revburner_free(TachDevice* tachdevice) return res; } -int revburner_init(TachDevice* tachdevice) +int revburner_init(USBDevice* tachdevice) { slogi("initializing revburner tachometer..."); //tachdevice->update_tachometer = revburner_device_update; diff --git a/src/monocoque/devices/usb/revburner.h b/src/monocoque/devices/usb/revburner.h index b1a4e67..b691b33 100644 --- a/src/monocoque/devices/usb/revburner.h +++ b/src/monocoque/devices/usb/revburner.h @@ -1,8 +1,10 @@ #ifndef _REVBURNER_H #define _REVBURNER_H -int revburner_update(TachDevice* tachdevice, int pulses); -int revburner_init(TachDevice* tachdevice); -int revburner_free(TachDevice* tachdevice); +#include "../simdevice.h" + +int revburner_update(USBDevice* tachdevice, int pulses); +int revburner_init(USBDevice* tachdevice); +int revburner_free(USBDevice* tachdevice); #endif diff --git a/src/monocoque/devices/usb/wheels/cammusc12.c b/src/monocoque/devices/usb/wheels/cammusc12.c index a847360..9060dd4 100644 --- a/src/monocoque/devices/usb/wheels/cammusc12.c +++ b/src/monocoque/devices/usb/wheels/cammusc12.c @@ -4,17 +4,21 @@ #include #include "cammusc12.h" +#include "../../serial/arduinoledlua.h" #include "../../../slog/slog.h" const int cammusc12_hidupdate_buf_size = 16; +const int cammusc12_hidledupdate_buf_size = 7; -int cammusc12_update(WheelDevice* wheeldevice, int maxrpm, int rpm, int gear, int velocity) +const int cammusc12_total_leds = 15; + +int cammusc12_update(USBDevice* usbdevice, int maxrpm, int rpm, int gear, int velocity) { int res = 0; unsigned char bytes[cammusc12_hidupdate_buf_size]; - + for (int x = 0; x < cammusc12_hidupdate_buf_size; x++) { bytes[x] = 0x00; @@ -43,9 +47,9 @@ int cammusc12_update(WheelDevice* wheeldevice, int maxrpm, int rpm, int gear, in bytes[6] = gear-1; slogt("writing bytes x%02xx%02xx%02xx%02xx%02x%02x%02x from rpm %i velocity %i gear %i", bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], rpm, velocity, gear); - if (wheeldevice->handle) + if (usbdevice->handle) { - res = hid_write(wheeldevice->handle, bytes, cammusc12_hidupdate_buf_size); + res = hid_write(usbdevice->handle, bytes, cammusc12_hidupdate_buf_size); } else { @@ -55,7 +59,7 @@ int cammusc12_update(WheelDevice* wheeldevice, int maxrpm, int rpm, int gear, in return res; } -int cammusc12_free(WheelDevice* wheeldevice) +int cammusc12_free(USBDevice* wheeldevice) { int res = 0; @@ -65,7 +69,7 @@ int cammusc12_free(WheelDevice* wheeldevice) return res; } -int cammusc12_init(WheelDevice* wheeldevice) +int cammusc12_init_(USBDevice* wheeldevice) { slogi("initializing cammus c12 wheel..."); @@ -84,3 +88,119 @@ int cammusc12_init(WheelDevice* wheeldevice) slogd("Found Cammus C12 Wheel..."); return res; } + +int cammusc12_init(USBDevice* wheeldevice, const char* luafile) +{ + + int res = cammusc12_init_(wheeldevice); + if(luafile == NULL) + { + return res; + } + + lua_State* L = luaL_newstate(); + luaL_openlibs(L); + + int top=lua_gettop(L); + int status = luaL_loadfile(L, luafile); + + if (status) { + /* If something went wrong, error message is at the top of */ + /* the stack */ + sloge("There is an issue with your lua script"); + fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1)); + //exit(1); + } + + lua_setglobal(L,"myFunc"); + + wheeldevice->m.L = L; + + return res; +} + + +int cammusc12_customled_update(USBDevice* usbdevice, SimData* simdata) +{ + int result = 1; + + + size_t bufsize = (cammusc12_total_leds * 3); + char ledbytes[bufsize]; + + + lua_State* L = usbdevice->m.L; + + lua_pushstring(L, "buff"); + lua_pushlightuserdata(L, &ledbytes); + lua_settable(L, LUA_REGISTRYINDEX); + + simdata_to_lua(L, simdata); + lua_setglobal(L, "simdata"); + + lua_pushinteger(L, cammusc12_total_leds); + lua_setglobal(L, "TotalLeds"); + + lua_register(L, "set_led_to_color", set_led_to_color); + lua_register(L, "set_led_range_to_color", set_led_range_to_color); + lua_register(L, "set_led_to_rgb_color", set_led_to_rgb_color); + lua_register(L, "set_led_range_to_rgb_color", set_led_range_to_rgb_color); + lua_register(L, "led_clear_all", led_clear_all); + + lua_pushinteger(L, LUALEDCOLOR_RED); + lua_setglobal(L, "RED"); + lua_pushinteger(L, LUALEDCOLOR_GREEN); + lua_setglobal(L, "GREEN"); + lua_pushinteger(L, LUALEDCOLOR_BLUE); + lua_setglobal(L, "BLUE"); + lua_pushinteger(L, LUALEDCOLOR_YELLOW); + lua_setglobal(L, "YELLOW"); + lua_pushinteger(L, LUALEDCOLOR_ORANGE); + lua_setglobal(L, "ORANGE"); + + 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)); + } + + int res = 0; + + unsigned char bytes[cammusc12_hidledupdate_buf_size]; + for(int x = 0; x < cammusc12_hidledupdate_buf_size; x++) + { + bytes[x] = 0x00; + } + bytes[0] = 0xFA; + bytes[1] = 0xFB; + bytes[2] = 0x02; + + for(int i = 0; i < cammusc12_total_leds; i++) + { + for (int x = 3; x < cammusc12_hidledupdate_buf_size; x++) + { + bytes[x] = 0x00; + } + uint8_t led = i; + uint8_t red = ledbytes[(i * 3) + 0]; + uint8_t green = ledbytes[(i * 3) + 1]; + uint8_t blue = ledbytes[(i * 3) + 2]; + + bytes[3] = led; + bytes[4] = red; + bytes[5] = green; + bytes[6] = blue; + + slogt("writing bytes x%02xx%02xx%02xx%02xx%02x%02x%02x from red %i green %i blue %i", bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], red, green, blue); + if (usbdevice->handle) + { + res = hid_write(usbdevice->handle, bytes, cammusc12_hidupdate_buf_size); + } + else + { + slogd("no handle"); + } + } + + return res; +} diff --git a/src/monocoque/devices/usb/wheels/cammusc12.h b/src/monocoque/devices/usb/wheels/cammusc12.h index 004a234..3c43fef 100644 --- a/src/monocoque/devices/usb/wheels/cammusc12.h +++ b/src/monocoque/devices/usb/wheels/cammusc12.h @@ -3,8 +3,10 @@ #include "../../wheeldevice.h" -int cammusc12_update(WheelDevice* wheeldevice, int maxrpm, int rpm, int gear, int velocity); -int cammusc12_init(WheelDevice* wheeldevice); -int cammusc12_free(WheelDevice* wheeldevice); +#include "../../simdevice.h" + +int cammusc12_update(USBDevice* wheeldevice, int maxrpm, int rpm, int gear, int velocity); +int cammusc12_init(USBDevice* wheeldevice, const char* luafile); +int cammusc12_free(USBDevice* wheeldevice); #endif diff --git a/src/monocoque/devices/usb/wheels/cammusc5.c b/src/monocoque/devices/usb/wheels/cammusc5.c index cf85f38..756a9f7 100644 --- a/src/monocoque/devices/usb/wheels/cammusc5.c +++ b/src/monocoque/devices/usb/wheels/cammusc5.c @@ -9,13 +9,13 @@ const int cammusc5_hidupdate_buf_size = 14; const int num_avail_leds = 9; -int cammusc5_update(WheelDevice* wheeldevice, int maxrpm, int rpm, int gear, int velocity) +int cammusc5_update(USBDevice* usbdevice, int maxrpm, int rpm, int gear, int velocity) { int res = 0; unsigned char bytes[cammusc5_hidupdate_buf_size]; - + for (int x = 0; x < cammusc5_hidupdate_buf_size; x++) { bytes[x] = 0x00; @@ -56,9 +56,9 @@ int cammusc5_update(WheelDevice* wheeldevice, int maxrpm, int rpm, int gear, int bytes[4] = gear-1; slogt("writing bytes x%02xx%02xx%02xx%02xx%02x from rpm %i velocity %i gear %i", bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], rpm, velocity, gear); - if (wheeldevice->handle) + if (usbdevice->handle) { - res = hid_write(wheeldevice->handle, bytes, cammusc5_hidupdate_buf_size); + res = hid_write(usbdevice->handle, bytes, cammusc5_hidupdate_buf_size); } else { @@ -68,17 +68,17 @@ int cammusc5_update(WheelDevice* wheeldevice, int maxrpm, int rpm, int gear, int return res; } -int cammusc5_free(WheelDevice* wheeldevice) +int cammusc5_free(USBDevice* usbdevice) { int res = 0; - hid_close(wheeldevice->handle); + hid_close(usbdevice->handle); res = hid_exit(); return res; } -int cammusc5_init(WheelDevice* wheeldevice) +int cammusc5_init(USBDevice* usbdevice) { slogi("initializing cammus c5 wheel..."); @@ -86,9 +86,9 @@ int cammusc5_init(WheelDevice* wheeldevice) res = hid_init(); - wheeldevice->handle = hid_open(0x3416, 0x1021, NULL); + usbdevice->handle = hid_open(0x3416, 0x1021, NULL); - if (!wheeldevice->handle) + if (!usbdevice->handle) { sloge("Could not find attached Cammus C5 Wheel"); res = hid_exit(); diff --git a/src/monocoque/devices/usb/wheels/cammusc5.h b/src/monocoque/devices/usb/wheels/cammusc5.h index e009be2..4c84c2c 100644 --- a/src/monocoque/devices/usb/wheels/cammusc5.h +++ b/src/monocoque/devices/usb/wheels/cammusc5.h @@ -1,10 +1,10 @@ #ifndef _CAMMUSC5_H #define _CAMMUSC5_H -#include "../../wheeldevice.h" +#include "../../simdevice.h" -int cammusc5_update(WheelDevice* wheeldevice, int maxrpm, int rpm, int gear, int velocity); -int cammusc5_init(WheelDevice* wheeldevice); -int cammusc5_free(WheelDevice* wheeldevice); +int cammusc5_update(USBDevice* wheeldevice, int maxrpm, int rpm, int gear, int velocity); +int cammusc5_init(USBDevice* wheeldevice); +int cammusc5_free(USBDevice* wheeldevice); #endif diff --git a/src/monocoque/devices/usbdevice.c b/src/monocoque/devices/usbdevice.c index 4c838cf..ad080bf 100644 --- a/src/monocoque/devices/usbdevice.c +++ b/src/monocoque/devices/usbdevice.c @@ -17,10 +17,10 @@ int usbdev_update(SimDevice* this, SimData* simdata) { case USBDEV_UNKNOWN : case USBDEV_TACHOMETER : - tachdev_update(&usbdevice->u.tachdevice, simdata); + tachdev_update(usbdevice, simdata); break; case USBDEV_WHEEL : - wheeldev_update(&usbdevice->u.wheeldevice, simdata); + wheeldev_update(usbdevice, simdata); break; case USBDEV_GENERICHAPTIC : usbhapticdev_update(&usbdevice->u.hapticdevice, simdata, this->hapticeffect.tyre, this->hapticeffect.useconfig, this->hapticeffect.configcheck, this->hapticeffect.tyrediameterconfig); @@ -39,10 +39,10 @@ int usbdev_free(SimDevice* this) { case USBDEV_UNKNOWN : case USBDEV_TACHOMETER : - tachdev_free(&usbdevice->u.tachdevice); + tachdev_free(usbdevice); break; case USBDEV_WHEEL : - wheeldev_free(&usbdevice->u.wheeldevice); + wheeldev_free(usbdevice); break; case USBDEV_GENERICHAPTIC : usbhapticdev_free(&usbdevice->u.hapticdevice); @@ -64,10 +64,10 @@ int usbdev_init(USBDevice* usbdevice, DeviceSettings* ds) { case USBDEV_UNKNOWN : case USBDEV_TACHOMETER : - error = tachdev_init(&usbdevice->u.tachdevice, ds); + error = tachdev_init(usbdevice, ds); break; case USBDEV_WHEEL : - error = wheeldev_init(&usbdevice->u.wheeldevice, ds); + error = wheeldev_init(usbdevice, ds); break; case USBDEV_GENERICHAPTIC : error = usbhapticdev_init(&usbdevice->u.hapticdevice, ds); diff --git a/src/monocoque/devices/wheeldevice.c b/src/monocoque/devices/wheeldevice.c index 1bdb1c3..ce2ae4f 100644 --- a/src/monocoque/devices/wheeldevice.c +++ b/src/monocoque/devices/wheeldevice.c @@ -5,21 +5,23 @@ #include "usb/wheels/cammusc5.h" #include "usb/wheels/cammusc12.h" #include "serial/moza.h" -#include "wheeldevice.h" + +#include "simdevice.h" #include "../helper/confighelper.h" #include "../simulatorapi/simapi/simapi/simdata.h" #include "../slog/slog.h" -int wheeldev_update(WheelDevice* wheeldevice, SimData* simdata) +int wheeldev_update(USBDevice* usbdevice, SimData* simdata) { + WheelDevice* wheeldevice = &usbdevice->u.wheeldevice; switch ( wheeldevice->type ) { case WHEELDEV_UNKNOWN : case WHEELDEV_CAMMUSC5 : - cammusc5_update(wheeldevice, simdata->maxrpm, simdata->rpms, simdata->gear, simdata->velocity); + cammusc5_update(usbdevice, simdata->maxrpm, simdata->rpms, simdata->gear, simdata->velocity); break; case WHEELDEV_CAMMUSC12 : - cammusc12_update(wheeldevice, simdata->maxrpm, simdata->rpms, simdata->gear, simdata->velocity); + cammusc12_update(usbdevice, simdata->maxrpm, simdata->rpms, simdata->gear, simdata->velocity); break; } @@ -27,46 +29,55 @@ int wheeldev_update(WheelDevice* wheeldevice, SimData* simdata) return 0; } -int wheeldev_free(WheelDevice* wheeldevice) +int wheeldev_free(USBDevice* usbdevice) { + WheelDevice* wheeldevice = &usbdevice->u.wheeldevice; switch ( wheeldevice->type ) { case WHEELDEV_UNKNOWN : case WHEELDEV_CAMMUSC5 : - cammusc5_update(wheeldevice, 0, 0, 0, 0); - cammusc5_free(wheeldevice); + cammusc5_update(usbdevice, 0, 0, 0, 0); + cammusc5_free(usbdevice); break; case WHEELDEV_CAMMUSC12 : - cammusc12_update(wheeldevice, 0, 0, 0, 0); - cammusc12_free(wheeldevice); + cammusc12_update(usbdevice, 0, 0, 0, 0); + cammusc12_free(usbdevice); break; } return 0; } -int wheeldev_init(WheelDevice* wheeldevice, DeviceSettings* ds) +int wheeldev_init(USBDevice* usbdevice, DeviceSettings* ds) { slogi("initializing wheel device..."); int error = 0; // detection of wheel model - + WheelDevice* wheeldevice = &usbdevice->u.wheeldevice; switch (ds->dev_subsubtype) { case SIMDEVSUBTYPE_CAMMUSC5: wheeldevice->type = WHEELDEV_CAMMUSC5; slogi("Attempting to initialize cammus C5"); - error = cammusc5_init(wheeldevice); + error = cammusc5_init(usbdevice); break; case SIMDEVSUBTYPE_CAMMUSC12: wheeldevice->type = WHEELDEV_CAMMUSC12; slogi("Attempting to initialize cammus C12"); - error = cammusc12_init(wheeldevice); + + if(ds->has_config == true) + { + error = cammusc12_init(usbdevice, ds->specific_config_file); + } + else + { + error = cammusc12_init(usbdevice, NULL); + } break; default: wheeldevice->type = WHEELDEV_UNKNOWN; slogw("Unknown cammus wheel detected, trying C5"); - error = cammusc5_init(wheeldevice); + error = cammusc5_init(usbdevice); break; } diff --git a/src/monocoque/devices/wheeldevice.h b/src/monocoque/devices/wheeldevice.h index f9769d9..9507b3e 100644 --- a/src/monocoque/devices/wheeldevice.h +++ b/src/monocoque/devices/wheeldevice.h @@ -1,7 +1,6 @@ #ifndef _WHEELDEVICE_H #define _WHEELDEVICE_H -#include #include "../helper/confighelper.h" #include "../simulatorapi/simapi/simapi/simdata.h" @@ -20,13 +19,9 @@ typedef struct { int id; WheelType type; - hid_device* handle; char* port; } WheelDevice; -int wheeldev_update(WheelDevice* wheeldevice, SimData* simdata); -int wheeldev_init(WheelDevice* wheeldevice, DeviceSettings* ds); -int wheeldev_free(WheelDevice* wheeldevice); #endif diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index fed1b32..6dde73b 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -801,24 +801,24 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co slogt("set port baud rate to %i, ampfactor %f", baud, ampfactor); - ds->has_config = false; - const char* temp2; - found = config_setting_lookup_string(device_settings, "config", &temp2); - slogt("config is %s found is %i", temp2, found); - if(strcicmp(temp2, "none") == 0) - { - slogt("config set to none"); - } - else - { - ds->has_config = true; - ds->serialdevsettings.config_file = strdup(temp2); - slogt("will try to load config file at %s", ds->serialdevsettings.config_file); - } } } + ds->has_config = false; + const char* temp2; + int found = config_setting_lookup_string(device_settings, "config", &temp2); + if(strcicmp(temp2, "none") == 0) + { + slogt("config set to none"); + } + else + { + ds->has_config = true; + ds->specific_config_file = strdup(temp2); + slogt("will try to load config file at %s", ds->specific_config_file); + } + return error; } diff --git a/src/monocoque/helper/confighelper.h b/src/monocoque/helper/confighelper.h index ad466ef..945bb4e 100644 --- a/src/monocoque/helper/confighelper.h +++ b/src/monocoque/helper/confighelper.h @@ -155,7 +155,7 @@ TachometerSettings; typedef struct { char* portdev; - char* config_file; + MotorPosition motorsposition; int numlights; int numleds; @@ -204,12 +204,12 @@ typedef struct MonocoqueTyreIdentifier tyre; double threshold; bool has_config; + char* specific_config_file; // union? TachometerSettings tachsettings; SerialDeviceSettings serialdevsettings; SoundDeviceSettings sounddevsettings; USBDeviceSettings usbdevsettings; - } DeviceSettings;