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

jandelgado / jled / 11768797831

10 Nov 2024 09:54PM UTC coverage: 98.403%. First build
11768797831

Pull #131

github

web-flow
Merge fdfcb84a6 into 41e527645
Pull Request #131: optionally return last brightness value from Update()

26 of 27 new or added lines in 2 files covered. (96.3%)

308 of 313 relevant lines covered (98.4%)

859.43 hits per line

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

100.0
/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
30
// to
31
// approximate the original function (so we do not need fp-ops).
32
// fade-off and breath functions are all derived from fade-on, see
33
// below.
34
static constexpr uint8_t kFadeOnTable[] = {0,   3,   13,  33, 68,
35
                                           118, 179, 232, 255}; // NOLINT
36

37
// https://www.wolframalpha.com/input/?i=plot+(exp(sin((x-100%2F2.)*PI%2F100))-0.36787944)*108.0++x%3D0+to+100
38
// The fade-on func is an approximation of
39
//   y(x) = exp(sin((t-period/2.) * PI / period)) - 0.36787944) * 108.)
40
uint8_t fadeon_func(uint32_t t, uint16_t period) {
44✔
41
    if (t + 1 >= period) return 255;  // kFullBrightness;
44✔
42

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

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

55
static uint32_t rand_ = 0;
56

57
void rand_seed(uint32_t seed) { rand_ = seed; }
4✔
58

59
uint8_t rand8() {
12✔
60
    if (rand_ & 1) {
12✔
61
        rand_ = (rand_ >> 1);
4✔
62
    } else {
63
        rand_ = (rand_ >> 1) ^ 0x7FFFF159;
8✔
64
    }
65

66
    return (uint8_t)rand_;
12✔
67
}
68

69
// scale a byte (val) by a byte (factor). scale8 has the following properties:
70
//   scale8(0, f) == 0 for all f
71
//   scale8(x, 255) == x for all x
72
uint8_t scale8(uint8_t val, uint8_t factor) {
76✔
73
    return (static_cast<uint16_t>(val)*static_cast<uint16_t>(1 + factor)) >> 8;
76✔
74
}
75

76
// interpolate a byte (val) to the interval [a,b].
77
uint8_t lerp8by8(uint8_t val, uint8_t a, uint8_t b) {
8,324✔
78
    if (a == 0 && b == 255) return val;  // optimize for most common case
8,324✔
79
    uint8_t delta = b - a;
44✔
80
    return a + scale8(val, delta);
44✔
81
}
82

83
};  // 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