-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitbuf.c
131 lines (115 loc) · 3.02 KB
/
bitbuf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*! \file bitbuf.c \brief Multipurpose bit buffer structure and methods. */
//*****************************************************************************
//
// File Name : 'bitbuf.c'
// Title : Multipurpose bit buffer structure and methods
// Author : Pascal Stang - Copyright (C) 2001-2002
// Created : 7/10/2002
// Revised : 7/10/2002
// Version : 0.5
// Target MCU : any
// Editor Tabs : 4
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************
#include "bitbuf.h"
// global variables
//! Initialize the bit buffer
// sets the start location and size of the buffer in memory
void bitbufInit(BitBuf* bitBuffer, unsigned char *start, unsigned short bytesize)
{
// set start pointer of the buffer
bitBuffer->dataptr = start;
bitBuffer->size = bytesize;
// initialize indexing and length
bitBuffer->dataindex = 0;
bitbufFlush(bitBuffer);
}
// access routines
//! Get a bit from the current position in the buffer
// returns the bit at the current position in the buffer
// and increments the bit position
unsigned char bitbufGet(BitBuf* bitBuffer)
{
unsigned char byte;
unsigned char bit;
// get current working byte
byte = bitBuffer->dataptr[bitBuffer->bytePos];
// read data bit
bit = (byte & (1<<bitBuffer->bitPos))?(1):(0);
// increment bit counter
if(bitBuffer->bitPos < 7)
{
bitBuffer->bitPos++;
}
else
{
// increment byte counter
bitBuffer->bitPos = 0;
bitBuffer->bytePos++;
}
// return bit value
return bit;
}
//! Get a bit from a given index into the buffer
// returns the bit at position [bitIndex] in the buffer
unsigned char bitbufGetAtIndex(BitBuf* bitBuffer, unsigned short bitIndex)
{
// return bit at index in buffer
return (bitBuffer->dataptr[bitIndex>>3] & (1<<(bitIndex & 0x07)))?(1):(0);
}
//! Store a bit at the current position in the buffer
// stores the bit at the current position in the buffer
// and increments the bit position
void bitbufStore(BitBuf* bitBuffer, unsigned char bit)
{
unsigned char byte;
// get current working byte
byte = bitBuffer->dataptr[bitBuffer->bytePos];
// apply data bit
if(bit)
byte |= (1<<bitBuffer->bitPos);
else
byte &= ~(1<<bitBuffer->bitPos);
// store data
bitBuffer->dataptr[bitBuffer->bytePos] = byte;
bitBuffer->datalength++;
// increment bit counter
if(bitBuffer->bitPos < 7)
{
bitBuffer->bitPos++;
}
else
{
// increment byte counter
bitBuffer->bitPos = 0;
bitBuffer->bytePos++;
}
}
void bitbufReset(BitBuf* bitBuffer)
{
// reset counters
bitBuffer->bytePos = 0;
bitBuffer->bitPos = 0;
}
void bitbufFlush(BitBuf* bitBuffer)
{
// flush contents of the buffer
bitBuffer->datalength = 0;
// reset indexing
bitbufReset(bitBuffer);
}
unsigned short bitbufGetDataLength(BitBuf* bitBuffer)
{
return bitBuffer->datalength;
}
/*
unsigned char bitbufIsNotFull(cBuffer* buffer)
{
// check to see if the buffer has room
// return true if there is room
return (buffer->datalength < buffer->size);
}
*/