Skip to content

Commit

Permalink
perf: convert all buffers in CanvasClient to static buffers and merge…
Browse files Browse the repository at this point in the history
… color and code buffers into a union

Signed-off-by: Aditya Agarwal <[email protected]>
  • Loading branch information
Aditya-A-garwal committed Feb 14, 2024
1 parent 37cc7d9 commit 90034bc
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 62 deletions.
62 changes: 51 additions & 11 deletions include/canvasClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,66 @@ class RowCompressor {

class CanvasClient {

constexpr static int DISPLAYBUF_LEN = 512;
constexpr static unsigned BUFFER_LEN = 4096;
constexpr static unsigned CLIENT_BUFFER_LEN = 4096;
constexpr static unsigned ROW_BUFFER_LEN = 512;

constexpr static uint16_t MAX_SSID_LEN = 64;
constexpr static uint16_t MAX_PASS_LEN = 64;
constexpr static unsigned MAX_SSID_LEN = 64;
constexpr static unsigned MAX_PASS_LEN = 64;

constexpr static uint16_t IP_LEN = 15;
constexpr static unsigned IP_LEN = 15;

constexpr static unsigned BUFFER_LEN = 4096;
struct client_buffer_t {

uint8_t bytes[BUFFER_LEN];
unsigned size = 0;
unsigned totalSent = 0;

bool has_space(unsigned insertsize) {

return (size + insertsize) <= CLIENT_BUFFER_LEN;

if ((size + insertsize) >= CLIENT_BUFFER_LEN) {
return false;
}
}

void append(const uint8_t *insertbytes, unsigned insertsize) {

memcpy(&bytes[size], insertbytes, insertsize);
size += insertsize;
}

bool flush(WiFiClient *client) {

if (size == 0) {
return true;
}

unsigned sent = client->write(bytes, size);
if (sent != size) {
return false;
}
totalSent += sent;

size = 0;
return true;
}
};

union row_buffer_t {
uint16_t color[ROW_BUFFER_LEN];
uint8_t code[ROW_BUFFER_LEN];
};
struct buffer {

uint8_t bytes[BUFFER_LEN] {};
unsigned used = 0;
};

static buffer buf;
// static buffer buf;
static client_buffer_t client_buffer;
static row_buffer_t rowbuf;

Canvas *canvas;

Expand All @@ -62,13 +107,8 @@ class CanvasClient {
char serverIP[IP_LEN];
uint16_t serverPort;

int status = WL_IDLE_STATUS;

WiFiClient client;

uint16_t colorBuffer[512];
uint8_t codeBuffer[512];

public:

enum ConnectionStatus {
Expand Down
81 changes: 30 additions & 51 deletions src/canvasClient.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "canvasClient.h"

CanvasClient::buffer CanvasClient::buf;

bool RowCompressor::compress(uint8_t *codes, uint16_t count) {

numSegments = 0;
Expand Down Expand Up @@ -44,6 +42,10 @@ uint16_t* RowCompressor::getSegmentArray() {
return segments;
}


CanvasClient::client_buffer_t CanvasClient::client_buffer;
CanvasClient::row_buffer_t CanvasClient::rowbuf;

CanvasClient::CanvasClient(Canvas *canvas)
: canvas {canvas}
{}
Expand All @@ -66,6 +68,8 @@ void CanvasClient::setServer(const char serverIp[], const uint16_t port) {

CanvasClient::ConnectionStatus CanvasClient::connect(uint16_t maxAttempt) {

signed status = WL_IDLE_STATUS;

if (WiFi.status() == WL_NO_MODULE) {
return NO_MODULE;
}
Expand Down Expand Up @@ -119,11 +123,11 @@ void CanvasClient::loadCanvas(uint8_t id) {
return;
}

colorBuffer[col] = code2color(client.read());
rowbuf.color[col] = code2color(client.read());
}

for (uint16_t j = 0; j < imageWidth; ++j) {
canvas->writePixel(row + 1, j + 1, colorBuffer[j]);
canvas->writePixel(row + 1, j + 1, rowbuf.color[j]);
}
}

Expand Down Expand Up @@ -153,7 +157,6 @@ void CanvasClient::saveCanvas(uint8_t id) {
unsigned flushTime = 0;

unsigned totalTime = 0;
unsigned sendAmt = 0;

x = micros();
if (client.connect(IPAddress(serverIP), serverPort) == 0) {
Expand All @@ -163,91 +166,67 @@ void CanvasClient::saveCanvas(uint8_t id) {
connectionTime += micros() - x;

x = micros();
buf.bytes[0] = '\x01';
buf.bytes[1] = id;

buf.bytes[2] = imageHeight & 0xFF;
buf.bytes[3] = (imageHeight >> 8) & 0xFF;

buf.bytes[4] = imageWidth & 0xFF;
buf.bytes[5] = (imageWidth >> 8) & 0xFF;

buf.used = 6;
client_buffer.append((const uint8_t *)"\x01", 1);
client_buffer.append(&id, 1);

client_buffer.append((const uint8_t *)&imageHeight, 2);
client_buffer.append((const uint8_t *)&imageWidth, 2);
headerTime = micros() - x;

for (uint16_t i = 0; i < imageHeight; ++i) {

x = micros();
for (unsigned j = 0; j < imageWidth; ++j) {
codeBuffer[j] = color2code(canvas->readPixel(i + 1, j + 1));
rowbuf.code[j] = color2code(canvas->readPixel(i + 1, j + 1));
}
readTime += micros() - x;

x = micros();
bool status = compressed.compress(codeBuffer, imageWidth);
bool compressable = compressed.compress(rowbuf.code, imageWidth);
compressionTime += micros() - x;

if (status) {
if (compressable) {

x = micros();

uint8_t numSegments = compressed.getNumSegments();
unsigned size = numSegments * sizeof(uint16_t);

if (buf.used+size+1 >= BUFFER_LEN) {

int _ = client.write(buf.bytes, buf.used);
if (_ != buf.used) {
if (!client_buffer.has_space(1 + size)) {
if (!client_buffer.flush(&client)) {
client.stop();
Serial.println("Error");
Serial.println("error");
return;
}
buf.used = 0;

sendAmt += _;
}

buf.bytes[buf.used++] = numSegments;
memcpy(&(buf.bytes[buf.used]), (uint8_t *)(compressed.getSegmentArray()), size);
buf.used += size;
client_buffer.append(&numSegments, 1);
client_buffer.append((const uint8_t *)compressed.getSegmentArray(), size);

compressedTxTime += micros() - x;
}
else {

x = micros();

if (imageWidth+buf.used+1 >= BUFFER_LEN) {

int _ = client.write(buf.bytes, buf.used);
if (_ != buf.used) {
if (!client_buffer.has_space(1 + imageWidth)) {
if (!client_buffer.flush(&client)) {
client.stop();
Serial.println("Error");
Serial.println("error");
return;
}
buf.used = 0;

sendAmt += _;
}

buf.bytes[buf.used++] = '\x00';
memcpy(&(buf.bytes[buf.used]), codeBuffer, imageWidth);
buf.used += imageWidth;
client_buffer.append((const uint8_t *)"\x00", 1);
client_buffer.append(rowbuf.code, imageWidth);

rawTxTime += micros() - x;
}
}

x = micros();
if (buf.used != 0) {
int _ = client.write(buf.bytes, buf.used);
if (_ != buf.used) {
client.stop();
Serial.println("Error");
return;
}
sendAmt += buf.used;
if (!client_buffer.flush(&client)) {
client.stop();
Serial.println("error");
return;
}
rawTxTime += micros() - x;

Expand All @@ -274,6 +253,6 @@ void CanvasClient::saveCanvas(uint8_t id) {
Serial.println();
Serial.print("Total Time: "); Serial.print(totalTime / 1000); Serial.println(" ms");
Serial.println();
Serial.print("Bytes sent: "); Serial.println(sendAmt);
Serial.print("Bytes sent: "); Serial.println(client_buffer.totalSent); client_buffer.totalSent = 0;
Serial.println();
}

0 comments on commit 90034bc

Please sign in to comment.