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