test

Dependencies:   Nanopb iSerial mbed BaseJpegDecode FatFileSystem SDFileSystem RingBuffer Camera_LS_Y201

Committer:
cgraham
Date:
Thu Sep 18 15:21:47 2014 +0000
Revision:
0:d69efd0ee139
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cgraham 0:d69efd0ee139 1 #include "BitPattern.h"
cgraham 0:d69efd0ee139 2
cgraham 0:d69efd0ee139 3 BitPattern::BitPattern()
cgraham 0:d69efd0ee139 4 {
cgraham 0:d69efd0ee139 5 clear();
cgraham 0:d69efd0ee139 6 }
cgraham 0:d69efd0ee139 7
cgraham 0:d69efd0ee139 8 BitPattern::BitPattern(uint32_t value, int size)
cgraham 0:d69efd0ee139 9 {
cgraham 0:d69efd0ee139 10 clear();
cgraham 0:d69efd0ee139 11 put(value, size);
cgraham 0:d69efd0ee139 12 }
cgraham 0:d69efd0ee139 13
cgraham 0:d69efd0ee139 14 BitPattern::BitPattern(const char* s)
cgraham 0:d69efd0ee139 15 {
cgraham 0:d69efd0ee139 16 clear();
cgraham 0:d69efd0ee139 17 operator+=(s);
cgraham 0:d69efd0ee139 18 }
cgraham 0:d69efd0ee139 19
cgraham 0:d69efd0ee139 20 void BitPattern::clear()
cgraham 0:d69efd0ee139 21 {
cgraham 0:d69efd0ee139 22 m_size = 0;
cgraham 0:d69efd0ee139 23 m_pat = 0;
cgraham 0:d69efd0ee139 24 }
cgraham 0:d69efd0ee139 25
cgraham 0:d69efd0ee139 26 void BitPattern::operator=(const char* s)
cgraham 0:d69efd0ee139 27 {
cgraham 0:d69efd0ee139 28 clear();
cgraham 0:d69efd0ee139 29 operator+=(s);
cgraham 0:d69efd0ee139 30 }
cgraham 0:d69efd0ee139 31
cgraham 0:d69efd0ee139 32 void BitPattern::operator+=(uint8_t c)
cgraham 0:d69efd0ee139 33 {
cgraham 0:d69efd0ee139 34 put(c);
cgraham 0:d69efd0ee139 35 }
cgraham 0:d69efd0ee139 36
cgraham 0:d69efd0ee139 37 void BitPattern::operator+=(const char* s)
cgraham 0:d69efd0ee139 38 {
cgraham 0:d69efd0ee139 39 for(int i = 0; i < 32 && s[i]; i++) {
cgraham 0:d69efd0ee139 40 char c = s[i];
cgraham 0:d69efd0ee139 41 put(c-'0', 1);
cgraham 0:d69efd0ee139 42 }
cgraham 0:d69efd0ee139 43 }
cgraham 0:d69efd0ee139 44
cgraham 0:d69efd0ee139 45 int BitPattern::operator [](int index)
cgraham 0:d69efd0ee139 46 {
cgraham 0:d69efd0ee139 47 uint32_t mask = 0x80000000;
cgraham 0:d69efd0ee139 48 for(int i = 0; i < index; i++) {
cgraham 0:d69efd0ee139 49 mask>>=1;
cgraham 0:d69efd0ee139 50 }
cgraham 0:d69efd0ee139 51 if (m_pat & mask) {
cgraham 0:d69efd0ee139 52 return 1;
cgraham 0:d69efd0ee139 53 }
cgraham 0:d69efd0ee139 54 return 0;
cgraham 0:d69efd0ee139 55 }
cgraham 0:d69efd0ee139 56
cgraham 0:d69efd0ee139 57 void BitPattern::put(uint32_t value, int size)
cgraham 0:d69efd0ee139 58 {
cgraham 0:d69efd0ee139 59 m_pat <<= size;
cgraham 0:d69efd0ee139 60 m_pat |= value;
cgraham 0:d69efd0ee139 61 m_size += size;
cgraham 0:d69efd0ee139 62 }
cgraham 0:d69efd0ee139 63
cgraham 0:d69efd0ee139 64 uint32_t BitPattern::peek(int size)
cgraham 0:d69efd0ee139 65 {
cgraham 0:d69efd0ee139 66 return m_pat >> (m_size - size);
cgraham 0:d69efd0ee139 67 }
cgraham 0:d69efd0ee139 68
cgraham 0:d69efd0ee139 69 uint32_t BitPattern::get(int size)
cgraham 0:d69efd0ee139 70 {
cgraham 0:d69efd0ee139 71 uint32_t r = peek(size);
cgraham 0:d69efd0ee139 72 m_size -= size;
cgraham 0:d69efd0ee139 73 m_pat &= (1<<m_size)-1;
cgraham 0:d69efd0ee139 74 return r;
cgraham 0:d69efd0ee139 75 }
cgraham 0:d69efd0ee139 76
cgraham 0:d69efd0ee139 77 int BitPattern::size()
cgraham 0:d69efd0ee139 78 {
cgraham 0:d69efd0ee139 79 return m_size;
cgraham 0:d69efd0ee139 80 }
cgraham 0:d69efd0ee139 81
cgraham 0:d69efd0ee139 82 bool BitPattern::match(BitPattern& b)
cgraham 0:d69efd0ee139 83 {
cgraham 0:d69efd0ee139 84 if (b.m_size > m_size) {
cgraham 0:d69efd0ee139 85 return false;
cgraham 0:d69efd0ee139 86 }
cgraham 0:d69efd0ee139 87 return peek(b.size()) == b.peek(b.size());
cgraham 0:d69efd0ee139 88 }
cgraham 0:d69efd0ee139 89
cgraham 0:d69efd0ee139 90 bool BitPattern::match(const char* s)
cgraham 0:d69efd0ee139 91 {
cgraham 0:d69efd0ee139 92 BitPattern a = s;
cgraham 0:d69efd0ee139 93 return match(a);
cgraham 0:d69efd0ee139 94 }
cgraham 0:d69efd0ee139 95
cgraham 0:d69efd0ee139 96 bool BitPattern::operator ==(BitPattern& b)
cgraham 0:d69efd0ee139 97 {
cgraham 0:d69efd0ee139 98 if (b.m_size == m_size) {
cgraham 0:d69efd0ee139 99 if (b.m_pat == m_pat) {
cgraham 0:d69efd0ee139 100 return true;
cgraham 0:d69efd0ee139 101 }
cgraham 0:d69efd0ee139 102 }
cgraham 0:d69efd0ee139 103 return false;
cgraham 0:d69efd0ee139 104 }