From da8045e7def9acc003c8de363caf9efaacf9716c Mon Sep 17 00:00:00 2001 From: Ollo Date: Sat, 7 Dec 2024 11:39:12 +0100 Subject: [PATCH] Refacored code; introduced all possible commands --- include/SerialCmd.h | 23 ++++++++++++-- include/controller.h | 16 ++++++++-- src/SerialCmd.cpp | 38 +++++++++++++++++++++-- src/main.cpp | 73 +++++++++++++++++++++++++++++++------------- 4 files changed, 122 insertions(+), 28 deletions(-) diff --git a/include/SerialCmd.h b/include/SerialCmd.h index 013fa4f..cf7a8fe 100644 --- a/include/SerialCmd.h +++ b/include/SerialCmd.h @@ -1,16 +1,35 @@ +/** + * @file SerialCmd.h + * @author Ollo + * @brief + * @version 0.1 + * @date 2024-12-06 + * + * @copyright Copyright (c) 2024 + * + */ #ifndef SERIALCMD_H #define SERIALCMD_H #include +/* Define commands: */ +#define SERIALCMD_COMMAND_SETLED "l" +#define SERIALCMD_COMMAND_AUTOMATIC "a" +#define SERIALCMD_COMMAND_FAN "f" + +#define SERIALCMD_COMMAND_HELP "ollpehelp" +#define SERIALCMD_PARAMETER_OFFSET 6 + int SerialCmd_readFromSerial (void); void SerialCmd_clearCmdArray (void); int SerialCmd_checkCmdArrayForPrefix (void); -uint32_t SerialCmd_parseColor(int startOffset); +uint32_t SerialCmd_parseColor(unsigned int startOffset); char SerialCmd_type(void); +void SerialCmd_help(void); -#endif /* SERIALCMD_H */ \ No newline at end of file +#endif /* SERIALCMD_H */ diff --git a/include/controller.h b/include/controller.h index a0acf9c..a8c7e4b 100644 --- a/include/controller.h +++ b/include/controller.h @@ -11,8 +11,20 @@ #ifndef FANLEDCTL_PINS #define FANLEDCTL_PINS -#define FAN_PIN D6 /**< Output Pin, controlling FAN speed via PWM */ -#define SIGNAL_PIN D7 /**< Input Pin, reading current FANs RPM*/ + +/****************************************************************************** + * BEHAVIOR DEFINES + ******************************************************************************/ + +#define LOW_FAN_SPEED 500 /**< Rounds per Minute */ + + +/****************************************************************************** + * PINOUT DEFINES + ******************************************************************************/ + +#define FAN_PIN D6 /**< Output Pin, controlling FAN speed via PWM */ +#define SIGNAL_PIN D7 /**< Input Pin, reading current FANs RPM*/ #define GPIO_DS18B20 D2 /**< One-Wire used for Dallas temperature sensor */ diff --git a/src/SerialCmd.cpp b/src/SerialCmd.cpp index 1000860..a607dd6 100644 --- a/src/SerialCmd.cpp +++ b/src/SerialCmd.cpp @@ -1,4 +1,14 @@ +/** + * @file SerialCmd.h + * @author Ollo + * @brief + * @version 0.1 + * @date 2024-12-06 + * + * @copyright Copyright (c) 2024 + * + */ #include "SerialCmd.h" #include "ColorUtil.h" #include @@ -7,6 +17,8 @@ // Serial fields #define CMD_MAX 128 +#define CMD_INDEX 5 /**< fiveth character defindes the command */ + static char myCmd[CMD_MAX]; /** @@ -74,11 +86,17 @@ int SerialCmd_checkCmdArrayForPrefix (void) } } +/** + * @brief + * Get type, see different commands in header file + * + * @return char + */ char SerialCmd_type(void) { if (SerialCmd_checkCmdArrayForPrefix()) { - return myCmd[5]; + return myCmd[CMD_INDEX]; } else { @@ -86,10 +104,15 @@ char SerialCmd_type(void) } } -uint32_t SerialCmd_parseColor(int startOffset) +/** + * @brief Color + * Extract color of the following six bytes + * @param startOffset + * @return uint32_t + */ +uint32_t SerialCmd_parseColor(unsigned int startOffset) { unsigned int colorParts[3]; /* 0: red, 1: green, 2: blue */ - unsigned int colorSum = 0; if (startOffset > (CMD_MAX - sizeof(colorParts))) { @@ -112,3 +135,12 @@ uint32_t SerialCmd_parseColor(int startOffset) return Color(r, g, b); } + +void SerialCmd_help(void) +{ + Serial.println(SERIALCMD_COMMAND_HELP); + Serial.println(F("help: " SERIALCMD_COMMAND_HELP)); + Serial.println(F("leds: " SERIALCMD_COMMAND_SETLED "followed by rrggbb (as hex)")); + Serial.println(F("manual fan control: " SERIALCMD_COMMAND_FAN " followed by xx (speed as hex)")); + Serial.println(F("automatic fan: " SERIALCMD_COMMAND_FAN " followed by xx (speed as hex)")); +} diff --git a/src/main.cpp b/src/main.cpp index 2ebc2c3..9ab4ddb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,13 @@ +/** + * @file main.cpp + * @author Ollo + * @brief + * @version 0.5 + * @date 2024-11-28 + * + * @copyright Copyright (c) 2024 + * + */ #include #include #include @@ -26,6 +36,11 @@ #define TEMP_SENSOR_MEASURE_SERIES 5 /**< Maximum resets */ #define FAN_RPM_READLEVEL 100 +#define RPM_CONTROL_DEACTIVATED -1 + +/******************* Settings for command mode ***************/ +#define COMMAND_BRIGHTNESS 200 + /****************************************************************************** * LOCAL VARIABLES ******************************************************************************/ @@ -35,6 +50,13 @@ Adafruit_NeoPixel* singleLed; OneWire oneWire(GPIO_DS18B20); DallasTemperature sensors(&oneWire); +static int mAutomaticTargetRPM = RPM_CONTROL_DEACTIVATED; + + +/****************************************************************************** + * LOCAL FUNCTIONS + ******************************************************************************/ + #ifdef FAN_ENABLED /** * @brief Get the Fan Speed in round per minutes @@ -65,6 +87,26 @@ void setFanSpeedPercent(int p) { uint8_t mRainbowIndex; #endif +float readTemperature(void) +{ + int sensorCount = sensors.getDeviceCount(); + if (sensorCount > 0) + { + sensors.requestTemperatures(); + float temp1Raw = sensors.getTempCByIndex(0); + if (temp1Raw > 0) + { + return temp1Raw; + } + } + + return INVALID_TEMP; +} + +/****************************************************************************** + * STANDARD ARDUINO FUNCTIONS + ******************************************************************************/ + /** * @brief Called once at start * Initialize hardware pins @@ -120,22 +162,6 @@ void setup() } -float readTemperature(void) -{ - int sensorCount = sensors.getDeviceCount(); - if (sensorCount > 0) - { - sensors.requestTemperatures(); - float temp1Raw = sensors.getTempCByIndex(0); - if (temp1Raw > 0) - { - return temp1Raw; - } - } - - return INVALID_TEMP; -} - /** * @brief Endless loop * (is called as fast as possible) @@ -160,7 +186,7 @@ void loop() } #ifdef FAN_ENABLED - if (actualFanSpeedRpm == 0) + if ((actualFanSpeedRpm >= 0) && (actualFanSpeedRpm < LOW_FAN_SPEED)) { if (i > FAN_RPM_READLEVEL) { @@ -183,17 +209,22 @@ void loop() if(checkCmd == 0) { - Serial.println("if you dont know what to do type \"ollpehelp\""); + Serial.println(F("if you dont know what to do type \"" SERIALCMD_COMMAND_HELP "\"")); } else { char type = SerialCmd_type(); switch (type) { - case 'a': - uint32_t color = SerialCmd_parseColor(6); + case SERIALCMD_COMMAND_SETLED[0]: + { + uint32_t color = SerialCmd_parseColor(SERIALCMD_PARAMETER_OFFSET); pixels->fill(color); - pixels->setBrightness(200); + pixels->setBrightness(COMMAND_BRIGHTNESS); + } + break; + case SERIALCMD_COMMAND_AUTOMATIC[0]: + mAutomaticTargetRPM = RPM_CONTROL_DEACTIVATED; break; } }