Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions rgboard/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Compiler settings
CXX = g++
CXXFLAGS = -Wall -O3 -g -Wextra -Wno-unused-parameter

# Source files and executable
SRCS = src/main.cpp src/display.cpp src/queue-client.cpp
OBJECTS = $(SRCS:.cpp=.o)
BINARIES = rgboard

# Where our RGB library resides
RGB_LIB_DISTRIBUTION = ..
RGB_INCDIR = $(RGB_LIB_DISTRIBUTION)/include
RGB_LIBDIR = $(RGB_LIB_DISTRIBUTION)/lib
RGB_LIBRARY_NAME = rgbmatrix
RGB_LIBRARY = $(RGB_LIBDIR)/lib$(RGB_LIBRARY_NAME).a

# Linker flags: add libcurl and jsoncpp
LDFLAGS += -L$(RGB_LIBDIR) -l$(RGB_LIBRARY_NAME) -lcurl -ljsoncpp -lrt -lm -lpthread

# Default target
all: $(BINARIES)

# Compile .o files
src/%.o: src/%.cpp
$(CXX) $(CXXFLAGS) -Iinclude -I$(RGB_INCDIR) -c -o $@ $<

# Link final binary
rgboard: $(OBJECTS) $(RGB_LIBRARY)
$(CXX) $(CXXFLAGS) -o $@ $(OBJECTS) $(LDFLAGS)

# Clean up
clean:
rm -f src/*.o $(BINARIES)

.PHONY: all clean
15 changes: 15 additions & 0 deletions rgboard/include/display.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Created by evalentin on 09/03/25.
//

#ifndef DISPLAY_H
#define DISPLAY_H

#include "led-matrix.h"

#include <jsoncpp/json/json.h> // JSON parsing

using rgb_matrix::RGBMatrix;
using rgb_matrix::Canvas;
void DrawDesignOnCanvas(Canvas* canvas, const Json::Value& pixel_data);
#endif //DISPLAY_H
30 changes: 30 additions & 0 deletions rgboard/include/queue-client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Created by evalentin on 01/05/25.
//

#ifndef QUEUE_CLIENT_H
#define QUEUE_CLIENT_H
#include <string>
#include <jsoncpp/json/json.h>

class QueueClient
{
public:
QueueClient(std::string email, std::string password);
bool GetDesign();
[[nodiscard]] int GetDisplayDuration() const;
Json::Value GetPixelData();

private:
std::string jwt;
std::string email;
std::string password;

// We store in these variables and get from here. Can't return a string and an int in the same function.
int display_duration{};
Json::Value pixel_data;

std::string GetJWT();
static size_t CurlWriteCallback(void* contents, size_t size, size_t nmemb, std::string* output);
};
#endif //QUEUE_CLIENT_H
58 changes: 58 additions & 0 deletions rgboard/src/display.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Created by evalentin on 09/03/25.
//

#include "../include/display.h"
#include <string> // std::string
#include <sstream> // std::stringstream, std::istringstream
#include <iostream> // std::cerr, std::cout


using rgb_matrix::RGBMatrix;
using rgb_matrix::Canvas;

void HexToRGB(const std::string& hex, int& r, int& g, int& b);

void DrawDesignOnCanvas(Canvas* canvas, const Json::Value& pixel_data)
{
constexpr int GRID_SIZE = 8; // pixel data is 8x scaled
canvas->Clear();

for (const auto& key : pixel_data.getMemberNames())
{
int x = 0, y = 0;
char comma;

std::stringstream coord_stream(key);
coord_stream >> x >> comma >> y;

// downscale
x /= GRID_SIZE;
y /= GRID_SIZE;

const std::string hex_color = pixel_data[key].asString();
int r, g, b;
HexToRGB(hex_color, r, g, b);

if (x >= 0 && x < canvas->width() && y >= 0 && y < canvas->height())
{
canvas->SetPixel(x, y, r, g, b);
}
}
}


// Convert hex string to RGB integers
void HexToRGB(const std::string& hex, int& r, int& g, int& b)
{
if (hex.length() == 7 && hex[0] == '#')
{
r = std::stoi(hex.substr(1, 2), nullptr, 16);
g = std::stoi(hex.substr(3, 2), nullptr, 16);
b = std::stoi(hex.substr(5, 2), nullptr, 16);
}
else
{
r = g = b = 0; // fallback to black
}
}
60 changes: 60 additions & 0 deletions rgboard/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Created by evalentin on 08/03/25.
//

#define ROWS 32
#define COLS 64
#define PARALLEL 2
#define HARDWARE "regular"
#define GPIO_SLOWDOWN 4

#include <unistd.h>

#include "queue-client.h"
#include "display.h"

int main(int argc, char* argv[])
{
// Matrix configuration
rgb_matrix::RGBMatrix::Options defaults;
rgb_matrix::RuntimeOptions runtime_options;

defaults.hardware_mapping = HARDWARE;
defaults.rows = ROWS;
defaults.cols = COLS;
defaults.parallel = PARALLEL;

runtime_options.gpio_slowdown = GPIO_SLOWDOWN;

// Initialize matrix
rgb_matrix::Canvas* canvas = rgb_matrix::CreateMatrixFromOptions(defaults, runtime_options);
if (canvas == nullptr)
{
std::fprintf(stderr, "Failed to initialize matrix.\n");
return 1;
}

// Authenticate with client
QueueClient client("email", "password");

// main loop: fetch + draw + wait
while (true)
{
if (client.GetDesign())
{
const Json::Value& pixel_data = client.GetPixelData();
int duration = client.GetDisplayDuration();

DrawDesignOnCanvas(canvas, pixel_data);
std::printf("Displaying design for %d seconds.\n", duration);
sleep(duration);
}
else
{
std::fprintf(stderr, "Failed to get design. Retrying in 5 seconds...\n");
sleep(5);
}
}

return 0;
}
Loading