Skip to content

Commit

Permalink
fix serializer to read/write from one contiguous block of memory. wor…
Browse files Browse the repository at this point in the history
…ks on hardware now.
  • Loading branch information
jrcurtis committed Mar 27, 2016
1 parent a8eaa21 commit 344671d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 34 deletions.
20 changes: 18 additions & 2 deletions include/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ typedef enum
LP_IS_ARP = 1 << 9
} LpFlags;

/// The number of notes needed for all 8 sequences. Two of these are used: one
/// for the live sequences data, and one for extra storage.
#define NOTE_BANK_SIZE (GRID_SIZE * SEQUENCE_LENGTH)

/// The size of the block of memory where the app header, and the two note
/// banks are stored, which is also used as a buffer for the serializer, which
/// is why it needs to be declared as one block of memroy.
#define LP_BUFFER_SIZE (sizeof(uint32_t) + 2 * NOTE_BANK_SIZE * sizeof(Note))

#define APP_ID (('S' << 8) | 'q')
#define DATA_VERSION (2)
#define APP_HEADER ((APP_ID << 16) | (DATA_VERSION))

/*******************************************************************************
* Global data
******************************************************************************/
Expand All @@ -53,11 +66,14 @@ extern uint16_t lp_tap_tempo_sum;
extern uint8_t lp_tap_tempo_counter;

// Data
extern uint8_t lp_buffer[LP_BUFFER_SIZE];
extern uint32_t* lp_app_header;
extern Note* lp_note_bank;
extern Note* lp_note_storage;

extern Scale lp_scale;
extern Voices lp_voices;
extern PadNotes lp_pad_notes;
extern NoteBank lp_note_bank;
extern NoteBank lp_note_storage;
extern Sequencer lp_sequencer;

// Setup UI elements
Expand Down
2 changes: 1 addition & 1 deletion include/note.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void note_init(Note* n);

void note_play(Note* n, uint8_t channel, uint8_t full_velocity);

void note_control(Note* n, Sequence_* s);
void note_control(Note* n, struct Sequence_* s);

void note_kill(Note* n, uint8_t channel);

Expand Down
4 changes: 0 additions & 4 deletions include/sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ typedef enum
#define seq_set_queued(f, x) (set_masked( \
(f), SEQ_QUEUED_MASK, SEQ_QUEUED_OFFSET, (x)))

/// The number of notes needed for all 8 sequences. Two of these are used: one
/// for the live sequences data, and one for extra storage.
typedef Note NoteBank[GRID_SIZE * SEQUENCE_LENGTH];

typedef struct Sequence_
{
uint16_t flags;
Expand Down
3 changes: 0 additions & 3 deletions include/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

#include "app.h"

extern const uint16_t APP_ID;
extern const uint16_t DATA_VERSION;

void serialize_app();
void deserialize_app();
void serialize_clear();
Expand Down
7 changes: 5 additions & 2 deletions src/seq.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ uint16_t lp_tap_tempo_sum = 0;
uint8_t lp_tap_tempo_counter = 0;

// Data
uint8_t lp_buffer[LP_BUFFER_SIZE];
uint32_t* lp_app_header = (uint32_t*)lp_buffer;
Note* lp_note_bank = (Note*)(lp_buffer + sizeof(uint32_t));
Note* lp_note_storage = (Note*)(lp_buffer + sizeof(uint32_t)) + NOTE_BANK_SIZE;

Scale lp_scale;
Voices lp_voices;
PadNotes lp_pad_notes;
NoteBank lp_note_bank;
NoteBank lp_note_storage;
Sequencer lp_sequencer;

// UI
Expand Down
56 changes: 34 additions & 22 deletions src/serializer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

#include <string.h>

#include "data.h"
#include "scale.h"
#include "sequencer.h"
Expand All @@ -7,9 +9,6 @@

#include "serializer.h"

const uint16_t APP_ID = ('S' << 8) | 'q';
const uint16_t DATA_VERSION = 1;

/*******************************************************************************
* Data layout
*
Expand All @@ -20,6 +19,8 @@ const uint16_t DATA_VERSION = 1;
* -------
* 4
*
* lp_note_bank 768
*
* lp_midi_port 1
* lp_rcv_clock_port 1
* lp_flags 2
Expand Down Expand Up @@ -67,8 +68,6 @@ const uint16_t DATA_VERSION = 1;
* -------
* 18
*
* lp_note_bank 768
*
* =======
* 916
*
Expand All @@ -78,29 +77,39 @@ const uint16_t DATA_VERSION = 1;
* Utility
******************************************************************************/

#define write_bytes(d) hal_write_flash(offset, (uint8_t*)&(d), sizeof(d)); \
#define write_bytes(d) memcpy(((uint8_t*)lp_note_storage) + offset, \
(uint8_t*)&(d), \
sizeof(d)); \
offset += sizeof(d);

#define read_bytes(d) hal_read_flash(offset, (uint8_t*)&(d), sizeof(d)); \
#define read_bytes(d) memcpy((uint8_t*)&d, \
((uint8_t*)lp_note_storage) + offset, \
sizeof(d)); \
offset += sizeof(d);

uint8_t is_data_saved()
{
uint16_t temp;
uint32_t temp;

hal_read_flash(0, (uint8_t*)&temp, sizeof(temp));
if (temp != APP_ID)
if (temp != APP_HEADER)
{
return 0;
}

hal_read_flash(sizeof(temp), (uint8_t*)&temp, sizeof(temp));
if (temp != DATA_VERSION)
return 1;
}

// After the serializer has trashed the note storage bank, restore it with
// empty notes.
void reset_note_storage()
{
for (uint16_t i = 0; i < GRID_SIZE * SEQUENCE_LENGTH; i++)
{
return 0;
lp_note_storage[i].note_number = -1;
lp_note_storage[i].velocity = 0;
lp_note_storage[i].flags = 0x00;
}

return 1;
}

/*******************************************************************************
Expand All @@ -111,8 +120,7 @@ void serialize_app()
{
uint32_t offset = 0;

write_bytes(APP_ID);
write_bytes(DATA_VERSION);
*lp_app_header = APP_HEADER;

write_bytes(lp_midi_port);
write_bytes(lp_rcv_clock_port);
Expand Down Expand Up @@ -154,7 +162,10 @@ void serialize_app()
write_bytes(lp_user_control_bank.channel_numbers);
write_bytes(lp_user_control_bank.bipolar_checkboxes);

write_bytes(lp_note_bank);
hal_write_flash(
0, lp_buffer, ((uint8_t*)lp_note_storage) + offset - lp_buffer);

reset_note_storage();
}

void serialize_clear()
Expand All @@ -164,9 +175,8 @@ void serialize_clear()
return;
}

uint16_t temp = 0;
uint32_t temp = 0;
hal_write_flash(0, (uint8_t*)&temp, sizeof(temp));
hal_write_flash(sizeof(temp), (uint8_t*)&temp, sizeof(temp));
}

/*******************************************************************************
Expand All @@ -180,7 +190,9 @@ void deserialize_app()
return;
}

uint32_t offset = sizeof(APP_ID) + sizeof(DATA_VERSION);
hal_read_flash(0, lp_buffer, USER_AREA_SIZE);

uint32_t offset = 0;
uint16_t temp16 = 0;
uint8_t temp8 = 0;

Expand All @@ -190,6 +202,7 @@ void deserialize_app()

read_bytes(temp16);
scale_set_notes(&lp_scale, temp16);
layout_assign_pads(&sequencer_get_active(&lp_sequencer)->layout);

read_bytes(temp8);
sequencer_set_tempo_millis(&lp_sequencer, temp8);
Expand Down Expand Up @@ -236,6 +249,5 @@ void deserialize_app()
read_bytes(lp_user_control_bank.channel_numbers);
read_bytes(lp_user_control_bank.bipolar_checkboxes);

read_bytes(lp_note_bank);

reset_note_storage();
}

0 comments on commit 344671d

Please sign in to comment.