Initial work

This commit is contained in:
Lawstorant 2024-08-14 11:35:37 +02:00 committed by Paul Dino Jones
parent 545168c3ab
commit f76fe53a3b
6 changed files with 182 additions and 3 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
/build /build
/src/monocoque/tags /src/monocoque/tags
/src/monocoque/simulatorapi/simapi /src/monocoque/simulatorapi/simapi
.vscode

View File

@ -0,0 +1,139 @@
#include <stdio.h>
#include <math.h>
#include <hidapi/hidapi.h>
#include "moza.h"
#include "../../slog/slog.h"
#define MOZA_TIMEOUT 1000
#define MOZA_SERIAL_TEMPLATE = {0x7e, 0x06, 0x41, 0x17, 0xfd, 0xde, 0x0, 0x0, 0x0, 0x0, 0x0}
#define MOZA_NUM_AVAILABLE_LEDS = 10
#define MOZA_BLINKING_BIT = 7
#define MOZA_MAGIC_VALUE = 0x0d
#define BIT(nr) (1UL << (nr))
unsigned char moza_checksum(unsigned char *data)
{
unsigned char ret = MOZA_MAGIC_VALUE;
for (short i = 0; i < sizeof(data)/sizeof(data[0]); i++)
{
ret += data[i];
}
return 0;
}
int moza_update(WheelDevice* wheeldevice, unsigned short maxrpm, unsigned short rpm)
{
unsigned char bytes[] = MOZA_SERIAL_TEMPLATE;
int size = sizeof(bytes)/sizeof(bytes[0])
if (rpm/maxrpm >= 0.8)
bytes[9] |= BIT(0);
if (rpm/maxrpm >= 0.83)
bytes[9] |= BIT(1);
if (rpm/maxrpm >= 0.86)
bytes[9] |= BIT(2);
if (rpm/maxrpm >= 0.89)
bytes[9] |= BIT(3);
if (rpm/maxrpm >= 0.91)
bytes[8] |= BIT(4);
if (rpm/maxrpm >= 0.92)
bytes[8] |= BIT(5);
if (rpm/maxrpm >= 0.93)
bytes[8] |= BIT(6);
if (rpm/maxrpm >= 0.94)
bytes[8] |= BIT(7);
if (rpm/maxrpm >= 0.96)
bytes[8] |= BIT(0);
if (rpm/maxrpm >= 0.96)
bytes[8] |= BIT(1);
// blinking
if (rpm/maxrpm >= 0.97)
bytes[9] |= BIT(MOZA_BLINKING_BIT);
bytes[10] = moza_checksum(bytes);
int result = 1;
if (serialdevice->port)
{
slogt("copying %i bytes to moza device", size);
result = check(sp_blocking_write(serialdevice->port, bytes, size, MOZA_TIMEOUT));
}
return result;
}
int moza_init(SerialDevice* serialdevice, const char* portdev)
{
slogi("initializing moza serial device...");
int error = 0;
char* port_name = strdup(portdev);
slogd("Looking for port %s.\n", port_name);
error = check(sp_get_port_by_name(port_name, &serialdevice->port));
if (error != 0)
{
return error;
}
slogd("Opening port.\n");
check(sp_open(serialdevice->port, SP_MODE_READ_WRITE));
slogd("Setting port to 115200 8N1, no flow control.\n");
check(sp_set_baudrate(serialdevice->port, 115200));
check(sp_set_bits(serialdevice->port, 8));
check(sp_set_parity(serialdevice->port, SP_PARITY_NONE));
check(sp_set_stopbits(serialdevice->port, 1));
check(sp_set_flowcontrol(serialdevice->port, SP_FLOWCONTROL_NONE));
free(port_name);
slogd("Successfully setup moza serial device...");
return 0;
}
int moza_free(WheelDevice* wheeldevice)
{
check(sp_close(serialdevice->port));
sp_free_port(serialdevice->port);
}
int check(enum sp_return result)
{
/* For this example we'll just exit on any error by calling abort(). */
char* error_message;
switch (result)
{
case SP_ERR_ARG:
//printf("Error: Invalid argument.\n");
return 1;
//abort();
case SP_ERR_FAIL:
error_message = sp_last_error_message();
//printf("error: failed: %s\n", error_message);
sloge("error: serial write failed: %s", error_message);
sp_free_error_message(error_message);
//abort();
case SP_ERR_SUPP:
printf("Error: Not supported.\n");
abort();
case SP_ERR_MEM:
printf("Error: Couldn't allocate memory.\n");
abort();
case SP_OK:
default:
return result;
}
}

View File

@ -0,0 +1,10 @@
#ifndef _MOZA_H
#define _MOZA_H
#include "../serialdevice.h"
int moza_update(SerialDevice* serialdevice, unsigned short maxrpm, unsigned short rpm);
int moza_init(SerialDevice* serialdevice);
int moza_free(SerialDevice* serialdevice);
#endif

View File

@ -8,6 +8,7 @@
#include "serialdevice.h" #include "serialdevice.h"
#include "hapticeffect.h" #include "hapticeffect.h"
#include "serial/arduino.h" #include "serial/arduino.h"
#include "serial/moza.h"
#include "../helper/parameters.h" #include "../helper/parameters.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simapi/simapi/simdata.h"
#include "../slog/slog.h" #include "../slog/slog.h"
@ -18,9 +19,16 @@ 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 +134,16 @@ int serialdev_free(SimDevice* this)
{ {
SerialDevice* serialdevice = (void *) this->derived; SerialDevice* serialdevice = (void *) this->derived;
arduino_free(serialdevice); switch (serialdevice->type)
{
case SERIALDEV_MOZA:
moza_free(serialdevice);
break;
default:
arduino_free(serialdevice);
}
free(serialdevice); free(serialdevice);
return 0; return 0;
@ -145,6 +162,15 @@ int serialdev_init(SerialDevice* serialdevice, const char* portdev, int motorspo
serialdevice->baud = baud; serialdevice->baud = baud;
error = arduino_init(serialdevice, portdev); error = arduino_init(serialdevice, portdev);
switch (serialdevice->type)
{
case SERIALDEV_MOZA:
error = moza_init(serialdevice, portdev);
break;
default:
error = arduino_init(serialdevice, portdev);
}
return error; return error;
} }

View File

@ -8,6 +8,7 @@ typedef enum
ARDUINODEV__SHIFTLIGHTS = 0, ARDUINODEV__SHIFTLIGHTS = 0,
ARDUINODEV__SIMWIND = 1, ARDUINODEV__SIMWIND = 1,
ARDUINODEV__HAPTIC = 2, ARDUINODEV__HAPTIC = 2,
MOZADEV = 3,
} }
SerialDeviceType; SerialDeviceType;

View File

@ -37,7 +37,8 @@ typedef struct {
typedef enum typedef enum
{ {
SERIALDEV_UNKNOWN = 0, SERIALDEV_UNKNOWN = 0,
SERIALDEV_ARDUINO = 1 SERIALDEV_ARDUINO = 1,
SERIALDEV_MOZA = 2,
} }
SerialType; SerialType;