Skip to content

Commit

Permalink
chore: total refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
trinami committed Mar 29, 2024
1 parent 719d104 commit 7f05e85
Show file tree
Hide file tree
Showing 6 changed files with 569 additions and 560 deletions.
39 changes: 39 additions & 0 deletions include/display.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef DISPLAY_H
#define DISPLAY_H

#include <Arduino.h>
#include "local_config.h"
#include "LEDMatrixDriver.hpp"
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>

uint8_t scrollbuffer[MAX_TEXT_LENGTH];

LEDMatrixDriver* led;
uint8_t texts[NUM_CHANNELS][MAX_TEXT_LENGTH];
uint8_t textcycle[MAX_TEXTCYCLE];
uint8_t num_textcycles = 1;
uint8_t current_cycle = 0;
uint8_t start_countdown = START_CYCLES;
uint8_t colIndex = 0;
uint16_t current_channel = 0;
uint16_t textIndex = 0;
uint16_t scrollWhitespace = 0;
uint16_t ledmatrix_width = INITIAL_LEDMATRIX_SEGMENTS * 8;
uint16_t blinkDelay = 0;
uint64_t marqueeCycleTimestamp = 0;
uint64_t marqueeDelayTimestamp = 0;
uint64_t marqueeBlinkTimestamp;

WiFiClient espClient;
PubSubClient client(espClient);

void getScrolltextFromBuffer(int channel);
void showStatus(char* text);
void nextChar();
void nextCol(uint8_t w);
void writeCol();
void marquee();

#endif
14 changes: 13 additions & 1 deletion include/local_config.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

// local configuration


// set to false if you do not want to publish anything:
const bool do_publishes = true;

Expand All @@ -25,3 +24,16 @@ const char* initialText = "no such text"; // Initial Text shown before t
#define MAX_TEXTCYCLE 32 // Maximum number of texts in one cycle
#define START_CYCLES 3 // Number of cycles for the start message
#define DEBUGPRINT 0 // set to 1 for debug prints
#define LOG_TELNET 0 // log over telnet

#ifndef TOPICROOT
#define TOPICROOT "ledMatrix"
#endif


#if LOG_TELNET
#include <TelnetStream.h>
#define LogTarget TelnetStream
#else
#define LogTarget Serial
#endif
12 changes: 12 additions & 0 deletions include/mqtt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef MQTT_H
#define MQTT_H

#include <Arduino.h>
#include "local_config.h"
#include <LEDMatrixDriver.hpp>
#include "display.h"

void printHex8(uint8_t *data, uint8_t length);
void mqttReceiveCallback(char* topic, byte* payload, unsigned int length);

#endif
134 changes: 134 additions & 0 deletions src/display.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include "display.h"

#include <LEDMatrixDriver.hpp>
#include "wmconfig.h"
#include "font.h"

uint8_t scrollbuffer[MAX_TEXT_LENGTH];

LEDMatrixDriver* led;
uint8_t texts[NUM_CHANNELS][MAX_TEXT_LENGTH];
uint8_t textcycle[MAX_TEXTCYCLE];
uint8_t num_textcycles = 1;
uint8_t current_cycle = 0;
uint8_t start_countdown = START_CYCLES;
uint8_t colIndex = 0;
uint16_t current_channel = 0;
uint16_t textIndex = 0;
uint16_t scrollWhitespace = 0;
uint16_t ledmatrix_width = INITIAL_LEDMATRIX_SEGMENTS * 8;
uint16_t blinkDelay = 0;
uint64_t marqueeCycleTimestamp = 0;
uint64_t marqueeDelayTimestamp = 0;
uint64_t marqueeBlinkTimestamp;

void getScrolltextFromBuffer(int channel)
{
LogTarget.print((String)"Show buffer " + channel + ": [");
bool eot = false;
for (int i = 0; i < MAX_TEXT_LENGTH; i++) {
if (eot) {
scrollbuffer[i] = 0;
} else {
uint8_t ch = texts[channel][i];
scrollbuffer[i] = ch;
if (i <= 30)
LogTarget.print(String(char(ch)));
if (!ch) {
eot = true;
LogTarget.println((String)"] (" + i + " bytes)");
}
}
}
scrollbuffer[MAX_TEXT_LENGTH - 1] = 0;
}

void showStatus(char* text)
{
Serial.println((String)"showStatus: " + text);
textIndex = 0;
current_channel = 0;
sprintf((char*)texts[current_channel], "%s", text);
getScrolltextFromBuffer(current_channel);
colIndex = 0;
marqueeDelayTimestamp = 0;
scrollWhitespace = 0;
led->clear();
uint8_t displayColumn = 0; // start left
while (displayColumn < ledmatrix_width) {
// write one column
uint8_t asc = scrollbuffer[textIndex] - 32;
uint16_t idx = pgm_read_word(&(font_index[asc]));
uint8_t w = pgm_read_byte(&(font[idx]));
uint8_t col = pgm_read_byte(&(font[colIndex + idx + 1]));
led->setColumn(displayColumn, col);
led->display();
displayColumn++;
if (++colIndex == w) {
displayColumn += 1;
colIndex = 0;
if (scrollbuffer[++textIndex] == '\0') {
break; //return; // done
textIndex = 0;
}
}
}
delay(500);
}


void nextChar()
{
if (scrollbuffer[++textIndex] == '\0')
{
textIndex = 0;
scrollWhitespace = ledmatrix_width; // start over with empty display
if (scrollDelay && do_publishes)
client.publish((((String)TOPICROOT "/" + devname + "/status").c_str()), "repeat");

if (start_countdown) {
start_countdown--;
} else {
current_channel = textcycle[current_cycle];
current_cycle = (current_cycle + 1) % num_textcycles;
getScrolltextFromBuffer(current_channel);
}
}
}

void nextCol(uint8_t w)
{
if (++colIndex == w)
{
scrollWhitespace = 2;
colIndex = 0;
nextChar();
}
}

void writeCol()
{
if (scrollWhitespace > 0)
{
scrollWhitespace--;
return;
}
uint8_t asc = scrollbuffer[textIndex] - 32;
uint16_t idx = pgm_read_word(&(font_index[asc]));
uint8_t w = pgm_read_byte(&(font[idx]));
uint8_t col = pgm_read_byte(&(font[colIndex + idx + 1]));
led->setColumn(ledmatrix_width - 1, col);
nextCol(w);
}

void marquee()
{
if (millis() < 1)
marqueeDelayTimestamp = 0;
if (millis() < marqueeDelayTimestamp)
return;
marqueeDelayTimestamp = millis() + scrollDelay;
led->scroll(LEDMatrixDriver::scrollDirection::scrollLeft);
writeCol();
led->display();
}
Loading

0 comments on commit 7f05e85

Please sign in to comment.