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

bblanchon / ArduinoStreamUtils / 4332192079

pending completion
4332192079

push

github

Benoit Blanchon
Fix "__FlashStringHelper is ambiguous" with arduino-pico

840 of 853 relevant lines covered (98.48%)

1269.0 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-2023
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())
235✔
26
      : _allocator(allocator) {}
235✔
27

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

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

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

54
    if (bufferSize > sizeAllowedOnStack)
60✔
55
      _allocator.deallocate(buffer);
10✔
56

57
    return n / 2;
60✔
58
  }
59

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

64
    if (!flushRemainder(target))
225✔
65
      return 0;
40✔
66

67
    if (!target.write(first))
185✔
68
      return 0;
50✔
69

70
    if (!target.write(second))
135✔
71
      _remainder = second;
75✔
72

73
    return 1;
135✔
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) {
235✔
89
    flushRemainder(target);
235✔
90
  }
235✔
91

92
 private:
93
  // Encode 4-bits to 7-bits using Hamming(7,4)
94
  uint8_t encode(uint8_t input) {
1,610✔
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,610✔
98
  }
99

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

105
    if (!target.write(_remainder))
145✔
106
      return false;
110✔
107

108
    _remainder = -1;
35✔
109
    return true;
35✔
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