diff --git a/src/monocoque/devices/CMakeLists.txt b/src/monocoque/devices/CMakeLists.txt index 6032d9d..914f18a 100644 --- a/src/monocoque/devices/CMakeLists.txt +++ b/src/monocoque/devices/CMakeLists.txt @@ -21,6 +21,8 @@ set(devices_source_files usb/revburner.c usb/cslelitev3.h usb/cslelitev3.c + usb/simagicp1000.h + usb/simagicp1000.c usb/wheels/cammusc5.h usb/wheels/cammusc5.c usb/wheels/cammusc12.h diff --git a/src/monocoque/devices/usb/cslelitev3.c b/src/monocoque/devices/usb/cslelitev3.c index b56c94e..0e42957 100644 --- a/src/monocoque/devices/usb/cslelitev3.c +++ b/src/monocoque/devices/usb/cslelitev3.c @@ -34,8 +34,8 @@ int cslelitev3_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, i } - fprintf(usbhapticdevice->handle, "%i\n", value); - fflush(usbhapticdevice->handle); + fprintf(usbhapticdevice->filehandle, "%i\n", value); + fflush(usbhapticdevice->filehandle); return res; } @@ -46,8 +46,8 @@ int cslelitev3_free(USBGenericHapticDevice* usbhapticdevice) free(usbhapticdevice->dev); - fflush(usbhapticdevice->handle); - fclose(usbhapticdevice->handle); + fflush(usbhapticdevice->filehandle); + fclose(usbhapticdevice->filehandle); return res; } @@ -91,9 +91,9 @@ int cslelitev3_init(USBGenericHapticDevice* usbhapticdevice) return res; } - usbhapticdevice->handle = fopen(usbhapticdevice->dev, "w"); + usbhapticdevice->filehandle = fopen(usbhapticdevice->dev, "w"); - if (!usbhapticdevice->handle) + if (!usbhapticdevice->filehandle) { sloge("Could not open pedal device..."); return res; diff --git a/src/monocoque/devices/usb/revburner.c b/src/monocoque/devices/usb/revburner.c index 191d284..76205b2 100644 --- a/src/monocoque/devices/usb/revburner.c +++ b/src/monocoque/devices/usb/revburner.c @@ -5,7 +5,7 @@ #include "tachdevice.h" #include "../slog/slog.h" -const int buf_size = 65; +const size_t buf_size = 8; int revburner_update(TachDevice* tachdevice, int pulses) diff --git a/src/monocoque/devices/usb/simagicp1000.c b/src/monocoque/devices/usb/simagicp1000.c new file mode 100644 index 0000000..54b838b --- /dev/null +++ b/src/monocoque/devices/usb/simagicp1000.c @@ -0,0 +1,133 @@ +#include +#include +#include +#include + +#include "usbhapticdevice.h" + +#include "../../helper/confighelper.h" +#include "../slog/slog.h" + +#define SIMAGIC_VENDOR_ID 0x0483 +#define SIMAGIC_PRODUCT_ID_P1000 0x0525 + +const size_t SIMAGIC_P1000_BUFSIZE = 49; +const char* SIMAGICP1000DEVSTRING = "SIMAGIC P1000 Pedals"; + +int senddevreport(USBGenericHapticDevice* usbhapticdevice, unsigned char* buf, size_t bufsize) +{ + int res = 0; + if (usbhapticdevice->handle) + { + res = hid_write(usbhapticdevice->handle, buf, SIMAGIC_P1000_BUFSIZE); + slogd("sent %i bytes to %s", bufsize, SIMAGICP1000DEVSTRING); + slogt("sent bytes %02x %02x %02x %02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); + } + else + { + slogd("no handle"); + } + return res; +} + +int simagicp1000_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, int play) +{ + + int res = 0; + int value = 0; + + unsigned char bytes[SIMAGIC_P1000_BUFSIZE]; + for (int x = 0; x < SIMAGIC_P1000_BUFSIZE; x++) + { + bytes[x] = 0x00; + } + + if (play > 0) + { + bytes[0] = 0xEC; + // we can add config settings for these values + bytes[2] = 0x01; + bytes[3] = 0x0A; + bytes[4] = 0xFF; + switch (effecttype) + { + case (EFFECT_TYRESLIP): + bytes[1] = 0x02; + senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + break; + case (EFFECT_TYRELOCK): + bytes[1] = 0x01; + senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + break; + case (EFFECT_ABSBRAKES): + bytes[1] = 0x02; + senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + bytes[1] = 0x01; + senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + break; + } + } + else + { + bytes[1] = 0x01; + senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + bytes[1] = 0x02; + senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + } + + return res; +} + +int simagicp1000_free(USBGenericHapticDevice* usbhapticdevice) +{ + int res = 0; + + hid_close(usbhapticdevice->handle); + res = hid_exit(); + + return res; + + return res; +} + +int simagicp1000_init(USBGenericHapticDevice* usbhapticdevice) +{ + slogi("initializing %s...", SIMAGICP1000DEVSTRING); + + int res = 0; + + res = hid_init(); + + usbhapticdevice->handle = hid_open(SIMAGIC_VENDOR_ID, SIMAGIC_PRODUCT_ID_P1000, NULL); + + if (!usbhapticdevice->handle) + { + sloge("Could not find attached %s", SIMAGICP1000DEVSTRING); + res = hid_exit(); + return 1; + } + else + { + unsigned char bytes[SIMAGIC_P1000_BUFSIZE]; + for (int x = 0; x < SIMAGIC_P1000_BUFSIZE; x++) + { + bytes[x] = 0x00; + } + bytes[0] = 0xF1; + bytes[1] = 0x17; + bytes[2] = 0x00; + bytes[3] = 0x00; + bytes[4] = 0x00; + bytes[5] = 0x01; + bytes[6] = 0x02; + res = senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + slogd("Initialization returned %i", res); + if(res != 0) + { + slogw("Problem with initialization of %s", SIMAGICP1000DEVSTRING); + } + } + + slogd("Found %s...", SIMAGICP1000DEVSTRING); + return res; +} diff --git a/src/monocoque/devices/usb/simagicp1000.h b/src/monocoque/devices/usb/simagicp1000.h new file mode 100644 index 0000000..77770c5 --- /dev/null +++ b/src/monocoque/devices/usb/simagicp1000.h @@ -0,0 +1,8 @@ +#ifndef _SIMAGICP1000_H +#define _SIMAGICP1000_H + +int simagicp1000_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, int play); +int simagicp1000_init(USBGenericHapticDevice* usbhapticdevice); +int simagicp1000_free(USBGenericHapticDevice* usbhapticdevice); + +#endif diff --git a/src/monocoque/devices/usbhapticdevice.c b/src/monocoque/devices/usbhapticdevice.c index c8320b6..b31ea6a 100644 --- a/src/monocoque/devices/usbhapticdevice.c +++ b/src/monocoque/devices/usbhapticdevice.c @@ -6,6 +6,8 @@ #include "usbhapticdevice.h" #include "hapticeffect.h" #include "usb/cslelitev3.h" +#include "usb/simagicp1000.h" + #include "../../helper/confighelper.h" #include "../../simulatorapi/simapi/simapi/simdata.h" #include "../../slog/slog.h" @@ -15,18 +17,34 @@ int usbhapticdev_update(USBGenericHapticDevice* usbhapticdevice, SimData* simdat { double play = slipeffect(simdata, usbhapticdevice->effecttype, tyre, usbhapticdevice->threshold, useconfig, configcheck, configfile); - + if (play != usbhapticdevice->state) { int rplay = 0; if(play > 0) { rplay = 1; - cslelitev3_update(usbhapticdevice, usbhapticdevice->effecttype, rplay); + switch ( usbhapticdevice->type ) + { + case USBHAPTIC_CSLELITEV3PEDALS: + cslelitev3_update(usbhapticdevice, usbhapticdevice->effecttype, rplay); + break; + case USBHAPTIC_SIMAGICP1000PEDALS: + simagicp1000_update(usbhapticdevice, usbhapticdevice->effecttype, rplay); + break; + } } else { - cslelitev3_update(usbhapticdevice, usbhapticdevice->effecttype, rplay); + switch ( usbhapticdevice->type ) + { + case USBHAPTIC_CSLELITEV3PEDALS: + cslelitev3_update(usbhapticdevice, usbhapticdevice->effecttype, rplay); + break; + case USBHAPTIC_SIMAGICP1000PEDALS: + simagicp1000_update(usbhapticdevice, usbhapticdevice->effecttype, rplay); + break; + } } usbhapticdevice->state = play; } @@ -36,7 +54,16 @@ int usbhapticdev_update(USBGenericHapticDevice* usbhapticdevice, SimData* simdat int usbhapticdev_free(USBGenericHapticDevice* usbhapticdevice) { slogt("closing usb haptic device"); - cslelitev3_free(usbhapticdevice); + switch ( usbhapticdevice->type ) + { + case USBHAPTIC_CSLELITEV3PEDALS: + cslelitev3_free(usbhapticdevice); + break; + case USBHAPTIC_SIMAGICP1000PEDALS: + simagicp1000_free(usbhapticdevice); + break; + } + return 0; } @@ -65,9 +92,18 @@ int usbhapticdev_init(USBGenericHapticDevice* usbhapticdevice, DeviceSettings* d slogi("initializing standalone usb haptic device..."); // detection of usb device model - usbhapticdevice->type = HAPTIC_UNKNOWN; - usbhapticdevice->type = HAPTIC_CSLELITEV3; - error = cslelitev3_init(usbhapticdevice); + switch (ds->dev_subsubtype) { + case (SIMDEVSUBTYPE_SIMAGICP1000PEDALS): + usbhapticdevice->type = USBHAPTIC_SIMAGICP1000PEDALS; + error = simagicp1000_init(usbhapticdevice); + break; + case (SIMDEVSUBTYPE_CSLELITEV3PEDALS): + usbhapticdevice->type = USBHAPTIC_CSLELITEV3PEDALS; + error = cslelitev3_init(usbhapticdevice); + break; + default: + slogw("Possibly unknown device"); + } if(usbhapticdevice->handle == 0) { diff --git a/src/monocoque/devices/usbhapticdevice.h b/src/monocoque/devices/usbhapticdevice.h index 6d9131b..bbc86ec 100644 --- a/src/monocoque/devices/usbhapticdevice.h +++ b/src/monocoque/devices/usbhapticdevice.h @@ -8,8 +8,9 @@ typedef enum { - HAPTIC_UNKNOWN = 0, - HAPTIC_CSLELITEV3 = 1 + USBHAPTIC_UNKNOWN = 0, + USBHAPTIC_CSLELITEV3PEDALS = 1, + USBHAPTIC_SIMAGICP1000PEDALS = 2 } HapticType; @@ -22,7 +23,8 @@ typedef struct int value0; int value1; VibrationEffectType effecttype; - FILE* handle; + FILE* filehandle; + hid_device* handle; char* dev; } USBGenericHapticDevice; diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index 1915609..e19b26e 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -99,22 +99,44 @@ int strtodevsubsubtype(const char* device_subsubtype, DeviceSettings* ds) { ds->dev_subsubtype = SIMDEVSUBTYPE_UNKNOWN; - if (strcicmp(device_subsubtype, "CammusC5") == 0) - { - ds->dev_subsubtype = SIMDEVSUBTYPE_CAMMUSC5; - } - if (strcicmp(device_subsubtype, "CammusC12") == 0) - { - ds->dev_subsubtype = SIMDEVSUBTYPE_CAMMUSC12; - } - if (strcicmp(device_subsubtype, "MozaR5") == 0) - { - ds->dev_subsubtype = SIMDEVSUBTYPE_MOZAR5; - } - if (strcicmp(device_subsubtype, "MozaR8") == 0) - { - ds->dev_subsubtype = SIMDEVSUBTYPE_MOZAR5; - } + + bool devfound = false; + if (strcicmp(device_subsubtype, "CammusC5") == 0) + { + ds->dev_subsubtype = SIMDEVSUBTYPE_CAMMUSC5; + bool devfound = true; + } + if (strcicmp(device_subsubtype, "CammusC12") == 0) + { + ds->dev_subsubtype = SIMDEVSUBTYPE_CAMMUSC12; + bool devfound = true; + } + if (strcicmp(device_subsubtype, "MozaR5") == 0) + { + ds->dev_subsubtype = SIMDEVSUBTYPE_MOZAR5; + bool devfound = true; + } + if (strcicmp(device_subsubtype, "MozaR8") == 0) + { + ds->dev_subsubtype = SIMDEVSUBTYPE_MOZAR5; + bool devfound = true; + } + if (strcicmp(device_subsubtype, "CSLELITEV3PEDALS") == 0) + { + ds->dev_subsubtype = SIMDEVSUBTYPE_CSLELITEV3PEDALS; + bool devfound = true; + } + if (strcicmp(device_subsubtype, "SIMAGICP1000PEDALS") == 0) + { + ds->dev_subsubtype = SIMDEVSUBTYPE_SIMAGICP1000PEDALS; + bool devfound = true; + } + + if(devfound == false) + { + slogw("%s does not appear to be a valid device sub sub type, but attempting to continue with other devices", device_subsubtype); + return MONOCOQUE_ERROR_INVALID_DEV; + } return MONOCOQUE_ERROR_NONE; } @@ -618,12 +640,7 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co } } - if (ds->dev_subtype == SIMDEVTYPE_USBHAPTIC) - { - // logic for different devices - } - - if (ds->dev_subtype == SIMDEVTYPE_USBWHEEL) + if (ds->dev_subtype == SIMDEVTYPE_USBHAPTIC || ds->dev_subtype == SIMDEVTYPE_USBWHEEL) { // logic for different devices int b = 0; diff --git a/src/monocoque/helper/confighelper.h b/src/monocoque/helper/confighelper.h index eb593eb..7f44806 100644 --- a/src/monocoque/helper/confighelper.h +++ b/src/monocoque/helper/confighelper.h @@ -33,10 +33,12 @@ DeviceSubType; typedef enum { - SIMDEVSUBTYPE_UNKNOWN = 0, - SIMDEVSUBTYPE_CAMMUSC5 = 1, - SIMDEVSUBTYPE_CAMMUSC12 = 2, - SIMDEVSUBTYPE_MOZAR5 = 3, + SIMDEVSUBTYPE_UNKNOWN = 0, + SIMDEVSUBTYPE_CAMMUSC5 = 1, + SIMDEVSUBTYPE_CAMMUSC12 = 2, + SIMDEVSUBTYPE_MOZAR5 = 3, + SIMDEVSUBTYPE_CSLELITEV3PEDALS = 4, + SIMDEVSUBTYPE_SIMAGICP1000PEDALS =5 } DeviceSubSubType;