arduino-ether-text-clock/serial_text_scroller/serial_text_scroller.ino

108 lines
2.5 KiB
C++

/*
Have a Serial connection send lines to be displayed on a screen / display.
If the display is too small, scroll the text along the display.
*/
#define BAUD_RATE 9600
#define STR_MAX_LENGTH 20
#define LINE_BUFFER_SIZE 4
void initDisplay();
void updateDisplay( const unsigned int tick );
void serialEvent();
char line_buffer[LINE_BUFFER_SIZE][STR_MAX_LENGTH + 1] = {// Small Ringbuffer to collect lines
"Liquid Crystal Boob",
"Captain",
"Pilot",
"Snippy"
};
int line_buffer_read = 0; // Index of Ringbuffer
int line_buffer_write = 0; // Index of Ringbuffer
const unsigned int timer_lps = 1000;
const unsigned int timer_display = 1000;
const unsigned int timer_print_line_buffers = 10000;
const unsigned int timer_switch_line_buffer = timer_print_line_buffers;
long lastMillis_lps = 0;
long lastMillis_display = 0;
long lastMillis_switch_line_buffer = 0;
long lastMillis_print_line_buffers = 0;
long loops = 0;
long lps = 0;
void setup() {
Serial.begin(BAUD_RATE);
initDisplay();
}
void loop() {
long currentMillis = millis();
loops++;
// Estimate Loops per Seconds
if ( currentMillis - lastMillis_lps > timer_lps ) {
lastMillis_lps = currentMillis;
lps = loops;
loops = 0;
}
// Update Screen once per 1s
if ( currentMillis - lastMillis_display > timer_display ) {
lastMillis_display = currentMillis;
updateDisplay( (currentMillis / 1000) );
}
// Display next line in line_buffer
if ( currentMillis - lastMillis_switch_line_buffer > timer_switch_line_buffer ) {
lastMillis_switch_line_buffer = currentMillis;
line_buffer_read = (line_buffer_read + 1 ) % LINE_BUFFER_SIZE;
}
// Print all Line Buffers once every 10 seconds
if ( currentMillis - lastMillis_print_line_buffers > timer_print_line_buffers ) {
lastMillis_print_line_buffers = currentMillis;
Serial.println(F("========================="));
Serial.println(F("Printing all Line buffers"));
Serial.println(F("========================="));
for (int i = 0; i < LINE_BUFFER_SIZE ; i++) {
if ( line_buffer_read == i )
{
Serial.print('r');
}
else
{
Serial.print(' ');
}
if ( line_buffer_write == i )
{
Serial.print('w');
}
else
{
Serial.print(' ');
}
Serial.print(" Line ");
Serial.print( i );
Serial.print(": '");
Serial.print( line_buffer[i] );
Serial.print("' ");
Serial.println();
}
Serial.println(F("========================="));
}
}