From f76fe53a3b4247b9fffa53ef06c8f6c4e9029488 Mon Sep 17 00:00:00 2001 From: Lawstorant Date: Wed, 14 Aug 2024 11:35:37 +0200 Subject: [PATCH] Initial work --- .gitignore | 2 + src/monocoque/devices/serial/moza.c | 139 +++++++++++++++++++++++++++ src/monocoque/devices/serial/moza.h | 10 ++ src/monocoque/devices/serialdevice.c | 30 +++++- src/monocoque/devices/serialdevice.h | 1 + src/monocoque/devices/simdevice.h | 3 +- 6 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 src/monocoque/devices/serial/moza.c create mode 100644 src/monocoque/devices/serial/moza.h diff --git a/.gitignore b/.gitignore index e4329dd..a142b79 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /build /src/monocoque/tags /src/monocoque/simulatorapi/simapi + +.vscode diff --git a/src/monocoque/devices/serial/moza.c b/src/monocoque/devices/serial/moza.c new file mode 100644 index 0000000..d172362 --- /dev/null +++ b/src/monocoque/devices/serial/moza.c @@ -0,0 +1,139 @@ +#include +#include + +#include + +#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; + } +} diff --git a/src/monocoque/devices/serial/moza.h b/src/monocoque/devices/serial/moza.h new file mode 100644 index 0000000..228f659 --- /dev/null +++ b/src/monocoque/devices/serial/moza.h @@ -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 diff --git a/src/monocoque/devices/serialdevice.c b/src/monocoque/devices/serialdevice.c index 53aecc1..f4ae92f 100644 --- a/src/monocoque/devices/serialdevice.c +++ b/src/monocoque/devices/serialdevice.c @@ -8,6 +8,7 @@ #include "serialdevice.h" #include "hapticeffect.h" #include "serial/arduino.h" +#include "serial/moza.h" #include "../helper/parameters.h" #include "../simulatorapi/simapi/simapi/simdata.h" #include "../slog/slog.h" @@ -18,9 +19,16 @@ int serialdev_update(SimDevice* this, SimData* simdata) { 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; } @@ -126,7 +134,16 @@ int serialdev_free(SimDevice* this) { SerialDevice* serialdevice = (void *) this->derived; - arduino_free(serialdevice); + switch (serialdevice->type) + { + case SERIALDEV_MOZA: + moza_free(serialdevice); + break; + + default: + arduino_free(serialdevice); + } + free(serialdevice); return 0; @@ -145,6 +162,15 @@ int serialdev_init(SerialDevice* serialdevice, const char* portdev, int motorspo serialdevice->baud = baud; 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; } diff --git a/src/monocoque/devices/serialdevice.h b/src/monocoque/devices/serialdevice.h index 0df20a7..a58c85b 100644 --- a/src/monocoque/devices/serialdevice.h +++ b/src/monocoque/devices/serialdevice.h @@ -8,6 +8,7 @@ typedef enum ARDUINODEV__SHIFTLIGHTS = 0, ARDUINODEV__SIMWIND = 1, ARDUINODEV__HAPTIC = 2, + MOZADEV = 3, } SerialDeviceType; diff --git a/src/monocoque/devices/simdevice.h b/src/monocoque/devices/simdevice.h index 94957ef..b6955c4 100644 --- a/src/monocoque/devices/simdevice.h +++ b/src/monocoque/devices/simdevice.h @@ -37,7 +37,8 @@ typedef struct { typedef enum { SERIALDEV_UNKNOWN = 0, - SERIALDEV_ARDUINO = 1 + SERIALDEV_ARDUINO = 1, + SERIALDEV_MOZA = 2, } SerialType;