added mqtt and cleanup

This commit is contained in:
2024-11-30 16:48:48 +01:00
parent 5a9bd1d57b
commit 31deb137f0
5 changed files with 75 additions and 26 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.vscode
secrets.h

View File

@@ -1,4 +1,5 @@
#include "LEDMatrix.h" #include "LEDMatrix.h"
#include "font.h"
LEDMatrix::LEDMatrix(uint8_t width, uint8_t height, uint16_t numLeds, uint16_t lastVisibleLed, CRGB* leds) LEDMatrix::LEDMatrix(uint8_t width, uint8_t height, uint16_t numLeds, uint16_t lastVisibleLed, CRGB* leds)
: kMatrixWidth(width), kMatrixHeight(height), NUM_LEDS(numLeds), LAST_VISIBLE_LED(lastVisibleLed), leds(leds) { : kMatrixWidth(width), kMatrixHeight(height), NUM_LEDS(numLeds), LAST_VISIBLE_LED(lastVisibleLed), leds(leds) {
@@ -41,3 +42,28 @@ void LEDMatrix::drawPixel(uint8_t x, uint8_t y, CRGB color) {
leds[index] = color; leds[index] = color;
} }
} }
void LEDMatrix::drawChar(char c, int x, int y, CRGB color) {
if (c < 32 || c > 127) return; // Ignore non-printable characters
for (int i = 0; i < 5; i++) {
uint8_t line = font[(c - 32) * 5 + i]; // Access the font array as a one-dimensional array
for (int j = 0; j < 7; j++) {
if (line & 0x1) {
drawPixel(x + i, y + j, color);
}
line >>= 1;
}
}
}
void LEDMatrix::drawText(const char* text, int x, int y, CRGB color) {
while (*text) {
drawChar(*text++, x, y, color);
x += 6; // Move to the next character position
if (x >= kMatrixWidth) { // Handle overflow to the next screen
x = 0;
y += 8; // Move to the next line
}
}
}

View File

@@ -9,6 +9,8 @@ public:
LEDMatrix(uint8_t width, uint8_t height, uint16_t numLeds, uint16_t lastVisibleLed, CRGB* leds); LEDMatrix(uint8_t width, uint8_t height, uint16_t numLeds, uint16_t lastVisibleLed, CRGB* leds);
uint16_t XY(uint8_t x, uint8_t y); uint16_t XY(uint8_t x, uint8_t y);
void drawPixel(uint8_t x, uint8_t y, CRGB color); void drawPixel(uint8_t x, uint8_t y, CRGB color);
void drawChar(char c, int x, int y, CRGB color);
void drawText(const char* text, int x, int y, CRGB color);
private: private:
const uint8_t kMatrixWidth; const uint8_t kMatrixWidth;

0
main/conf.h Normal file
View File

View File

@@ -1,6 +1,9 @@
#include <FastLED.h> #include <FastLED.h>
#include "LEDMatrix.h" #include "LEDMatrix.h"
#include "font.h"
#include <WiFi.h>
#include <MQTT.h>
#include "secrets.h"
// Params for width and height // Params for width and height
const uint8_t kMatrixWidth = 32; const uint8_t kMatrixWidth = 32;
@@ -13,42 +16,58 @@ const uint8_t kMatrixHeight = 16;
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
LEDMatrix matrix(kMatrixWidth, kMatrixHeight, NUM_LEDS, LAST_VISIBLE_LED, leds); LEDMatrix matrix(kMatrixWidth, kMatrixHeight, NUM_LEDS, LAST_VISIBLE_LED, leds);
WiFiClient net;
MQTTClient client;
void setup() { void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS); FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(40); FastLED.setBrightness(40);
FastLED.clear(); FastLED.clear();
FastLED.show(); FastLED.show();
}
void drawChar(char c, int x, int y, CRGB color) { WiFi.begin(ssid, password);
if (c < 32 || c > 127) return; // Ignore non-printable characters client.begin("192.168.178.155", 1883, net);
client.onMessage(messageReceived);
for (int i = 0; i < 5; i++) { connect();
uint8_t line = font[(c - 32) * 5 + i]; // Access the font array as a one-dimensional array
for (int j = 0; j < 7; j++) {
if (line & 0x1) {
matrix.drawPixel(x + i, y + j, color);
}
line >>= 1;
}
}
}
//TODO: Fix letter overflow
void drawText(const char* text, int x, int y, CRGB color) {
while (*text) {
drawChar(*text++, x, y, color);
x += 6; // Move to the next character position
if (x >= kMatrixWidth) { // Handle overflow to the next screen
x = 0;
y += 8; // Move to the next line
}
}
} }
void loop() { void loop() {
FastLED.clear(); client.loop();
drawText("abwawawawaa", 0, 0, CRGB::White); //reconnect if it loses connection with mqtt broker
FastLED.show(); if (!client.connected()) {
connect();
}
}
//mqtt stuffs
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000); delay(1000);
} }
Serial.print("\nconnecting...");
while (!client.connect("espMatrix")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe("Matrix/Text");
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
FastLED.clear();
matrix.drawText(payload.c_str(), 0, 0, CRGB::White);
FastLED.show();
// Note: Do not use the client in the callback to publish, subscribe or
// unsubscribe as it may cause deadlocks when other things arrive while
// sending and receiving acknowledgments. Instead, change a global variable,
// or push to a queue and handle it in the loop after calling `client.loop()`.
}