wefwe
Dependencies: mbed C12832 DogM163 FatFileSystem
_bitio.cpp@28:19aac2daf669, 2019-05-08 (annotated)
- Committer:
- JostBaus
- Date:
- Wed May 08 13:48:54 2019 +0000
- Revision:
- 28:19aac2daf669
Wavspiller using RiceGulumb
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JostBaus | 28:19aac2daf669 | 1 | #include "mbed.h" |
JostBaus | 28:19aac2daf669 | 2 | #include "string" |
JostBaus | 28:19aac2daf669 | 3 | #include "_bitio.h" |
JostBaus | 28:19aac2daf669 | 4 | |
JostBaus | 28:19aac2daf669 | 5 | |
JostBaus | 28:19aac2daf669 | 6 | FILE *infp, *outfp; |
JostBaus | 28:19aac2daf669 | 7 | int getcount, putcount; |
JostBaus | 28:19aac2daf669 | 8 | Uint bitbuf; |
JostBaus | 28:19aac2daf669 | 9 | |
JostBaus | 28:19aac2daf669 | 10 | void init_bit_o(void) |
JostBaus | 28:19aac2daf669 | 11 | { |
JostBaus | 28:19aac2daf669 | 12 | putcount = 8; bitbuf = 0; |
JostBaus | 28:19aac2daf669 | 13 | } |
JostBaus | 28:19aac2daf669 | 14 | |
JostBaus | 28:19aac2daf669 | 15 | void init_bit_i(void) |
JostBaus | 28:19aac2daf669 | 16 | { |
JostBaus | 28:19aac2daf669 | 17 | getcount = 0; bitbuf = 0; |
JostBaus | 28:19aac2daf669 | 18 | } |
JostBaus | 28:19aac2daf669 | 19 | |
JostBaus | 28:19aac2daf669 | 20 | |
JostBaus | 28:19aac2daf669 | 21 | Uint getbit(void) |
JostBaus | 28:19aac2daf669 | 22 | { |
JostBaus | 28:19aac2daf669 | 23 | int flag; |
JostBaus | 28:19aac2daf669 | 24 | if (--getcount >= 0) return (bitbuf >> getcount) & 1U; |
JostBaus | 28:19aac2daf669 | 25 | getcount = 7; |
JostBaus | 28:19aac2daf669 | 26 | if((flag = fgetc(infp))==EOF)return OVERRUN; |
JostBaus | 28:19aac2daf669 | 27 | else bitbuf = flag; |
JostBaus | 28:19aac2daf669 | 28 | return (bitbuf >> 7) & 1U; |
JostBaus | 28:19aac2daf669 | 29 | } |
JostBaus | 28:19aac2daf669 | 30 | |
JostBaus | 28:19aac2daf669 | 31 | Uint getbits(int n) |
JostBaus | 28:19aac2daf669 | 32 | { |
JostBaus | 28:19aac2daf669 | 33 | Uint x = 0; |
JostBaus | 28:19aac2daf669 | 34 | if (n < 1 || 25 < n) return 0; |
JostBaus | 28:19aac2daf669 | 35 | while (n > getcount) { |
JostBaus | 28:19aac2daf669 | 36 | n -= getcount; |
JostBaus | 28:19aac2daf669 | 37 | x |= rightbits(getcount, bitbuf) << n; |
JostBaus | 28:19aac2daf669 | 38 | bitbuf = fgetc(infp); getcount = 8; |
JostBaus | 28:19aac2daf669 | 39 | } |
JostBaus | 28:19aac2daf669 | 40 | getcount -= n; |
JostBaus | 28:19aac2daf669 | 41 | return x | rightbits(n, bitbuf >> getcount); |
JostBaus | 28:19aac2daf669 | 42 | } |
JostBaus | 28:19aac2daf669 | 43 | |
JostBaus | 28:19aac2daf669 | 44 | |
JostBaus | 28:19aac2daf669 | 45 | void putbit(Uint bit) |
JostBaus | 28:19aac2daf669 | 46 | { |
JostBaus | 28:19aac2daf669 | 47 | putcount--; |
JostBaus | 28:19aac2daf669 | 48 | if (bit != 0) bitbuf |= (1 << putcount); |
JostBaus | 28:19aac2daf669 | 49 | if (putcount == 0) { |
JostBaus | 28:19aac2daf669 | 50 | if (fputc(bitbuf, outfp) == EOF) error("can't write\r\n"); |
JostBaus | 28:19aac2daf669 | 51 | bitbuf = 0; putcount = 8; |
JostBaus | 28:19aac2daf669 | 52 | } |
JostBaus | 28:19aac2daf669 | 53 | } |
JostBaus | 28:19aac2daf669 | 54 | |
JostBaus | 28:19aac2daf669 | 55 | |
JostBaus | 28:19aac2daf669 | 56 | void putbits(int n, Uint x) |
JostBaus | 28:19aac2daf669 | 57 | { |
JostBaus | 28:19aac2daf669 | 58 | if (n < 1 || 25 < n) return; |
JostBaus | 28:19aac2daf669 | 59 | while (n >= putcount) { |
JostBaus | 28:19aac2daf669 | 60 | n -= putcount; |
JostBaus | 28:19aac2daf669 | 61 | bitbuf |= rightbits(putcount, x >> n); |
JostBaus | 28:19aac2daf669 | 62 | if (fputc(bitbuf, outfp) == EOF) error("cant write\r\n"); |
JostBaus | 28:19aac2daf669 | 63 | bitbuf = 0U; putcount = 8; |
JostBaus | 28:19aac2daf669 | 64 | } |
JostBaus | 28:19aac2daf669 | 65 | putcount -= n; |
JostBaus | 28:19aac2daf669 | 66 | bitbuf |= rightbits(n, x) << putcount; |
JostBaus | 28:19aac2daf669 | 67 | } |