Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code format #104

Merged
merged 3 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BasedOnStyle: Google
ColumnLimit: 120
IndentWidth: 4
TabWidth: 4
UseTab: ForContinuationAndIndentation
AccessModifierOffset: -4
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
indent_size = 4
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = true

[*.{yml,yaml}]
indent_style = space
indent_size = 2
22 changes: 22 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Check Code Format

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Check src format
uses: DoozyX/[email protected]
with:
source: './src'
extensions: 'h,cpp'
clangFormatVersion: 9
- name: Check examples format
uses: DoozyX/[email protected]
with:
source: './examples'
extensions: 'h,cpp,ino'
clangFormatVersion: 9
10 changes: 10 additions & 0 deletions examples/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BasedOnStyle: Google
ColumnLimit: 120
IndentWidth: 4
TabWidth: 4
UseTab: ForContinuationAndIndentation
AccessModifierOffset: -4

AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortLambdasOnASingleLine: false
4 changes: 2 additions & 2 deletions examples/AmbientBacklight/AmbientBacklight.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <FastLED.h>

// Hint: The channels are swapped in iCUE, so the first channel in iCUE is here channel 2
#define DATA_PIN_CHANNEL_1 2 // For the monitor backlight
#define DATA_PIN_CHANNEL_1 2 // For the monitor backlight
#define DATA_PIN_CHANNEL_2 3

CRGB ledsChannel1[84];
Expand All @@ -36,7 +36,7 @@ void setup() {
ledController.onUpdateHook(0, []() {
// gamma correction with gamma value 2.0. Use napplyGamma_video for other gamma values.
CLP::gammaCorrection(&ledController, 0);
//napplyGamma_video(ledsChannel1, 84, 2.2);
// napplyGamma_video(ledsChannel1, 84, 2.2);
});
}

Expand Down
10 changes: 6 additions & 4 deletions examples/CommanderPRO/CommanderPRO.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "SimpleFanController.h"
#include "ThermistorTemperatureController.h"
#include <CorsairLightingProtocol.h>
#include <FastLED.h>

#include "SimpleFanController.h"
#include "ThermistorTemperatureController.h"

#define DATA_PIN_CHANNEL_1 2
#define DATA_PIN_CHANNEL_2 3

Expand All @@ -35,7 +36,8 @@
CorsairLightingFirmware firmware = corsairCommanderPROFirmware();
ThermistorTemperatureController temperatureController;
FastLEDController ledController(&temperatureController, true);
SimpleFanController fanController(&temperatureController, FAN_UPDATE_RATE, EEPROM_ADDRESS + ledController.getEEPROMSize());
SimpleFanController fanController(&temperatureController, FAN_UPDATE_RATE,
EEPROM_ADDRESS + ledController.getEEPROMSize());
CorsairLightingProtocolController cLP(&ledController, &temperatureController, &fanController, &firmware);
CorsairLightingProtocolHID cHID(&cLP);

Expand Down Expand Up @@ -68,4 +70,4 @@ void loop() {
FastLED.show();
}
fanController.updateFans();
}
}
56 changes: 26 additions & 30 deletions examples/CommanderPRO/PWMFan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,46 @@
*/
#include "PWMFan.h"

PWMFan::PWMFan(uint8_t pwmPin, uint16_t minRPM ,uint16_t maxRPM) : pwmPin(pwmPin), minRPM(minRPM), maxRPM(maxRPM)
{
PWMFan::PWMFan(uint8_t pwmPin, uint16_t minRPM, uint16_t maxRPM) : pwmPin(pwmPin), minRPM(minRPM), maxRPM(maxRPM) {
pinMode(pwmPin, OUTPUT);
analogWrite(pwmPin, 0);
switch (digitalPinToTimer(pwmPin)) {
case TIMER0B:/* 3 */
case TIMER0B: /* 3 */
#ifdef DEBUG
Serial.println(F("Pin not supported as PWM fan pin"));
Serial.println(F("We don't want to mess up Arduino time functions"));
#endif // DEBUG
break;
case TIMER3A:/* 5 */
TCCR3B = (TCCR3B & B11111000) | 0x01;
break;
case TIMER4D:/* 6 */
//PLLFRQ = (PLLFRQ & B11001111) | (0x03 << PLLTM0);
TCCR4B = (TCCR4B & B11110000) | 0x01;
break;
case TIMER1A:/* 9 */
TCCR1B = (TCCR1B & B11111000) | 0x01;
break;
case TIMER1B:/* 10 */
TCCR1B = (TCCR1B & B11111000) | 0x01;
break;
default:
Serial.println(F("Pin not supported as PWM fan pin"));
Serial.println(F("We don't want to mess up Arduino time functions"));
#endif // DEBUG
break;
case TIMER3A: /* 5 */
TCCR3B = (TCCR3B & B11111000) | 0x01;
break;
case TIMER4D: /* 6 */
// PLLFRQ = (PLLFRQ & B11001111) | (0x03 << PLLTM0);
TCCR4B = (TCCR4B & B11110000) | 0x01;
break;
case TIMER1A: /* 9 */
TCCR1B = (TCCR1B & B11111000) | 0x01;
break;
case TIMER1B: /* 10 */
TCCR1B = (TCCR1B & B11111000) | 0x01;
break;
default:
#ifdef DEBUG
Serial.println(F("Pin not supported as PWM fan pin"));
#endif // DEBUG
break;
Serial.println(F("Pin not supported as PWM fan pin"));
#endif // DEBUG
break;
}
}

void PWMFan::setPower(uint8_t percentage)
{
void PWMFan::setPower(uint8_t percentage) {
analogWrite(pwmPin, percentage);
}

uint8_t PWMFan::calculatePowerFromSpeed(uint16_t rpm)
{
uint8_t PWMFan::calculatePowerFromSpeed(uint16_t rpm) {
rpm = constrain(rpm, minRPM, maxRPM);
return ((float)(rpm - minRPM) / (float)(maxRPM - minRPM)) * 255;
}

uint16_t PWMFan::calculateSpeedFromPower(uint8_t power)
{
uint16_t PWMFan::calculateSpeedFromPower(uint8_t power) {
return map(power, 0, 255, minRPM, maxRPM);
}
7 changes: 4 additions & 3 deletions examples/CommanderPRO/PWMFan.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
class PWMFan {
public:
/**
* PWM fan which maps speed to power using linear interpolation.
* This fan does not read the real RPM values. The Arduino timer for the given pin will be set to higher speed.
*
* PWM fan which maps speed to power using linear interpolation. This fan does not read the real RPM values. The
* Arduino timer for the given pin will be set to higher speed.
*
* @param pwmPin the Arduino pwm pin for this fan. Not all PWM pins are supported.
* @param minRPM the speed in RPM at 0% power
* @param maxRPM the speed in RPM at 100% power
Expand All @@ -31,6 +31,7 @@ class PWMFan {
virtual void setPower(uint8_t percentage);
virtual uint8_t calculatePowerFromSpeed(uint16_t rpm);
virtual uint16_t calculateSpeedFromPower(uint8_t power);

protected:
const uint8_t pwmPin;
const uint16_t minRPM;
Expand Down
72 changes: 29 additions & 43 deletions examples/CommanderPRO/SimpleFanController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,31 @@
limitations under the License.
*/
#include "SimpleFanController.h"

#include <EEPROM.h>

SimpleFanController::SimpleFanController(TemperatureController* temperatureController, uint16_t updateRate, uint16_t eEPROMAdress) : temperatureController(temperatureController), updateRate(updateRate), eEPROMAdress(eEPROMAdress)
{
SimpleFanController::SimpleFanController(TemperatureController* temperatureController, uint16_t updateRate,
uint16_t eEPROMAdress)
: temperatureController(temperatureController), updateRate(updateRate), eEPROMAdress(eEPROMAdress) {
load();
}

void SimpleFanController::addFan(uint8_t index, PWMFan* fan)
{
void SimpleFanController::addFan(uint8_t index, PWMFan* fan) {
if (index >= FAN_NUM) {
return;
}
fans[index] = fan;
switch (fanData[index].mode)
{
case FAN_CONTROL_MODE_FIXED_POWER:
fanData[index].speed = fan->calculateSpeedFromPower(fanData[index].power);
break;
case FAN_CONTROL_MODE_FIXED_RPM:
fanData[index].power = fan->calculatePowerFromSpeed(fanData[index].speed);
break;
switch (fanData[index].mode) {
case FAN_CONTROL_MODE_FIXED_POWER:
fanData[index].speed = fan->calculateSpeedFromPower(fanData[index].power);
break;
case FAN_CONTROL_MODE_FIXED_RPM:
fanData[index].power = fan->calculatePowerFromSpeed(fanData[index].speed);
break;
}
}

bool SimpleFanController::updateFans()
{
bool SimpleFanController::updateFans() {
long currentUpdate = millis();
long lastUpdateNumber = lastUpdate / updateRate;
long currentUpdateNumber = currentUpdate / updateRate;
Expand All @@ -63,8 +62,7 @@ bool SimpleFanController::updateFans()
const uint8_t& group = fanData[i].tempGroup;
if (group == FAN_CURVE_TEMP_GROUP_EXTERNAL) {
temp = externalTemp[i];
}
else if (group < TEMPERATURE_NUM) {
} else if (group < TEMPERATURE_NUM) {
temp = temperatureController->getTemperature(group);
}

Expand All @@ -73,16 +71,15 @@ bool SimpleFanController::updateFans()

if (temp <= fanCurve.temperatures[0]) {
speed = fanCurve.rpms[0];
}
else if (temp > fanCurve.temperatures[FAN_CURVE_POINTS_NUM - 1]) {
} else if (temp > fanCurve.temperatures[FAN_CURVE_POINTS_NUM - 1]) {
speed = fanCurve.rpms[FAN_CURVE_POINTS_NUM - 1];
}
else {
} else {
for (uint8_t p = 0; p < FAN_CURVE_POINTS_NUM - 1; p++) {
if (temp > fanCurve.temperatures[p + 1]) {
continue;
}
speed = map(temp, fanCurve.temperatures[p], fanCurve.temperatures[p + 1], fanCurve.rpms[p], fanCurve.rpms[p + 1]);
speed = map(temp, fanCurve.temperatures[p], fanCurve.temperatures[p + 1], fanCurve.rpms[p],
fanCurve.rpms[p + 1]);
break;
}
}
Expand All @@ -96,71 +93,60 @@ bool SimpleFanController::updateFans()
return false;
}

uint16_t SimpleFanController::getFanSpeed(uint8_t fan)
{
uint16_t SimpleFanController::getFanSpeed(uint8_t fan) {
return fanData[fan].speed;
}

void SimpleFanController::setFanSpeed(uint8_t fan, uint16_t speed)
{
void SimpleFanController::setFanSpeed(uint8_t fan, uint16_t speed) {
fanData[fan].speed = speed;
fanData[fan].mode = FAN_CONTROL_MODE_FIXED_RPM;
fanData[fan].power = fans[fan] != nullptr ? fans[fan]->calculatePowerFromSpeed(speed) : 0;
triggerSave = true;
}

uint8_t SimpleFanController::getFanPower(uint8_t fan)
{
uint8_t SimpleFanController::getFanPower(uint8_t fan) {
return fanData[fan].power;
}

void SimpleFanController::setFanPower(uint8_t fan, uint8_t percentage)
{
void SimpleFanController::setFanPower(uint8_t fan, uint8_t percentage) {
fanData[fan].power = percentage;
fanData[fan].mode = FAN_CONTROL_MODE_FIXED_POWER;
fanData[fan].speed = fans[fan] != nullptr ? fans[fan]->calculateSpeedFromPower(percentage) : 0;
triggerSave = true;
}

void SimpleFanController::setFanCurve(uint8_t fan, uint8_t group, FanCurve& fanCurve)
{
void SimpleFanController::setFanCurve(uint8_t fan, uint8_t group, FanCurve& fanCurve) {
fanData[fan].fanCurve = fanCurve;
fanData[fan].tempGroup = group;
fanData[fan].mode = FAN_CONTROL_MODE_CURVE;
triggerSave = true;
}

void SimpleFanController::setFanExternalTemperature(uint8_t fan, uint16_t temp)
{
void SimpleFanController::setFanExternalTemperature(uint8_t fan, uint16_t temp) {
externalTemp[fan] = temp;
}

void SimpleFanController::setFanForce3PinMode(bool flag)
{
void SimpleFanController::setFanForce3PinMode(bool flag) {
force3PinMode = flag;
}

FanDetectionType SimpleFanController::getFanDetectionType(uint8_t fan)
{
FanDetectionType SimpleFanController::getFanDetectionType(uint8_t fan) {
return fanData[fan].detectionType;
}

void SimpleFanController::setFanDetectionType(uint8_t fan, FanDetectionType type)
{
void SimpleFanController::setFanDetectionType(uint8_t fan, FanDetectionType type) {
if (fanData[fan].detectionType != type) {
fanData[fan].detectionType = type;
triggerSave = true;
}
}

bool SimpleFanController::load()
{
bool SimpleFanController::load() {
EEPROM.get(eEPROMAdress, fanData);
return true;
}

bool SimpleFanController::save()
{
bool SimpleFanController::save() {
#ifdef DEBUG
Serial.println(F("Save fan data to EEPROM."));
#endif
Expand Down
Loading