Refactored serial arduino devices. Created custom structs for sending data to arduino devices.

This commit is contained in:
Paul Dino Jones 2024-03-05 04:51:49 +00:00
parent d2e0bebc4e
commit eb18a05c51
9 changed files with 138 additions and 15 deletions

View File

@ -0,0 +1,25 @@
#ifndef _SHIFTLIGHTSDATA_H
#define _SHIFTLIGHTSDATA_H
#include <stdint.h>
#include <stdbool.h>
typedef struct
{
char color_1_red;
char color_1_green;
char color_1_blue;
char color_2_red;
char color_2_green;
char color_2_blue;
char color_3_red;
char color_3_green;
char color_3_blue;
uint32_t maxrpm;
uint32_t rpm;
uint32_t pulses;
}
ShiftLightsData;
#endif

View File

@ -1,7 +1,7 @@
#include <FastLED.h>
#include "simdata.h"
#define SIMDATA_SIZE sizeof(SimData)
#define SIMDATA_SIZE sizeof(ShiftLightsData)
#define LED_PIN 7
#define NUM_LEDS 6
@ -39,11 +39,8 @@ void setup()
}
FastLED.clear();
sd.rpms = 0;
sd.rpm = 0;
sd.maxrpm = 6500;
sd.altitude = 10;
sd.pulses = 40000;
sd.velocity = 10;
}
void loop()
@ -55,9 +52,8 @@ void loop()
{
Serial.readBytes(buff, SIMDATA_SIZE);
memcpy(&sd, &buff, SIMDATA_SIZE);
rpm = sd.rpms;
rpm = sd.rpm;
maxrpm = sd.maxrpm;
}

15
src/arduino/simwind/simwind.h Executable file
View File

@ -0,0 +1,15 @@
#ifndef _SIMWINDDATA_H
#define _SIMWINDDATA_H
#include <stdint.h>
#include <stdbool.h>
typedef struct
{
float velocity;
float fanpower;
}
SimWindData;
#endif

View File

@ -1,7 +1,7 @@
#include <Adafruit_MotorShield.h>
#include "simdata.h"
#include "simwind.h"
#define BYTE_SIZE sizeof(SimData)
#define BYTE_SIZE sizeof(SimWindData)
#define KPHTOMPH .621317
#define FANPOWER .6

View File

@ -8,17 +8,39 @@
#define arduino_timeout 2000
int arduino_update(SerialDevice* serialdevice, SimData* simdata)
int arduino_update(SerialDevice* serialdevice, void* data, size_t size)
{
int result = 1;
if (serialdevice->port)
{
result = check(sp_blocking_write(serialdevice->port, simdata, sizeof(SimData), arduino_timeout));
result = check(sp_blocking_write(serialdevice->port, data, size, arduino_timeout));
}
return result;
}
//int arduino_shiftlights_update(SerialDevice* serialdevice, SimData* simdata)
//{
// int result = 1;
// if (serialdevice->port)
// {
// result = check(sp_blocking_write(serialdevice->port, simdata, sizeof(SimData), arduino_timeout));
// }
//
// return result;
//}
//
//int arduino_simwind_update(SerialDevice* serialdevice, SimData* simdata)
//{
// int result = 1;
// if (serialdevice->port)
// {
// result = check(sp_blocking_write(serialdevice->port, simdata, sizeof(SimData), arduino_timeout));
// }
//
// return result;
//}
int arduino_init(SerialDevice* serialdevice, const char* portdev)
{
slogi("initializing arduino serial device...");
@ -80,3 +102,4 @@ int check(enum sp_return result)
return result;
}
}

View File

@ -4,8 +4,9 @@
#include "../simdevice.h"
#include "../serialdevice.h"
int arduino_update(SerialDevice* serialdevice, SimData* simdata);
int arduino_init(SerialDevice* serialdevice, const char* portdev);
int arduino_update(SerialDevice* serialdevice, void* data, size_t size);
int arduino_init(SerialDevice* serialdevice, const char* portdev);
int arduino_free(SerialDevice* serialdevice);
int check(enum sp_return result);

View File

@ -15,10 +15,42 @@ int serialdev_update(SimDevice* this, SimData* simdata)
{
SerialDevice* serialdevice = (void *) this->derived;
arduino_update(serialdevice, simdata);
arduino_update(serialdevice, simdata, sizeof(SimData));
return 0;
}
int arduino_shiftlights_update(SimDevice* this, SimData* simdata)
{
SerialDevice* serialdevice = (void *) this->derived;
int result = 1;
serialdevice->u.shiftlightsdata.maxrpm = simdata->maxrpm;
serialdevice->u.shiftlightsdata.rpm = simdata->rpms;
// we can add configs to set all the colors
// i can move the size to the initialization since it should not change
size_t size = sizeof(ShiftLightsData);
arduino_update(serialdevice, &serialdevice->u.shiftlightsdata, size);
return result;
}
int arduino_simwind_update(SimDevice* this, SimData* simdata)
{
SerialDevice* serialdevice = (void *) this->derived;
int result = 1;
serialdevice->u.simwinddata.velocity = simdata->velocity;
// this can be added to the config, all config dependent can be added to init
serialdevice->u.simwinddata.fanpower = 0.6;
size_t size = sizeof(SimWindData);
arduino_update(serialdevice, &serialdevice->u.simwinddata, size);
return result;
}
int serialdev_free(SimDevice* this)
{
SerialDevice* serialdevice = (void *) this->derived;
@ -43,6 +75,8 @@ int serialdev_init(SerialDevice* serialdevice, const char* portdev)
}
static const vtable serial_simdevice_vtable = { &serialdev_update, &serialdev_free };
static const vtable arduino_shiftlights_vtable = { &arduino_shiftlights_update, &serialdev_free };
static const vtable arduino_simwind_vtable = { &arduino_simwind_update, &serialdev_free };
SerialDevice* new_serial_device(DeviceSettings* ds) {
@ -53,6 +87,20 @@ SerialDevice* new_serial_device(DeviceSettings* ds) {
this->m.derived = this;
this->m.vtable = &serial_simdevice_vtable;
slogt("Attempting to configure arduino device with subtype: %i", ds->dev_subtype);
switch (ds->dev_subtype) {
case (SIMDEVTYPE_SHIFTLIGHTS):
this->devicetype = ARDUINODEV__SHIFTLIGHTS;
this->m.vtable = &arduino_shiftlights_vtable;
slogi("Initializing arduino device for shiftlights.");
break;
case (SIMDEVTYPE_SIMWIND):
this->devicetype = ARDUINODEV__SIMWIND;
this->m.vtable = &arduino_simwind_vtable;
slogi("Initializing arduino devices for sim wind.");
break;
}
int error = serialdev_init(this, ds->serialdevsettings.portdev);
if (error != 0)

View File

@ -3,5 +3,11 @@
#include <libserialport.h>
typedef enum
{
ARDUINODEV__SHIFTLIGHTS = 0,
ARDUINODEV__SIMWIND = 1,
}
SerialDeviceType;
#endif

View File

@ -9,6 +9,8 @@
#include "../helper/confighelper.h"
#include "../simulatorapi/simapi/simapi/simdata.h"
#include "../../arduino/simwind/simwind.h"
#include "../../arduino/shiftlights/shiftlights.h"
typedef struct SimDevice SimDevice;
@ -43,10 +45,17 @@ typedef struct
int id;
SerialType type;
struct sp_port* port;
SerialDeviceType devicetype;
union
{
SimWindData simwinddata;
ShiftLightsData shiftlightsdata;
} u;
}
SerialDevice;
int serialdev_update(SimDevice* this, SimData* simdata);
int arduino_shiftlights_update(SimDevice* this, SimData* simdata);
int arduino_simwind_update(SimDevice* this, SimData* simdata);
int serialdev_free(SimDevice* this);
SerialDevice* new_serial_device(DeviceSettings* ds);