Cleaned up code and added features

This commit is contained in:
Arne van Iterson 2020-05-29 16:16:58 +02:00
parent ed1bc1fdb4
commit 51b45c2ca7
4 changed files with 262 additions and 158 deletions

3
res/data.json Normal file
View File

@ -0,0 +1,3 @@
{
"device-id": "1A86:7523"
}

View File

@ -5,10 +5,16 @@
#include <Button.h>
#include <Encoder.h>
Adafruit_PCD8544 display = Adafruit_PCD8544(14, 15, 16, 17, 18);
#include "logo.h"
Button button(4);
Encoder encoder(3, 2);
// SCLK, DIN, D/C, SCE, RST
// 10k, 10k, 10k, 1k, 10k
Adafruit_PCD8544 display = Adafruit_PCD8544(8, 9, 19, 17, 18);
Button button1(4);
Button button2(6);
Button button3(7);
Encoder encoder(2, 3);
// General vars
unsigned long time;
@ -24,26 +30,30 @@ long position = 0;
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;
// Set backlight
boolean light = 0;
int timeout = 10000;
void toggleBacklight(int value = 128) {
if (light) {
analogWrite(5, 0);
light = 0;
} else {
analogWrite(5, value);
light = 1;
void setBacklight(int value)
{
analogWrite(5, value);
if (value != 0)
{
light = value;
}
}
void setContrast(int value)
{
display.setContrast(value);
contrast = value;
}
// Set variables for menu
int programLenght;
int currentProgram = 0;
@ -52,7 +62,8 @@ boolean menuUpdate = true;
float volume = -1;
// Draw screen
void drawMenu() {
void drawMenu()
{
display.clearDisplay();
display.setFont(&Picopixel);
@ -71,33 +82,40 @@ void drawMenu() {
display.setFont(NULL);
// Check for connection
if (connected && dataReady) {
if (connected && dataReady)
{
// Program name
drawName();
// Volume Bar
drawBar();
} else {
}
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);
if (!blink)
{
display.setCursor(9, 10);
display.print("Waiting for");
if (!connected)
{
display.setCursor(12, 18);
display.print("Connection");
} else if (!dataReady)
display.setCursor(4, 28);
display.print("Waiting for connection");
}
else if (!dataReady)
{
display.setCursor(30, 18);
display.print("Data");
display.setCursor(15, 28);
display.print("Waiting for data");
}
}
display.setFont(NULL);
}
// Bottom Bar
@ -108,7 +126,9 @@ void drawMenu() {
if (editing)
{
display.print(" - OK + ");
} else {
}
else
{
display.print(" < OK > ");
}
}
@ -117,10 +137,11 @@ void drawMenu() {
}
// Bar position
const int pos[2] = { 4, 26 };
const int pos[2] = {4, 26};
// Draw volume bar and text
void drawBar() {
void drawBar()
{
// Set fonts and size
display.setFont(&Picopixel);
display.setCursor(4, pos[1] - 2);
@ -133,11 +154,16 @@ void drawBar() {
}
// Center percentage
if (volume == 0) {
if (volume == 0)
{
display.setCursor(39, pos[1] - 2);
} else if (volume == 100) {
}
else if (volume == 100)
{
display.setCursor(36, pos[1] - 2);
} else {
}
else
{
display.setCursor(37, pos[1] - 2);
}
@ -159,7 +185,8 @@ void drawBar() {
}
// Draw program name in the middle of the display or scroll it
void drawName() {
void drawName()
{
int width = 0;
for (size_t i = 0; i < sizeof(programName); i++)
{
@ -172,163 +199,204 @@ void drawName() {
if (width > 84)
{
// TODO: Make the text scroll if the name is too long
} else {
}
else
{
int x = (84 / 2) - (width / 2);
display.setCursor(x, 10);
}
display.print(programName);
}
void processData() {
if (newData == true) {
strcpy(tempChars, receivedChars);
void processData()
{
if (newData == true)
{
strcpy(tempChars, receivedChars);
char * strtokIndx;
char *strtokIndx;
strtokIndx = strtok(tempChars,",");
strcpy(command, strtokIndx);
strtokIndx = strtok(tempChars, ",");
strcpy(command, strtokIndx);
strtokIndx = strtok(NULL, ",");
strtokIndx = strtok(NULL, ",");
if (strcmp("HELLO", command) == 0)
{
connected = true;
Serial.println("<OK>");
Serial.println("<COUNT>");
Serial.println("<SWITCH," + (String)currentProgram + ">");
}
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;
dataReady = true;
if (strcmp("HELLO", command) == 0)
{
connected = true;
Serial.println("<OK>");
Serial.println("<COUNT>");
Serial.println("<SWITCH," + (String)currentProgram + ">");
}
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;
dataReady = true;
}
}
void checkSerial() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
void checkSerial()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
while (Serial.available() > 0 && newData == false)
{
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
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 (recvInProgress == true)
{
if (!editing)
if (rc != endMarker)
{
if (currentProgram == 0)
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
currentProgram = programLenght - 1;
} else {
currentProgram--;
}
Serial.println("<SWITCH," + (String)currentProgram + ">");
} else {
if (volume != 0)
{
Serial.println("<SETVOL," + (String)(volume - 1) + ">");
ndx = numChars - 1;
}
}
position = newPosition;
}
if (newPosition > position + 3) // Rotating clockwise
{
if (!editing)
else
{
if (currentProgram == programLenght - 1)
{
currentProgram = 0;
} else {
currentProgram++;
}
Serial.println("<SWITCH," + (String)currentProgram + ">");
} else {
if (volume != 100)
{
Serial.println("<SETVOL," + (String)(volume + 1) + ">");
}
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
position = newPosition;
}
else if (rc == startMarker)
{
recvInProgress = true;
}
}
if (button.pressed())
processData();
}
boolean input = false;
// Check for button input
void checkInput(unsigned long time)
{
input = false;
int value = 0;
long newPosition = encoder.read();
// Encoder button
if (button1.pressed())
{
input = true;
editing = (editing) ? 0 : 1;
}
if (newPosition != position)
{
if (newPosition < position - 3) // Rotating anti-clockwise
{
value = -1;
}
else if (newPosition > position + 3)
{ // Rotating clockwise
value = 1;
}
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))
{
setContrast(contrast + value);
}
}
else if (!editing)
{
// Change program
if (currentProgram + value < 0)
{
currentProgram = programLenght - 1;
}
else if (currentProgram + value > programLenght - 1)
{
currentProgram = 0;
}
else
{
currentProgram = currentProgram + value;
}
Serial.println("<SWITCH," + (String)(currentProgram) + ">");
}
else
{
// Change volume
if (!(volume + value < 0) && !(volume + value > 100))
{
Serial.println("<SETVOL," + (String)(volume + value) + ">");
}
}
}
}
}
// Init arduino
void setup() {
void setup()
{
// Start serial connection
Serial.begin(115200);
// Init button
button.begin();
button1.begin();
button2.begin();
button3.begin();
// Init display
display.begin();
display.setContrast(50);
toggleBacklight();
setContrast(contrast);
setBacklight(light);
display.clearDisplay();
display.display();
// TODO: Remove testing variables
currentProgram = 0;
}
void loop() {
void loop()
{
// Check time
time = millis();
menuUpdate = (time % 1000 == 0) ? true : false;
@ -343,7 +411,23 @@ void loop() {
blink = false;
}
if (input || newData)
{
lightTimeout = 10;
setBacklight(light);
}
if (lightTimeout <= 0)
{
setBacklight(0);
}
else
{
if (time % 1000 == 0)
{
lightTimeout--;
}
}
// Check for serial data or commands
checkSerial();
@ -352,15 +436,17 @@ void loop() {
if (connected && dataReady)
{
// Draw menu every second or every 0.1 second when editing
if (menuUpdate || (editing && time % 100 == 0)) {
if (menuUpdate || (editing && time % 100 == 0))
{
menuUpdate = false;
drawMenu();
}
// Check input
checkInput(time);
} else {
}
else
{
if (menuUpdate)
{
// Draw menu with "Waiting for connection", name and volume are ignored bij drawMenu()

14
src/arduino/logo.h Normal file
View File

@ -0,0 +1,14 @@
// 'AudioMixer', 84x16px
const unsigned char logo [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0x30, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x33, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x31, 0xfc, 0x00,
0x05, 0x00, 0x3f, 0xc8, 0x00, 0x00, 0x00, 0x33, 0x30, 0x04, 0x00, 0x04, 0x00, 0x20, 0x40, 0x00,
0x00, 0x00, 0x33, 0x78, 0x04, 0x00, 0x04, 0x00, 0x22, 0x40, 0x00, 0x00, 0x00, 0x33, 0x79, 0xfd,
0x05, 0xfd, 0x3f, 0xa2, 0x49, 0x0b, 0xfb, 0xe0, 0x33, 0x31, 0x05, 0x05, 0x01, 0x20, 0xa2, 0x48,
0x92, 0x0a, 0x00, 0x33, 0x31, 0x05, 0x05, 0x01, 0x20, 0xa2, 0x48, 0x62, 0xf2, 0x00, 0x33, 0x31,
0x05, 0x05, 0x01, 0x20, 0xa2, 0x48, 0x92, 0x02, 0x00, 0x33, 0x31, 0x05, 0xfd, 0xfd, 0x3f, 0xa2,
0x49, 0x0b, 0xfa, 0x00, 0x37, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x37,
0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x30, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

View File

@ -11,7 +11,7 @@ def main():
sessions = AudioUtilities.GetAllSessions()
print (str(len(sessions)) + " audio sessions found:")
for session in sessions:
print (" " + str(session))
print (" " + str(session.DisplayName) + ": " + str(session.Process and session.Process.name()))
# Init serial connection
ports = serial.tools.list_ports.comports()
@ -70,13 +70,14 @@ def main():
if program.DisplayName == "@%SystemRoot%\System32\AudioSrv.Dll,-202":
name = "Systeem"
# TODO: This line is being overwritten somehow
elif session.Process and session.Process.name() == "firefox.exe":
elif str(program.Process and program.Process.name()) == "firefox.exe":
name = "Firefox"
elif str(program.Process and program.Process.name()) == "Discord.exe":
name = "Discord"
elif program.DisplayName != "":
name = str(program.DisplayName)
else:
name = str(session.Process and session.Process.name())
name = str(program.Process and program.Process.name())
board.write(("<NAME," + name + ">").encode())
board.write(("<GETVOL," + str(round(volume * 100)) + ">").encode())