church_streamer/arduino/arduino.ino
2024-02-13 23:54:45 +00:00

103 lines
2.7 KiB
C++

/*
Blink
The circuit:
* VFD Data to digital pin 2 (blue)
* VFD Clock to digital pin 3 (yellow)
* VFD Chip select to digital pin 4 (green)
* VFD VCC (power) to 5V
* VFD Ground (power) to Ground
*/
// include the library code:
#include <SPI_VFD.h>
// initialize the library with the numbers of the interface pins
SPI_VFD vfd(2, 3, 4);
// | 1 2|
// |12345678901234567890|
String vfd_line_top = "Booting up... ";
String vfd_line_bottom = "Waiting for stream. ";
unsigned int vfd_line_position = 0;
bool vfd_line_print = false;
unsigned long millis_last_incoming_ping = 0;
unsigned long millis_last_outgoing_ping = 0;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(9600);
// set up the VFD's number of columns and rows:
vfd.begin(20, 2);
update_display();
}
void update_display() {
vfd.setCursor(0, 0);
vfd.print(vfd_line_top);
vfd.setCursor(0, 1);
vfd.print(vfd_line_bottom);
}
void check_serial() {
while (Serial.available() > 0) {
millis_last_incoming_ping = millis();
int incomingByte = Serial.read();
if (incomingByte == 0 || incomingByte == '^') {
incomingByte = vfd_line_position = 0;
}
if (incomingByte < 32) {
continue;
}
if (vfd_line_position < 20) {
vfd_line_top.setCharAt(vfd_line_position, incomingByte);
} else {
vfd_line_bottom.setCharAt(vfd_line_position - 20, incomingByte);
}
vfd_line_position = (vfd_line_position + 1) % 40;
vfd_line_print = vfd_line_position == 0;
if (vfd_line_print) {
break;
}
}
}
// the loop function runs over and over again forever
void loop() {
// We've been running for over 50 days and wrapped to zero. Reset all our timers
if (millis() < 10000 && (millis_last_outgoing_ping > 10000 || millis_last_incoming_ping > 10000)) {
millis_last_outgoing_ping = 0;
millis_last_incoming_ping = 0;
}
check_serial();
// If it's been 10 seconds since last incoming data
if (millis() - millis_last_incoming_ping > 30000) {
// |12345678901234567890|
vfd_line_top = "Error stream is down";
vfd_line_bottom = "Verify or restart...";
vfd_line_print = true;
millis_last_incoming_ping = millis();
}
// Send outgoing ping every 5 seconds if we can.
if (millis() - millis_last_outgoing_ping > 30000 && Serial.availableForWrite()) {
Serial.write('^');
millis_last_outgoing_ping = millis();
}
// If we have something to print, print it
if (vfd_line_print) {
vfd_line_print = false;
update_display();
}
delay(100);
}