Compare commits

..

No commits in common. "d25ca364447f3914f7aece579e1dad54356c7993" and "162261e54ff83b591fef394a8f606b394d3057fa" have entirely different histories.

2 changed files with 114 additions and 159 deletions

View File

@ -10,28 +10,16 @@ Adafruit_PCD8544 display = Adafruit_PCD8544(14, 15, 16, 17, 18);
Button button(4); Button button(4);
Encoder encoder(3, 2); Encoder encoder(3, 2);
// General vars
unsigned long time; unsigned long time;
boolean connected = false; int connected = 0;
boolean dataReady = false; int editing = 0;
boolean editing = false; int blink = 0;
boolean blink = false;
// Encoder related vars
long position = 0; long position = 0;
// Serial related vars
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
char command[numChars] = {0};
boolean newData = false;
// Set backlight // Set backlight
boolean light = 0; int light = 0;
int timeout = 10000; int timeout = 10000;
void toggleBacklight(int value = 128) { void toggleBacklight(int value = 128) {
@ -47,17 +35,16 @@ void toggleBacklight(int value = 128) {
// Set variables for menu // Set variables for menu
int programLenght; int programLenght;
int currentProgram = 0; int currentProgram = 0;
char programName[numChars] = {0}; int menuUpdate = 1;
boolean menuUpdate = true; int volume;
float volume = -1;
// Draw screen // Draw screen
void drawMenu() { void drawMenu(String name, float percentage) {
display.clearDisplay(); display.clearDisplay();
display.setFont(&Picopixel); display.setFont(&Picopixel);
// Check for connection // Check for connection
if (connected && dataReady) if (connected == 1)
{ {
// Draw top bar // Draw top bar
display.setCursor(0, 4); display.setCursor(0, 4);
@ -71,41 +58,34 @@ void drawMenu() {
display.setFont(NULL); display.setFont(NULL);
// Check for connection // Check for connection
if (connected && dataReady) { if (connected == 1) {
// Program name // Program name
drawName(); drawName(name);
// Volume Bar // Volume Bar
drawBar(); drawBar(percentage);
} else { } else {
// Blink connection message if not connected // Blink connection message if not connected
if (time % 500 == 0) if (time % 500 == 0)
{ {
blink = (!blink) ? true : false; blink = (blink == 0) ? 1 : 0;
} }
if (!blink) if (blink == 0)
{ {
display.setCursor(9, 10); display.setCursor(9, 10);
display.print("Waiting for"); display.print("Waiting for");
if (!connected)
{
display.setCursor(12, 18); display.setCursor(12, 18);
display.print("Connection"); display.print("Connection");
} else if (!dataReady)
{
display.setCursor(30, 18);
display.print("Data");
}
} }
} }
// Bottom Bar // Bottom Bar
display.drawLine(0, 38, 84, 38, BLACK); display.drawLine(0, 38, 84, 38, BLACK);
if (connected && dataReady) if (connected == 1)
{ {
display.setCursor(0, 40); display.setCursor(0, 40);
if (editing) if (editing == 1)
{ {
display.print(" - OK + "); display.print(" - OK + ");
} else { } else {
@ -120,30 +100,30 @@ void drawMenu() {
const int pos[2] = { 4, 26 }; const int pos[2] = { 4, 26 };
// Draw volume bar and text // Draw volume bar and text
void drawBar() { void drawBar(float percentage) {
// 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 // Blink percentage if editing
if (editing && time % 500 == 0) if (editing == 1 && time % 500 == 0)
{ {
blink = (!blink) ? 1 : 0; blink = (blink == 0) ? 1 : 0;
} }
// Center percentage // Center percentage
if (volume == 0) { if (percentage == 0) {
display.setCursor(39, pos[1] - 2); display.setCursor(39, pos[1] - 2);
} else if (volume == 100) { } else if (percentage == 100) {
display.setCursor(36, pos[1] - 2); display.setCursor(36, pos[1] - 2);
} else { } else {
display.setCursor(37, pos[1] - 2); display.setCursor(37, pos[1] - 2);
} }
if (!blink) if (blink == 0)
{ {
display.print((String)round(volume) + "%"); display.print((String)round(percentage) + "%");
} }
// Draw volume bar // Draw volume bar
@ -154,22 +134,13 @@ void drawBar() {
display.drawPixel(pos[0] + (76 / 4) * 3 - 1, pos[1] + 8, BLACK); display.drawPixel(pos[0] + (76 / 4) * 3 - 1, pos[1] + 8, BLACK);
display.drawLine(pos[0] + 75, pos[1] + 8, pos[0] + 75, pos[1] + 9, BLACK); display.drawLine(pos[0] + 75, pos[1] + 8, pos[0] + 75, pos[1] + 9, BLACK);
display.fillRect(pos[0], pos[1], round(volume / 100 * 76), 8, BLACK); display.fillRect(pos[0], pos[1], round(percentage / 100 * 76), 8, BLACK);
display.setFont(NULL); display.setFont(NULL);
} }
// Draw program name in the middle of the display or scroll it // Draw program name in the middle of the display or scroll it
// TODO: Fix char lenght void drawName(String name) {
void drawName() { int width = name.length() * 6;
int width = 0;
for (size_t i = 0; i < sizeof(programName); i++)
{
if (programName[i] != NULL)
{
width = width + 6;
}
}
if (width > 84) if (width > 84)
{ {
// TODO: Make the text scroll if the name is too long // TODO: Make the text scroll if the name is too long
@ -177,48 +148,85 @@ void drawName() {
int x = (84 / 2) - (width / 2); int x = (84 / 2) - (width / 2);
display.setCursor(x, 10); display.setCursor(x, 10);
} }
display.print(programName); display.print(name);
} }
// Check for button input
void checkInput(unsigned long time) {
long newPosition = encoder.read();
if (newPosition != position) {
menuUpdate = 1;
if (newPosition < position) // Rotating anti-clockwise
{
if (editing != 1)
{
if (currentProgram == 0)
{
currentProgram = programLenght - 1;
} else {
currentProgram--;
}
} else {
volume--;
}
}
if (newPosition > position) // Rotating clockwise
{
if (editing != 1)
{
if (currentProgram == programLenght - 1)
{
currentProgram = 0;
} else {
currentProgram++;
}
} else {
volume++;
}
}
position = newPosition;
}
if (button.pressed())
{
editing = (editing == 1) ? 0 : 1;
blink = 0;
}
}
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
char command[numChars] = {0};
char data[numChars] = {0};
int intData = 0;
boolean newData = false;
void processData() { void processData() {
if (newData == true) { if (newData == true) {
Serial.print("Raw data recieved: ");
Serial.println(receivedChars);
strcpy(tempChars, receivedChars); strcpy(tempChars, receivedChars);
char * strtokIndx; char * strtokIndx;
strtokIndx = strtok(tempChars,","); strtokIndx = strtok(tempChars,",");
strcpy(command, strtokIndx); strcpy(command, strtokIndx);
Serial.print("Command: ");
Serial.println(command);
strtokIndx = strtok(NULL, ","); strtokIndx = strtok(NULL, ",");
if (atoi(strtokIndx) > 0) // Integer data
if (strcmp("HELLO", command) == 0)
{ {
connected = true; intData = atoi(strtokIndx);
Serial.println("<OK>"); } else { // String data
} strcpy(data, strtokIndx);
else if (strcmp("COUNT", command) == 0)
{
programLenght = atoi(strtokIndx);
Serial.println("<OK>");
}
else if (strcmp("CURRENT", command) == 0)
{
currentProgram = atoi(strtokIndx);
Serial.println("<OK>");
}
else if (strcmp("NAME", command) == 0)
{
strcpy(programName, strtokIndx);
Serial.println("<OK>");
}
else if (strcmp("GETVOL", command) == 0)
{
volume = atoi(strtokIndx);
Serial.println("<OK>");
} }
newData = false; newData = false;
dataReady = true;
} }
} }
@ -256,58 +264,6 @@ void checkSerial() {
processData(); processData();
} }
// Check for button input
// TODO: Improve encoder input
void checkInput(unsigned long time) {
long newPosition = encoder.read();
if (newPosition != position) {
menuUpdate = true;
if (newPosition < position - 3) // Rotating anti-clockwise
{
if (!editing)
{
if (currentProgram == 0)
{
currentProgram = programLenght - 1;
} else {
currentProgram--;
}
Serial.println("<NAME," + (String)currentProgram + ">");
} else {
if (volume != 0)
{
Serial.println("<SETVOL," + (String)(volume - 1) + ">");
}
}
position = newPosition;
}
if (newPosition > position + 3) // Rotating clockwise
{
if (!editing)
{
if (currentProgram == programLenght - 1)
{
currentProgram = 0;
} else {
currentProgram++;
}
Serial.println("<NAME," + (String)currentProgram + ">");
} else {
if (volume != 100)
{
Serial.println("<SETVOL," + (String)(volume + 1) + ">");
}
}
position = newPosition;
}
}
if (button.pressed())
{
editing = (editing) ? 0 : 1;
}
}
// Init arduino // Init arduino
void setup() { void setup() {
// Start serial connection // Start serial connection
@ -324,47 +280,50 @@ void setup() {
display.display(); display.display();
// TODO: Remove testing variables // TODO: Remove testing variables
programLenght = 3;
currentProgram = 0; currentProgram = 0;
volume = 50;
connected = 1;
} }
void loop() { void loop() {
// Check time // Check time
time = millis(); time = millis();
menuUpdate = (time % 1000 == 0) ? true : false; menuUpdate = (time % 1000 == 0) ? 1 : 0;
if (programLenght == 0 || volume == -1 || strcmp("", programName) == 0)
{
dataReady = false;
}
if (!editing && connected && dataReady)
{
blink = false;
}
// Check for serial data or commands // Check for serial data or commands
checkSerial(); checkSerial();
// Check if serial connection is established // Check if serial connection is established
if (connected) if (connected == 1)
{ {
// Draw menu every second or every 0.1 second when editing // Draw menu every second or every 0.1 second when editing
if (menuUpdate || (editing && time % 100 == 0)) { if (menuUpdate == 1 || (editing == 1 && time % 100 == 0)) {
menuUpdate = false; menuUpdate = 0;
drawMenu(); // TODO: Get serial data for program
switch (currentProgram)
{
case 0:
drawMenu("Firefox", volume);
break;
case 1:
drawMenu("Discord", volume);
break;
case 2:
drawMenu("Steam", volume);
break;
}
} }
// Check input // Check input
checkInput(time); checkInput(time);
} else { } else {
if (menuUpdate) if (menuUpdate == 1)
{ {
// Draw menu with "Waiting for connection", name and volume are ignored bij drawMenu() // Draw menu with "Waiting for connection", name and volume are ignored bij drawMenu()
menuUpdate = false; menuUpdate = 0;
drawMenu(); drawMenu("", 0.0);
} }
} }
} }

View File

@ -29,12 +29,8 @@ def main():
board.port = input("\nSelect COM port: ") board.port = input("\nSelect COM port: ")
board.open() board.open()
print ("Waiting for arduino...")
time.sleep(5) time.sleep(5)
board.write("<HELLO>".encode("utf-8")) board.write("HELLO".encode("utf-8"))
board.write("<COUNT,3>".encode("utf-8"))
board.write("<GETVOL,50>".encode("utf-8"))
board.write("<NAME,Test>".encode("utf-8"))
if __name__ == "__main__": if __name__ == "__main__":