added settings support and general cleanup

This commit is contained in:
RLF
2024-01-15 23:50:03 -05:00
parent 3f281b7bbf
commit d77dc2f89f

View File

@@ -1,7 +1,9 @@
/*
* UNO Monitor Receiver (4-bit LCD Version)
* UNO Monitor Receiver v.0.1.0 (4-bit LCD Version)
* Kidacro <kidacro@archamedis.net>
*
* https://kidacro.archamedis.net/git/kidacro/Arduino/src/branch/main/uno-stats-monitor
* uno_reciever.ino - Arduino Receiver
*
* Waits for input over serial connection and displays on LCD & LED
* Receives IR events and reports them over serial connection to a daemon
*
@@ -10,11 +12,16 @@
* End char is: '@'
* 2nd char denotes which line number/mode to use
*
* Setting Schema:
* LCDLength:LCDDelay:LEDBrightness:LEDDelay
* 16:5:1:3
*
* Example:
* Line 1: #1Archamedis Status@
* Line 2: #299% free / etc.@
* LAVG 3: #324@
*
* LAVG -: #-24@
* LSET 0: #016:5:1:3@
* Output Schema:
* Multimedia Controls use single characters to denote which button was pressed
* P power
@@ -56,7 +63,7 @@
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* 10K potentiometer:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
@@ -78,15 +85,14 @@
// LCD vars
const int LCDRS = 12, LCDEN = 11, LCDD4 = 5, LCDD5 = 4, LCDD6 = 3, LCDD7 = 2;
int LCDLength = 16;
int LCDDelay = 5;
int LCDDelay = 10;
LiquidCrystal LCD(LCDRS, LCDEN, LCDD4, LCDD5, LCDD6, LCDD7);
// LED vars
const int LEDIN = 10, LEDCS = 9, LEDCLK = 8;
LedControl LEDlc=LedControl(LEDIN,LEDCLK,LEDCS,1);
int LEDBrightness = 1; // default = 8 / max = 15
unsigned long LEDdelaytime1=3;
unsigned long LEDdelaytime2=3;
int LEDBrightness = 8; // default = 8 / max = 15
unsigned long LEDDelay=10;
// IR vars
const int IRSIG = 13;
@@ -131,27 +137,27 @@ void IRSetup(void)
Irrecv.enableIRIn();
}
void LcdL1(boolean wipe = false)
// dynamic lines & character wiping (LCDLength)
void LcdLd(int line = 1, boolean wipe = false)
{
delay(LCDDelay);
LCD.setCursor(0, 0); // Define the cursor position as the 1st position of the 1st line
char myline[LCDLength];
int x = 0;
if(wipe) {
LcdWrite(" "); // 16 spaces for a blank line
LCD.setCursor(0, 0);
// physical line #'s to logical
if(line > 0) {
line--;
}
delay(LCDDelay);
}
void LcdL2(boolean wipe = false)
{
delay(LCDDelay);
LCD.setCursor(0, 1); // Define the cursor position as the 1st position of the 2nd line
LCD.setCursor(0, line); // Define the cursor position as the 1st position of the specified line
if(wipe) {
LcdWrite(" "); // 16 spaces for a blank line
LCD.setCursor(0, 1);
for(x=0;x<LCDLength;x++) {
myline[x] = ' ';
}
LcdWrite(myline);
LCD.setCursor(0, line);
}
delay(LCDDelay);
@@ -159,10 +165,10 @@ void LcdL2(boolean wipe = false)
void LcdIntro(void)
{
// Print a "waiting" message to the LCD.
LcdL1();
LCD.print("Starting Up...");
LcdL2();
// Print a "intro" message to the LCD.
LcdLd(1);
LCD.print("UnoMonitor 0.1.0");
LcdLd(2);
LCD.print("Waiting for data");
}
@@ -170,13 +176,13 @@ void LcdCls(void)
{
delay(LCDDelay);
LCD.clear(); // The screen is empty
LcdL1(); // the cursor position is zeroed
LcdLd(1); // the cursor position is zeroed
delay(LCDDelay);
}
/*
TODO: returns starting position of text or 0 for 1st position
void LcdCenter(int mylen)
int LcdCenter(int mylen)
{
int mypos = 0;
@@ -206,25 +212,24 @@ void LcdWrite(String mymessage)
// Output character by character
for(i=0; i < mylen; i++) {
char c = mymessage[i];
if(c != '\b') {
delay(10);
delay(LCDDelay);
LCD.print(c);
delay(10);
}
delay(LCDDelay);
}
}
// TODO: make segment_row & segment_col dynamic and configurable
void LedIntro() {
for(int row=0;row<8;row++) {
for(int col=0;col<8;col++) {
delay(LEDdelaytime2);
delay(LEDDelay);
LEDlc.setLed(0,row,col,true);
delay(LEDdelaytime2);
delay(LEDDelay);
for(int i=0;i<col;i++) {
LEDlc.setLed(0,row,col,false);
delay(LEDdelaytime2);
delay(LEDDelay);
LEDlc.setLed(0,row,col,true);
delay(LEDdelaytime2);
delay(LEDDelay);
}
}
}
@@ -271,20 +276,21 @@ void LedShowLoad(const char *load)
// segments as rows
for(int row=0;row<8;row++) {
for(int col=0;col<segments;col++) {
delay(LEDdelaytime2);
delay(LEDDelay);
LEDlc.setLed(0,row,col,true);
delay(LEDdelaytime2);
delay(LEDDelay);
for(int i=0;i<col;i++) {
LEDlc.setLed(0,row,col,false);
delay(LEDdelaytime2);
delay(LEDDelay);
LEDlc.setLed(0,row,col,true);
delay(LEDdelaytime2);
delay(LEDDelay);
}
}
}
}
// IR translator
// TODO: read codes from setup program for custom remotes
void IRtranslate()
{
switch(IRresults.value) {
@@ -358,42 +364,54 @@ void SERrecv()
// 1st character denotes function:
// 1 - print on 1st line as is
// 2 - print on 2nd line as is
// 3 - display load on LCD matrix
// 4 - set a setting
// - - display load on LCD matrix
// 0 - set a setting
void procNewData()
{
int line = 0;
if(SERnewData == true) {
if(DEBUG) {
Serial.print("Receiver says: ");
Serial.println(SERrecvChars);
}
// Chooser
// dev: we have to define 1st character (line number) as BS and when printing, search for that BS character to skip printing that 1 character
// print on line 1
if(SERrecvChars[0] == '1') {
LcdL1(true);
SERrecvChars[0] = '\b';
// print to given line on LCD
if(SERrecvChars[0] != '-' && SERrecvChars[0] != '0') {
line = SERrecvChars[0] - '0';
LcdLd(line,true);
memmove(SERrecvChars, SERrecvChars+1, strlen(SERrecvChars));
LcdWrite(SERrecvChars);
}
// print on line 2
else if(SERrecvChars[0] == '2') {
LcdL2(true);
SERrecvChars[0] = '\b';
LcdWrite(SERrecvChars);
}
// show load on led matrix
else if(SERrecvChars[0] == '3') {
SERrecvChars[0] = ' ';
LedShowLoad(SERrecvChars);
// show load on LED matrix
if(SERrecvChars[0] == '-') {
memmove(SERrecvChars, SERrecvChars+1, strlen(SERrecvChars));
LedShowLoad(SERrecvChars);
}
// load new settings
if(SERrecvChars[0] == '0') {
memmove(SERrecvChars, SERrecvChars+1, strlen(SERrecvChars));
LoadSettings(SERrecvChars);
}
SERnewData = false;
}
}
void LoadSettings(char* mysettings)
{
// Settings schema:
// LCDLength:LCDDelay:LEDBrightness:LEDDelay
// 16:5:1:3
LCDLength = atoi(strtok(mysettings, ":"));
LCDDelay = atoi(strtok(NULL, ":"));
LEDBrightness = atoi(strtok(NULL, ":"));
LEDDelay = atoi(strtok(NULL, ":"));
}
void setup (void)
{
// init HW
@@ -414,8 +432,8 @@ void loop (void)
{
// process IR signal
if(Irrecv.decode(&IRresults)) {
IRtranslate();
Irrecv.resume(); // receive the next value
IRtranslate(); // process result
Irrecv.resume(); // receive the next command
}
// process serial input