Commit 5cdc03a4 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Make queue abstract data type

parent 5cc1110b
PRG = crowbx
OBJ = crowbx.o midi.o dac.o voice.o uart.o gate.o poly.o mono.o
OBJ = crowbx.o midi.o dac.o voice.o uart.o gate.o poly.o mono.o queue.o
MCU_TARGET = atmega328p
#MCU_TARGET = at90s2313
#MCU_TARGET = at90s2333
......
#include "queue.h"
uint8_t
q_advance(struct queue *q, uint8_t i)
{
return (i + 1) % (q->size + 1);
}
uint8_t
q_full(struct queue *q)
{
return q_advance(q, q->tail) == q->head;
}
void
q_enqueue(struct queue *q, uint8_t x)
{
if (q_full(q)) {
return;
}
q->array[q->tail] = x;
q->tail = q_advance(q, q->tail);
}
uint8_t
q_empty(struct queue *q)
{
return q->tail == q->head;
}
uint8_t
q_dequeue(struct queue *q)
{
if (q_empty(q)) {
return q->size;
}
uint8_t x = q->array[q->head];
q->head = q_advance(q, q->head);
return x;
}
#ifndef _queue_h
#define _queue_h
#include <inttypes.h>
struct queue {
uint8_t head;
uint8_t tail;
uint8_t *array;
uint8_t size;
};
uint8_t q_full(struct queue *q);
void q_enqueue(struct queue *q, uint8_t x);
uint8_t q_empty(struct queue *q);
uint8_t q_dequeue(struct queue *q);
#endif
#include "voice.h"
#include "queue.h"
// The voice assigner moves voices between the 'avail' (available) queue
// and the 'playing' list. The 'avail' data structure is a queue to get a
......@@ -38,50 +39,31 @@
// avail is a fixed-size queue implemented with an array and two
// pointers. It is used to track which voices are available (i.e. not
// playing). It is a queue in order to get 'rotating' voice assignment.
#define AVAIL_SIZE (NUM_VOICES + 1)
uint8_t avail[AVAIL_SIZE];
uint8_t avail_head;
uint8_t avail_tail;
uint8_t
avail_advance(uint8_t i)
{
return (i + 1) % AVAIL_SIZE;
}
uint8_t
avail_is_full(void)
{
return avail_advance(avail_tail) == avail_head;
}
uint8_t avail_array[NUM_VOICES + 1];
struct queue avail = {
.head = 0,
.tail = 0,
.array = avail_array,
.size = NUM_VOICES,
};
void
avail_enqueue(uint8_t v)
{
if (avail_is_full()) {
return;
}
avail[avail_tail] = v;
avail_tail = avail_advance(avail_tail);
q_enqueue(&avail, v);
}
uint8_t
avail_is_empty(void)
{
return avail_tail == avail_head;
return q_empty(&avail);
}
uint8_t
avail_dequeue(void)
{
if (avail_is_empty()) {
return NUM_VOICES;
}
uint8_t v = avail[avail_head];
avail_head = avail_advance(avail_head);
return v;
return q_dequeue(&avail);
}
// playing is an ordered set implemented as a sort of bounded singly linked
......@@ -145,8 +127,8 @@ uint8_t notes[NUM_VOICES];
void
voice_init(void)
{
avail_head = 0;
avail_tail = 0;
avail.head = 0;
avail.tail = 0;
for (uint8_t i = 0; i < NUM_VOICES; i++) {
avail_enqueue(i);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment