made lists use pointers again :/ didn't know what i was doing...

and fixed a tiny little logic bug in ...exec_key()

and now scheduling works as expected! :D for all three timers!!!
partial-rewrite
Ben Blazak 2013-05-24 21:48:45 -07:00
parent 58255dc29b
commit 71d541be2f
9 changed files with 66 additions and 67 deletions

View File

@ -122,7 +122,7 @@ static _layout_t PROGMEM _layout;
*/
static struct {
bool tick_keypresses : 1;
} _flags;
} _flags = { .tick_keypresses = true };
// ----------------------------------------------------------------------------

View File

@ -61,13 +61,13 @@ void kb__layout__exec_key(bool pressed, uint8_t row, uint8_t column) {
if (pressed)
pressed_layer[row][column] = layer;
_flags.tick_keypresses = (pressed) ? true : false; // set default
(*function)();
if (_flags.tick_keypresses)
timer___tick_keypresses();
_flags.tick_keypresses = true; // restore default
return;
}
}

View File

@ -34,7 +34,7 @@
*
* - If you want to iterate through a list, use something like
*
* for (node_t * node = list.head; node; node = node->_super.next) {
* for (node_t * node = list->head; node; node = node->_super.next) {
* // do stuff
* }
*
@ -77,12 +77,12 @@ typedef struct list__list_t {
// ----------------------------------------------------------------------------
void * list__insert (list__list_t list, int8_t index, void * node);
void * list__peek (list__list_t list, int8_t index);
void * list__pop_index (list__list_t list, int8_t index);
void * list__pop_node (list__list_t list, void * node);
void * list__pop_node_next (list__list_t list, void * node);
void list__free_all (list__list_t list);
void * list__insert (list__list_t * list, int8_t index, void * node);
void * list__peek (list__list_t * list, int8_t index);
void * list__pop_index (list__list_t * list, int8_t index);
void * list__pop_node (list__list_t * list, void * node);
void * list__pop_node_next (list__list_t * list, void * node);
void list__free_all (list__list_t * list);
// ----------------------------------------------------------------------------
@ -120,7 +120,7 @@ void list__free_all (list__list_t list);
* Insert `node` at position `index % list->length`
*
* Arguments:
* - `list`: The list to be operated on
* - `list`: A pointer to the list to be operated on
* - `index`: An `int8_t` indicating the position the new node will occupy
* - `node`: A `void *` pointer to the node to insert
*
@ -134,7 +134,7 @@ void list__free_all (list__list_t list);
* Return a pointer to the node at position `index % list->length`
*
* Arguments:
* - `list`: The list to be operated on
* - `list`: A pointer to the list to be operated on
* - `index`: An `int8_t` indicating the position the new node will occupy
*
* Returns:
@ -152,7 +152,7 @@ void list__free_all (list__list_t list);
* responsibility.
*
* Arguments:
* - `list`: The list to be operated on
* - `list`: A pointer to the list to be operated on
* - `index`: An `int8_t` indicating the position the new node will occupy
*
* Returns:
@ -169,7 +169,7 @@ void list__free_all (list__list_t list);
* responsibility.
*
* Arguments:
* - `list`: The list to be operated on
* - `list`: A pointer to the list to be operated on
* - `node`: A `void *` pointer to the node to pop
*
* Returns:
@ -183,7 +183,7 @@ void list__free_all (list__list_t list);
* next element, if such an element exists
*
* Arguments:
* - `list`: The list to be operated on
* - `list`: A pointer to the list to be operated on
* - `node`: A `void *` pointer to the node to pop (and free)
*
* Returns:
@ -202,6 +202,6 @@ void list__free_all (list__list_t list);
* Free all node pointers in `list`
*
* Arguments:
* - `list`: The list to be operated on
* - `list`: A pointer to the list to be operated on
*/

View File

@ -23,36 +23,36 @@
// ----------------------------------------------------------------------------
void * list__insert(list_t list, int8_t index, void * node) {
void * list__insert(list_t * list, int8_t index, void * node) {
// if `node` does not exist
if (!node)
return NULL;
list.length++;
list->length++;
if (list.length == 1) {
if (list->length == 1) {
// insert as only node (no others exist yet)
list.head = node;
list.tail = node;
list->head = node;
list->tail = node;
N(node)->next = NULL;
} else {
index %= list.length;
index %= list->length;
if (index == 0) {
// insert as first node
N(node)->next = list.head;
list.head = node;
N(node)->next = list->head;
list->head = node;
} else if (index == list.length-1) {
} else if (index == list->length-1) {
// insert as last node
N(list.tail)->next = node;
list.tail = node;
N(list->tail)->next = node;
list->tail = node;
N(node)->next = NULL;
} else {
// insert as other node
node_t * previous = list.head;
node_t * previous = list->head;
for (uint8_t i=1; i<index; i++)
previous = previous->next;
N(node)->next = previous->next;
@ -63,68 +63,68 @@ void * list__insert(list_t list, int8_t index, void * node) {
return node;
}
void * list__peek(list_t list, int8_t index) {
void * list__peek(list_t * list, int8_t index) {
// if no nodes exist
if (list.length == 0)
if (list->length == 0)
return NULL;
index %= list.length;
index %= list->length;
// if last node
if (index == list.length-1)
return list.tail;
if (index == list->length-1)
return list->tail;
// else
node_t * node = list.head;
node_t * node = list->head;
for (uint8_t i=0; i<index; i++)
node = N(node)->next;
return node;
}
void * list__pop_index(list_t list, int8_t index) {
void * list__pop_index(list_t * list, int8_t index) {
// if no nodes exist
if (list.length == 0)
if (list->length == 0)
return NULL;
index %= list.length;
index %= list->length;
node_t * node;
if (index == 0) {
// pop first node
node = list.head;
list.head = N(node)->next;
node = list->head;
list->head = N(node)->next;
} else {
// find the `index-1`th node
node_t * previous = list.head;
node_t * previous = list->head;
for (uint8_t i=1; i<index; i++)
previous = previous->next;
// if last node
if (index == list.length-1)
list.tail = previous;
if (index == list->length-1)
list->tail = previous;
// pop the node at `index`
node = previous->next;
previous->next = N(node)->next;
}
list.length--;
list->length--;
return node;
}
void * list__pop_node(list_t list, void * node) {
void * list__pop_node(list_t * list, void * node) {
// if `node` does not exist, or no nodes exist
if (!node || list.length == 0)
if (!node || list->length == 0)
return NULL;
node_t * previous = list.head;
node_t * previous = list->head;
if (node == list.head) {
if (node == list->head) {
// pop first node
list.head = N(node)->next;
list->head = N(node)->next;
} else {
// find the previous node (if `node` is in `list`)
@ -135,19 +135,19 @@ void * list__pop_node(list_t list, void * node) {
}
// if last node
if (node == list.tail)
list.tail = previous;
if (node == list->tail)
list->tail = previous;
// pop the node
previous->next = N(node)->next;
}
list.length--;
list->length--;
return node;
}
void * list__pop_node_next(list_t list, void * node ) {
void * list__pop_node_next(list_t * list, void * node ) {
if (!list__pop_node(list, node))
return NULL; // `node` was not in `list`
@ -157,11 +157,11 @@ void * list__pop_node_next(list_t list, void * node ) {
return next;
}
void list__free_all(list_t list) {
void list__free_all(list_t * list) {
node_t * node;
while (list.head) {
node = list.head;
list.head = node->next;
while (list->head) {
node = list->head;
list->head = node->next;
free(node);
}
}

View File

@ -27,7 +27,7 @@
// ----------------------------------------------------------------------------
static volatile uint16_t _milliseconds__counter;
static list__list_t _milliseconds__scheduled_events;
static list__list_t * _milliseconds__scheduled_events = &(list__list_t){};
// ----------------------------------------------------------------------------

View File

@ -23,8 +23,8 @@ typedef struct {
// ----------------------------------------------------------------------------
uint8_t event_list__append( list__list_t list,
uint16_t ticks,
uint8_t event_list__append( list__list_t * list,
uint16_t ticks,
void(*function)(void) ) {
if (!function) return 0; // success: nothing to do
@ -39,8 +39,8 @@ uint8_t event_list__append( list__list_t list,
return 0; // success
}
void event_list__tick(list__list_t list) {
for (event_t * event = list.head; event;) {
void event_list__tick(list__list_t * list) {
for (event_t * event = list->head; event;) {
if (event->ticks == 0) {
(*event->function)();
event = list__pop_node_next(list, event);

View File

@ -28,10 +28,10 @@
// ----------------------------------------------------------------------------
uint8_t event_list__append ( list__list_t list,
uint16_t ticks,
uint8_t event_list__append ( list__list_t * list,
uint16_t ticks,
void(*function)(void) );
void event_list__tick (list__list_t list);
void event_list__tick (list__list_t * list);
// ----------------------------------------------------------------------------

View File

@ -17,8 +17,8 @@
// ----------------------------------------------------------------------------
#define DEFINE_TIMER(name) \
static uint16_t _##name##__counter; \
static list__list_t _##name##__scheduled_events; \
static uint16_t _##name##__counter; \
static list__list_t * _##name##__scheduled_events = &(list__list_t){}; \
\
uint16_t timer__get_##name(void) { \
return _##name##__counter; \

View File

@ -13,5 +13,4 @@
SRC += $(wildcard $(CURDIR)/*.c)
SRC += $(wildcard $(CURDIR)/device/$(MCU).c)
SRC += $(wildcard $(CURDIR)/event-list/$(MCU).c)