More cleaning up

This commit is contained in:
Arne van Iterson 2020-05-29 19:36:32 +02:00
parent 51b45c2ca7
commit c075fcba4c

View File

@ -18,28 +18,37 @@ Encoder encoder(2, 3);
// General vars
unsigned long time;
int dt = 0;
boolean connected = false;
boolean dataReady = false;
boolean editing = false;
boolean blink = false;
int interval = 0;
// Encoder related vars
long position = 0;
boolean input = false;
// Serial related vars
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
char command[numChars] = {0};
boolean newData = false;
// Screen related vars
int light = 128;
int contrast = 60;
int lightTimeout = 10;
int lightTimeout = 20;
void setBacklight(int value)
// Set variables for menu
int programLenght;
int currentProgram = 0;
char programName[numChars] = {0};
float volume = -1;
void setBacklight(int value) // Set backlight
{
analogWrite(5, value);
if (value != 0)
@ -48,21 +57,13 @@ void setBacklight(int value)
}
}
void setContrast(int value)
void setContrast(int value) // Set contrast
{
display.setContrast(value);
contrast = value;
}
// Set variables for menu
int programLenght;
int currentProgram = 0;
char programName[numChars] = {0};
boolean menuUpdate = true;
float volume = -1;
// Draw screen
void drawMenu()
void drawMenu() // Draw screen
{
display.clearDisplay();
display.setFont(&Picopixel);
@ -74,9 +75,22 @@ void drawMenu()
display.setCursor(0, 4);
display.print((String)(currentProgram + 1) + "/" + (String)(programLenght));
if (button2.read() == Button::PRESSED)
{
display.setCursor(73, 4);
display.print((String)round(contrast));
}
else if (button3.read() == Button::PRESSED)
{
display.setCursor(73, 4);
display.print((String)round(((float)light / 255) * 100));
}
else
{
display.setCursor(73, 4);
display.print("USB");
}
}
display.drawLine(0, 6, 84, 6, BLACK);
display.setFont(NULL);
@ -93,11 +107,6 @@ void drawMenu()
else
{
// Blink connection message if not connected
if (time % 500 == 0)
{
blink = (!blink) ? true : false;
}
display.drawBitmap(0, 7, logo, 84, 16, 1);
display.setFont(&Picopixel);
@ -136,23 +145,15 @@ void drawMenu()
display.display();
}
// Bar position
const int pos[2] = {4, 26};
// Draw volume bar and text
void drawBar()
void drawBar() // Draw volume bar and text
{
int pos[2] = {4, 26};
// Set fonts and size
display.setFont(&Picopixel);
display.setCursor(4, pos[1] - 2);
display.print("Volume:");
// Blink percentage if editing
if (editing && time % 500 == 0)
{
blink = (!blink) ? 1 : 0;
}
// Center percentage
if (volume == 0)
{
@ -167,7 +168,7 @@ void drawBar()
display.setCursor(37, pos[1] - 2);
}
if (!blink)
if (!blink || !editing)
{
display.print((String)round(volume) + "%");
}
@ -184,8 +185,7 @@ void drawBar()
display.setFont(NULL);
}
// Draw program name in the middle of the display or scroll it
void drawName()
void drawName() // Draw program name in the middle of the display or scroll it
{
int width = 0;
for (size_t i = 0; i < sizeof(programName); i++)
@ -208,7 +208,7 @@ void drawName()
display.print(programName);
}
void processData()
void processData() // Process serial data
{
if (newData == true)
{
@ -228,22 +228,27 @@ void processData()
Serial.println("<COUNT>");
Serial.println("<SWITCH," + (String)currentProgram + ">");
}
else if (strcmp("COUNT", command) == 0)
if (strcmp("COUNT", command) == 0)
{
programLenght = atoi(strtokIndx);
Serial.println("<OK>");
}
else if (strcmp("CURRENT", command) == 0)
if (strcmp("SWITCH", command) == 0)
{
currentProgram = atoi(strtokIndx);
Serial.println("<OK>");
Serial.println("<SWITCH," + (String)currentProgram + ">");
}
else if (strcmp("NAME", command) == 0)
if (strcmp("NAME", command) == 0)
{
strcpy(programName, strtokIndx);
Serial.println("<OK>");
}
else if (strcmp("GETVOL", command) == 0)
if (strcmp("GETVOL", command) == 0)
{
volume = atoi(strtokIndx);
Serial.println("<OK>");
@ -254,7 +259,7 @@ void processData()
}
}
void checkSerial()
void checkSerial() // Get data from serial bus
{
static boolean recvInProgress = false;
static byte ndx = 0;
@ -295,19 +300,21 @@ void checkSerial()
processData();
}
boolean input = false;
// Check for button input
void checkInput(unsigned long time)
void checkInput() // Check for button input
{
input = false;
int value = 0;
long newPosition = encoder.read();
// Check if any button is pressed
if (button1.read() == Button::PRESSED || button2.read() == Button::PRESSED || button3.read() == Button::PRESSED)
{
input = true;
}
// Encoder button
if (button1.pressed())
{
input = true;
editing = (editing) ? 0 : 1;
}
@ -326,17 +333,9 @@ void checkInput(unsigned long time)
if (value != 0)
{
position = newPosition;
menuUpdate = true;
input = true;
if (button2.read() == Button::PRESSED)
{
// Change backlight
if (!(light + value < 0) && !(light + value > 254))
{
setBacklight(light + value);
}
} else if (button3.read() == Button::PRESSED)
{
// Change contrast
if (!(contrast + value < 0) && !(contrast + value > 100))
@ -344,6 +343,14 @@ void checkInput(unsigned long time)
setContrast(contrast + value);
}
}
else if (button3.read() == Button::PRESSED)
{
// Change backlight
if (!(light + value < 0) && !(light + value > 254))
{
setBacklight(light + value);
}
}
else if (!editing)
{
// Change program
@ -374,8 +381,7 @@ void checkInput(unsigned long time)
}
}
// Init arduino
void setup()
void setup() // Init arduino
{
// Start serial connection
Serial.begin(115200);
@ -389,17 +395,20 @@ void setup()
display.begin();
setContrast(contrast);
setBacklight(light);
display.clearDisplay();
display.display();
}
void loop()
void loop() // Loop code
{
// Check time
dt = millis() - time;
time = millis();
menuUpdate = (time % 1000 == 0) ? true : false;
Serial.println(interval);
// Check for serial data or commands
checkSerial();
if (programLenght == 0 || volume == -1 || strcmp("", programName) == 0)
{
@ -411,47 +420,40 @@ void loop()
blink = false;
}
if (interval >= 500) // Aprox every half second, every loop takes about 25 ms
{
interval = 0;
blink = (!blink) ? true : false;
lightTimeout--;
}
else
{
interval = interval + dt;
}
if (input || newData)
{
lightTimeout = 10;
lightTimeout = 20;
setBacklight(light);
}
if (lightTimeout <= 0)
{
setBacklight(0);
editing = false;
}
else
{
if (time % 1000 == 0)
{
lightTimeout--;
}
}
// Check for serial data or commands
checkSerial();
// Check if serial connection is established
if (connected && dataReady)
{
// Draw menu every second or every 0.1 second when editing
if (menuUpdate || (editing && time % 100 == 0))
{
menuUpdate = false;
drawMenu();
}
// Check input
checkInput(time);
checkInput();
}
else
{
if (menuUpdate)
{
// Draw menu with "Waiting for connection", name and volume are ignored bij drawMenu()
menuUpdate = false;
drawMenu();
}
}
}