church_streamer/arduino/arduino.ino

121 lines
3.1 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 = "Filadelfia streamer ";
String vfd_line_bottom = " ";
unsigned int vfd_line_position = 0;
bool vfd_first_boot = true;
bool vfd_line_print = false;
bool show_loading = false;
unsigned long millis_last_incoming_ping = 0;
unsigned long millis_booting_bar = 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) {
vfd_first_boot = false;
millis_last_incoming_ping = millis();
int incomingByte = Serial.read();
if (incomingByte == '~') {
if (Serial.availableForWrite()) {
Serial.write("pong^");
}
incomingByte = 0;
}
if (incomingByte == '^') {
incomingByte = vfd_line_position = 0;
}
if (incomingByte <= 0x0F) {
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() {
check_serial();
if (vfd_first_boot) {
if (!show_loading && millis() > 1000) {
show_loading = true;
vfd_line_top = "Booting, please wait";
vfd_line_bottom = "[------------------]";
vfd_line_print = true;
}
if (millis() - millis_booting_bar > 3158) {
millis_booting_bar = millis();
int bars = min((int)floor(millis_booting_bar / 3158), 18);
for (int i = 0; i < bars; i++) {
vfd_line_bottom.setCharAt(i + 1, 0xFF);
}
vfd_line_print = true;
}
}
// If it's been 60 seconds since startup or
// 30 seconds since last message
if ((vfd_first_boot && millis() - millis_last_incoming_ping > 60000)
|| (!vfd_first_boot && millis() - millis_last_incoming_ping > 30000)) {
vfd_first_boot = false;
// |12345678901234567890|
vfd_line_top = "Stream down! check ";
vfd_line_bottom = "or restart computer.";
vfd_line_print = true;
millis_last_incoming_ping = millis();
}
// If we have something to print, print it
if (vfd_line_print) {
vfd_line_print = false;
update_display();
}
delay(100);
}