From 9e7af882a4fab88871786d6c6cc60dbf72595f3d Mon Sep 17 00:00:00 2001 From: Ben Blazak Date: Mon, 16 Jul 2012 18:45:04 -0700 Subject: [PATCH] finished linked lists in lib! and moved the header for common data types (from lib/data-types.h to lib/data-types/common.h) --- src/TODO | 13 ------ src/keyboard/ergodox.c | 2 +- src/keyboard/ergodox.h | 2 +- src/keyboard/ergodox/layout.h | 2 +- src/keyboard/ergodox/layout/qwerty.c | 2 +- src/keyboard/ergodox/matrix.c | 2 +- src/keyboard/ergodox/matrix.h | 2 +- src/keyboard/ergodox/mcp23018--private.h | 2 +- src/keyboard/ergodox/mcp23018.c | 2 +- src/keyboard/ergodox/teensy-2-0.c | 2 +- src/keyboard/ergodox/teensy-2-0.h | 2 +- src/lib/{data-types.h => data-types/common.h} | 5 +-- src/lib/data-types/linked-list.c | 42 ++++++++----------- src/lib/data-types/linked-list.h | 28 ++++++------- src/lib/data-types/list.h | 29 +++++++++++++ src/lib/data-types/queue.h | 29 +++++++++++++ src/lib/data-types/stack.h | 29 +++++++++++++ src/lib/key-functions.c | 2 +- src/lib/key-functions.h | 2 +- ...sb 2.0 spec sec 9 (usb device framework).h | 2 +- src/main.c | 2 +- 21 files changed, 133 insertions(+), 70 deletions(-) rename src/lib/{data-types.h => data-types/common.h} (86%) create mode 100644 src/lib/data-types/list.h create mode 100644 src/lib/data-types/queue.h create mode 100644 src/lib/data-types/stack.h diff --git a/src/TODO b/src/TODO index 3e90b63..d398be1 100644 --- a/src/TODO +++ b/src/TODO @@ -1,15 +1,2 @@ --+ set USB vendor ID = 0x1d50 // Openmoko, Inc. --+ set USB product ID = 0x6028 // ErgoDox ergonomic keyboard - -// ------- - --+ link to the [ErgoDox website] (http://ergodox.org/) in toplevel readme.md - --+ update licence.md with "except when otherwise noted" or something similar, to - reflect the fact that i have code (in src/lib, and contrib) under different - licences, with different authors - -// ------- - - make new branch and update for fredrick's new PCB (pre-prototype) diff --git a/src/keyboard/ergodox.c b/src/keyboard/ergodox.c index a55bfb9..ce01457 100644 --- a/src/keyboard/ergodox.c +++ b/src/keyboard/ergodox.c @@ -7,7 +7,7 @@ * ------------------------------------------------------------------------- */ -#include "lib/data-types.h" +#include "lib/data-types/common.h" #include "ergodox/matrix.h" #include "ergodox/mcp23018--private.h" diff --git a/src/keyboard/ergodox.h b/src/keyboard/ergodox.h index 00ba51e..df27410 100644 --- a/src/keyboard/ergodox.h +++ b/src/keyboard/ergodox.h @@ -11,7 +11,7 @@ #ifndef ERGODOX_h #define ERGODOX_h - #include "lib/data-types.h" + #include "lib/data-types/common.h" #include "ergodox/layout.h" // number of layers, layout #include "ergodox/led.h" // logical led controls diff --git a/src/keyboard/ergodox/layout.h b/src/keyboard/ergodox/layout.h index e58eb51..125f34d 100644 --- a/src/keyboard/ergodox/layout.h +++ b/src/keyboard/ergodox/layout.h @@ -12,7 +12,7 @@ #define LAYOUT_h #include - #include "lib/data-types.h" + #include "lib/data-types/common.h" #include "lib/key-functions.h" // for `kbfun_funptr_t` #include "matrix.h" // for number of rows and columns diff --git a/src/keyboard/ergodox/layout/qwerty.c b/src/keyboard/ergodox/layout/qwerty.c index 1e5490d..bff022b 100644 --- a/src/keyboard/ergodox/layout/qwerty.c +++ b/src/keyboard/ergodox/layout/qwerty.c @@ -10,7 +10,7 @@ #include -#include "lib/data-types.h" +#include "lib/data-types/common.h" #include "lib/usb/usage-page/keyboard--short-names.h" #include "lib/key-functions.h" diff --git a/src/keyboard/ergodox/matrix.c b/src/keyboard/ergodox/matrix.c index 3a1f9e7..564d857 100644 --- a/src/keyboard/ergodox/matrix.c +++ b/src/keyboard/ergodox/matrix.c @@ -7,7 +7,7 @@ * ------------------------------------------------------------------------- */ -#include "lib/data-types.h" +#include "lib/data-types/common.h" #include "matrix.h" diff --git a/src/keyboard/ergodox/matrix.h b/src/keyboard/ergodox/matrix.h index 1903a8f..1dfe086 100644 --- a/src/keyboard/ergodox/matrix.h +++ b/src/keyboard/ergodox/matrix.h @@ -10,7 +10,7 @@ #ifndef MATRIX_h #define MATRIX_h - #include "lib/data-types.h" + #include "lib/data-types/common.h" #define KB_ROWS 12 // must match real life #define KB_COLUMNS 7 // must match real life diff --git a/src/keyboard/ergodox/mcp23018--private.h b/src/keyboard/ergodox/mcp23018--private.h index a48b9b4..8533ebf 100644 --- a/src/keyboard/ergodox/mcp23018--private.h +++ b/src/keyboard/ergodox/mcp23018--private.h @@ -10,7 +10,7 @@ #ifndef MCP23018_h_PRIVATE #define MCP23018_h_PRIVATE - #include "lib/data-types.h" + #include "lib/data-types/common.h" #include "matrix.h" #define MCP23018_TWI_ADDRESS 0b0100000 diff --git a/src/keyboard/ergodox/mcp23018.c b/src/keyboard/ergodox/mcp23018.c index 793d332..3e50b7b 100644 --- a/src/keyboard/ergodox/mcp23018.c +++ b/src/keyboard/ergodox/mcp23018.c @@ -8,7 +8,7 @@ #include -#include "lib/data-types.h" +#include "lib/data-types/common.h" #include "lib/twi.h" // `TWI_FREQ` defined in "teensy-2-0.c" #include "matrix.h" diff --git a/src/keyboard/ergodox/teensy-2-0.c b/src/keyboard/ergodox/teensy-2-0.c index 3f037f0..6b436fd 100644 --- a/src/keyboard/ergodox/teensy-2-0.c +++ b/src/keyboard/ergodox/teensy-2-0.c @@ -9,7 +9,7 @@ #include #include -#include "lib/data-types.h" +#include "lib/data-types/common.h" #define TWI_FREQ 400000 #include "lib/twi.h" diff --git a/src/keyboard/ergodox/teensy-2-0.h b/src/keyboard/ergodox/teensy-2-0.h index 65fa67d..e8dd0b1 100644 --- a/src/keyboard/ergodox/teensy-2-0.h +++ b/src/keyboard/ergodox/teensy-2-0.h @@ -11,7 +11,7 @@ #define TEENSY_2_0_h #include // for the register macros - #include "lib/data-types.h" + #include "lib/data-types/common.h" // LED control diff --git a/src/lib/data-types.h b/src/lib/data-types/common.h similarity index 86% rename from src/lib/data-types.h rename to src/lib/data-types/common.h index 4c625a4..8f537da 100644 --- a/src/lib/data-types.h +++ b/src/lib/data-types/common.h @@ -7,13 +7,12 @@ * ------------------------------------------------------------------------- */ -#ifndef DATA_TYPES_h - #define DATA_TYPES_h +#ifndef DATA_TYPES_COMMON_h + #define DATA_TYPES_COMMON_h #include #include #include - #include "data-types/linked-list.h" #endif diff --git a/src/lib/data-types/linked-list.c b/src/lib/data-types/linked-list.c index 97dc1f8..3244a1d 100644 --- a/src/lib/data-types/linked-list.c +++ b/src/lib/data-types/linked-list.c @@ -1,10 +1,5 @@ /* ---------------------------------------------------------------------------- - * linked list - * - * Notes: - * - When 'position' is used, it referes to the position of the node in the - * list, not the node's offset. E.g. the node with position == 1 is the - * first node in the list. + * linked list : code * ---------------------------------------------------------------------------- * Copyright (c) 2012 Ben Blazak * Released under The MIT License (MIT) (see "license.md") @@ -13,16 +8,16 @@ #include -#include "lib/data-types.h" +#include "lib/data-types/common.h" #include "linked-list.h" // local macros (undefined later) #define _NEW_POINTER(type, name) type * name = (type *) malloc(sizeof(type)) -#define _list_t linked_list_t -#define _node_t linked_list_node_t -#define _data_t LINKED_LIST_DATA_TYPE +#define _list_t linked_list_t +#define _node_t linked_list_node_t +#define _data_t LINKED_LIST_DATA_TYPE /* @@ -53,7 +48,6 @@ _list_t * linked_list_new(void) { * - 1 => the second node in the list * - -1 => the last node in the list * - -2 => the second from the last node in the list - * - '0' is undefined (returns 'failure') * - out of bounds positions wrap around, so: * - [length] => 0 => the first node in the list * - -[length+1] => -1 => the last node in the list @@ -61,14 +55,21 @@ _list_t * linked_list_new(void) { * Returns * - success: the pointer to the list that was passed * - failure: NULL + * + * Notes + * - in this function, 'list->length' is incremented before the index is + * calculated|used, so that we have a consistent way to think of adding an + * element to the end of the list */ -_list_t * linked_list_insert(_list_t * list, _data_t data, int index) { +_list_t * linked_list_insert(_list_t * list, int index, _data_t data) { _NEW_POINTER(_node_t, node); if (!node) return NULL; + list->length++; + node->data = data; - if (list->length == 0) { + if (list->length == 1) { // insert as only node (no others exist yet) list->head = node; list->tail = node; @@ -98,7 +99,6 @@ _list_t * linked_list_insert(_list_t * list, _data_t data, int index) { } } - list->length++; return list; } @@ -185,12 +185,6 @@ _data_t linked_list_pop(_list_t * list, int index) { return data; } -/* - * find() - * TODO - */ -// TODO - /* * copy() * @@ -199,13 +193,13 @@ _data_t linked_list_pop(_list_t * list, int index) { * - failure: NULL */ _list_t * linked_list_copy(_list_t * list) { - _NEW_POINTER(_list_t, copy); + _list_t * copy = linked_list_new(); if (!copy) return NULL; bool error; _node_t * node = list->head; - for (uint8_t i=0; i<(list->length); i++) { - error = ! linked_list_insert(copy, node->data, -1); + for (int i=0; i<(list->length); i++) { + error = ! linked_list_insert(copy, -1, node->data); if (error) { linked_list_free(copy); return NULL; @@ -223,7 +217,7 @@ _list_t * linked_list_copy(_list_t * list) { */ void linked_list_free(_list_t * list) { _node_t * node; - for (uint8_t i=0; i<(list->length); i++) { + for (int i=0; i<(list->length); i++) { node = list->head; list->head = list->head->next; free(node); diff --git a/src/lib/data-types/linked-list.h b/src/lib/data-types/linked-list.h index a73ea82..525dcb9 100644 --- a/src/lib/data-types/linked-list.h +++ b/src/lib/data-types/linked-list.h @@ -7,10 +7,10 @@ * ------------------------------------------------------------------------- */ -#ifndef LINKED_LIST_h - #define LINKED_LIST_h +#ifndef DATA_TYPES_LINKED_LIST_h + #define DATA_TYPES_LINKED_LIST_h - #include "lib/data-types.h" + #include "lib/data-types/common.h" // default data type for the list @@ -26,28 +26,24 @@ }; struct linked_list { - uint8_t length; + uint8_t length; // 'uint8_t' to save ram struct linked_list_node * head; struct linked_list_node * tail; }; // typedefs - typedef struct linked_list linked_list_t; - typedef struct linked_list_node linked_list_node_t; + typedef struct linked_list linked_list_t; + typedef struct linked_list_node linked_list_node_t; // functions #define _list_t linked_list_t #define _data_t LINKED_LIST_DATA_TYPE - // TODO - _list_t * linked_list_new (void); - _list_t * linked_list_add_head (_list_t * list, _data_t data); - _list_t * linked_list_add_tail (_list_t * list, _data_t data); - _data_t linked_list_pop_head (_list_t * list); - _data_t linked_list_pop_tail (_list_t * list); - _data_t linked_list_read (_list_t * list, uint8_t position); - _list_t * linked_list_copy (_list_t * list); - void linked_list_free (_list_t * list); - // /TODO + _list_t * linked_list_new (void); + _list_t * linked_list_insert (_list_t * list, int index, _data_t data); + _data_t linked_list_peek (_list_t * list, int index); + _data_t linked_list_pop (_list_t * list, int index); + _list_t * linked_list_copy (_list_t * list); + void linked_list_free (_list_t * list); #undef _list_t #undef _data_t diff --git a/src/lib/data-types/list.h b/src/lib/data-types/list.h new file mode 100644 index 0000000..be63a24 --- /dev/null +++ b/src/lib/data-types/list.h @@ -0,0 +1,29 @@ +/* ---------------------------------------------------------------------------- + * list : exports + * ---------------------------------------------------------------------------- + * Copyright (c) 2012 Ben Blazak + * Released under The MIT License (MIT) (see "license.md") + * Project located at + * ------------------------------------------------------------------------- */ + + +#ifndef DATA_TYPES_LIST_h + #define DATA_TYPES_LIST_h + + #ifdef LIST_DATA_TYPE + #define LINKED_LIST_DATA_TYPE LIST_DATA_TYPE + #endif + #include "linked-list.h" + + typedef linked_list_t * list_t; + typedef linked_list_node_t * list_node_t; + + #define list_new linked_list_new + #define list_insert linked_list_insert + #define list_peek linked_list_peek + #define list_pop linked_list_pop + #define list_copy linked_list_copy + #define list_free linked_list_free + +#endif + diff --git a/src/lib/data-types/queue.h b/src/lib/data-types/queue.h new file mode 100644 index 0000000..c3049f5 --- /dev/null +++ b/src/lib/data-types/queue.h @@ -0,0 +1,29 @@ +/* ---------------------------------------------------------------------------- + * queue : exports + * ---------------------------------------------------------------------------- + * Copyright (c) 2012 Ben Blazak + * Released under The MIT License (MIT) (see "license.md") + * Project located at + * ------------------------------------------------------------------------- */ + + +#ifndef DATA_TYPES_QUEUE_h + #define DATA_TYPES_QUEUE_h + + #ifdef QUEUE_DATA_TYPE + #define LINKED_LIST_DATA_TYPE QUEUE_DATA_TYPE + #endif + #include "linked-list.h" + + typedef linked_list_t * queue_t; + typedef linked_list_node_t * queue_node_t; + + #define queue_new linked_list_new + #define queue_append(list, data) linked_list_insert(list, -1, data) + #define queue_peek linked_list_peek + #define queue_pop(list) linked_list_pop(list, 0) + #define queue_copy linked_list_copy + #define queue_free linked_list_free + +#endif + diff --git a/src/lib/data-types/stack.h b/src/lib/data-types/stack.h new file mode 100644 index 0000000..46c9407 --- /dev/null +++ b/src/lib/data-types/stack.h @@ -0,0 +1,29 @@ +/* ---------------------------------------------------------------------------- + * stack : exports + * ---------------------------------------------------------------------------- + * Copyright (c) 2012 Ben Blazak + * Released under The MIT License (MIT) (see "license.md") + * Project located at + * ------------------------------------------------------------------------- */ + + +#ifndef DATA_TYPES_STACK_h + #define DATA_TYPES_STACK_h + + #ifdef STACK_DATA_TYPE + #define LINKED_LIST_DATA_TYPE STACK_DATA_TYPE + #endif + #include "linked-list.h" + + typedef linked_list_t * stack_t; + typedef linked_list_node_t * stack_node_t; + + #define stack_new linked_list_new + #define stack_push(list, data) linked_list_insert(list, 0, data) + #define stack_peek linked_list_peek + #define stack_pop(list) linked_list_pop(list, 0) + #define stack_copy linked_list_copy + #define stack_free linked_list_free + +#endif + diff --git a/src/lib/key-functions.c b/src/lib/key-functions.c index c7f2e2c..57be346 100644 --- a/src/lib/key-functions.c +++ b/src/lib/key-functions.c @@ -13,7 +13,7 @@ #include #include "lib-other/pjrc/usb_keyboard/usb_keyboard.h" -#include "lib/data-types.h" +#include "lib/data-types/common.h" #include "lib/usb/usage-page/keyboard.h" #include "keyboard.h" diff --git a/src/lib/key-functions.h b/src/lib/key-functions.h index b6e87ce..b822cf2 100644 --- a/src/lib/key-functions.h +++ b/src/lib/key-functions.h @@ -10,7 +10,7 @@ #ifndef KEY_FUNCTIONS_h #define KEY_FUNCTIONS_h - #include "lib/data-types.h" + #include "lib/data-types/common.h" // -------------------------------------------------------------------- // include the appropriate 'matrix.h' diff --git a/src/lib/usb/notes from usb 2.0 spec sec 9 (usb device framework).h b/src/lib/usb/notes from usb 2.0 spec sec 9 (usb device framework).h index f6650d6..ac4b22c 100644 --- a/src/lib/usb/notes from usb 2.0 spec sec 9 (usb device framework).h +++ b/src/lib/usb/notes from usb 2.0 spec sec 9 (usb device framework).h @@ -36,7 +36,7 @@ // ---------------------------------------------------------------------------- -#include "lib/data-types.h" +#include "lib/data-types/common.h" // ---------------------------------------------------------------------------- diff --git a/src/main.c b/src/main.c index 78ec964..339c9a6 100644 --- a/src/main.c +++ b/src/main.c @@ -10,7 +10,7 @@ #include #include "lib-other/pjrc/usb_keyboard/usb_keyboard.h" -#include "lib/data-types.h" +#include "lib/data-types/common.h" #include "lib/key-functions.h" #include "keyboard.h"