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

jandelgado / jled / 22038646038

15 Feb 2026 03:57PM UTC coverage: 96.857% (-1.6%) from 98.457%
22038646038

Pull #142

github

web-flow
Merge a5b6f7862 into e5e6257c3
Pull Request #142: Add support for 16-bit PWM resolution

105 of 114 new or added lines in 6 files covered. (92.11%)

2 existing lines in 1 file now uncovered.

339 of 350 relevant lines covered (96.86%)

716.44 hits per line

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

77.78
/src/jled_base.cpp
1
// Copyright (c) 2017 Jan Delgado <jdelgado[at]gmx.net>
2
// https://github.com/jandelgado/jled
3
//
4
// Permission is hereby granted, free of charge, to any person obtaining a copy
5
// of this software and associated documentation files (the "Software"), to
6
// deal in the Software without restriction, including without limitation the
7
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
// sell copies of the Software, and to permit persons to whom the Software is
9
// furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
// IN THE SOFTWARE.
21
//
22
#include "jled_base.h"  // NOLINT
23

24
namespace jled {
25

26
// pre-calculated fade-on function. This table samples the function
27
//   y(x) =  exp(sin((t - period / 2.) * PI / period)) - 0.36787944)
28
//   * 108.
29
// at x={0,32,...,256}. In FadeOnFunc() we us linear interpolation to
30
// approximate the original function (so we do not need fp-ops).
31
// fade-off and breath functions are all derived from fade-on, see
32
// below.
33
static constexpr uint8_t kFadeOnTable[] = {0,   3,   13,  33, 68,
34
                                           118, 179, 232, 255}; // NOLINT
35

36
// https://www.wolframalpha.com/input/?i=plot+(exp(sin((x-100%2F2.)*PI%2F100))-0.36787944)*108.0++x%3D0+to+100
37
// The fade-on func is an approximation of
38
//   y(x) = exp(sin((t-period/2.) * PI / period)) - 0.36787944) * 108.)
39

40
// 8-bit specialization: uses pre-computed 8-bit table
41
template<>
42
uint8_t fadeon_func<uint8_t>(uint32_t t, uint16_t period) {
44✔
43
    if (t + 1 >= period) return 255;
44✔
44

45
    // approximate by linear interpolation.
46
    // scale t according to period to 0..255
47
    t = ((t << 8) / period) & 0xff;
32✔
48
    const auto i = (t >> 5);  // -> i will be in range 0 .. 7
32✔
49
    const auto y0 = kFadeOnTable[i];
32✔
50
    const auto y1 = kFadeOnTable[i + 1];
32✔
51
    const auto x0 = i << 5;  // *32
32✔
52

53
    // y(t) = mt+b, with m = dy/dx = (y1-y0)/32 = (y1-y0) >> 5
54
    return (((t - x0) * (y1 - y0)) >> 5) + y0;
32✔
55
}
56

57
// 16-bit specialization: interpolate from 8-bit table, then scale to 16-bit
58
template<>
NEW
59
uint16_t fadeon_func<uint16_t>(uint32_t t, uint16_t period) {
×
NEW
60
    if (t + 1 >= period) return 65535;
×
61

62
    // Get 8-bit value from table
NEW
63
    const uint8_t val8 = fadeon_func<uint8_t>(t, period);
×
64

65
    // Scale 8-bit [0,255] to 16-bit [0,65535]
66
    // Use formula: val16 = (val8 * 65535) / 255 = (val8 * 257)
67
    // This is exact: 255 * 257 = 65535
NEW
68
    return static_cast<uint16_t>(val8) * 257;
×
69
}
70

71
static uint32_t rand_ = 0;
72

73
void rand_seed(uint32_t seed) { rand_ = seed; }
4✔
74

75
uint8_t rand8() {
12✔
76
    if (rand_ & 1) {
12✔
77
        rand_ = (rand_ >> 1);
4✔
78
    } else {
79
        rand_ = (rand_ >> 1) ^ 0x7FFFF159;
8✔
80
    }
81

82
    return (uint8_t)rand_;
12✔
83
}
84

85
// Legacy 8-bit functions - these are now implemented as inline template wrappers
86
// in jled_base.h, but we keep the non-inline versions here for backwards
87
// compatibility with older code that may link against them.
88

89
};  // namespace jled
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

© 2026 Coveralls, Inc