/* ring_buffer.c * * Copyright (c) 2002, 2003 Bruce Kroeze * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * --------------------------------- * * basic ring buffer for PICs * * Revisions: * 25 Dec 2002 - Initial Version */ #ifndef MAX_RING_SIZE #define MAX_RING_SIZE 16 #endif #ifndef RING_TYPE #define RING_TYPE char #endif RING_TYPE ring[MAX_RING_SIZE]; int ring_read_ix; int ring_write_ix; void ring_clear() { int i; for (i=0; i < MAX_RING_SIZE; ++i) ring[i] = 0; ring_read_ix = 0; ring_write_ix = 0; } void ring_append(RING_TYPE ch) { ring[ring_write_ix] = ch; ring_write_ix++; if (ring_write_ix == MAX_RING_SIZE) ring_write_ix = 0; } int ring_offset(ix, offset) { int val; val = ix + offset; if (val >= MAX_RING_SIZE) { val -= MAX_RING_SIZE; } return val; } RING_TYPE ring_peek(int offset) { int ix; ix = ring_offset(ring_read_ix, offset); return ring[ix]; } int inc_ring_read_ix() { if (ring_read_ix != ring_write_ix) { ++ring_read_ix; if (ring_read_ix == MAX_RING_SIZE) ring_read_ix = 0; return 1; } else { return 0; } } RING_TYPE ring_pop() { RING_TYPE ret; ret = ring[ring_read_ix]; ring[ring_read_ix] = 0; inc_ring_read_ix(); return ret; } int ring_read(RING_TYPE *buf, int count) { int i, j; for (i=0; i < count; ++i) { buf[i] = ring[ring_read_ix]; if (!inc_ring_read_ix()) break; } while (i < count) { buf[i] = 0; ++i; } } #ifdef RINGTEST int get_raw_ring(RING_TYPE *buf) { int ix; for (ix=0; ix