finished linked lists in lib!

and moved the header for common data types (from lib/data-types.h to
lib/data-types/common.h)
partial-rewrite
Ben Blazak 2012-07-16 18:45:04 -07:00
parent 813569cfe0
commit 9e7af882a4
21 changed files with 133 additions and 70 deletions

View File

@ -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) - make new branch and update for fredrick's new PCB (pre-prototype)

View File

@ -7,7 +7,7 @@
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
#include "lib/data-types.h" #include "lib/data-types/common.h"
#include "ergodox/matrix.h" #include "ergodox/matrix.h"
#include "ergodox/mcp23018--private.h" #include "ergodox/mcp23018--private.h"

View File

@ -11,7 +11,7 @@
#ifndef ERGODOX_h #ifndef ERGODOX_h
#define 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/layout.h" // number of layers, layout
#include "ergodox/led.h" // logical led controls #include "ergodox/led.h" // logical led controls

View File

@ -12,7 +12,7 @@
#define LAYOUT_h #define LAYOUT_h
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include "lib/data-types.h" #include "lib/data-types/common.h"
#include "lib/key-functions.h" // for `kbfun_funptr_t` #include "lib/key-functions.h" // for `kbfun_funptr_t`
#include "matrix.h" // for number of rows and columns #include "matrix.h" // for number of rows and columns

View File

@ -10,7 +10,7 @@
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include "lib/data-types.h" #include "lib/data-types/common.h"
#include "lib/usb/usage-page/keyboard--short-names.h" #include "lib/usb/usage-page/keyboard--short-names.h"
#include "lib/key-functions.h" #include "lib/key-functions.h"

View File

@ -7,7 +7,7 @@
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
#include "lib/data-types.h" #include "lib/data-types/common.h"
#include "matrix.h" #include "matrix.h"

View File

@ -10,7 +10,7 @@
#ifndef MATRIX_h #ifndef MATRIX_h
#define 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_ROWS 12 // must match real life
#define KB_COLUMNS 7 // must match real life #define KB_COLUMNS 7 // must match real life

View File

@ -10,7 +10,7 @@
#ifndef MCP23018_h_PRIVATE #ifndef MCP23018_h_PRIVATE
#define MCP23018_h_PRIVATE #define MCP23018_h_PRIVATE
#include "lib/data-types.h" #include "lib/data-types/common.h"
#include "matrix.h" #include "matrix.h"
#define MCP23018_TWI_ADDRESS 0b0100000 #define MCP23018_TWI_ADDRESS 0b0100000

View File

@ -8,7 +8,7 @@
#include <util/twi.h> #include <util/twi.h>
#include "lib/data-types.h" #include "lib/data-types/common.h"
#include "lib/twi.h" // `TWI_FREQ` defined in "teensy-2-0.c" #include "lib/twi.h" // `TWI_FREQ` defined in "teensy-2-0.c"
#include "matrix.h" #include "matrix.h"

View File

@ -9,7 +9,7 @@
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include "lib/data-types.h" #include "lib/data-types/common.h"
#define TWI_FREQ 400000 #define TWI_FREQ 400000
#include "lib/twi.h" #include "lib/twi.h"

View File

@ -11,7 +11,7 @@
#define TEENSY_2_0_h #define TEENSY_2_0_h
#include <avr/io.h> // for the register macros #include <avr/io.h> // for the register macros
#include "lib/data-types.h" #include "lib/data-types/common.h"
// LED control // LED control

View File

@ -7,13 +7,12 @@
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
#ifndef DATA_TYPES_h #ifndef DATA_TYPES_COMMON_h
#define DATA_TYPES_h #define DATA_TYPES_COMMON_h
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include "data-types/linked-list.h"
#endif #endif

View File

@ -1,10 +1,5 @@
/* ---------------------------------------------------------------------------- /* ----------------------------------------------------------------------------
* linked list * linked list : code
*
* 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.
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
* Copyright (c) 2012 Ben Blazak <benblazak.dev@gmail.com> * Copyright (c) 2012 Ben Blazak <benblazak.dev@gmail.com>
* Released under The MIT License (MIT) (see "license.md") * Released under The MIT License (MIT) (see "license.md")
@ -13,16 +8,16 @@
#include <stdlib.h> #include <stdlib.h>
#include "lib/data-types.h" #include "lib/data-types/common.h"
#include "linked-list.h" #include "linked-list.h"
// local macros (undefined later) // local macros (undefined later)
#define _NEW_POINTER(type, name) type * name = (type *) malloc(sizeof(type)) #define _NEW_POINTER(type, name) type * name = (type *) malloc(sizeof(type))
#define _list_t linked_list_t #define _list_t linked_list_t
#define _node_t linked_list_node_t #define _node_t linked_list_node_t
#define _data_t LINKED_LIST_DATA_TYPE #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 second node in the list
* - -1 => the last node in the list * - -1 => the last node in the list
* - -2 => the second from 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: * - out of bounds positions wrap around, so:
* - [length] => 0 => the first node in the list * - [length] => 0 => the first node in the list
* - -[length+1] => -1 => the last node in the list * - -[length+1] => -1 => the last node in the list
@ -61,14 +55,21 @@ _list_t * linked_list_new(void) {
* Returns * Returns
* - success: the pointer to the list that was passed * - success: the pointer to the list that was passed
* - failure: NULL * - 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); _NEW_POINTER(_node_t, node);
if (!node) return NULL; if (!node) return NULL;
list->length++;
node->data = data; node->data = data;
if (list->length == 0) { if (list->length == 1) {
// insert as only node (no others exist yet) // insert as only node (no others exist yet)
list->head = node; list->head = node;
list->tail = 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; return list;
} }
@ -185,12 +185,6 @@ _data_t linked_list_pop(_list_t * list, int index) {
return data; return data;
} }
/*
* find()
* TODO
*/
// TODO
/* /*
* copy() * copy()
* *
@ -199,13 +193,13 @@ _data_t linked_list_pop(_list_t * list, int index) {
* - failure: NULL * - failure: NULL
*/ */
_list_t * linked_list_copy(_list_t * list) { _list_t * linked_list_copy(_list_t * list) {
_NEW_POINTER(_list_t, copy); _list_t * copy = linked_list_new();
if (!copy) return NULL; if (!copy) return NULL;
bool error; bool error;
_node_t * node = list->head; _node_t * node = list->head;
for (uint8_t i=0; i<(list->length); i++) { for (int i=0; i<(list->length); i++) {
error = ! linked_list_insert(copy, node->data, -1); error = ! linked_list_insert(copy, -1, node->data);
if (error) { if (error) {
linked_list_free(copy); linked_list_free(copy);
return NULL; return NULL;
@ -223,7 +217,7 @@ _list_t * linked_list_copy(_list_t * list) {
*/ */
void linked_list_free(_list_t * list) { void linked_list_free(_list_t * list) {
_node_t * node; _node_t * node;
for (uint8_t i=0; i<(list->length); i++) { for (int i=0; i<(list->length); i++) {
node = list->head; node = list->head;
list->head = list->head->next; list->head = list->head->next;
free(node); free(node);

View File

@ -7,10 +7,10 @@
* ------------------------------------------------------------------------- */ * ------------------------------------------------------------------------- */
#ifndef LINKED_LIST_h #ifndef DATA_TYPES_LINKED_LIST_h
#define 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 // default data type for the list
@ -26,28 +26,24 @@
}; };
struct linked_list { struct linked_list {
uint8_t length; uint8_t length; // 'uint8_t' to save ram
struct linked_list_node * head; struct linked_list_node * head;
struct linked_list_node * tail; struct linked_list_node * tail;
}; };
// typedefs // typedefs
typedef struct linked_list linked_list_t; typedef struct linked_list linked_list_t;
typedef struct linked_list_node linked_list_node_t; typedef struct linked_list_node linked_list_node_t;
// functions // functions
#define _list_t linked_list_t #define _list_t linked_list_t
#define _data_t LINKED_LIST_DATA_TYPE #define _data_t LINKED_LIST_DATA_TYPE
// TODO _list_t * linked_list_new (void);
_list_t * linked_list_new (void); _list_t * linked_list_insert (_list_t * list, int index, _data_t data);
_list_t * linked_list_add_head (_list_t * list, _data_t data); _data_t linked_list_peek (_list_t * list, int index);
_list_t * linked_list_add_tail (_list_t * list, _data_t data); _data_t linked_list_pop (_list_t * list, int index);
_data_t linked_list_pop_head (_list_t * list); _list_t * linked_list_copy (_list_t * list);
_data_t linked_list_pop_tail (_list_t * list); void linked_list_free (_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
#undef _list_t #undef _list_t
#undef _data_t #undef _data_t

29
src/lib/data-types/list.h Normal file
View File

@ -0,0 +1,29 @@
/* ----------------------------------------------------------------------------
* list : exports
* ----------------------------------------------------------------------------
* Copyright (c) 2012 Ben Blazak <benblazak.dev@gmail.com>
* Released under The MIT License (MIT) (see "license.md")
* Project located at <https://github.com/benblazak/ergodox-firmware>
* ------------------------------------------------------------------------- */
#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

View File

@ -0,0 +1,29 @@
/* ----------------------------------------------------------------------------
* queue : exports
* ----------------------------------------------------------------------------
* Copyright (c) 2012 Ben Blazak <benblazak.dev@gmail.com>
* Released under The MIT License (MIT) (see "license.md")
* Project located at <https://github.com/benblazak/ergodox-firmware>
* ------------------------------------------------------------------------- */
#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

View File

@ -0,0 +1,29 @@
/* ----------------------------------------------------------------------------
* stack : exports
* ----------------------------------------------------------------------------
* Copyright (c) 2012 Ben Blazak <benblazak.dev@gmail.com>
* Released under The MIT License (MIT) (see "license.md")
* Project located at <https://github.com/benblazak/ergodox-firmware>
* ------------------------------------------------------------------------- */
#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

View File

@ -13,7 +13,7 @@
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include "lib-other/pjrc/usb_keyboard/usb_keyboard.h" #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 "lib/usb/usage-page/keyboard.h"
#include "keyboard.h" #include "keyboard.h"

View File

@ -10,7 +10,7 @@
#ifndef KEY_FUNCTIONS_h #ifndef KEY_FUNCTIONS_h
#define KEY_FUNCTIONS_h #define KEY_FUNCTIONS_h
#include "lib/data-types.h" #include "lib/data-types/common.h"
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// include the appropriate 'matrix.h' // include the appropriate 'matrix.h'

View File

@ -36,7 +36,7 @@
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#include "lib/data-types.h" #include "lib/data-types/common.h"
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -10,7 +10,7 @@
#include <util/delay.h> #include <util/delay.h>
#include "lib-other/pjrc/usb_keyboard/usb_keyboard.h" #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 "lib/key-functions.h"
#include "keyboard.h" #include "keyboard.h"