Skip to content

Commit

Permalink
misc: add log levels and add SetPUPTriggerCallback (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalkbrenner authored Apr 14, 2024
1 parent 1d17694 commit 6045a3e
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 61 deletions.
27 changes: 26 additions & 1 deletion include/DMDUtil/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@
#include <cstdarg>
#include <string>

typedef void(DMDUTILCALLBACK* DMDUtil_LogCallback)(const char* format, va_list args);
typedef enum
{
DMDUtil_LogLevel_INFO,
DMDUtil_LogLevel_ERROR,
DMDUtil_LogLevel_DEBUG
} DMDUtil_LogLevel;

typedef void(DMDUTILCALLBACK* DMDUtil_LogCallback)(DMDUtil_LogLevel logLevel, const char* format, va_list args);

typedef void(DMDUTILCALLBACK* DMDUtil_PUPTriggerCallback)(uint16_t id, void* userData);

struct DMDUtil_PUPTriggerCallbackContext
{
DMDUtil_PUPTriggerCallback callback;
void* pUserData;
};

namespace DMDUtil
{
Expand Down Expand Up @@ -58,8 +73,16 @@ class DMDUTILAPI Config
const char* GetDMDServerAddr() const { return m_dmdServerAddr.c_str(); }
void SetDMDServerPort(int port) { m_dmdServerPort = port; }
int GetDMDServerPort() const { return m_dmdServerPort; }
DMDUtil_LogLevel GetLogLevel() const { return m_logLevel; }
void SetLogLevel(DMDUtil_LogLevel logLevel) { m_logLevel = logLevel; }
DMDUtil_LogCallback GetLogCallback() const { return m_logCallback; }
void SetLogCallback(DMDUtil_LogCallback callback) { m_logCallback = callback; }
DMDUtil_PUPTriggerCallbackContext GetPUPTriggerCallbackContext() const { return m_pupTriggerCallbackContext; }
void SetPUPTriggerCallback(DMDUtil_PUPTriggerCallback callback, void* pUserData)
{
m_pupTriggerCallbackContext.callback = callback;
m_pupTriggerCallbackContext.pUserData = pUserData;
}

private:
Config();
Expand All @@ -85,7 +108,9 @@ class DMDUTILAPI Config
bool m_pixelcade;
std::string m_pixelcadeDevice;
int m_pixelcadeMatrix;
DMDUtil_LogLevel m_logLevel;
DMDUtil_LogCallback m_logCallback;
DMDUtil_PUPTriggerCallbackContext m_pupTriggerCallbackContext;
};

} // namespace DMDUtil
2 changes: 1 addition & 1 deletion include/DMDUtil/DMD.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class DMDUTILAPI DMD
void UpdateData(const uint8_t* pData, int depth, uint16_t width, uint16_t height, uint8_t r, uint8_t g, uint8_t b,
Mode mode, bool buffered = false);
void AdjustRGB24Depth(uint8_t* pData, uint8_t* pDstData, int length, uint8_t* palette, uint8_t depth);
void handleTrigger(uint16_t id);
void HandleTrigger(uint16_t id);

void DmdFrameThread();
void LevelDMDThread();
Expand Down
4 changes: 4 additions & 0 deletions src/Config.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "DMDUtil/Config.h"

#include <cstring>

namespace DMDUtil
{

Expand Down Expand Up @@ -33,7 +35,9 @@ Config::Config()
m_dmdServer = false;
m_dmdServerAddr = "localhost";
m_dmdServerPort = 6789;
m_logLevel = DMDUtil_LogLevel_INFO;
m_logCallback = nullptr;
memset(&m_pupTriggerCallbackContext, 0, sizeof(m_pupTriggerCallbackContext));
}

} // namespace DMDUtil
31 changes: 20 additions & 11 deletions src/DMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ void PUPDMDCALLBACK PUPDMDLogCallback(const char* format, va_list args, const vo
char buffer[1024];
vsnprintf(buffer, sizeof(buffer), format, args);

Log("%s", buffer);
Log(DMDUtil_LogLevel_INFO, "%s", buffer);
}

void ZEDMDCALLBACK ZeDMDLogCallback(const char* format, va_list args, const void* pUserData)
{
char buffer[1024];
vsnprintf(buffer, sizeof(buffer), format, args);

Log("%s", buffer);
Log(DMDUtil_LogLevel_INFO, "%s", buffer);
}

bool DMD::m_finding = false;
Expand Down Expand Up @@ -169,12 +169,14 @@ bool DMD::ConnectDMDServer()
{
Config* const pConfig = Config::GetInstance();
sockpp::initialize();
Log("Connecting DMDServer on %s:%d", pConfig->GetDMDServerAddr(), pConfig->GetDMDServerPort());
Log(DMDUtil_LogLevel_INFO, "Connecting DMDServer on %s:%d", pConfig->GetDMDServerAddr(),
pConfig->GetDMDServerPort());
m_pDMDServerConnector =
new sockpp::tcp_connector({pConfig->GetDMDServerAddr(), (in_port_t)pConfig->GetDMDServerPort()});
if (!m_pDMDServerConnector)
{
Log("DMDServer connection to %s:%d failed!", pConfig->GetDMDServerAddr(), pConfig->GetDMDServerPort());
Log(DMDUtil_LogLevel_INFO, "DMDServer connection to %s:%d failed!", pConfig->GetDMDServerAddr(),
pConfig->GetDMDServerPort());
}
}
return (m_pDMDServerConnector);
Expand Down Expand Up @@ -668,7 +670,7 @@ void DMD::ZeDMDThread()
}
m_serumMutex.unlock();

if (triggerID > 0) handleTrigger(triggerID);
if (triggerID > 0) HandleTrigger(triggerID);
}
else
{
Expand Down Expand Up @@ -831,7 +833,7 @@ void DMD::PixelcadeDMDThread()
m_pUpdateBufferQueue[bufferPosition]->height, &triggerID);
m_serumMutex.unlock();

if (triggerID > 0) handleTrigger(triggerID);
if (triggerID > 0) HandleTrigger(triggerID);
}
else
{
Expand Down Expand Up @@ -1006,7 +1008,7 @@ void DMD::RGB24DMDThread()
m_pUpdateBufferQueue[bufferPosition]->height, &triggerID);
m_serumMutex.unlock();

if (triggerID > 0) handleTrigger(triggerID);
if (triggerID > 0) HandleTrigger(triggerID);
}
else
{
Expand Down Expand Up @@ -1425,17 +1427,24 @@ void DMD::PupDMDThread()
free(pFrame);
}

if (triggerID > 0) handleTrigger(triggerID);
if (triggerID > 0) HandleTrigger(triggerID);
}
}
}
}
}

void DMD::handleTrigger(uint16_t id)
void DMD::HandleTrigger(uint16_t id)
{
Log("PUP Trigger D%d", id);
// @todo
static Config* pConfig = Config::GetInstance();

Log(DMDUtil_LogLevel_DEBUG, "HandleTrigger: id=D%d", id);

DMDUtil_PUPTriggerCallbackContext callbackContext = pConfig->GetPUPTriggerCallbackContext();
if (callbackContext.callback != nullptr)
{
(*callbackContext.callback)(id, callbackContext.pUserData);
}
}

} // namespace DMDUtil
10 changes: 6 additions & 4 deletions src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
namespace DMDUtil
{

void Log(const char *format, ...)
void Log(DMDUtil_LogLevel logLevel, const char* format, ...)
{
DMDUtil_LogCallback logCallback = Config::GetInstance()->GetLogCallback();
static Config* pConfig = pConfig->GetInstance();

if (!logCallback) return;
DMDUtil_LogCallback logCallback = pConfig->GetLogCallback();

if (!logCallback || logLevel < pConfig->GetLogLevel()) return;

va_list args;
va_start(args, format);
(*(logCallback))(format, args);
(*(logCallback))(logLevel, format, args);
va_end(args);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Logger.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include "DMDUtil/Config.h"

namespace DMDUtil
{

void Log(const char *format, ...);
void Log(DMDUtil_LogLevel logLevel, const char* format, ...);

}
28 changes: 14 additions & 14 deletions src/PixelcadeDMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ PixelcadeDMD* PixelcadeDMD::Connect(const char* pDevice, int matrix, int width,

if (pDevice && *pDevice != 0)
{
Log("Connecting to Pixelcade on %s...", pDevice);
Log(DMDUtil_LogLevel_INFO, "Connecting to Pixelcade on %s...", pDevice);

pPixelcadeDMD = Open(pDevice, matrix, width, height);

if (!pPixelcadeDMD) Log("Unable to connect to Pixelcade on %s", pDevice);
if (!pPixelcadeDMD) Log(DMDUtil_LogLevel_INFO, "Unable to connect to Pixelcade on %s", pDevice);
}
else
{
Log("Searching for Pixelcade...");
Log(DMDUtil_LogLevel_INFO, "Searching for Pixelcade...");

struct sp_port** ppPorts;
enum sp_return result = sp_list_ports(&ppPorts);
Expand All @@ -75,7 +75,7 @@ PixelcadeDMD* PixelcadeDMD::Connect(const char* pDevice, int matrix, int width,
sp_free_port_list(ppPorts);
}

if (!pPixelcadeDMD) Log("Unable to find Pixelcade");
if (!pPixelcadeDMD) Log(DMDUtil_LogLevel_INFO, "Unable to find Pixelcade");
}

return pPixelcadeDMD;
Expand Down Expand Up @@ -116,16 +116,16 @@ PixelcadeDMD* PixelcadeDMD::Open(const char* pDevice, int matrix, int width, int
{
sp_close(pSerialPort);
sp_free_port(pSerialPort);
// Log("Pixelcade expected new connection to return 0x0, but got 0x%02d", response[0]);
// Log(DMDUtil_LogLevel_INFO, "Pixelcade expected new connection to return 0x0, but got 0x%02d", response[0]);
return nullptr;
}

if (response[1] != 'I' || response[2] != 'O' || response[3] != 'I' || response[4] != 'O')
{
sp_close(pSerialPort);
sp_free_port(pSerialPort);
// Log("Pixelcade expected magic code to equal IOIO but got %c%c%c%c", response[1], response[2], response[3],
// response[4]);
// Log(DMDUtil_LogLevel_INFO, "Pixelcade expected magic code to equal IOIO but got %c%c%c%c", response[1],
// response[2], response[3], response[4]);
return nullptr;
}

Expand All @@ -138,8 +138,8 @@ PixelcadeDMD* PixelcadeDMD::Open(const char* pDevice, int matrix, int width, int
char firmware[9] = {0};
memcpy(firmware, response + 21, 8);

Log("Pixelcade found: device=%s, Hardware ID=%s, Bootloader ID=%s, Firmware=%s", pDevice, hardwareId, bootloaderId,
firmware);
Log(DMDUtil_LogLevel_INFO, "Pixelcade found: device=%s, Hardware ID=%s, Bootloader ID=%s, Firmware=%s", pDevice,
hardwareId, bootloaderId, firmware);

return new PixelcadeDMD(pSerialPort, matrix, width, height);
}
Expand Down Expand Up @@ -171,7 +171,7 @@ void PixelcadeDMD::Run()
m_pThread = new std::thread(
[this]()
{
Log("PixelcadeDMD run thread starting");
Log(DMDUtil_LogLevel_INFO, "PixelcadeDMD run thread starting");
EnableRgbLedMatrix(4, 16);

int errors = 0;
Expand Down Expand Up @@ -212,23 +212,23 @@ void PixelcadeDMD::Run()
{
if (errors > 0)
{
Log("Communication to Pixelcade restored after %d frames", errors);
Log(DMDUtil_LogLevel_INFO, "Communication to Pixelcade restored after %d frames", errors);
errors = 0;
}
}
else if (response == 0)
{
if (errors++ > PIXELCADE_MAX_NO_RESPONSE)
{
Log("Error while transmitting to Pixelcade: no response for the past %d frames",
Log(DMDUtil_LogLevel_INFO, "Error while transmitting to Pixelcade: no response for the past %d frames",
PIXELCADE_MAX_NO_RESPONSE);
m_running = false;
}
}
else if (response == SP_ERR_FAIL)
{
char* pMessage = sp_last_error_message();
Log("Error while transmitting to Pixelcade: %s", pMessage);
Log(DMDUtil_LogLevel_INFO, "Error while transmitting to Pixelcade: %s", pMessage);
sp_free_error_message(pMessage);
m_running = false;
}
Expand All @@ -251,7 +251,7 @@ void PixelcadeDMD::Run()

m_pSerialPort = nullptr;

Log("PixelcadeDMD run thread finished");
Log(DMDUtil_LogLevel_INFO, "PixelcadeDMD run thread finished");
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/Serum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Serum* Serum::Load(const char* const altColorPath, const char* const romName)
return nullptr;
}

Log("Serum loaded: romName=%s, width=%d, height=%d, numColors=%d, numTriggers=%d", romName, width, height, numColors,
numTriggers);
Log(DMDUtil_LogLevel_INFO, "Serum loaded: romName=%s, width=%d, height=%d, numColors=%d, numTriggers=%d", romName,
width, height, numColors, numTriggers);

m_isLoaded = true;

Expand Down
Loading

0 comments on commit 6045a3e

Please sign in to comment.