Skip to content

Commit

Permalink
WiFi performance
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalkbrenner committed Dec 13, 2023
1 parent 1ab5862 commit 0dfa996
Show file tree
Hide file tree
Showing 7 changed files with 1,013 additions and 107 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ set(MINIZ_SOURCES
src/miniz/miniz.c
)

set(KOMIHASH_SOURCES
src/komihash/komihash.h
)

if(NOT EXISTS ${SERIALPORT_LIBRARIES})
get_filename_component(SERIALPORT_CMAKE ../libserialport/CMakeLists.txt ABSOLUTE)
if(EXISTS ${SERIALPORT_CMAKE})
Expand All @@ -69,6 +73,7 @@ endif()

set(LIBRARY_SOURCES
${MINIZ_SOURCES}
${KOMIHASH_SOURCES}
src/ZeDMDComm.h
src/ZeDMDComm.cpp
src/ZeDMDWiFi.h
Expand Down
134 changes: 80 additions & 54 deletions src/ZeDMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ void ZeDMD::SetBrightness(int brightness)
void ZeDMD::SaveSettings()
{
m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::SaveSettings);
// Avoid that client resets the device before settings are saved.
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
}

void ZeDMD::EnablePreDownscaling()
Expand Down Expand Up @@ -153,14 +155,31 @@ void ZeDMD::DisableUpscaling()
m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::DisableUpscaling);
}

void ZeDMD::SetWiFiSSID(const char* ssid)
void ZeDMD::SetWiFiSSID(const char* const ssid)
{
m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::SetWiFiSSID, (uint8_t*)ssid, strlen(ssid));
int size = strlen(ssid);
uint8_t data[129] = {0};
data[0] = (uint8_t)size;
memcpy(&data[1], (uint8_t*)ssid, size);
m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::SetWiFiSSID, data, 129);
}

void ZeDMD::SetWiFiPassword(const char* password)
void ZeDMD::SetWiFiPassword(const char* const password)
{
m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::SetWiFiPassword, (uint8_t*)password, strlen(password));
int size = strlen(password);
uint8_t data[129] = { 0 };
data[0] = (uint8_t)size;
memcpy(&data[1], (uint8_t*)password, size);
m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::SetWiFiPassword, data, 129);
}

void ZeDMD::SetWiFiPort(int port)
{
uint8_t data[2];
data[0] = (uint8_t)(port >> 8 & 0xFF);
data[1] = (uint8_t)(port & 0xFF);

m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::SetWiFiPort, data, 2);
}

bool ZeDMD::OpenWiFi(const char *ip, int port)
Expand Down Expand Up @@ -211,29 +230,29 @@ bool ZeDMD::Open(int width, int height)

void ZeDMD::SetPalette(uint8_t *pPalette)
{
m_paletteChanged = false;
if (memcmp(&m_palette, pPalette, 192))
{
SetPalette(pPalette, 64);
m_paletteChanged = true;
}
SetPalette(pPalette, 64);
}

void ZeDMD::SetPalette(uint8_t* pPalette, int numColors)
{
memcpy(&m_palette, pPalette, numColors * 3);
m_paletteChanged = false;
if (memcmp(&m_palette, pPalette, numColors * 3))
{
memcpy(&m_palette, pPalette, numColors * 3);
m_paletteChanged = true;
}
}

void ZeDMD::SetDefaultPalette(int bitDepth)
{
switch (bitDepth)
{
case 2:
SetPalette(m_DmdDefaultPalette2Bit);
SetPalette(m_DmdDefaultPalette2Bit, 4);
break;

default:
SetPalette(m_DmdDefaultPalette4Bit);
SetPalette(m_DmdDefaultPalette4Bit, 16);
}
}

Expand All @@ -257,7 +276,7 @@ void ZeDMD::ClearScreen()

void ZeDMD::RenderGray2(uint8_t *pFrame)
{
if (!(m_usb || m_wifi) || !UpdateFrameBuffer8(pFrame))
if (!(m_usb || m_wifi) || !(UpdateFrameBuffer8(pFrame) || m_paletteChanged))
{
return;
}
Expand Down Expand Up @@ -287,21 +306,29 @@ void ZeDMD::RenderGray2(uint8_t *pFrame)

void ZeDMD::RenderGray4(uint8_t *pFrame)
{
if (!(m_usb || m_wifi) || !UpdateFrameBuffer8(pFrame))
{
return;
}
if (!(m_usb || m_wifi) || !(UpdateFrameBuffer8(pFrame) || m_paletteChanged))
{
return;
}

int width;
int height;
int width;
int height;

int bufferSize = Scale(m_pScaledFrameBuffer, m_pFrameBuffer, 1, &width, &height) / 8 * 4;
Split(m_pPlanes, width, height, 4, m_pScaledFrameBuffer);
int bufferSize = Scale(m_pScaledFrameBuffer, m_pFrameBuffer, 1, &width, &height) / 8 * 4;
if (m_usb)
{
Split(m_pPlanes, width, height, 4, m_pScaledFrameBuffer);

memcpy(m_pCommandBuffer, m_palette, 48);
memcpy(m_pCommandBuffer + 48, m_pPlanes, bufferSize);
memcpy(m_pCommandBuffer, m_palette, 48);
memcpy(m_pCommandBuffer + 48, m_pPlanes, bufferSize);

m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::ColGray4, m_pCommandBuffer, 48 + bufferSize);
m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::ColGray4, m_pCommandBuffer, 48 + bufferSize);
}
else if (m_wifi)
{
ConvertToRgb24(m_pPlanes, m_pScaledFrameBuffer, bufferSize);
m_pZeDMDWiFi->QueueCommand(ZEDMD_WIFI_COMMAND::UDP_RGB24, m_pPlanes, bufferSize * 3, width, height);
}
}

void ZeDMD::RenderColoredGray6(uint8_t *pFrame, uint8_t *pPalette, uint8_t *pRotations)
Expand All @@ -312,43 +339,42 @@ void ZeDMD::RenderColoredGray6(uint8_t *pFrame, uint8_t *pPalette, uint8_t *pRot

void ZeDMD::RenderColoredGray6(uint8_t *pFrame, uint8_t *pRotations)
{
if (!m_usb && !m_wifi)
return;
if (!(m_usb || m_wifi) || !(UpdateFrameBuffer8(pFrame) || m_paletteChanged))
return;

if (UpdateFrameBuffer8(pFrame) || m_paletteChanged)
{
int width;
int height;
int width;
int height;

int bufferSize = Scale(m_pScaledFrameBuffer, m_pFrameBuffer, 1, &width, &height);
if (m_usb)
{
Split(m_pPlanes, width, height, 6, m_pScaledFrameBuffer);
int bufferSize = Scale(m_pScaledFrameBuffer, m_pFrameBuffer, 1, &width, &height);
if (m_usb)
{
Split(m_pPlanes, width, height, 6, m_pScaledFrameBuffer);

bufferSize = bufferSize / 8 * 6;
bufferSize = bufferSize / 8 * 6;

memcpy(m_pCommandBuffer, m_palette, 192);
memcpy(m_pCommandBuffer + 192, m_pPlanes, bufferSize);
memcpy(m_pCommandBuffer, m_palette, 192);
memcpy(m_pCommandBuffer + 192, m_pPlanes, bufferSize);

if (pRotations)
memcpy(m_pCommandBuffer + 192 + bufferSize, pRotations, 24);
else
memset(m_pCommandBuffer + 192 + bufferSize, 255, 24);
if (pRotations)
memcpy(m_pCommandBuffer + 192 + bufferSize, pRotations, 24);
else
memset(m_pCommandBuffer + 192 + bufferSize, 255, 24);

m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::ColGray6, m_pCommandBuffer, 192 + bufferSize + 24);
}
else if (m_wifi)
{
ConvertToRgb24(m_pPlanes, m_pScaledFrameBuffer, bufferSize, m_palette);
m_pZeDMDWiFi->QueueCommand(ZEDMD_WIFI_COMMAND::UDP_RGB24, m_pPlanes, bufferSize * 3, width, height);
}
}
m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::ColGray6, m_pCommandBuffer, 192 + bufferSize + 24);
}
else if (m_wifi)
{
ConvertToRgb24(m_pPlanes, m_pScaledFrameBuffer, bufferSize, m_palette);
m_pZeDMDWiFi->QueueCommand(ZEDMD_WIFI_COMMAND::UDP_RGB24, m_pPlanes, bufferSize * 3, width, height);
}
}

void ZeDMD::RenderRgb24(uint8_t *pFrame)
{
if (!m_usb || !UpdateFrameBuffer24(pFrame))
return;
if (!(m_usb || m_wifi) || !UpdateFrameBuffer24(pFrame))
{
return;
}

int width;
int height;
Expand All @@ -357,11 +383,11 @@ void ZeDMD::RenderRgb24(uint8_t *pFrame)

if (m_usb)
{
m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::RGB24, m_pFrameBuffer, bufferSize);
m_pZeDMDComm->QueueCommand(ZEDMD_COMM_COMMAND::RGB24, m_pCommandBuffer, bufferSize);
}
else if (m_wifi)
{
m_pZeDMDWiFi->QueueCommand(ZEDMD_WIFI_COMMAND::UDP_RGB24, m_pFrameBuffer, bufferSize, width, height);
m_pZeDMDWiFi->QueueCommand(ZEDMD_WIFI_COMMAND::UDP_RGB24, m_pCommandBuffer, bufferSize, width, height);
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/ZeDMD.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ class ZEDMDAPI ZeDMD
void DisablePreUpscaling();
void EnableUpscaling();
void DisableUpscaling();
void SetWiFiSSID(const char* ssid);
void SetWiFiPassword(const char* password);
void SetWiFiSSID(const char* const ssid);
void SetWiFiPassword(const char* const password);
void SetWiFiPort(int port);

void ClearScreen();
void RenderGray2(uint8_t *frame);
Expand Down Expand Up @@ -142,8 +143,9 @@ extern ZEDMDAPI void ZeDMD_EnablePreUpscaling(ZeDMD* pZeDMD) { return pZeDMD->En
extern ZEDMDAPI void ZeDMD_DisablePreUpscaling(ZeDMD* pZeDMD) { return pZeDMD->DisablePreUpscaling(); };
extern ZEDMDAPI void ZeDMD_EnableUpscaling(ZeDMD* pZeDMD) { return pZeDMD->EnableUpscaling(); };
extern ZEDMDAPI void ZeDMD_DisableUpscaling(ZeDMD* pZeDMD) { return pZeDMD->DisableUpscaling(); };
extern ZEDMDAPI void ZeDMD_SetWiFiSSID(ZeDMD* pZeDMD, const char* ssid) { return pZeDMD->SetWiFiSSID(ssid); };
extern ZEDMDAPI void ZeDMD_SetWiFiPassword(ZeDMD* pZeDMD, const char* password) { return pZeDMD->SetWiFiPassword(password); };
extern ZEDMDAPI void ZeDMD_SetWiFiSSID(ZeDMD* pZeDMD, const char* const ssid) { return pZeDMD->SetWiFiSSID(ssid); };
extern ZEDMDAPI void ZeDMD_SetWiFiPassword(ZeDMD* pZeDMD, const char* const password) { return pZeDMD->SetWiFiPassword(password); };
extern ZEDMDAPI void ZeDMD_SetWiFiPort(ZeDMD* pZeDMD, int port) { return pZeDMD->SetWiFiPort(port); };

extern ZEDMDAPI void ZeDMD_ClearScreen(ZeDMD* pZeDMD) { return pZeDMD->ClearScreen(); };
extern ZEDMDAPI void ZeDMD_RenderGray2(ZeDMD* pZeDMD, uint8_t *frame) { return pZeDMD->RenderGray2(frame); };
Expand Down
1 change: 1 addition & 0 deletions src/ZeDMDComm.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef enum
EnableFlowControlV2 = 0x1a,
SetWiFiSSID = 0x1b,
SetWiFiPassword = 0x1c,
SetWiFiPort = 0x1d,
SaveSettings = 0x1e,
Reset = 0x1f,
GetVersionBytes = 0x20,
Expand Down
Loading

0 comments on commit 0dfa996

Please sign in to comment.