From 787cc9bd1d1c5f48ab853a2654cc423622a901e2 Mon Sep 17 00:00:00 2001 From: Nikos Verschore Date: Wed, 15 Jul 2020 23:35:30 +0200 Subject: [PATCH] Test and fix the implementation of Led Timings --- src/deck/drivers/interface/ledring12.h | 21 +++++++---- src/deck/drivers/src/ledring12.c | 34 +++++++++--------- src/modules/src/mem.c | 50 ++++++++++++++++---------- 3 files changed, 63 insertions(+), 42 deletions(-) diff --git a/src/deck/drivers/interface/ledring12.h b/src/deck/drivers/interface/ledring12.h index b4f60c0ae..7c2e75aca 100644 --- a/src/deck/drivers/interface/ledring12.h +++ b/src/deck/drivers/interface/ledring12.h @@ -11,19 +11,26 @@ #endif #ifndef LEDRING_TIME_MEM_SIZE -#define LEDRING_TIME_MEM_SIZE 50 +#define LEDRING_TIME_MEM_SIZE 10 #endif -typedef struct timings +extern uint8_t ledringmem[NBR_LEDS * 2]; + +typedef struct __attribute__((packed)) timing { uint8_t duration; // How long this color should be show in parts of 1/25s. So 25 will show the color 1s, before going to the next color. uint8_t color[2]; // What color should be shown in RGB565 format. - unsigned leds:5; // What led should be show, 0 equals all leds. - bool fade; // Fade from previous colour to this colour during the full duration. - unsigned rotate:2; // Speed of the rotation, number of seconds per revolution + unsigned leds:4; // What led should be show, 0 equals all leds. + bool fade:1; // Fade from previous colour to this colour during the full duration. + unsigned rotate:3; // Speed of the rotation, number of seconds per revolution +} ledtiming; + +typedef struct timings +{ + uint32_t hash; // Hash will later be used to check if the contents is already uploaded, so we don't reupload + ledtiming timings[LEDRING_TIME_MEM_SIZE]; } ledtimings; -extern uint8_t ledringmem[NBR_LEDS * 2]; -extern ledtimings ledringtimingsmem[LEDRING_TIME_MEM_SIZE]; +extern ledtimings ledringtimingsmem; #endif //__LEDRING12_H__ diff --git a/src/deck/drivers/src/ledring12.c b/src/deck/drivers/src/ledring12.c index 4103d0cb3..98abab472 100644 --- a/src/deck/drivers/src/ledring12.c +++ b/src/deck/drivers/src/ledring12.c @@ -113,7 +113,7 @@ static const uint8_t part_black[] = BLACK; uint8_t ledringmem[NBR_LEDS * 2]; -ledtimings ledringtimingsmem[LEDRING_TIME_MEM_SIZE]; +ledtimings ledringtimingsmem; /**************** Black (LEDs OFF) ***************/ @@ -769,7 +769,7 @@ static void locSrvStatus(uint8_t buffer[][3], bool reset) } } -static bool isTimeMemDone(ledtimings current) +static bool isTimeMemDone(ledtiming current) { return current.duration == 0 && current.color[0] == 0 && current.color[1] == 0; @@ -782,7 +782,6 @@ static float timeEffectRotation = 0; static void timeMemEffect(uint8_t outputBuffer[][3], bool reset) { - // Start timer when going to this if (reset) { for (int i = 0; i < NBR_LEDS; i++) { @@ -792,10 +791,10 @@ static void timeMemEffect(uint8_t outputBuffer[][3], bool reset) timeEffectRotation = 0; timeEffectTime = usecTimestamp() / 1000; - timeEffectI = 1; + timeEffectI = 0; } - - ledtimings current = ledringtimingsmem[timeEffectI]; + + ledtiming current = ledringtimingsmem.timings[timeEffectI]; // Stop when completed if (isTimeMemDone(current)) @@ -816,12 +815,12 @@ static void timeMemEffect(uint8_t outputBuffer[][3], bool reset) COPY_COLOR(timeEffectPrevBuffer[current.leds], color); } - timeEffectRotation += current.duration / (current.rotate * 1000); - // Goto next effect + if(current.rotate) + timeEffectRotation += 1.0f * current.duration * LEDRING_TIME_MEM_SEC / (current.rotate * 1000); timeEffectTime += LEDRING_TIME_MEM_SEC * current.duration; timeEffectI++; - current = ledringtimingsmem[timeEffectI]; + current = ledringtimingsmem.timings[timeEffectI]; if (isTimeMemDone(current)) return; @@ -836,8 +835,7 @@ static void timeMemEffect(uint8_t outputBuffer[][3], bool reset) } if (current.fade) { - float percent = - (time - timeEffectTime) / (current.duration * LEDRING_TIME_MEM_SEC); + float percent = 1.0 * (time - timeEffectTime) / (current.duration * LEDRING_TIME_MEM_SEC); if (current.leds == 0) for (int i = 0; i < NBR_LEDS; i++) for (int j = 0; j < 3; j++) @@ -856,17 +854,21 @@ static void timeMemEffect(uint8_t outputBuffer[][3], bool reset) } } - float rotate = timeEffectRotation + (time - timeEffectTime) / (current.rotate * 1000); + float rotate = timeEffectRotation; + if(current.rotate) { + rotate += 1.0f * (time - timeEffectTime) / (current.rotate * 1000); + } + int shift = rotate * NBR_LEDS; - float percentShift = rotate - shift; + float percentShift = rotate * NBR_LEDS - shift; shift = shift % NBR_LEDS; // Output current leds for (int i = 0; i < NBR_LEDS; i++) for (int j = 0; j < 3; j++) - currentBuffer[(i+shift) % NBR_LEDS][j] = - (1 - percentShift) * currentBuffer[i][j] + - percentShift * currentBuffer[(i+1) % NBR_LEDS][j]; + outputBuffer[(i+shift) % NBR_LEDS][j] = + percentShift * currentBuffer[i][j] + + (1-percentShift) * currentBuffer[(i+1) % NBR_LEDS][j]; } /**************** Effect list ***************/ diff --git a/src/modules/src/mem.c b/src/modules/src/mem.c index 1577c2d8b..550cd1f79 100644 --- a/src/modules/src/mem.c +++ b/src/modules/src/mem.c @@ -98,6 +98,7 @@ #define MEM_TYPE_LH 0x14 #define MEM_TYPE_TESTER 0x15 #define MEM_TYPE_USD 0x16 +#define MEM_TYPE_LEDMEM 0x17 #define MEM_LOCO_INFO 0x0000 #define MEM_LOCO_ANCHOR_BASE 0x1000 @@ -129,6 +130,8 @@ static uint8_t handleEepromRead(uint32_t memAddr, uint8_t readLen, uint8_t* star static uint8_t handleEepromWrite(uint32_t memAddr, uint8_t writeLen, uint8_t* startOfData); static uint8_t handleLedMemRead(uint32_t memAddr, uint8_t readLen, uint8_t* startOfData); static uint8_t handleLedMemWrite(uint32_t memAddr, uint8_t writeLen, uint8_t* startOfData); +static uint8_t handleLedTimingsMemRead(uint32_t memAddr, uint8_t readLen, uint8_t* startOfData); +static uint8_t handleLedTimingsMemWrite(uint32_t memAddr, uint8_t writeLen, uint8_t* startOfData); static uint8_t handleTrajectoryMemRead(uint32_t memAddr, uint8_t readLen, uint8_t* startOfData); static uint8_t handleTrajectoryMemWrite(uint32_t memAddr, uint8_t writeLen, uint8_t* startOfData); static uint8_t handleLighthouseMemRead(uint32_t memAddr, uint8_t readLen, uint8_t* startOfData); @@ -240,7 +243,7 @@ static void createInfoResponse(CRTPPacket* p, uint8_t memId) { createInfoResponseBody(p, MEM_TYPE_LED12, sizeof(ledringmem), noData); break; case LEDTIMINGSMEM_ID: - createInfoResponseBody(p, MEM_TYPE_LED12, sizeof(ledringtimingsmem), noData); + createInfoResponseBody(p, MEM_TYPE_LEDMEM, sizeof(ledringtimingsmem.timings), noData); break; case LOCO_ID: createInfoResponseBody(p, MEM_TYPE_LOCO, MEM_LOCO_ANCHOR_BASE + MEM_LOCO_ANCHOR_PAGE_SIZE * LOCO_MESSAGE_NR_OF_ANCHORS, noData); @@ -304,13 +307,7 @@ static void memReadProcess(CRTPPacket* p) { break; case LEDTIMINGSMEM_ID: - { - if (memAddr + readLen <= sizeof(ledringtimingsmem) && - memcpy(startOfData, &(ledringtimingsmem[memAddr]), readLen)) - status = STATUS_OK; - else - status = EIO; - } + status = handleLedTimingsMemRead(memAddr, readLen, startOfData); break; case LOCO_ID: @@ -489,17 +486,7 @@ static void memWriteProcess(CRTPPacket* p) { break; case LEDTIMINGSMEM_ID: - { - if ((memAddr + writeLen) <= sizeof(ledringtimingsmem)) - { - memcpy(&(ledringtimingsmem[memAddr]), startOfData, writeLen); - MEM_DEBUG("LED TIMINGS write addr:%i, led:%i\n", memAddr, writeLen); - } - else - { - MEM_DEBUG("\nLED TIMINGS write failed! addr:%i, led:%i\n", memAddr, writeLen); - } - } + status = handleLedTimingsMemWrite(memAddr, writeLen, startOfData); break; case TRAJ_ID: @@ -604,6 +591,31 @@ static uint8_t handleTrajectoryMemWrite(uint32_t memAddr, uint8_t writeLen, uint return status; } +static uint8_t handleLedTimingsMemRead(uint32_t memAddr, uint8_t readLen, uint8_t* startOfData) { + uint8_t status = EIO; + + if (memAddr + readLen <= sizeof(ledringtimingsmem.timings)) { + uint8_t* mem = (uint8_t*) &ledringtimingsmem.timings; + memcpy(startOfData, mem+memAddr, readLen); + status = STATUS_OK; + } + + return status; +} + + +static uint8_t handleLedTimingsMemWrite(uint32_t memAddr, uint8_t writeLen, uint8_t* startOfData) { + uint8_t status = EIO; + + if ((memAddr + writeLen) <= sizeof(ledringtimingsmem.timings)) { + uint8_t* mem = (uint8_t*) &ledringtimingsmem.timings; + memcpy(mem+memAddr, startOfData, writeLen); + status = STATUS_OK; + } + + return status; +} + static uint8_t handleLighthouseMemRead(uint32_t memAddr, uint8_t readLen, uint8_t* startOfData) { uint8_t status = EIO;