From d25ca364447f3914f7aece579e1dad54356c7993 Mon Sep 17 00:00:00 2001 From: Arne van Iterson Date: Wed, 20 May 2020 23:06:03 +0200 Subject: [PATCH] More work on serial communication Arduino side is nearly working --- arduino-mixer.ino | 232 ++++++++++++++++++++++++++-------------------- python/start.py | 6 +- 2 files changed, 134 insertions(+), 104 deletions(-) diff --git a/arduino-mixer.ino b/arduino-mixer.ino index 86ccf1e..6c8abfa 100644 --- a/arduino-mixer.ino +++ b/arduino-mixer.ino @@ -12,9 +12,10 @@ Encoder encoder(3, 2); // General vars unsigned long time; -int connected = 0; -int editing = 0; -int blink = 0; +boolean connected = false; +boolean dataReady = false; +boolean editing = false; +boolean blink = false; // Encoder related vars long position = 0; @@ -25,14 +26,12 @@ char receivedChars[numChars]; char tempChars[numChars]; char command[numChars] = {0}; -char data[numChars] = {0}; -int intData = 0; boolean newData = false; // Set backlight -int light = 0; +boolean light = 0; int timeout = 10000; void toggleBacklight(int value = 128) { @@ -49,16 +48,16 @@ void toggleBacklight(int value = 128) { int programLenght; int currentProgram = 0; char programName[numChars] = {0}; -int menuUpdate = 1; -float volume; +boolean menuUpdate = true; +float volume = -1; // Draw screen -void drawMenu(char name, float percentage) { +void drawMenu() { display.clearDisplay(); display.setFont(&Picopixel); // Check for connection - if (connected == 1) + if (connected && dataReady) { // Draw top bar display.setCursor(0, 4); @@ -72,34 +71,41 @@ void drawMenu(char name, float percentage) { display.setFont(NULL); // Check for connection - if (connected == 1) { + if (connected && dataReady) { // Program name - drawName(name); + drawName(); // Volume Bar - drawBar(percentage); + drawBar(); } else { // Blink connection message if not connected if (time % 500 == 0) { - blink = (blink == 0) ? 1 : 0; + blink = (!blink) ? true : false; } - if (blink == 0) + if (!blink) { display.setCursor(9, 10); display.print("Waiting for"); - display.setCursor(12, 18); - display.print("Connection"); + if (!connected) + { + display.setCursor(12, 18); + display.print("Connection"); + } else if (!dataReady) + { + display.setCursor(30, 18); + display.print("Data"); + } } } // Bottom Bar display.drawLine(0, 38, 84, 38, BLACK); - if (connected == 1) + if (connected && dataReady) { display.setCursor(0, 40); - if (editing == 1) + if (editing) { display.print(" - OK + "); } else { @@ -114,30 +120,30 @@ void drawMenu(char name, float percentage) { const int pos[2] = { 4, 26 }; // Draw volume bar and text -void drawBar(float percentage) { +void drawBar() { // Set fonts and size display.setFont(&Picopixel); display.setCursor(4, pos[1] - 2); display.print("Volume:"); // Blink percentage if editing - if (editing == 1 && time % 500 == 0) + if (editing && time % 500 == 0) { - blink = (blink == 0) ? 1 : 0; + blink = (!blink) ? 1 : 0; } // Center percentage - if (percentage == 0) { + if (volume == 0) { display.setCursor(39, pos[1] - 2); - } else if (percentage == 100) { + } else if (volume == 100) { display.setCursor(36, pos[1] - 2); } else { display.setCursor(37, pos[1] - 2); } - if (blink == 0) + if (!blink) { - display.print((String)round(percentage) + "%"); + display.print((String)round(volume) + "%"); } // Draw volume bar @@ -148,14 +154,22 @@ void drawBar(float percentage) { 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.fillRect(pos[0], pos[1], round(percentage / 100 * 76), 8, BLACK); + display.fillRect(pos[0], pos[1], round(volume / 100 * 76), 8, BLACK); display.setFont(NULL); } // Draw program name in the middle of the display or scroll it // TODO: Fix char lenght -void drawName(char name) { - int width = name.length() * 6; +void drawName() { + int width = 0; + for (size_t i = 0; i < sizeof(programName); i++) + { + if (programName[i] != NULL) + { + width = width + 6; + } + } + if (width > 84) { // TODO: Make the text scroll if the name is too long @@ -163,97 +177,48 @@ void drawName(char name) { int x = (84 / 2) - (width / 2); display.setCursor(x, 10); } - display.print(name); -} - -// Check for button input -// TODO: Improve encoder 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; - } + display.print(programName); } void processData() { if (newData == true) { - Serial.print("Raw data recieved: "); - Serial.println(receivedChars); - strcpy(tempChars, receivedChars); char * strtokIndx; strtokIndx = strtok(tempChars,","); strcpy(command, strtokIndx); - Serial.print("Command: "); - Serial.println(command); strtokIndx = strtok(NULL, ","); - if (atoi(strtokIndx) > 0) // Integer data - { - intData = atoi(strtokIndx); - Serial.print("Int data: "); - Serial.println(intData); - } else { // String data - strcpy(data, strtokIndx); - Serial.print("String data: "); - Serial.println(data); - } if (strcmp("HELLO", command) == 0) { + connected = true; Serial.println(""); } else if (strcmp("COUNT", command) == 0) { - programLenght = intData; + programLenght = atoi(strtokIndx); + Serial.println(""); + } + else if (strcmp("CURRENT", command) == 0) + { + currentProgram = atoi(strtokIndx); + Serial.println(""); } else if (strcmp("NAME", command) == 0) { - // Set program name + strcpy(programName, strtokIndx); + Serial.println(""); } else if (strcmp("GETVOL", command) == 0) { - // Set volume + volume = atoi(strtokIndx); + Serial.println(""); } newData = false; + dataReady = true; } } @@ -291,6 +256,58 @@ void checkSerial() { 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(""); + } else { + if (volume != 0) + { + Serial.println(""); + } + } + position = newPosition; + } + if (newPosition > position + 3) // Rotating clockwise + { + if (!editing) + { + if (currentProgram == programLenght - 1) + { + currentProgram = 0; + } else { + currentProgram++; + } + Serial.println(""); + } else { + if (volume != 100) + { + Serial.println(""); + } + } + position = newPosition; + } + } + + if (button.pressed()) + { + editing = (editing) ? 0 : 1; + } +} + // Init arduino void setup() { // Start serial connection @@ -307,38 +324,47 @@ void setup() { display.display(); // TODO: Remove testing variables - programLenght = 3; currentProgram = 0; - volume = 50; - connected = 1; } void loop() { // Check time time = millis(); - menuUpdate = (time % 1000 == 0) ? 1 : 0; + menuUpdate = (time % 1000 == 0) ? true : false; + + if (programLenght == 0 || volume == -1 || strcmp("", programName) == 0) + { + dataReady = false; + } + + if (!editing && connected && dataReady) + { + blink = false; + } + + // Check for serial data or commands checkSerial(); // Check if serial connection is established - if (connected == 1) + if (connected) { // Draw menu every second or every 0.1 second when editing - if (menuUpdate == 1 || (editing == 1 && time % 100 == 0)) { - menuUpdate = 0; - drawMenu(programName, volume); + if (menuUpdate || (editing && time % 100 == 0)) { + menuUpdate = false; + drawMenu(); } // Check input checkInput(time); } else { - if (menuUpdate == 1) + if (menuUpdate) { // Draw menu with "Waiting for connection", name and volume are ignored bij drawMenu() - menuUpdate = 0; - drawMenu("", 0.0); + menuUpdate = false; + drawMenu(); } } } diff --git a/python/start.py b/python/start.py index dab664e..da1eaab 100644 --- a/python/start.py +++ b/python/start.py @@ -29,8 +29,12 @@ def main(): board.port = input("\nSelect COM port: ") board.open() + print ("Waiting for arduino...") time.sleep(5) - board.write("HELLO".encode("utf-8")) + board.write("".encode("utf-8")) + board.write("".encode("utf-8")) + board.write("".encode("utf-8")) + board.write("".encode("utf-8")) if __name__ == "__main__":