Refacored code; introduced all possible commands

This commit is contained in:
Ollo 2024-12-07 11:39:12 +01:00
parent 30deee981b
commit da8045e7de
4 changed files with 122 additions and 28 deletions

View File

@ -1,16 +1,35 @@
/**
* @file SerialCmd.h
* @author Ollo
* @brief
* @version 0.1
* @date 2024-12-06
*
* @copyright Copyright (c) 2024
*
*/
#ifndef SERIALCMD_H #ifndef SERIALCMD_H
#define SERIALCMD_H #define SERIALCMD_H
#include <stdint.h> #include <stdint.h>
/* 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); int SerialCmd_readFromSerial (void);
void SerialCmd_clearCmdArray (void); void SerialCmd_clearCmdArray (void);
int SerialCmd_checkCmdArrayForPrefix (void); int SerialCmd_checkCmdArrayForPrefix (void);
uint32_t SerialCmd_parseColor(int startOffset); uint32_t SerialCmd_parseColor(unsigned int startOffset);
char SerialCmd_type(void); char SerialCmd_type(void);
void SerialCmd_help(void);
#endif /* SERIALCMD_H */ #endif /* SERIALCMD_H */

View File

@ -11,8 +11,20 @@
#ifndef FANLEDCTL_PINS #ifndef FANLEDCTL_PINS
#define 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 */ #define GPIO_DS18B20 D2 /**< One-Wire used for Dallas temperature sensor */

View File

@ -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 "SerialCmd.h"
#include "ColorUtil.h" #include "ColorUtil.h"
#include <Arduino.h> #include <Arduino.h>
@ -7,6 +17,8 @@
// Serial fields // Serial fields
#define CMD_MAX 128 #define CMD_MAX 128
#define CMD_INDEX 5 /**< fiveth character defindes the command */
static char myCmd[CMD_MAX]; 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) char SerialCmd_type(void)
{ {
if (SerialCmd_checkCmdArrayForPrefix()) if (SerialCmd_checkCmdArrayForPrefix())
{ {
return myCmd[5]; return myCmd[CMD_INDEX];
} }
else 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 colorParts[3]; /* 0: red, 1: green, 2: blue */
unsigned int colorSum = 0;
if (startOffset > (CMD_MAX - sizeof(colorParts))) if (startOffset > (CMD_MAX - sizeof(colorParts)))
{ {
@ -112,3 +135,12 @@ uint32_t SerialCmd_parseColor(int startOffset)
return Color(r, g, b); 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)"));
}

View File

@ -1,3 +1,13 @@
/**
* @file main.cpp
* @author Ollo
* @brief
* @version 0.5
* @date 2024-11-28
*
* @copyright Copyright (c) 2024
*
*/
#include <Arduino.h> #include <Arduino.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <DallasTemperature.h> #include <DallasTemperature.h>
@ -26,6 +36,11 @@
#define TEMP_SENSOR_MEASURE_SERIES 5 /**< Maximum resets */ #define TEMP_SENSOR_MEASURE_SERIES 5 /**< Maximum resets */
#define FAN_RPM_READLEVEL 100 #define FAN_RPM_READLEVEL 100
#define RPM_CONTROL_DEACTIVATED -1
/******************* Settings for command mode ***************/
#define COMMAND_BRIGHTNESS 200
/****************************************************************************** /******************************************************************************
* LOCAL VARIABLES * LOCAL VARIABLES
******************************************************************************/ ******************************************************************************/
@ -35,6 +50,13 @@ Adafruit_NeoPixel* singleLed;
OneWire oneWire(GPIO_DS18B20); OneWire oneWire(GPIO_DS18B20);
DallasTemperature sensors(&oneWire); DallasTemperature sensors(&oneWire);
static int mAutomaticTargetRPM = RPM_CONTROL_DEACTIVATED;
/******************************************************************************
* LOCAL FUNCTIONS
******************************************************************************/
#ifdef FAN_ENABLED #ifdef FAN_ENABLED
/** /**
* @brief Get the Fan Speed in round per minutes * @brief Get the Fan Speed in round per minutes
@ -65,6 +87,26 @@ void setFanSpeedPercent(int p) {
uint8_t mRainbowIndex; uint8_t mRainbowIndex;
#endif #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 * @brief Called once at start
* Initialize hardware pins * 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 * @brief Endless loop
* (is called as fast as possible) * (is called as fast as possible)
@ -160,7 +186,7 @@ void loop()
} }
#ifdef FAN_ENABLED #ifdef FAN_ENABLED
if (actualFanSpeedRpm == 0) if ((actualFanSpeedRpm >= 0) && (actualFanSpeedRpm < LOW_FAN_SPEED))
{ {
if (i > FAN_RPM_READLEVEL) if (i > FAN_RPM_READLEVEL)
{ {
@ -183,17 +209,22 @@ void loop()
if(checkCmd == 0) 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 else
{ {
char type = SerialCmd_type(); char type = SerialCmd_type();
switch (type) switch (type)
{ {
case 'a': case SERIALCMD_COMMAND_SETLED[0]:
uint32_t color = SerialCmd_parseColor(6); {
uint32_t color = SerialCmd_parseColor(SERIALCMD_PARAMETER_OFFSET);
pixels->fill(color); pixels->fill(color);
pixels->setBrightness(200); pixels->setBrightness(COMMAND_BRIGHTNESS);
}
break;
case SERIALCMD_COMMAND_AUTOMATIC[0]:
mAutomaticTargetRPM = RPM_CONTROL_DEACTIVATED;
break; break;
} }
} }