Corsair Lighting Protocol  0.15.0
Control LEDs connected to an Arduino with iCUE
LEDController.h
Go to the documentation of this file.
1 /*
2  Copyright 2019 Leon Kiefer
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 #pragma once
17 
23 #include <FastLED.h>
24 
25 #include "Arduino.h"
26 #include "CLPUtils.h"
27 #include "ILEDController.h"
28 
29 #define CHANNEL_NUM 2
30 #define GROUPS_NUM 6
31 
37 enum class ChannelMode : byte {
39  Disabled = 0x00,
42  HardwarePlayback = 0x01,
45  SoftwarePlayback = 0x02
46 };
47 
48 bool inline isValidChannelMode(const ChannelMode channelMode) {
49  return channelMode == ChannelMode::Disabled || channelMode == ChannelMode::HardwarePlayback ||
50  channelMode == ChannelMode::SoftwarePlayback;
51 }
52 
57 enum class PortType : byte {
59  WS2812B = 0x01,
61  UCS1903 = 0x02
62 };
63 
64 bool inline isValidPortType(const PortType portType) {
65  return portType == PortType::WS2812B || portType == PortType::UCS1903;
66 }
67 
68 // LED group mode
69 #define GROUP_MODE_Rainbow_Wave 0x00
70 #define GROUP_MODE_Color_Shift 0x01
71 #define GROUP_MODE_Color_Pulse 0x02
72 #define GROUP_MODE_Color_Wave 0x03
73 #define GROUP_MODE_Static 0x04
74 #define GROUP_MODE_Temperature 0x05
75 #define GROUP_MODE_Visor 0x06
76 #define GROUP_MODE_Marquee 0x07
77 #define GROUP_MODE_Blink 0x08
78 #define GROUP_MODE_Sequential 0x09
79 #define GROUP_MODE_Rainbow 0x0A
80 
84 enum class GroupSpeed : byte { High = 0x00, Medium = 0x01, Low = 0x02 };
85 
86 bool inline isValidGroupSpeed(const GroupSpeed groupSpeed) {
87  return groupSpeed == GroupSpeed::High || groupSpeed == GroupSpeed::Medium || groupSpeed == GroupSpeed::Low;
88 }
89 
93 enum class GroupDirection : byte { Backward = 0x00, Forward = 0x01 };
94 
95 bool inline isValidGroupDirection(const GroupDirection groupDirection) {
96  return groupDirection == GroupDirection::Backward || groupDirection == GroupDirection::Forward;
97 }
98 
102 enum class GroupExtra : byte { Alternating = 0x00, Random = 0x01 };
103 
104 bool inline isValidGroupExtra(const GroupExtra groupExtra) {
105  return groupExtra == GroupExtra::Alternating || groupExtra == GroupExtra::Random;
106 }
107 
108 #define GROUP_TEMP_GROUP_EXTERNAL 255
109 
114 struct LEDGroup {
118  uint8_t ledIndex = 0;
122  uint8_t ledCount = 0;
123  byte mode = GROUP_MODE_Rainbow_Wave;
124  GroupSpeed speed = GroupSpeed::High;
125  GroupDirection direction = GroupDirection::Forward;
126  GroupExtra extra = GroupExtra::Alternating;
127  byte tempGroup = GROUP_TEMP_GROUP_EXTERNAL;
128 
129  CRGB color1;
130  CRGB color2;
131  CRGB color3;
132 
133  uint16_t temp1;
134  uint16_t temp2;
135  uint16_t temp3;
136 };
137 
141 struct LEDChannel {
145  uint8_t brightness = 255;
150  uint8_t ledCount = 0;
151  PortType ledPortType = PortType::WS2812B;
152 
153  LEDGroup groups[GROUPS_NUM];
154  uint8_t groupsSet = 0;
155 };
156 
162 public:
163  virtual void handleLEDControl(const Command& command, const CorsairLightingProtocolResponse* response) override;
171  virtual bool isValidLEDChannel(const LEDChannel& ledChannel);
179  virtual bool isValidLEDGroup(const LEDGroup& ledGroup);
186  const LEDChannel& getChannel(uint8_t channelIndex);
190  virtual void reset();
191 
192 protected:
193  LEDChannel channels[CHANNEL_NUM];
197  bool triggerSave = false;
201  unsigned long lastCommand = 0;
202 
206  virtual void triggerLEDUpdate() = 0;
214  virtual uint8_t getLEDStripMask(uint8_t channel, uint8_t group);
221  virtual void setLEDExternalTemperature(uint8_t channel, uint16_t temp) = 0;
222  virtual bool setLEDGroup(uint8_t channel, uint8_t groupIndex, LEDGroup& group);
233  virtual void setLEDColorValues(uint8_t channel, uint8_t color, uint8_t offset, const uint8_t* values,
234  size_t len) = 0;
242  virtual bool setLEDMode(uint8_t channel, ChannelMode mode);
250  virtual bool setLEDBrightness(uint8_t channel, uint8_t brightness);
258  virtual bool setLEDPortType(uint8_t channel, PortType ledPortType);
265  virtual void clearLEDColorValues(uint8_t channel) = 0;
266  virtual bool clearLEDGroups(uint8_t channel);
273  virtual void startLEDAutodetection(uint8_t channel);
282  virtual uint8_t getLEDAutodetectionResult(uint8_t channel) = 0;
283  virtual bool save() = 0;
284  virtual bool load() = 0;
288  bool saveIfNeeded();
289 };
ChannelMode
Definition: LEDController.h:37
GroupSpeed
Definition: LEDController.h:84
GroupExtra
Definition: LEDController.h:102
GroupDirection
Definition: LEDController.h:93
PortType
Definition: LEDController.h:57
Definition: CorsairLightingProtocolResponse.h:20
Definition: ILEDController.h:25
Definition: LEDController.h:161
virtual void handleLEDControl(const Command &command, const CorsairLightingProtocolResponse *response) override
Definition: LEDController.cpp:20
virtual void setLEDExternalTemperature(uint8_t channel, uint16_t temp)=0
virtual void clearLEDColorValues(uint8_t channel)=0
virtual void triggerLEDUpdate()=0
virtual void reset()
Definition: LEDController.cpp:217
virtual uint8_t getLEDAutodetectionResult(uint8_t channel)=0
bool triggerSave
Definition: LEDController.h:197
bool saveIfNeeded()
Definition: LEDController.cpp:269
virtual uint8_t getLEDStripMask(uint8_t channel, uint8_t group)
Definition: LEDController.cpp:224
virtual void setLEDColorValues(uint8_t channel, uint8_t color, uint8_t offset, const uint8_t *values, size_t len)=0
virtual void startLEDAutodetection(uint8_t channel)
Definition: LEDController.cpp:265
const LEDChannel & getChannel(uint8_t channelIndex)
Definition: LEDController.cpp:215
unsigned long lastCommand
Definition: LEDController.h:201
virtual bool isValidLEDChannel(const LEDChannel &ledChannel)
Definition: LEDController.cpp:196
virtual bool setLEDBrightness(uint8_t channel, uint8_t brightness)
Definition: LEDController.cpp:249
virtual bool setLEDMode(uint8_t channel, ChannelMode mode)
Definition: LEDController.cpp:241
virtual bool setLEDPortType(uint8_t channel, PortType ledPortType)
Definition: LEDController.cpp:257
virtual bool isValidLEDGroup(const LEDGroup &ledGroup)
Definition: LEDController.cpp:209
Definition: CorsairLightingProtocolConstants.h:110
Definition: LEDController.h:141
uint8_t brightness
Definition: LEDController.h:145
uint8_t ledCount
Definition: LEDController.h:150
Definition: LEDController.h:114
uint8_t ledCount
Definition: LEDController.h:122
uint8_t ledIndex
Definition: LEDController.h:118