Skip to content

Commit

Permalink
keira: liltracker: add reserved bytes, change signature to LILT
Browse files Browse the repository at this point in the history
  • Loading branch information
and3rson committed Apr 12, 2024
1 parent 1cd8778 commit 4a60aed
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
4 changes: 2 additions & 2 deletions firmware/keira/src/apps/liltracker/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <stdint.h>

typedef struct noteinfo_t {
int index; // [0;11]
int octave;
uint8_t index; // [0;11]
uint8_t octave;
char* toStr();
void add(int16_t semitoneCount);
void fromFrequency(float frequency);
Expand Down
11 changes: 11 additions & 0 deletions firmware/keira/src/apps/liltracker/pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ int Pattern::calculateWriteBufferSize() {
Acquire acquire(xMutex);
int32_t bufferSize = 0;

// Count 32 reserved bytes
bufferSize += 32;

// Calculate all channels
for (int32_t channelIndex = 0; channelIndex < CHANNEL_COUNT; channelIndex++) {
// Calculate channel settings
Expand All @@ -84,6 +87,11 @@ int Pattern::writeToBuffer(uint8_t* buffer) {
Acquire acquire(xMutex);
int32_t offset = 0;

// Write 32 reserved bytes
while (offset < 32) {
buffer[offset++] = 0;
}

// Write all channels
for (int32_t channelIndex = 0; channelIndex < CHANNEL_COUNT; channelIndex++) {
// Write channel settings
Expand All @@ -108,6 +116,9 @@ int Pattern::readFromBuffer(const uint8_t* buffer) {
Acquire acquire(xMutex);
int32_t offset = 0;

// Skip 32 reserved bytes
offset = 32;

// Read all channels
for (int32_t channelIndex = 0; channelIndex < CHANNEL_COUNT; channelIndex++) {
// Read channel settings
Expand Down
9 changes: 5 additions & 4 deletions firmware/keira/src/apps/liltracker/pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ typedef enum {
} event_type_t;

typedef struct event_t {
noteinfo_t note;
uint8_t volume;
event_type_t type;
effect_t effect;
noteinfo_t note; // 2 bytes
uint8_t volume; // 1 byte
event_type_t type; // 1 byte
effect_t effect; // 2 bytes
uint8_t reserved[10]; // 10 bytes
} event_t;

typedef struct {
Expand Down
25 changes: 21 additions & 4 deletions firmware/keira/src/apps/liltracker/track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,17 @@ void Track::setBPM(int16_t bpm) {
this->bpm = CLAMP(bpm, 30, 900);
}

void Track::reset() {
Acquire acquire(xMutex, true);
pages.resize(0);
patterns.resize(0);
pages.resize(4);
patterns.resize(4);
}

int32_t Track::calculateWriteBufferSize() {
Acquire acquire(xMutex, true);
int32_t size = 3 * sizeof(int16_t); // Header, BPM, pattern count, page count
int32_t size = 32 * sizeof(int16_t); // Header (signature + reserved), BPM, pattern count, page count
for (int16_t i = 0; i < getPatternCount(); i++) {
size += getPattern(i)->calculateWriteBufferSize();
}
Expand All @@ -87,10 +95,16 @@ int32_t Track::writeToBuffer(uint8_t* data) {
Acquire acquire(xMutex, true);
int32_t offset = 0;

// Write header ("LIL")
// Write header signature ("LILT")
data[offset++] = 'L';
data[offset++] = 'I';
data[offset++] = 'L';
data[offset++] = 'T';

// Write header reserved bytes until offset 32
while (offset < 32) {
data[offset++] = 0;
}

// Write BPM
WRITE_TO_BUFFER(data, bpm);
Expand Down Expand Up @@ -118,11 +132,14 @@ int32_t Track::readFromBuffer(const uint8_t* data) {
Acquire acquire(xMutex, true);
int32_t offset = 0;

// Read header ("LIL")
if (data[offset++] != 'L' || data[offset++] != 'I' || data[offset++] != 'L') {
// Read header signature ("LIL")
if (data[offset++] != 'L' || data[offset++] != 'I' || data[offset++] != 'L' || data[offset++] != 'T') {
return -1;
}

// Skip header reserved bytes until offset 32
offset = 32;

// Read BPM
READ_FROM_BUFFER(bpm, data);

Expand Down
1 change: 1 addition & 0 deletions firmware/keira/src/apps/liltracker/track.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Track {
page_t* getPage(int16_t index);
int16_t getBPM();
void setBPM(int16_t bpm);
void reset();

int32_t calculateWriteBufferSize();
int32_t writeToBuffer(uint8_t* data);
Expand Down

0 comments on commit 4a60aed

Please sign in to comment.