• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bblanchon / ArduinoStreamUtils / 9872110687

10 Jul 2024 09:58AM CUT coverage: 98.468%. Remained the same
9872110687

push

github

bblanchon
Set version to 1.9.0

964 of 979 relevant lines covered (98.47%)

2069.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.31
/src/StreamUtils/Policies/HammingEncodingPolicy.hpp
1
// StreamUtils - github.com/bblanchon/ArduinoStreamUtils
2
// Copyright Benoit Blanchon 2019-2024
3
// MIT License
4

5
#pragma once
6

7
#if defined(WIN32) || defined(__WIN32) || defined(__WIN32__)
8
#include <malloc.h>
9
#else
10
#include <alloca.h>
11
#endif
12

13
#include "../Configuration.hpp"
14

15
namespace StreamUtils {
16

17
template <int N, int K, typename TAllocator>
18
class HammingEncodingPolicy;
19

20
template <typename TAllocator>
21
class HammingEncodingPolicy<7, 4, TAllocator> {
22
  const static size_t sizeAllowedOnStack = STREAMUTILS_STACK_BUFFER_MAX_SIZE;
23

24
 public:
25
  HammingEncodingPolicy(TAllocator allocator = TAllocator())
281✔
26
      : _allocator(allocator) {}
281✔
27

28
  size_t write(Print &target, const uint8_t *data, size_t size) {
84✔
29
    if (!flushRemainder(target))
84✔
30
      return 0;
12✔
31

32
    size_t bufferSize = size * 2;
72✔
33
    uint8_t *buffer;
34
    if (bufferSize > sizeAllowedOnStack) {
72✔
35
      buffer = static_cast<uint8_t *>(_allocator.allocate(bufferSize));
24✔
36
      if (!buffer) {
24✔
37
        bufferSize = sizeAllowedOnStack;
12✔
38
        buffer = static_cast<uint8_t *>(alloca(bufferSize));
12✔
39
      }
40
    } else {
41
      buffer = static_cast<uint8_t *>(alloca(bufferSize));
48✔
42
    }
43

44
    for (size_t i = 0, j = 0; j < bufferSize; i++) {
768✔
45
      buffer[j++] = encode(data[i] >> 4);
696✔
46
      buffer[j++] = encode(data[i] & 0x0f);
696✔
47
    }
48
    size_t n = target.write(buffer, bufferSize);
72✔
49
    if (n & 1) {
72✔
50
      _remainder = buffer[n];
24✔
51
      ++n;
24✔
52
    }
53

54
    if (bufferSize > sizeAllowedOnStack)
72✔
55
      _allocator.deallocate(buffer);
12✔
56

57
    return n / 2;
72✔
58
  }
59

60
  size_t write(Print &target, uint8_t data) {
269✔
61
    uint8_t first = encode(data >> 4);
269✔
62
    uint8_t second = encode(data & 0x0f);
269✔
63

64
    if (!flushRemainder(target))
269✔
65
      return 0;
48✔
66

67
    if (!target.write(first))
221✔
68
      return 0;
60✔
69

70
    if (!target.write(second))
161✔
71
      _remainder = second;
89✔
72

73
    return 1;
161✔
74
  }
75

76
  void flush(Stream &target) {
×
77
    flushRemainder(target);
×
78
    target.flush();
×
79
  }
×
80

81
  void flush(Print &target) {
5✔
82
    flushRemainder(target);
5✔
83
#if STREAMUTILS_PRINT_FLUSH_EXISTS
84
    target.flush();
5✔
85
#endif
86
  }
5✔
87

88
  void implicitFlush(Print &target) {
281✔
89
    flushRemainder(target);
281✔
90
  }
281✔
91

92
 private:
93
  // Encode 4-bits to 7-bits using Hamming(7,4)
94
  uint8_t encode(uint8_t input) {
1,930✔
95
    static uint8_t table[] = {0x00, 0x71, 0x62, 0x13, 0x54, 0x25, 0x36, 0x47,
96
                              0x38, 0x49, 0x5A, 0x2B, 0x6C, 0x1D, 0x0E, 0x7F};
97
    return table[input];
1,930✔
98
  }
99

100
  template <typename TTarget>
101
  bool flushRemainder(TTarget &target) {
639✔
102
    if (_remainder < 0)
639✔
103
      return true;
466✔
104

105
    if (!target.write(_remainder))
173✔
106
      return false;
132✔
107

108
    _remainder = -1;
41✔
109
    return true;
41✔
110
  }
111

112
  TAllocator _allocator;
113
  int8_t _remainder = -1;
114
};
115

116
}  // namespace StreamUtils
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc