diff --git a/src/monocoque/devices/CMakeLists.txt b/src/monocoque/devices/CMakeLists.txt index e4ca55f..d61ef38 100644 --- a/src/monocoque/devices/CMakeLists.txt +++ b/src/monocoque/devices/CMakeLists.txt @@ -9,6 +9,8 @@ set(devices_source_files serialdevice.c tachdevice.h tachdevice.c + wheeldevice.h + wheeldevice.c usbhapticdevice.h usbhapticdevice.c hapticeffect.h @@ -19,6 +21,8 @@ set(devices_source_files usb/revburner.c usb/cslelitev3.h usb/cslelitev3.c + usb/wheels/cammusc5.h + usb/wheels/cammusc5.c sound/usb_generic_shaker.h sound/usb_generic_shaker.c sound/usb_generic_shaker_pulse.c diff --git a/src/monocoque/devices/simdevice.h b/src/monocoque/devices/simdevice.h index 1dba2cc..154186d 100644 --- a/src/monocoque/devices/simdevice.h +++ b/src/monocoque/devices/simdevice.h @@ -73,7 +73,8 @@ typedef enum { USBDEV_UNKNOWN = 0, USBDEV_TACHOMETER = 1, - USBDEV_GENERICHAPTIC = 2 + USBDEV_GENERICHAPTIC = 2, + USBDEV_WHEEL = 3 } USBType; @@ -85,6 +86,7 @@ typedef struct union { TachDevice tachdevice; + WheelDevice wheeldevice; USBGenericHapticDevice hapticdevice; } u; } diff --git a/src/monocoque/devices/usb/wheels/cammusc5.c b/src/monocoque/devices/usb/wheels/cammusc5.c new file mode 100644 index 0000000..5d1ece2 --- /dev/null +++ b/src/monocoque/devices/usb/wheels/cammusc5.c @@ -0,0 +1,89 @@ +#include + +#include + +#include "cammusc5.h" +#include "../../../slog/slog.h" + +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 res = 0; + + unsigned char bytes[cammusc5_hidupdate_buf_size]; + + // byte 1 must be fc it seems + bytes[0] = 0xFC; + + + // byte 2 is number of lit leds, assuming 9 available leds, + // if we send 10, all leds will blink singling a gear change + // attempting to build in a margin before the maxrpm is achieved + int rpmmargin = .05*maxrpm; + int rpminterval = (maxrpm-rpmmargin) / (num_avail_leds+1); + + int litleds = 0; + for (int x = 1; x <= (num_avail_leds+1); x++) + { + if(rpm >= (rpminterval * x)) + { + litleds = x; + } + } + bytes[1] = litleds; + + // bytes 2 and 3 are a 16 bit velocity + if ( velocity > 0 ) + { + bytes[3] = (velocity >> 8) & 0xFF; + bytes[2] = velocity & 0xFF; + } + + // byte 4 is gear + bytes[4] = gear; + + + if (wheeldevice->handle) + { + res = hid_write(wheeldevice->handle, bytes, cammusc5_hidupdate_buf_size); + } + else + { + slogd("no handle"); + } + + return res; +} + +int cammusc5_free(WheelDevice* wheeldevice) +{ + int res = 0; + + hid_close(wheeldevice->handle); + res = hid_exit(); + + return res; +} + +int cammusc5_init(WheelDevice* wheeldevice) +{ + slogi("initializing cammus c5 wheel..."); + + int res = 0; + + res = hid_init(); + + wheeldevice->handle = hid_open(0x3416, 0x0301, NULL); + + if (!wheeldevice->handle) + { + sloge("Could not find attached Cammus C5 Wheel"); + res = hid_exit(); + return 1; + } + slogd("Found Cammus C5 Wheel..."); + return res; +} diff --git a/src/monocoque/devices/usb/wheels/cammusc5.h b/src/monocoque/devices/usb/wheels/cammusc5.h new file mode 100644 index 0000000..e009be2 --- /dev/null +++ b/src/monocoque/devices/usb/wheels/cammusc5.h @@ -0,0 +1,10 @@ +#ifndef _CAMMUSC5_H +#define _CAMMUSC5_H + +#include "../../wheeldevice.h" + +int cammusc5_update(WheelDevice* wheeldevice, int maxrpm, int rpm, int gear, int velocity); +int cammusc5_init(WheelDevice* wheeldevice); +int cammusc5_free(WheelDevice* wheeldevice); + +#endif diff --git a/src/monocoque/devices/usbdevice.c b/src/monocoque/devices/usbdevice.c index 3046158..4c838cf 100644 --- a/src/monocoque/devices/usbdevice.c +++ b/src/monocoque/devices/usbdevice.c @@ -19,6 +19,9 @@ int usbdev_update(SimDevice* this, SimData* simdata) case USBDEV_TACHOMETER : tachdev_update(&usbdevice->u.tachdevice, simdata); break; + case USBDEV_WHEEL : + wheeldev_update(&usbdevice->u.wheeldevice, simdata); + break; case USBDEV_GENERICHAPTIC : usbhapticdev_update(&usbdevice->u.hapticdevice, simdata, this->hapticeffect.tyre, this->hapticeffect.useconfig, this->hapticeffect.configcheck, this->hapticeffect.tyrediameterconfig); break; @@ -38,6 +41,9 @@ int usbdev_free(SimDevice* this) case USBDEV_TACHOMETER : tachdev_free(&usbdevice->u.tachdevice); break; + case USBDEV_WHEEL : + wheeldev_free(&usbdevice->u.wheeldevice); + break; case USBDEV_GENERICHAPTIC : usbhapticdev_free(&usbdevice->u.hapticdevice); break; @@ -60,6 +66,9 @@ int usbdev_init(USBDevice* usbdevice, DeviceSettings* ds) case USBDEV_TACHOMETER : error = tachdev_init(&usbdevice->u.tachdevice, ds); break; + case USBDEV_WHEEL : + error = wheeldev_init(&usbdevice->u.wheeldevice, ds); + break; case USBDEV_GENERICHAPTIC : error = usbhapticdev_init(&usbdevice->u.hapticdevice, ds); break; @@ -81,8 +90,17 @@ USBDevice* new_usb_device(DeviceSettings* ds, MonocoqueSettings* ms) { - + // TODO: turn this into a switch when we get more devices this->type = USBDEV_TACHOMETER; + if(ds->dev_subtype == SIMDEVTYPE_USBWHEEL) + { + this->type = USBDEV_WHEEL; + } + + + // really generic haptic isn't and shouldn't be it's own type + // it's an attribute that is added to a device via composition + // same if that haptic device is a serial device if (ds->dev_subtype == SIMDEVTYPE_USBHAPTIC) { this->m.hapticeffect.threshold = ds->threshold; diff --git a/src/monocoque/devices/usbdevice.h b/src/monocoque/devices/usbdevice.h index 74adc88..9d35998 100644 --- a/src/monocoque/devices/usbdevice.h +++ b/src/monocoque/devices/usbdevice.h @@ -2,6 +2,7 @@ #define _USBDEVICE_H #include "tachdevice.h" +#include "wheeldevice.h" #include "usbhapticdevice.h" #endif diff --git a/src/monocoque/devices/wheeldevice.c b/src/monocoque/devices/wheeldevice.c new file mode 100644 index 0000000..b91c1b2 --- /dev/null +++ b/src/monocoque/devices/wheeldevice.c @@ -0,0 +1,51 @@ +#include +#include +#include + +#include "wheels/cammusc5.h" +#include "wheeldevice.h" +#include "../../helper/confighelper.h" +#include "../../simulatorapi/simapi/simapi/simdata.h" +#include "../../slog/slog.h" + +int wheeldev_update(WheelDevice* wheeldevice, SimData* simdata) +{ + switch ( wheeldevice->type ) + { + case WHEELDEV_UNKNOWN : + case WHEELDEV_CAMMUSC5 : + cammusc5_update(wheeldevice, simdata->maxrpm, simdata->rpms, simdata->gear, simdata->velocity); + break; + + } + + return 0; +} + +int wheeldev_free(WheelDevice* wheeldevice) +{ + switch ( wheeldevice->type ) + { + case WHEELDEV_UNKNOWN : + case WHEELDEV_CAMMUSC5 : + cammusc5_update(wheeldevice, 0, 0, 0, 0); + cammusc5_free(wheeldevice); + break; + } + + return 0; +} + +int wheeldev_init(WheelDevice* wheeldevice, DeviceSettings* ds) +{ + slogi("initializing wheel device..."); + int error = 0; + // detection of wheel model + wheeldevice->type = WHEELDEV_UNKNOWN; + wheeldevice->type = WHEELDEV_CAMMUSC5; + + + error = cammusc5_init(wheeldevice); + + return error; +} diff --git a/src/monocoque/devices/wheeldevice.h b/src/monocoque/devices/wheeldevice.h new file mode 100644 index 0000000..f4f75df --- /dev/null +++ b/src/monocoque/devices/wheeldevice.h @@ -0,0 +1,29 @@ +#ifndef _WHEELDEVICE_H +#define _WHEELDEVICE_H + +#include +#include "../helper/confighelper.h" +#include "../simulatorapi/simapi/simapi/simdata.h" + +//typedef int (*tachdev_update)(int revs); + +typedef enum +{ + WHEELDEV_UNKNOWN = 0, + WHEELDEV_CAMMUSC5 = 1 +} +WheelType; + +typedef struct +{ + int id; + WheelType type; + hid_device* handle; +} +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 94e45dc..69ef359 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -107,6 +107,11 @@ int strtodevsubtype(const char* device_subtype, DeviceSettings* ds, int simdev) ds->dev_subtype = SIMDEVTYPE_TACHOMETER; break; } + if (strcicmp(device_subtype, "Wheel") == 0) + { + ds->dev_subtype = SIMDEVTYPE_USBWHEEL; + break; + } if (strcicmp(device_subtype, "UsbHaptic") == 0 || strcicmp(device_subtype, "Haptic") == 0) { ds->dev_subtype = SIMDEVTYPE_USBHAPTIC; diff --git a/src/monocoque/helper/confighelper.h b/src/monocoque/helper/confighelper.h index 7f68695..0d79093 100644 --- a/src/monocoque/helper/confighelper.h +++ b/src/monocoque/helper/confighelper.h @@ -25,7 +25,8 @@ typedef enum SIMDEVTYPE_USBHAPTIC = 2, SIMDEVTYPE_SHIFTLIGHTS = 3, SIMDEVTYPE_SIMWIND = 4, - SIMDEVTYPE_SERIALHAPTIC = 5 + SIMDEVTYPE_SERIALHAPTIC = 5, + SIMDEVTYPE_USBWHEEL = 6 } DeviceSubType;