Skip to content

Commit

Permalink
Test and fix the implementation of Led Timings
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfje committed Jul 15, 2020
1 parent ffbb0c2 commit 787cc9b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
21 changes: 14 additions & 7 deletions src/deck/drivers/interface/ledring12.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
34 changes: 18 additions & 16 deletions src/deck/drivers/src/ledring12.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) ***************/

Expand Down Expand Up @@ -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;
Expand All @@ -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++) {
Expand All @@ -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))
Expand All @@ -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;
Expand All @@ -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++)
Expand All @@ -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 ***************/
Expand Down
50 changes: 31 additions & 19 deletions src/modules/src/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 787cc9b

Please sign in to comment.