RGBLED  1.0.0
Easily control RGB LEDs.
RGBLED.hpp
Go to the documentation of this file.
1 /*
2 The MIT License (MIT)
3 
4 Copyright (c) 2016 Vasil Kalchev
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
35 #pragma once
36 
37 #include <inttypes.h>
38 #include <math.h>
39 #include <Arduino.h>
40 
42 enum class CommonElectrode : bool {
43  anode = 1, cathode = 0,
44 };
45 
47 enum State : bool {
48  ON = 1, OFF = 0,
49 };
50 
52 const uint8_t PWM_MAX = 255;
53 
54 
55 class RGBLED {
56 public:
58 
64  RGBLED(uint8_t red_pin, uint8_t green_pin, uint8_t blue_pin, CommonElectrode commonElectrode = CommonElectrode::cathode);
65 
67  void on();
68 
70  void off();
71 
73 
78  State state() const;
79 
81 
84  uint8_t brightness() const;
85 
87 
90  void brightness(uint8_t brightness);
92 
97  void operator()(uint8_t brightness);
98 
100 
103  uint8_t red() const;
105 
108  void red(uint8_t redValue);
110 
113  uint8_t green() const;
115 
118  void green(uint8_t greenValue);
120 
123  uint8_t blue() const;
125 
128  void blue(uint8_t blueValue);
130 
135  void operator()(uint8_t redValue, uint8_t greenValue, uint8_t blueValue);
136 
138 
141  uint8_t red_correction() const;
143 
146  void red_correction(uint8_t red_correction);
148 
151  uint8_t green_correction() const;
153 
156  void green_correction(uint8_t green_correction);
158 
161  uint8_t blue_correction() const;
163 
166  void blue_correction(uint8_t blue_correction);
167 
169 
172  void show(bool force = false) const;
173 
174 private:
175  void recalculate_total_correction();
176  struct Channel {
177  explicit Channel(uint8_t pin) : pin(pin), value(0), value_last(0), correction(1), total_correction(1) {}
178  const uint8_t pin;
179  uint8_t value;
180  uint8_t value_last;
181  float correction;
182  float total_correction;
183  };
184  Channel _red;
185  Channel _green;
186  Channel _blue;
187  CommonElectrode _commonElectrode;
188  float _brightness;
189  State _state;
190 };
State
State enum.
Definition: RGBLED.hpp:47
Definition: RGBLED.hpp:55
const uint8_t PWM_MAX
The maximum value of the PWM.
Definition: RGBLED.hpp:52
CommonElectrode
Common electrode enum.
Definition: RGBLED.hpp:42