-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement 10 slot note priority system.
Overwrite oldest note when additional notes come in.
- Loading branch information
1 parent
483fe47
commit 197d7c9
Showing
6 changed files
with
108 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "note_priority.h" | ||
#include <memory.h> | ||
|
||
void note_priority_init(NotePriorityStack* notePriority) | ||
{ | ||
notePriority->top = -1; | ||
memset(notePriority->slot, 0, NOTE_PRIORITY_LENGTH); | ||
} | ||
|
||
// 0 1 2 ... | ||
// Oldest -> Most Recent | ||
void note_priority_push(NotePriorityStack* notePriority, u8 pitch) | ||
{ | ||
if (notePriority->top == NOTE_PRIORITY_LENGTH - 1) { | ||
// rewrite array, evicting oldest item | ||
for (u16 i = 0; i < notePriority->top; i++) { | ||
notePriority->slot[i] = notePriority->slot[i + 1]; | ||
} | ||
notePriority->top--; | ||
} | ||
notePriority->slot[++notePriority->top] = pitch; | ||
} | ||
|
||
u8 note_priority_pop(NotePriorityStack* notePriority) | ||
{ | ||
if (notePriority->top == -1) { | ||
return 0; | ||
} | ||
|
||
return notePriority->slot[notePriority->top--]; | ||
} | ||
|
||
void note_priority_remove(NotePriorityStack* notePriority, u8 pitch) | ||
{ | ||
s16 writeCursor = -1; | ||
for (u16 i = 0; i <= notePriority->top; i++) { | ||
u8 item = notePriority->slot[i]; | ||
if (item == pitch) { | ||
continue; | ||
} else { | ||
notePriority->slot[++writeCursor] = item; | ||
} | ||
} | ||
notePriority->top = writeCursor; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
#include <types.h> | ||
|
||
#define NOTE_PRIORITY_LENGTH 10 | ||
|
||
typedef struct NotePriorityStack { | ||
u8 slot[NOTE_PRIORITY_LENGTH]; | ||
s16 top; | ||
} NotePriorityStack; | ||
|
||
void note_priority_init(NotePriorityStack* notePriority); | ||
void note_priority_push(NotePriorityStack* notePriority, u8 pitch); | ||
u8 note_priority_pop(NotePriorityStack* notePriority); | ||
void note_priority_remove(NotePriorityStack* notePriority, u8 pitch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "cmocka_inc.h" | ||
|
||
#include "note_priority.h" | ||
|
||
static NotePriorityStack testStack; | ||
|
||
static int test_note_priority_setup(UNUSED void** state) | ||
{ | ||
note_priority_init(&testStack); | ||
return 0; | ||
} | ||
|
||
static void test_note_priority_evicts_old_items(UNUSED void** state) | ||
{ | ||
const u16 additive = 50; | ||
|
||
for (u16 i = 0; i <= 11; i++) { | ||
note_priority_push(&testStack, i + additive); | ||
} | ||
|
||
for (s16 i = 11; i > 1; i--) { | ||
u8 item = note_priority_pop(&testStack); | ||
u8 expected = i + additive; | ||
assert_int_equal(item, expected); | ||
} | ||
|
||
u8 nilPop = note_priority_pop(&testStack); | ||
assert_int_equal(nilPop, 0); | ||
} |