added mqtt and cleanup
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.vscode
|
||||
secrets.h
|
@@ -1,4 +1,5 @@
|
||||
#include "LEDMatrix.h"
|
||||
#include "font.h"
|
||||
|
||||
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) {
|
||||
@@ -41,3 +42,28 @@ void LEDMatrix::drawPixel(uint8_t x, uint8_t y, CRGB 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
|
||||
}
|
||||
}
|
||||
}
|
@@ -9,6 +9,8 @@ public:
|
||||
LEDMatrix(uint8_t width, uint8_t height, uint16_t numLeds, uint16_t lastVisibleLed, CRGB* leds);
|
||||
uint16_t XY(uint8_t x, uint8_t y);
|
||||
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:
|
||||
const uint8_t kMatrixWidth;
|
||||
|
0
main/conf.h
Normal file
0
main/conf.h
Normal file
@@ -1,6 +1,9 @@
|
||||
#include <FastLED.h>
|
||||
#include "LEDMatrix.h"
|
||||
#include "font.h"
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <MQTT.h>
|
||||
#include "secrets.h"
|
||||
|
||||
// Params for width and height
|
||||
const uint8_t kMatrixWidth = 32;
|
||||
@@ -13,42 +16,58 @@ const uint8_t kMatrixHeight = 16;
|
||||
CRGB leds[NUM_LEDS];
|
||||
LEDMatrix matrix(kMatrixWidth, kMatrixHeight, NUM_LEDS, LAST_VISIBLE_LED, leds);
|
||||
|
||||
WiFiClient net;
|
||||
MQTTClient client;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
|
||||
FastLED.setBrightness(40);
|
||||
FastLED.clear();
|
||||
FastLED.show();
|
||||
}
|
||||
|
||||
void drawChar(char c, int x, int y, CRGB color) {
|
||||
if (c < 32 || c > 127) return; // Ignore non-printable characters
|
||||
WiFi.begin(ssid, password);
|
||||
client.begin("192.168.178.155", 1883, net);
|
||||
client.onMessage(messageReceived);
|
||||
|
||||
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) {
|
||||
matrix.drawPixel(x + i, y + j, color);
|
||||
}
|
||||
line >>= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
connect();
|
||||
|
||||
//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() {
|
||||
FastLED.clear();
|
||||
drawText("abwawawawaa", 0, 0, CRGB::White);
|
||||
FastLED.show();
|
||||
client.loop();
|
||||
//reconnect if it loses connection with mqtt broker
|
||||
if (!client.connected()) {
|
||||
connect();
|
||||
}
|
||||
}
|
||||
|
||||
//mqtt stuffs
|
||||
void connect() {
|
||||
Serial.print("checking wifi...");
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.print(".");
|
||||
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()`.
|
||||
}
|
Reference in New Issue
Block a user