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

bblanchon / ArduinoStreamUtils / 9872110687

10 Jul 2024 09:58AM UTC 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

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

5
#pragma once
6

7
#include <Arduino.h>
8

9
#include "../Buffers/LinearBuffer.hpp"
10
#include "../Configuration.hpp"
11
#include "../Polyfills.hpp"
12

13
namespace StreamUtils {
14

15
struct WriteWaitingPolicy {
16
 public:
17
  WriteWaitingPolicy(Polyfills::function wait)
252✔
18
      : _wait(Polyfills::move(wait)), _timeout(1000) {}
252✔
19

20
  size_t write(Print &target, const uint8_t *data, size_t size) {
144✔
21
    unsigned long startTime = millis();
144✔
22
    size_t totalWritten = 0;
144✔
23

24
    for (;;) {
25
      size_t n = target.write(data, size);
252✔
26
      size -= n;
252✔
27
      data += n;
252✔
28
      totalWritten += n;
252✔
29
      if (size == 0 || millis() - startTime >= _timeout)
252✔
30
        return totalWritten;
144✔
31
      _wait();
108✔
32
    }
108✔
33
  }
34

35
  size_t write(Print &target, uint8_t data) {
540✔
36
    unsigned long startTime = millis();
540✔
37

38
    for (;;) {
39
      if (target.write(data))
576✔
40
        return 1;
504✔
41
      if (millis() - startTime >= _timeout)
72✔
42
        return 0;
36✔
43
      _wait();
36✔
44
    }
45
  }
46

47
  template <typename TTarget>
48
  void flush(TTarget &target) {
×
49
    target.flush();
×
50
  }
×
51

52
  void implicitFlush(Print &) {}
252✔
53

54
  void setTimeout(unsigned long timeout) {
72✔
55
    _timeout = timeout;
72✔
56
  }
72✔
57

58
 private:
59
  Polyfills::function _wait;
60
  unsigned long _timeout;
61
};
62

63
}  // 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