updated the logger for static use without a static interface
added the RGB sensor isl29125 added WIFI
This commit is contained in:
@@ -0,0 +1,216 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// \file isl29125.h
|
||||
/// \brief File description
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// vbchaos software design
|
||||
//
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2023 vbchaos
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
#ifndef MAIN_PLATFORM_INC_ISL29125_H_
|
||||
#define MAIN_PLATFORM_INC_ISL29125_H_
|
||||
|
||||
/**
|
||||
* isl29125 implementation
|
||||
* \defgroup isl29125
|
||||
* \brief {group_description}
|
||||
* \addtogroup {Layer}
|
||||
*
|
||||
* Detailed description
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Include files
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
// CompilerIncludes
|
||||
// All include files that are provided by the compiler directly
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
// ProjectIncludes
|
||||
// All include files that are provided by the project
|
||||
#include "ISerialBus.h"
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Type definitions.
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
class isl29125
|
||||
{
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// Public Section
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
public:
|
||||
|
||||
struct rgb_t
|
||||
{
|
||||
uint16_t red;
|
||||
uint16_t green;
|
||||
uint16_t blue;
|
||||
} RGB_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
POWER_DOWN = 0,
|
||||
GREEN = 1,
|
||||
RED = 2,
|
||||
BLUE = 3,
|
||||
STANDBY = 4,
|
||||
RGB = 5,
|
||||
GREEN_RED = 6,
|
||||
GREEN_BLUE = 7
|
||||
} Mode_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
LOW = 0,
|
||||
HIGH = 1
|
||||
} Range_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
RES_12BIT = 0,
|
||||
RES_16BIT = 1
|
||||
} Resolution_t;
|
||||
|
||||
const uint8_t deviceID = 0x7D;
|
||||
|
||||
// Class Constructor
|
||||
isl29125(uint8_t slaveAddress, ISerialBus<uint8_t>& serialPort);
|
||||
FunctionStatus initialize(void);
|
||||
|
||||
FunctionStatus setMode(Mode_t mode);
|
||||
FunctionStatus setRange(Range_t range);
|
||||
FunctionStatus setResolution(Resolution_t resolutionß);
|
||||
|
||||
FunctionStatus getRGB(struct rgb_t* rgb);
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// Protected Section
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
protected:
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// Private Section
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
private:
|
||||
|
||||
struct __attribute__ ((packed)) memorymap
|
||||
{
|
||||
uint8_t device_id;
|
||||
struct
|
||||
{
|
||||
uint8_t mode :3;
|
||||
uint8_t range :1;
|
||||
uint8_t bits :1;
|
||||
uint8_t sync :1;
|
||||
uint8_t RESERVERED :2;
|
||||
} configuration1;
|
||||
struct
|
||||
{
|
||||
uint8_t alscc :6;
|
||||
uint8_t RESERVED :1;
|
||||
uint8_t ircom :1;
|
||||
} configuration2;
|
||||
struct
|
||||
{
|
||||
uint8_t intsel :2;
|
||||
uint8_t prst :2;
|
||||
uint8_t conven :1;
|
||||
uint8_t RESERVED :3;
|
||||
} configuration3;
|
||||
struct
|
||||
{
|
||||
uint8_t lowByte;
|
||||
uint8_t highByte;
|
||||
} lowThreshold;
|
||||
struct
|
||||
{
|
||||
uint8_t lowByte;
|
||||
uint8_t highByte;
|
||||
} highThreshold;
|
||||
struct
|
||||
{
|
||||
uint8_t RESERVED1 :2;
|
||||
uint8_t grbcf :2;
|
||||
uint8_t RESERVED2 :1;
|
||||
uint8_t boutf :1;
|
||||
uint8_t convenf :1;
|
||||
uint8_t rgbthf :1;
|
||||
} statusFlags;
|
||||
union
|
||||
{
|
||||
uint16_t word;
|
||||
struct
|
||||
{
|
||||
uint8_t lowByte;
|
||||
uint8_t highByte;
|
||||
};
|
||||
} greenData;
|
||||
union
|
||||
{
|
||||
uint16_t word;
|
||||
struct
|
||||
{
|
||||
uint8_t lowByte;
|
||||
uint8_t highByte;
|
||||
};
|
||||
} redData;
|
||||
union
|
||||
{
|
||||
uint16_t word;
|
||||
struct
|
||||
{
|
||||
uint8_t lowByte;
|
||||
uint8_t highByte;
|
||||
};
|
||||
} blueData;
|
||||
};
|
||||
|
||||
struct memorymap memorymap;
|
||||
|
||||
uint8_t slaveAddress;
|
||||
ISerialBus<uint8_t>& bus;
|
||||
bool initialized;
|
||||
|
||||
// Reads the device ID directly into the memory map
|
||||
FunctionStatus getDeviceID(void);
|
||||
// Reads all configuration registers into the memory map
|
||||
FunctionStatus getConfiguration(void);
|
||||
// Reads all Threshold registers into the memory map
|
||||
FunctionStatus getTheshold(void);
|
||||
// Reads the status register into the memory map
|
||||
FunctionStatus getStatusFlags(void);
|
||||
// Reads the RGB data registers into the memory map
|
||||
FunctionStatus getRGBRegisters(void);
|
||||
// Read the full memory map from device into the local memory map, which creates a perfect memory copy
|
||||
FunctionStatus getCompleteRegisterMap(void);
|
||||
|
||||
};
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
#endif /* MAIN_PLATFORM_INC_ISL29125_H_ */
|
||||
@@ -53,7 +53,7 @@
|
||||
|
||||
#if defined(ENABLE_SERIAL_LOGGING)
|
||||
#define LOGGER_LOG(severity,...) \
|
||||
debugLogger.log(__FILE__, __func__, __LINE__, severity, ##__VA_ARGS__)
|
||||
logger::log(__FILE__, __func__, __LINE__, severity, ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOGGER_LOG(severity, message)
|
||||
#endif
|
||||
@@ -98,9 +98,8 @@
|
||||
// Type definitions.
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class logger;
|
||||
extern logger debugLogger;
|
||||
//class logger;
|
||||
//extern logger debugLogger;
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
@@ -126,7 +125,7 @@ class logger
|
||||
// Class Constructor
|
||||
logger(uint32_t queuesize, ISerialBus<uint8_t>& serialPort);
|
||||
|
||||
FunctionStatus log(const char* fileName, const char* functionName, int lineNumber, LogType logType, const char* format, ...);
|
||||
static FunctionStatus log(const char* fileName, const char* functionName, int lineNumber, LogType logType, const char* format, ...);
|
||||
|
||||
// The Logger task - should be called by the system scheduler regularly
|
||||
void task();
|
||||
@@ -160,18 +159,20 @@ class logger
|
||||
|
||||
void composeTypeParameterList(void);
|
||||
|
||||
void composeLogQueueItem(struct LogQueueItem* logQueueItem, std::string fileName, std::string functionName,
|
||||
int lineNumber, LogType logType, std::string context);
|
||||
static void composeLogQueueItem(struct LogQueueItem* logQueueItem, const std::string& fileName, const std::string& functionName,
|
||||
int lineNumber, LogType logType, const std::string& context);
|
||||
|
||||
std::list<struct typeParameters> typeParameterList;
|
||||
|
||||
std::list<struct LogQueueItem> queue;
|
||||
static std::list<struct LogQueueItem> queue;
|
||||
ISerialBus<uint8_t>& port;
|
||||
uint32_t queuesize;
|
||||
uint32_t numberOfLostMessages;
|
||||
static uint32_t queuesize;
|
||||
static uint32_t numberOfLostMessages;
|
||||
static bool overflowRecovery;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// \file wifi.h
|
||||
/// \brief File description
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// vbchaos software design
|
||||
//
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2023 vbchaos
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
#ifndef MAIN_INC_WIFI_H_
|
||||
#define MAIN_INC_WIFI_H_
|
||||
|
||||
/**
|
||||
* wifi implementation
|
||||
* \defgroup wifi
|
||||
* \brief {group_description}
|
||||
* \addtogroup {Layer}
|
||||
*
|
||||
* Detailed description
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Include files
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
// CompilerIncludes
|
||||
// All include files that are provided by the compiler directly
|
||||
//#include "esp_system.h"
|
||||
#include "esp_event.h"
|
||||
//#include "esp_log.h"
|
||||
//
|
||||
#include "freertos/FreeRTOS.h"
|
||||
//#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
||||
|
||||
// ProjectIncludes
|
||||
// All include files that are provided by the project
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Type definitions.
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
class Wifi
|
||||
{
|
||||
public:
|
||||
|
||||
Wifi(void);
|
||||
|
||||
void start_client(void);
|
||||
|
||||
private:
|
||||
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
static int s_retry_num;
|
||||
static const char *TAG;
|
||||
|
||||
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
|
||||
|
||||
};
|
||||
|
||||
|
||||
/** @} */
|
||||
|
||||
#endif /* MAIN_INC_WIFI_H_ */
|
||||
@@ -0,0 +1,195 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// \file isl29125.cpp
|
||||
/// \brief Description
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// vbchaos software design
|
||||
//
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2023 vbchaos
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Include files
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <isl29125.h>
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Type definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// File-scope variables
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
isl29125::isl29125(uint8_t slaveAddress, ISerialBus<uint8_t>& serialPort) : slaveAddress {slaveAddress}, bus {serialPort}
|
||||
{
|
||||
initialized = false;
|
||||
}
|
||||
|
||||
|
||||
FunctionStatus isl29125::initialize(void)
|
||||
{
|
||||
FunctionStatus returnValue = FUNCTION_STATUS_OK;
|
||||
|
||||
// Get the Device ID
|
||||
returnValue = getDeviceID();
|
||||
|
||||
if (returnValue == FUNCTION_STATUS_OK)
|
||||
{
|
||||
// Verify the Device ID against the default known ID
|
||||
if (memorymap.device_id != deviceID)
|
||||
{
|
||||
returnValue = FUNCTION_STATUS_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
if (returnValue == FUNCTION_STATUS_OK)
|
||||
{
|
||||
// Fetch the complete register map for an initial copy
|
||||
returnValue = getCompleteRegisterMap();
|
||||
}
|
||||
|
||||
if (returnValue == FUNCTION_STATUS_OK)
|
||||
{
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
FunctionStatus isl29125::setMode(Mode_t mode)
|
||||
{
|
||||
FunctionStatus returnValue = FUNCTION_STATUS_OK;
|
||||
if (!initialized)
|
||||
{
|
||||
returnValue = FUNCTION_STATUS_NOT_INITIALIZED;
|
||||
}
|
||||
else
|
||||
{
|
||||
memorymap.configuration1.mode = mode;
|
||||
returnValue = bus.write(slaveAddress, 0x01, (uint8_t*)&memorymap.configuration1, 1);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
FunctionStatus isl29125::setRange(Range_t range)
|
||||
{
|
||||
FunctionStatus returnValue = FUNCTION_STATUS_OK;
|
||||
if (!initialized)
|
||||
{
|
||||
returnValue = FUNCTION_STATUS_NOT_INITIALIZED;
|
||||
}
|
||||
else
|
||||
{
|
||||
memorymap.configuration1.range = range;
|
||||
returnValue = bus.write(slaveAddress, 0x01, (uint8_t*)&memorymap.configuration1, 1);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
FunctionStatus isl29125::setResolution(Resolution_t resolution)
|
||||
{
|
||||
FunctionStatus returnValue = FUNCTION_STATUS_OK;
|
||||
if (!initialized)
|
||||
{
|
||||
returnValue = FUNCTION_STATUS_NOT_INITIALIZED;
|
||||
}
|
||||
else
|
||||
{
|
||||
memorymap.configuration1.bits = resolution;
|
||||
returnValue = bus.write(slaveAddress, 0x01, (uint8_t*)&memorymap.configuration1, 1);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
FunctionStatus isl29125::getRGB(struct rgb_t* rgb)
|
||||
{
|
||||
FunctionStatus returnValue = FUNCTION_STATUS_OK;
|
||||
if (!initialized)
|
||||
{
|
||||
returnValue = FUNCTION_STATUS_NOT_INITIALIZED;
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = getRGBRegisters();
|
||||
}
|
||||
if (returnValue == FUNCTION_STATUS_OK)
|
||||
{
|
||||
rgb->blue = memorymap.blueData.word;
|
||||
rgb->green = memorymap.greenData.word;
|
||||
rgb->red = memorymap.redData.word;
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
FunctionStatus isl29125::getDeviceID(void)
|
||||
{
|
||||
uint32_t actualLength;
|
||||
return bus.read(slaveAddress, 0x00, (uint8_t*)&memorymap, 1, &actualLength);
|
||||
}
|
||||
|
||||
|
||||
FunctionStatus isl29125::getConfiguration(void)
|
||||
{
|
||||
uint32_t actualLength;
|
||||
return bus.read(slaveAddress, 0x01, (uint8_t*)&memorymap, 3, &actualLength);
|
||||
}
|
||||
|
||||
|
||||
FunctionStatus isl29125::getTheshold(void)
|
||||
{
|
||||
uint32_t actualLength;
|
||||
return bus.read(slaveAddress, 0x04, (uint8_t*)&memorymap, 4, &actualLength);
|
||||
}
|
||||
|
||||
|
||||
FunctionStatus isl29125::getStatusFlags(void)
|
||||
{
|
||||
uint32_t actualLength;
|
||||
return bus.read(slaveAddress, 0x08, (uint8_t*)&memorymap, 1, &actualLength);
|
||||
}
|
||||
|
||||
|
||||
FunctionStatus isl29125::getRGBRegisters(void)
|
||||
{
|
||||
uint32_t actualLength;
|
||||
return bus.read(slaveAddress, 0x09, (uint8_t*)&memorymap, 6, &actualLength);
|
||||
}
|
||||
|
||||
|
||||
FunctionStatus isl29125::getCompleteRegisterMap(void)
|
||||
{
|
||||
uint32_t actualLength;
|
||||
return bus.read(slaveAddress, 0x00, (uint8_t*)&memorymap, 1, &actualLength);
|
||||
}
|
||||
@@ -49,8 +49,12 @@
|
||||
// Function definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
std::list<struct logger::LogQueueItem> logger::queue;
|
||||
uint32_t logger::queuesize = 16;
|
||||
uint32_t logger::numberOfLostMessages = 0;
|
||||
bool logger::overflowRecovery = false;
|
||||
|
||||
logger::logger(uint32_t queuesize, ISerialBus<uint8_t>& serialPort) : port {serialPort}, queuesize {queuesize}
|
||||
logger::logger(uint32_t queuesize, ISerialBus<uint8_t>& serialPort) : port {serialPort}
|
||||
{
|
||||
numberOfLostMessages = 0;
|
||||
// Compose the debug type level list
|
||||
@@ -63,14 +67,13 @@ FunctionStatus logger::log(const char* fileName, const char* functionName, int l
|
||||
FunctionStatus returnValue = FUNCTION_STATUS_OK;
|
||||
|
||||
#if defined(ENABLE_SERIAL_LOGGING)
|
||||
bool overflowRecovery = false;
|
||||
int nrOfMessages;
|
||||
struct LogQueueItem logQueueItem;
|
||||
va_list ap;
|
||||
|
||||
nrOfMessages = queue.size();
|
||||
|
||||
if((nrOfMessages == queuesize - 1) && !overflowRecovery)
|
||||
if((nrOfMessages >= queuesize - 1) && !overflowRecovery)
|
||||
{
|
||||
// Queue almost full, only one entry left. Log a warning instead
|
||||
composeLogQueueItem(&logQueueItem, __FILE__, __func__, __LINE__, LOGTYPE_WARNING, "Log queue overflow");
|
||||
@@ -82,8 +85,7 @@ FunctionStatus logger::log(const char* fileName, const char* functionName, int l
|
||||
else if((nrOfMessages == 0) && overflowRecovery)
|
||||
{
|
||||
// Queue empty again after an overflow
|
||||
char str[128];
|
||||
snprintf(str, sizeof(str) / sizeof(str[0]), "%d messages lost", (unsigned int)numberOfLostMessages);
|
||||
std::string str = std::to_string(numberOfLostMessages) + " messages were lost in the logger due to QUEUE overflow";
|
||||
composeLogQueueItem(&logQueueItem, __FILE__, __func__, __LINE__, LOGTYPE_WARNING, str);
|
||||
queue.push_back(logQueueItem);
|
||||
|
||||
@@ -130,46 +132,16 @@ void logger::composeTypeParameterList(void)
|
||||
|
||||
|
||||
#if defined(ENABLE_SERIAL_LOGGING)
|
||||
void logger::composeLogQueueItem(struct LogQueueItem* logQueueItem, std::string fileName, std::string functionName,
|
||||
int lineNumber, LogType logType, std::string context)
|
||||
void logger::composeLogQueueItem(struct LogQueueItem* logQueueItem, const std::string& fileName, const std::string& functionName,
|
||||
int lineNumber, LogType logType, const std::string& context)
|
||||
{
|
||||
|
||||
logQueueItem->logType = logType;
|
||||
logQueueItem->context = context;
|
||||
// strncpy((char*)&(logQueueItem->context[0]), context, contextSize);
|
||||
// logQueueItem->context[contextSize - 1] = '\0';
|
||||
|
||||
if(logType != LOGTYPE_PRINT)
|
||||
{
|
||||
int fileNameIndex = 0;
|
||||
|
||||
// If filename starts with "src/", strip this part
|
||||
if((fileName[0] == 's') &&
|
||||
(fileName[1] == 'r') &&
|
||||
(fileName[2] == 'c') &&
|
||||
(fileName[3] == '/'))
|
||||
{
|
||||
fileNameIndex = 4;
|
||||
}
|
||||
|
||||
// It is known that the strncpy use can potentially truncate the source string, meaning
|
||||
// that the target string size is smaller then the original string
|
||||
// This is not a problem in this particular case, so the compiler warning is disabled
|
||||
// for this situation
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstringop-truncation"
|
||||
// All logtypes except LOGTYPE_PRINT need filename, functionname and linenumber.
|
||||
logQueueItem->fileName = fileName;
|
||||
logQueueItem->functionName = functionName;
|
||||
// strncpy((char*)&(logQueueItem->fileName[0]), &fileName[fileNameIndex], fileNameSize);
|
||||
// strncpy((char*)&(logQueueItem->functionName[0]), functionName, functionNameSize);
|
||||
#pragma GCC diagnostic pop
|
||||
logQueueItem->lineNumber = lineNumber;
|
||||
|
||||
// Fix terminating null byte in strncpy in case string to be copied is too long
|
||||
// logQueueItem->fileName[fileNameSize - 1] = '\0';
|
||||
// logQueueItem->functionName[functionNameSize - 1] = '\0';
|
||||
}
|
||||
logQueueItem->fileName = fileName;
|
||||
logQueueItem->functionName = functionName;
|
||||
logQueueItem->lineNumber = lineNumber;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -186,7 +158,7 @@ void logger::task()
|
||||
// Get the first log item from queue
|
||||
logQueueItem = queue.front();
|
||||
// Remove the item from the queue
|
||||
this->queue.pop_front();
|
||||
queue.pop_front();
|
||||
|
||||
|
||||
if(logQueueItem.logType == LOGTYPE_PRINT)
|
||||
@@ -194,7 +166,7 @@ void logger::task()
|
||||
// Raw print
|
||||
#if defined(ENABLE_SERIAL_LOGGING)
|
||||
|
||||
port.write(NO_DEVICE_ADDRESS, NO_REGISTER_ADDRESS, (uint8_t*)logQueueItem.context.c_str(), logQueueItem.context.length() + 1);
|
||||
port.write(NO_DEVICE_ADDRESS, NO_REGISTER_ADDRESS, (uint8_t*)logQueueItem.context.c_str(), logQueueItem.context.length());
|
||||
#endif
|
||||
|
||||
}
|
||||
@@ -208,7 +180,8 @@ void logger::task()
|
||||
// Find the correct Log level type
|
||||
auto it = std::find_if(typeParameterList.begin(), typeParameterList.end(), [&logQueueItem](const struct typeParameters& obj) {return obj.logType == logQueueItem.logType;});
|
||||
|
||||
std::string outputString = it->vt100Prefix + " "
|
||||
std::string outputString = "\n\r"
|
||||
+ it->vt100Prefix + " "
|
||||
+ it->vt100Prefix + " "
|
||||
+ std::to_string(seconds) + " "
|
||||
+ logQueueItem.fileName + " "
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// \file wifi.cpp
|
||||
/// \brief Description
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// vbchaos software design
|
||||
//
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2023 vbchaos
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Include files
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
#include "string.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_wifi.h"
|
||||
|
||||
#include "wifi.h"
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#define EXAMPLE_ESP_MAXIMUM_RETRY 5
|
||||
|
||||
/* The event group allows multiple bits for each event, but we only care about two events:
|
||||
* - we are connected to the AP with an IP
|
||||
* - we failed to connect after the maximum amount of retries */
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
#define ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA_WPA2_PSK
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Type definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// File-scope variables
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
static const char* ssid = "Kowalski";
|
||||
static const char* pass = "madagascar";
|
||||
//static const char* ssid = "vbchaos";
|
||||
//static const char* pass = "mijninternet";
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
EventGroupHandle_t Wifi::s_wifi_event_group = xEventGroupCreate();
|
||||
int Wifi::s_retry_num = 0;
|
||||
const char* Wifi::TAG;
|
||||
|
||||
Wifi::Wifi()
|
||||
{
|
||||
TAG = "wifi station";
|
||||
}
|
||||
|
||||
void Wifi::event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
||||
{
|
||||
esp_wifi_connect();
|
||||
}
|
||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
||||
{
|
||||
if (Wifi::s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY)
|
||||
{
|
||||
esp_wifi_connect();
|
||||
s_retry_num++;
|
||||
LOGGER_INFO("%s - retry to connect to the AP", TAG);
|
||||
}
|
||||
else
|
||||
{
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
}
|
||||
LOGGER_ERROR("%S - connect to the AP fail", TAG);
|
||||
}
|
||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
||||
{
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t*) event_data;
|
||||
LOGGER_SUCCESS("%s - got ip: %d:%d:%d:%d", TAG, IP2STR(&event->ip_info.ip));
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
void Wifi::start_client(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_sta();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
|
||||
|
||||
wifi_config_t wifi_config;
|
||||
|
||||
memset(&wifi_config, 0, sizeof(wifi_config));
|
||||
|
||||
strncpy((char*)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
|
||||
strncpy((char*)wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
|
||||
|
||||
wifi_config.sta.threshold.authmode = ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD;
|
||||
wifi_config.sta.sae_pwe_h2e = WPA3_SAE_PWE_BOTH;
|
||||
|
||||
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
LOGGER_INFO("%s - wifi_init_sta finished", TAG);
|
||||
|
||||
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
|
||||
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
portMAX_DELAY);
|
||||
|
||||
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
|
||||
* happened. */
|
||||
if (bits & WIFI_CONNECTED_BIT)
|
||||
{
|
||||
LOGGER_SUCCESS("%s - connected to ap SSID:%s password:%s", TAG, ssid, pass);
|
||||
}
|
||||
else if (bits & WIFI_FAIL_BIT)
|
||||
{
|
||||
LOGGER_ERROR("%s - Failed to connect to SSID:%s, password:%s", TAG, ssid, pass);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER_ERROR("%s - UNEXPECTED EVENT", TAG);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user