Refactored serial arduino devices. Created custom structs for sending data to arduino devices.
This commit is contained in:
parent
d2e0bebc4e
commit
eb18a05c51
|
|
@ -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
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#include <FastLED.h>
|
#include <FastLED.h>
|
||||||
#include "simdata.h"
|
#include "simdata.h"
|
||||||
|
|
||||||
#define SIMDATA_SIZE sizeof(SimData)
|
#define SIMDATA_SIZE sizeof(ShiftLightsData)
|
||||||
|
|
||||||
#define LED_PIN 7
|
#define LED_PIN 7
|
||||||
#define NUM_LEDS 6
|
#define NUM_LEDS 6
|
||||||
|
|
@ -39,11 +39,8 @@ void setup()
|
||||||
}
|
}
|
||||||
FastLED.clear();
|
FastLED.clear();
|
||||||
|
|
||||||
sd.rpms = 0;
|
sd.rpm = 0;
|
||||||
sd.maxrpm = 6500;
|
sd.maxrpm = 6500;
|
||||||
sd.altitude = 10;
|
|
||||||
sd.pulses = 40000;
|
|
||||||
sd.velocity = 10;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
|
|
@ -55,9 +52,8 @@ void loop()
|
||||||
{
|
{
|
||||||
Serial.readBytes(buff, SIMDATA_SIZE);
|
Serial.readBytes(buff, SIMDATA_SIZE);
|
||||||
memcpy(&sd, &buff, SIMDATA_SIZE);
|
memcpy(&sd, &buff, SIMDATA_SIZE);
|
||||||
rpm = sd.rpms;
|
rpm = sd.rpm;
|
||||||
maxrpm = sd.maxrpm;
|
maxrpm = sd.maxrpm;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
#ifndef _SIMWINDDATA_H
|
||||||
|
#define _SIMWINDDATA_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
float velocity;
|
||||||
|
float fanpower;
|
||||||
|
}
|
||||||
|
SimWindData;
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#include <Adafruit_MotorShield.h>
|
#include <Adafruit_MotorShield.h>
|
||||||
#include "simdata.h"
|
#include "simwind.h"
|
||||||
|
|
||||||
#define BYTE_SIZE sizeof(SimData)
|
#define BYTE_SIZE sizeof(SimWindData)
|
||||||
#define KPHTOMPH .621317
|
#define KPHTOMPH .621317
|
||||||
#define FANPOWER .6
|
#define FANPOWER .6
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,17 +8,39 @@
|
||||||
|
|
||||||
#define arduino_timeout 2000
|
#define arduino_timeout 2000
|
||||||
|
|
||||||
int arduino_update(SerialDevice* serialdevice, SimData* simdata)
|
int arduino_update(SerialDevice* serialdevice, void* data, size_t size)
|
||||||
{
|
{
|
||||||
int result = 1;
|
int result = 1;
|
||||||
if (serialdevice->port)
|
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;
|
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)
|
int arduino_init(SerialDevice* serialdevice, const char* portdev)
|
||||||
{
|
{
|
||||||
slogi("initializing arduino serial device...");
|
slogi("initializing arduino serial device...");
|
||||||
|
|
@ -80,3 +102,4 @@ int check(enum sp_return result)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@
|
||||||
#include "../simdevice.h"
|
#include "../simdevice.h"
|
||||||
#include "../serialdevice.h"
|
#include "../serialdevice.h"
|
||||||
|
|
||||||
int arduino_update(SerialDevice* serialdevice, SimData* simdata);
|
|
||||||
|
int arduino_update(SerialDevice* serialdevice, void* data, size_t size);
|
||||||
int arduino_init(SerialDevice* serialdevice, const char* portdev);
|
int arduino_init(SerialDevice* serialdevice, const char* portdev);
|
||||||
int arduino_free(SerialDevice* serialdevice);
|
int arduino_free(SerialDevice* serialdevice);
|
||||||
int check(enum sp_return result);
|
int check(enum sp_return result);
|
||||||
|
|
|
||||||
|
|
@ -15,10 +15,42 @@ int serialdev_update(SimDevice* this, SimData* simdata)
|
||||||
{
|
{
|
||||||
SerialDevice* serialdevice = (void *) this->derived;
|
SerialDevice* serialdevice = (void *) this->derived;
|
||||||
|
|
||||||
arduino_update(serialdevice, simdata);
|
|
||||||
|
|
||||||
|
arduino_update(serialdevice, simdata, sizeof(SimData));
|
||||||
return 0;
|
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)
|
int serialdev_free(SimDevice* this)
|
||||||
{
|
{
|
||||||
SerialDevice* serialdevice = (void *) this->derived;
|
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 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) {
|
SerialDevice* new_serial_device(DeviceSettings* ds) {
|
||||||
|
|
||||||
|
|
@ -53,6 +87,20 @@ SerialDevice* new_serial_device(DeviceSettings* ds) {
|
||||||
this->m.derived = this;
|
this->m.derived = this;
|
||||||
this->m.vtable = &serial_simdevice_vtable;
|
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);
|
int error = serialdev_init(this, ds->serialdevsettings.portdev);
|
||||||
|
|
||||||
if (error != 0)
|
if (error != 0)
|
||||||
|
|
|
||||||
|
|
@ -3,5 +3,11 @@
|
||||||
|
|
||||||
#include <libserialport.h>
|
#include <libserialport.h>
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
ARDUINODEV__SHIFTLIGHTS = 0,
|
||||||
|
ARDUINODEV__SIMWIND = 1,
|
||||||
|
}
|
||||||
|
SerialDeviceType;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@
|
||||||
#include "../helper/confighelper.h"
|
#include "../helper/confighelper.h"
|
||||||
#include "../simulatorapi/simapi/simapi/simdata.h"
|
#include "../simulatorapi/simapi/simapi/simdata.h"
|
||||||
|
|
||||||
|
#include "../../arduino/simwind/simwind.h"
|
||||||
|
#include "../../arduino/shiftlights/shiftlights.h"
|
||||||
|
|
||||||
typedef struct SimDevice SimDevice;
|
typedef struct SimDevice SimDevice;
|
||||||
|
|
||||||
|
|
@ -43,10 +45,17 @@ typedef struct
|
||||||
int id;
|
int id;
|
||||||
SerialType type;
|
SerialType type;
|
||||||
struct sp_port* port;
|
struct sp_port* port;
|
||||||
|
SerialDeviceType devicetype;
|
||||||
|
union
|
||||||
|
{
|
||||||
|
SimWindData simwinddata;
|
||||||
|
ShiftLightsData shiftlightsdata;
|
||||||
|
} u;
|
||||||
}
|
}
|
||||||
SerialDevice;
|
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);
|
int serialdev_free(SimDevice* this);
|
||||||
|
|
||||||
SerialDevice* new_serial_device(DeviceSettings* ds);
|
SerialDevice* new_serial_device(DeviceSettings* ds);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue