Files
Arduino/uno-stats-monitor/monitor_uno.ino
2023-12-26 21:50:49 -05:00

215 lines
4.6 KiB
C++

/*
* UNO Monitor Receiver (4bit LCD Version)
* kidacro@archamedis.net
*
* Waits for input over serial connection and displays on LCD
*
* Input Schema:
* Start char is: '#'
* End char is: '@'
* 2nd char denotes which line number to use
*
* Example:
* Line 1: #1Archamedis Status@
* Line 2: #299% free / etc.@
*
*/
// include the library code:
#include <LiquidCrystal.h>
// LCD Pinout:
// LCD RS pin to digital pin 12
// LCD Enable pin to digital pin 11
// LCD D4 pin to digital pin 5
// LCD D5 pin to digital pin 4
// LCD D6 pin to digital pin 3
// LCD D7 pin to digital pin 2
// LCD R/W pin to ground
// LCD VSS pin to ground
// LCD VCC pin to 5V
// 10K resistor:
// ends to +5V and ground
// wiper to LCD VO pin (pin 3)
// Variable Init
// LCD vars
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int LCDRS = 12, LCDEN = 11, LCDD4 = 5, LCDD5 = 4, LCDD6 = 3, LCDD7 = 2;
int LCDLength = 16;
LiquidCrystal LCD(LCDRS, LCDEN, LCDD4, LCDD5, LCDD6, LCDD7);
// Serial vars
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
// Debugging (serial console must be connected)
bool DEBUG = 1;
void LcdSetup(void) {
// set up the LCD's number of columns and rows:
LCD.begin(LCDLength, 2);
// Print a "waiting" message to the LCD.
LcdL1();
LCD.print("Starting Up...");
LcdL2();
LCD.print("Waiting for data");
}
void LcdCls(void) {
delay(10);
LCD.clear(); // The screen is empty
LcdL1(); // the cursor position is zeroed
delay(10);
}
void LcdL1() {
delay(10);
LCD.setCursor(0, 0); // Define the cursor position as the 1st position of the 1st line
delay(10);
}
void LcdL2() {
delay(10);
LCD.setCursor(0, 1); // Define the cursor position as the 1st position of the 2nd line
delay(10);
}
// returns starting position of text
void LcdCenter(int mylen) {
int mypos = 0;
return mypos;
}
void LcdWrite(String mymessage) {
int i = 0;
unsigned int mylen = mymessage.length();
// trunicate string to lcd line length
if(mylen > LCDLength) {
mylen = LCDLength;
}
// Debug input and length
if(DEBUG) {
Serial.println("\n\nString: ");
Serial.println(mymessage);
Serial.println(mymessage.length());
}
// Output string
//LCD.print(mymessage);
// Output character by character
for(i=0; i < mylen; i++) {
char c = mymessage[i];
if(c != '\b') {
delay(10);
LCD.print(c);
delay(10);
}
}
}
void LcdWriteExample() {
LcdCls();
LcdL1();
LcdWrite("Archamedis Stats");
LcdL2();
LcdWrite("4.3ghz 89% 68C");
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '#';
char endMarker = '@';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if(recvInProgress == true) {
if(rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if(rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if(newData == true) {
if(DEBUG) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
}
// Print on line 1 or 2
// dev: fucking c and its fixed arrays
// we have to define 1st character (line number) as BS and when printing, search for that BS character to skip printing that 1 character, grrr
if(receivedChars[0] == '1') {
LcdL1();
LcdWrite(" "); // 16 spaces for a blank line
LcdL1();
receivedChars[0] = '\b';
LcdWrite(receivedChars);
}
else if(receivedChars[0] == '2') {
LcdL2();
LcdWrite(" "); // 16 spaces for a blank line
LcdL2();
receivedChars[0] = '\b';
LcdWrite(receivedChars);
}
newData = false;
}
}
void setup (void) {
// Init LCD panel
LcdSetup();
// Init Debugging output
if(DEBUG) {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
// Init normal operation
else {
Serial.begin(9600);
}
}
// main()
void loop (void) {
// process input
recvWithStartEndMarkers();
showNewData();
}