Reorganization and config updates for Moza wheel

This commit is contained in:
Paul Dino Jones 2024-09-19 18:50:54 -04:00
parent 525dd68b4b
commit c0755aef54
10 changed files with 71 additions and 51 deletions

View File

@ -30,6 +30,8 @@ set(devices_source_files
sound/usb_generic_shaker_pulse.c sound/usb_generic_shaker_pulse.c
serial/arduino.h serial/arduino.h
serial/arduino.c serial/arduino.c
serial/moza.h
serial/moza.c
) )
include_directories("." "usb" "sound" "serial") include_directories("." "usb" "sound" "serial")

View File

@ -1,5 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <math.h> #include <math.h>
#include <string.h>
#include <hidapi/hidapi.h> #include <hidapi/hidapi.h>
@ -7,10 +9,10 @@
#include "../../slog/slog.h" #include "../../slog/slog.h"
#define MOZA_TIMEOUT 1000 #define MOZA_TIMEOUT 1000
#define MOZA_SERIAL_TEMPLATE = {0x7e, 0x06, 0x41, 0x17, 0xfd, 0xde, 0x0, 0x0, 0x0, 0x0, 0x0} #define MOZA_SERIAL_TEMPLATE {0x7e, 0x06, 0x41, 0x17, 0xfd, 0xde, 0x0, 0x0, 0x0, 0x0, 0x0}
#define MOZA_NUM_AVAILABLE_LEDS = 10 #define MOZA_NUM_AVAILABLE_LEDS 10
#define MOZA_BLINKING_BIT = 7 #define MOZA_BLINKING_BIT 7
#define MOZA_MAGIC_VALUE = 0x0d #define MOZA_MAGIC_VALUE 0x0d
#define BIT(nr) (1UL << (nr)) #define BIT(nr) (1UL << (nr))
unsigned char moza_checksum(unsigned char *data) unsigned char moza_checksum(unsigned char *data)
@ -24,10 +26,10 @@ unsigned char moza_checksum(unsigned char *data)
return 0; return 0;
} }
int moza_update(WheelDevice* wheeldevice, unsigned short maxrpm, unsigned short rpm) int moza_update(SerialDevice* serialdevice, unsigned short maxrpm, unsigned short rpm)
{ {
unsigned char bytes[] = MOZA_SERIAL_TEMPLATE; unsigned char bytes[] = MOZA_SERIAL_TEMPLATE;
int size = sizeof(bytes)/sizeof(bytes[0]) int size = sizeof(bytes)/sizeof(bytes[0]);
if (rpm/maxrpm >= 0.8) if (rpm/maxrpm >= 0.8)
bytes[9] |= BIT(0); bytes[9] |= BIT(0);
@ -69,7 +71,7 @@ int moza_update(WheelDevice* wheeldevice, unsigned short maxrpm, unsigned short
if (serialdevice->port) if (serialdevice->port)
{ {
slogt("copying %i bytes to moza device", size); slogt("copying %i bytes to moza device", size);
result = check(sp_blocking_write(serialdevice->port, bytes, size, MOZA_TIMEOUT)); result = moza_serial_check(sp_blocking_write(serialdevice->port, bytes, size, MOZA_TIMEOUT));
} }
return result; return result;
@ -82,34 +84,35 @@ int moza_init(SerialDevice* serialdevice, const char* portdev)
char* port_name = strdup(portdev); char* port_name = strdup(portdev);
slogd("Looking for port %s.\n", port_name); slogd("Looking for port %s.\n", port_name);
error = check(sp_get_port_by_name(port_name, &serialdevice->port)); error = moza_serial_check(sp_get_port_by_name(port_name, &serialdevice->port));
if (error != 0) if (error != 0)
{ {
return error; return error;
} }
slogd("Opening port.\n"); slogd("Opening port.\n");
check(sp_open(serialdevice->port, SP_MODE_READ_WRITE)); moza_serial_check(sp_open(serialdevice->port, SP_MODE_READ_WRITE));
slogd("Setting port to 115200 8N1, no flow control.\n"); slogd("Setting port to 115200 8N1, no flow control.\n");
check(sp_set_baudrate(serialdevice->port, 115200)); moza_serial_check(sp_set_baudrate(serialdevice->port, 115200));
check(sp_set_bits(serialdevice->port, 8)); moza_serial_check(sp_set_bits(serialdevice->port, 8));
check(sp_set_parity(serialdevice->port, SP_PARITY_NONE)); moza_serial_check(sp_set_parity(serialdevice->port, SP_PARITY_NONE));
check(sp_set_stopbits(serialdevice->port, 1)); moza_serial_check(sp_set_stopbits(serialdevice->port, 1));
check(sp_set_flowcontrol(serialdevice->port, SP_FLOWCONTROL_NONE)); moza_serial_check(sp_set_flowcontrol(serialdevice->port, SP_FLOWCONTROL_NONE));
free(port_name); free(port_name);
slogd("Successfully setup moza serial device..."); slogd("Successfully setup moza serial device...");
return 0; return 0;
} }
int moza_free(WheelDevice* wheeldevice) int moza_free(SerialDevice* serialdevice)
{ {
check(sp_close(serialdevice->port)); moza_serial_check(sp_close(serialdevice->port));
sp_free_port(serialdevice->port); sp_free_port(serialdevice->port);
} }
int check(enum sp_return result) // i have to move this some place common
int moza_serial_check(enum sp_return result)
{ {
/* For this example we'll just exit on any error by calling abort(). */ /* For this example we'll just exit on any error by calling abort(). */
char* error_message; char* error_message;
@ -128,10 +131,10 @@ int check(enum sp_return result)
//abort(); //abort();
case SP_ERR_SUPP: case SP_ERR_SUPP:
printf("Error: Not supported.\n"); printf("Error: Not supported.\n");
abort(); //abort();
case SP_ERR_MEM: case SP_ERR_MEM:
printf("Error: Couldn't allocate memory.\n"); printf("Error: Couldn't allocate memory.\n");
abort(); //abort();
case SP_OK: case SP_OK:
default: default:
return result; return result;

View File

@ -2,10 +2,12 @@
#define _MOZA_H #define _MOZA_H
#include "../serialdevice.h" #include "../serialdevice.h"
#include "../simdevice.h"
int moza_update(SerialDevice* serialdevice, unsigned short maxrpm, unsigned short rpm); int moza_update(SerialDevice* serialdevice, unsigned short maxrpm, unsigned short rpm);
int moza_init(SerialDevice* serialdevice, const char* portdev); int moza_init(SerialDevice* serialdevice, const char* portdev);
int moza_free(SerialDevice* serialdevice); int moza_free(SerialDevice* serialdevice);
int moza_serial_check(enum sp_return result);
#endif #endif

View File

@ -19,15 +19,7 @@ int serialdev_update(SimDevice* this, SimData* simdata)
{ {
SerialDevice* serialdevice = (void *) this->derived; SerialDevice* serialdevice = (void *) this->derived;
switch (serialdevice->type)
{
case SERIALDEV_MOZA:
moza_update(serialdevice, simdata->maxrpm, simdata->rpms);
break;
default:
arduino_update(serialdevice, simdata, sizeof(SimData)); arduino_update(serialdevice, simdata, sizeof(SimData));
}
return 0; return 0;
} }
@ -126,7 +118,6 @@ int arduino_simhaptic_update(SimDevice* this, SimData* simdata)
size_t size = sizeof(SimHapticData); size_t size = sizeof(SimHapticData);
arduino_update(serialdevice, &serialdevice->u.simhapticdata, size); arduino_update(serialdevice, &serialdevice->u.simhapticdata, size);
return result; return result;
} }
@ -134,42 +125,35 @@ int serialdev_free(SimDevice* this)
{ {
SerialDevice* serialdevice = (void *) this->derived; SerialDevice* serialdevice = (void *) this->derived;
switch (serialdevice->type)
{
case SERIALDEV_MOZA:
moza_free(serialdevice);
break;
default:
arduino_free(serialdevice); arduino_free(serialdevice);
}
free(serialdevice); free(serialdevice);
return 0; return 0;
} }
int serialdev_init(SerialDevice* serialdevice, const char* portdev, int motorsposition, int baud) int serialdev_init(SerialDevice* serialdevice, DeviceSettings* ds)
{ {
slogi("initializing serial device..."); slogi("initializing serial device...");
int error = 0; int error = 0;
serialdevice->type = SERIALDEV_UNKNOWN;
serialdevice->type = SERIALDEV_ARDUINO;
serialdevice->motorsposition = motorsposition; serialdevice->motorsposition = ds->serialdevsettings.motorsposition;
serialdevice->baudrate = ds->serialdevsettings.baud;
serialdevice->baud = baud; serialdevice->baud = baud;
error = arduino_init(serialdevice, portdev); error = arduino_init(serialdevice, portdev);
switch (serialdevice->type) switch (serialdevice->type)
{ {
case SERIALDEV_MOZA: case SERIALDEV_WHEEL:
error = moza_init(serialdevice, portdev);
// the wheel stuff assumed it was a usb
//error = wheeldev_init(&serialdevice->u.wheeldevice, ds);
error = moza_init(serialdevice, ds->serialdevsettings.portdev);
break; break;
default: default:
error = arduino_init(serialdevice, portdev, baud); error = arduino_init(serialdevice, ds->serialdevsettings.portdev, ds->serialdevsettings.baud);
} }
@ -189,6 +173,7 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms) {
this->m.free = &simdevfree; this->m.free = &simdevfree;
this->m.derived = this; this->m.derived = this;
this->m.vtable = &serial_simdevice_vtable; this->m.vtable = &serial_simdevice_vtable;
this->type = SERIALDEV_ARDUINO;
slogt("Attempting to configure arduino device with subtype: %i", ds->dev_subtype); slogt("Attempting to configure arduino device with subtype: %i", ds->dev_subtype);
switch (ds->dev_subtype) { switch (ds->dev_subtype) {
@ -217,6 +202,17 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms) {
this->ampfactor = ds->serialdevsettings.ampfactor; this->ampfactor = ds->serialdevsettings.ampfactor;
slogi("Initializing arduino device for haptic effects."); slogi("Initializing arduino device for haptic effects.");
break; break;
case (SIMDEVTYPE_SERIALWHEEL):
this->type = SERIALDEV_WHEEL;
switch (ds->dev_subsubtype) {
case SIMDEVSUBTYPE_MOZAR5:
default:
//move this stuff to wheeldevice or it's own serial wheel device module
this->devicetype = SERIALDEV__MOZAR5;
break;
}
} }
if(this->devicetype == ARDUINODEV__HAPTIC) if(this->devicetype == ARDUINODEV__HAPTIC)
@ -230,7 +226,7 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms) {
this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config; this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config;
} }
int error = serialdev_init(this, ds->serialdevsettings.portdev, ds->serialdevsettings.motorsposition, ds->serialdevsettings.baud); int error = serialdev_init(this, ds);
if (error != 0) if (error != 0)
{ {

View File

@ -2,13 +2,14 @@
#define _SERIALDEVICE_H #define _SERIALDEVICE_H
#include <libserialport.h> #include <libserialport.h>
#include "wheeldevice.h"
typedef enum typedef enum
{ {
ARDUINODEV__SHIFTLIGHTS = 0, ARDUINODEV__SHIFTLIGHTS = 0,
ARDUINODEV__SIMWIND = 1, ARDUINODEV__SIMWIND = 1,
ARDUINODEV__HAPTIC = 2, ARDUINODEV__HAPTIC = 2,
MOZADEV = 3, SERIALDEV__MOZAR5 = 3,
} }
SerialDeviceType; SerialDeviceType;

View File

@ -38,7 +38,7 @@ typedef enum
{ {
SERIALDEV_UNKNOWN = 0, SERIALDEV_UNKNOWN = 0,
SERIALDEV_ARDUINO = 1, SERIALDEV_ARDUINO = 1,
SERIALDEV_MOZA = 2, SERIALDEV_WHEEL = 2,
} }
SerialType; SerialType;
@ -52,6 +52,7 @@ typedef struct
SerialDeviceType devicetype; SerialDeviceType devicetype;
// move these two they only apply to the haptic device // move these two they only apply to the haptic device
int motorsposition; int motorsposition;
int baudrate;
double ampfactor; double ampfactor;
double state; double state;
union union
@ -59,6 +60,7 @@ typedef struct
SimWindData simwinddata; SimWindData simwinddata;
SimHapticData simhapticdata; SimHapticData simhapticdata;
ShiftLightsData shiftlightsdata; ShiftLightsData shiftlightsdata;
WheelDevice wheeldevice;
} u; } u;
} }
SerialDevice; SerialDevice;

View File

@ -4,6 +4,7 @@
#include "usb/wheels/cammusc5.h" #include "usb/wheels/cammusc5.h"
#include "usb/wheels/cammusc12.h" #include "usb/wheels/cammusc12.h"
#include "serial/moza.h"
#include "wheeldevice.h" #include "wheeldevice.h"
#include "../helper/confighelper.h" #include "../helper/confighelper.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simapi/simapi/simdata.h"

View File

@ -11,7 +11,8 @@ typedef enum
{ {
WHEELDEV_UNKNOWN = 0, WHEELDEV_UNKNOWN = 0,
WHEELDEV_CAMMUSC5 = 1, WHEELDEV_CAMMUSC5 = 1,
WHEELDEV_CAMMUSC12 = 2 WHEELDEV_CAMMUSC12 = 2,
WHEELDEV_MOZAR5 = 3
} }
WheelType; WheelType;
@ -20,6 +21,7 @@ typedef struct
int id; int id;
WheelType type; WheelType type;
hid_device* handle; hid_device* handle;
char* port;
} }
WheelDevice; WheelDevice;

View File

@ -107,6 +107,10 @@ int strtodevsubsubtype(const char* device_subsubtype, DeviceSettings* ds)
{ {
ds->dev_subsubtype = SIMDEVSUBTYPE_CAMMUSC12; ds->dev_subsubtype = SIMDEVSUBTYPE_CAMMUSC12;
} }
if (strcicmp(device_subsubtype, "MozaR8") == 0)
{
ds->dev_subsubtype = SIMDEVSUBTYPE_MOZAR5;
}
return MONOCOQUE_ERROR_NONE; return MONOCOQUE_ERROR_NONE;
} }
@ -149,6 +153,11 @@ int strtodevsubtype(const char* device_subtype, DeviceSettings* ds, int simdev)
ds->dev_subtype = SIMDEVTYPE_SERIALHAPTIC; ds->dev_subtype = SIMDEVTYPE_SERIALHAPTIC;
break; break;
} }
if (strcicmp(device_subtype, "Wheel") == 0)
{
ds->dev_subtype = SIMDEVTYPE_SERIALWHEEL;
break;
}
case SIMDEV_SOUND: case SIMDEV_SOUND:
ds->is_valid = true; ds->is_valid = true;
break; break;

View File

@ -26,7 +26,8 @@ typedef enum
SIMDEVTYPE_SHIFTLIGHTS = 3, SIMDEVTYPE_SHIFTLIGHTS = 3,
SIMDEVTYPE_SIMWIND = 4, SIMDEVTYPE_SIMWIND = 4,
SIMDEVTYPE_SERIALHAPTIC = 5, SIMDEVTYPE_SERIALHAPTIC = 5,
SIMDEVTYPE_USBWHEEL = 6 SIMDEVTYPE_USBWHEEL = 6,
SIMDEVTYPE_SERIALWHEEL = 7
} }
DeviceSubType; DeviceSubType;
@ -35,6 +36,7 @@ typedef enum
SIMDEVSUBTYPE_UNKNOWN = 0, SIMDEVSUBTYPE_UNKNOWN = 0,
SIMDEVSUBTYPE_CAMMUSC5 = 1, SIMDEVSUBTYPE_CAMMUSC5 = 1,
SIMDEVSUBTYPE_CAMMUSC12 = 2, SIMDEVSUBTYPE_CAMMUSC12 = 2,
SIMDEVSUBTYPE_MOZAR5 = 3,
} }
DeviceSubSubType; DeviceSubSubType;