-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapa102.cpp
77 lines (65 loc) · 2.04 KB
/
apa102.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* File: apa102.cpp */
/* Date: Wed 25 Mar 2015 10:03:22 PM CET */
/* Author: Fabian Wermelinger */
/* Tag: Implements APA102 LED */
/* Copyright 2015 Fabian Wermelinger. All Rights Reserved. */
#include <cstdio>
#include "apa102.h"
using namespace std;
APA102::APA102(const unsigned int n) : n_led(n),
nbits_start(32), nbits_end(32*(n/65 + 1)),
rawdata((nbits_start/8 + n*sizeof(LED) + nbits_end/8), 0xff),
leds((LED*)(&rawdata[0] + nbits_start/8))
{
for (unsigned int i=0; i<nbits_start/8; ++i)
rawdata[i] = 0;
}
APA102::~APA102()
{
}
// LED implementation
APA102::LED::LED() : brightness(0xff), B(0xff), G(0xff), R(0xff) {}
APA102::LED::LED(const unsigned int bright, const RAWTYPE red, const RAWTYPE green, const RAWTYPE blue)
: brightness(0xe0), B(blue), G(green), R(red)
{
// bright in percent
if (bright > 100) brightness = 0xff;
else set_brightness(bright);
}
APA102::LED::LED(const RAWTYPE red, const RAWTYPE green, const RAWTYPE blue)
: brightness(0xff), B(blue), G(green), R(red)
{
}
APA102::LED::LED(const unsigned int bright, const unsigned int color)
: brightness(0xe0), B(color), G(color>>8), R(color>>16)
{
// bright in percent
if (bright > 100) brightness = 0xff;
else set_brightness(bright);
}
APA102::LED::LED(const unsigned int color)
: brightness(0xff), B(color), G(color>>8), R(color>>16)
{
}
void APA102::LED::increase_brightness()
{
RAWTYPE cur_bright = brightness & 0x1f;
if (cur_bright < 31) ++cur_bright;
brightness = 0xe0 + cur_bright;
}
void APA102::LED::decrease_brightness()
{
RAWTYPE cur_bright = brightness & 0x1f;
if (cur_bright > 0) --cur_bright;
brightness = 0xe0 + cur_bright;
}
void APA102::LED::state() const
{
printf("Brightness: %.1f%% ", static_cast<float>(brightness & 0x1f)/31.0f*100.0f);
printf("(Level %i)\n", brightness & 0x1f);
printf("Red: %i\n", R);
printf("Green: %i\n", G);
printf("Blue: %i\n", B);
printf("Hex: 0x%.6X\n", (R<<16) + (G<<8) + B);
printf("Address: %p\n", this);
}