Update loop to 60fps. Update some arduino devices to 115200. Refactor shiftlights code for performance.

This commit is contained in:
Paul Dino Jones 2024-09-04 12:16:21 -04:00
parent f31947dc69
commit 2fb0383091
6 changed files with 84 additions and 57 deletions

View File

@ -6,20 +6,21 @@
typedef struct typedef struct
{ {
unsigned char color_1_red; uint32_t litleds;
unsigned char color_1_green; //unsigned char color_1_red;
unsigned char color_1_blue; //unsigned char color_1_green;
unsigned char color_2_red; //unsigned char color_1_blue;
unsigned char color_2_green; //unsigned char color_2_red;
unsigned char color_2_blue; //unsigned char color_2_green;
unsigned char color_3_red; //unsigned char color_2_blue;
unsigned char color_3_green; //unsigned char color_3_red;
unsigned char color_3_blue; //unsigned char color_3_green;
unsigned char space_1; //unsigned char color_3_blue;
unsigned char space_2; //unsigned char space_1;
unsigned char space_3; //unsigned char space_2;
uint32_t maxrpm; //unsigned char space_3;
uint32_t rpm; //uint32_t maxrpm;
//uint32_t rpm;
} }
ShiftLightsData; ShiftLightsData;

View File

@ -7,15 +7,15 @@
#define NUM_LEDS 6 #define NUM_LEDS 6
#define BRIGHTNESS 40 #define BRIGHTNESS 40
#define COLOR1A 0 #define COLOR1R 0
#define COLOR1G 255
#define COLOR1B 0 #define COLOR1B 0
#define COLOR1C 255 #define COLOR2R 255
#define COLOR2A 0 #define COLOR2G 255
#define COLOR2B 255 #define COLOR2B 0
#define COLOR2C 0 #define COLOR3R 255
#define COLOR3A 255 #define COLOR3G 0
#define COLOR3B 0 #define COLOR3B 0
#define COLOR3C 0
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
ShiftLightsData sd; ShiftLightsData sd;
@ -39,62 +39,69 @@ void setup()
} }
FastLED.clear(); FastLED.clear();
sd.rpm = 0; sd.litleds = 0;
sd.maxrpm = 6500; //sd.rpm = 0;
//sd.maxrpm = 6500;
} }
void loop() void loop()
{ {
int l = 0; int l = 0;
int lit = sd.litleds;
char buff[SIMDATA_SIZE]; char buff[SIMDATA_SIZE];
if (Serial.available() >= SIMDATA_SIZE) if (Serial.available() >= SIMDATA_SIZE)
{ {
Serial.readBytes(buff, SIMDATA_SIZE); Serial.readBytes(buff, SIMDATA_SIZE);
memcpy(&sd, &buff, SIMDATA_SIZE); memcpy(&sd, &buff, SIMDATA_SIZE);
rpm = sd.rpm; lit = sd.litleds;
maxrpm = sd.maxrpm;
} }
//if (Serial.available() >= SIMDATA_SIZE)
//{
// Serial.readBytes(buff, SIMDATA_SIZE);
// memcpy(&sd, &buff, SIMDATA_SIZE);
// rpm = sd.rpm;
// maxrpm = sd.maxrpm;
//}
while (l < numlights) //while (l < numlights)
{ //{
lights[l] = 0; // lights[l] = 0;
l++; // l++;
} //}
l = -1; //l = -1;
int rpmlights = 0; //int rpmlights = 0;
while (rpm > rpmlights) //while (rpm > rpmlights)
{ //{
if (l>=0) // if (l>=0)
{ // {
lights[l] = 1; // lights[l] = 1;
} // }
l++; // l++;
rpmlights = rpmlights + (((maxrpm-250)/numlights)); // rpmlights = rpmlights + (((maxrpm-250)/numlights));
} //}
l = 0; l = 0;
FastLED.clear(); FastLED.clear();
while (l < numlights) while (l < lit)
{ {
if (l >= numlights / 2) if (l >= numlights / 2)
{ {
leds[l] = CRGB ( COLOR1A, COLOR1B, COLOR1C); leds[l] = CRGB ( COLOR2R, COLOR2G, COLOR2B);
} }
if (l < numlights / 2) if (l < numlights / 2)
{ {
leds[l] = CRGB ( COLOR2A, COLOR2B, COLOR2C); leds[l] = CRGB ( COLOR1R, COLOR1G, COLOR1B);
} }
if (l == numlights - 1) if (l == numlights - 1)
{ {
leds[l] = CRGB ( COLOR3A, COLOR3B, COLOR3C); leds[l] = CRGB ( COLOR3R, COLOR3G, COLOR3B);
} }
if (lights[l] <= 0) //if (lights[l] <= 0)
{ //{
leds[l] = CRGB ( 0, 0, 0); // leds[l] = CRGB ( 0, 0, 0);
} //}
FastLED.show();
l++; l++;
} }
FastLED.show();
} }

View File

@ -22,7 +22,7 @@ int effect3 = 0;
int effect4 = 0; int effect4 = 0;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(115200);
if (!AFMS.begin()) { if (!AFMS.begin()) {
Serial.println("Could not find Motor Shield. Check wiring."); Serial.println("Could not find Motor Shield. Check wiring.");
while (1); while (1);

View File

@ -14,7 +14,7 @@ SimWindData sd;
int velocity = 0; int velocity = 0;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(115200);
if (!AFMS.begin()) { if (!AFMS.begin()) {
Serial.println("Could not find Motor Shield. Check wiring."); Serial.println("Could not find Motor Shield. Check wiring.");
while (1); while (1);

View File

@ -28,9 +28,28 @@ int arduino_shiftlights_update(SimDevice* this, SimData* simdata)
int result = 1; int result = 1;
serialdevice->u.shiftlightsdata.maxrpm = simdata->maxrpm; int num_avail_leds = 6;
serialdevice->u.shiftlightsdata.rpm = simdata->rpms; int rpm = simdata->rpms;
slogt("Updating arduino device rpms to %i", serialdevice->u.shiftlightsdata.rpm); int maxrpm = simdata->maxrpm;
int litleds = 0;
if(rpm > 0 && maxrpm > 0)
{
int rpmmargin = ceil(.05*maxrpm);
int rpminterval = (maxrpm-rpmmargin) / (num_avail_leds);
for (int l = 1; l <= (num_avail_leds); l++)
{
if(rpm >= (rpminterval * l))
{
litleds = l;
}
}
}
serialdevice->u.shiftlightsdata.litleds = litleds;
//serialdevice->u.shiftlightsdata.rpm = simdata->rpms;
slogt("Updating arduino device lights to %i", serialdevice->u.shiftlightsdata.litleds);
// we can add configs to set all the colors // we can add configs to set all the colors
// i can move the size to the initialization since it should not change // i can move the size to the initialization since it should not change
size_t size = sizeof(ShiftLightsData); size_t size = sizeof(ShiftLightsData);

View File

@ -305,7 +305,7 @@ void datacheckcallback(uv_timer_t* handle)
simdata->tyrediameter[1] = -1; simdata->tyrediameter[1] = -1;
simdata->tyrediameter[2] = -1; simdata->tyrediameter[2] = -1;
simdata->tyrediameter[3] = -1; simdata->tyrediameter[3] = -1;
uv_timer_start(&datamaptimer, shmdatamapcallback, 2000, 125); uv_timer_start(&datamaptimer, shmdatamapcallback, 2000, 16);
uv_timer_stop(handle); uv_timer_stop(handle);
} }
} }