bus-arduino/arduino_bus_display/arduino_bus_display.ino
2026-07-22 21:11:28 +03:00

89 lines
1.9 KiB
C++

#include <LiquidCrystal.h>
#include <stdint.h>
#include <string.h>
constexpr uint8_t PIN_RS = 6;
constexpr uint8_t PIN_EN = 7;
constexpr uint8_t PIN_DB4 = 8;
constexpr uint8_t PIN_DB5 = 9;
constexpr uint8_t PIN_DB6 = 10;
constexpr uint8_t PIN_DB7 = 11;
constexpr unsigned long SERIAL_BAUD = 115200;
constexpr size_t LCD_COLUMNS = 16;
constexpr size_t LCD_ROWS = 2;
constexpr size_t INPUT_BUFFER_SIZE = 40;
LiquidCrystal lcd(PIN_RS, PIN_EN, PIN_DB4, PIN_DB5, PIN_DB6, PIN_DB7);
char line1[LCD_COLUMNS + 1] = "Booting... ";
char line2[LCD_COLUMNS + 1] = "Waiting data... ";
char inputBuffer[INPUT_BUFFER_SIZE];
size_t inputPos = 0;
void copyLine(char* destination, const char* source) {
for (size_t i = 0; i < LCD_COLUMNS; ++i) {
if (source[i] == '\0') {
for (size_t j = i; j < LCD_COLUMNS; ++j) {
destination[j] = ' ';
}
destination[LCD_COLUMNS] = '\0';
return;
}
destination[i] = source[i];
}
destination[LCD_COLUMNS] = '\0';
}
void renderDisplay() {
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
}
void handleMessage(const char* message) {
if (strncmp(message, "L1:", 3) == 0) {
copyLine(line1, message + 3);
renderDisplay();
return;
}
if (strncmp(message, "L2:", 3) == 0) {
copyLine(line2, message + 3);
renderDisplay();
}
}
void readSerial() {
while (Serial.available() > 0) {
const char incoming = static_cast<char>(Serial.read());
if (incoming == '\r') {
continue;
}
if (incoming == '\n') {
inputBuffer[inputPos] = '\0';
handleMessage(inputBuffer);
inputPos = 0;
continue;
}
if (inputPos + 1 < INPUT_BUFFER_SIZE) {
inputBuffer[inputPos++] = incoming;
} else {
inputPos = 0;
}
}
}
void setup() {
lcd.begin(LCD_COLUMNS, LCD_ROWS);
renderDisplay();
Serial.begin(SERIAL_BAUD);
}
void loop() {
readSerial();
}