2020-05-20 19:33:05 +02:00
|
|
|
#include <SPI.h>
|
|
|
|
#include <Adafruit_GFX.h>
|
|
|
|
#include <Adafruit_PCD8544.h>
|
|
|
|
#include <Fonts/Picopixel.h>
|
|
|
|
#include <Button.h>
|
|
|
|
#include <Encoder.h>
|
|
|
|
|
|
|
|
Adafruit_PCD8544 display = Adafruit_PCD8544(14, 15, 16, 17, 18);
|
|
|
|
|
|
|
|
Button button(4);
|
|
|
|
Encoder encoder(3, 2);
|
|
|
|
|
2020-05-20 20:22:41 +02:00
|
|
|
// General vars
|
2020-05-20 19:33:05 +02:00
|
|
|
unsigned long time;
|
2020-05-20 23:06:03 +02:00
|
|
|
boolean connected = false;
|
|
|
|
boolean dataReady = false;
|
|
|
|
boolean editing = false;
|
|
|
|
boolean blink = false;
|
2020-05-20 19:33:05 +02:00
|
|
|
|
2020-05-20 20:22:41 +02:00
|
|
|
// Encoder related vars
|
2020-05-20 19:33:05 +02:00
|
|
|
long position = 0;
|
|
|
|
|
2020-05-20 20:22:41 +02:00
|
|
|
// Serial related vars
|
|
|
|
const byte numChars = 32;
|
|
|
|
char receivedChars[numChars];
|
|
|
|
char tempChars[numChars];
|
|
|
|
|
|
|
|
char command[numChars] = {0};
|
|
|
|
|
|
|
|
boolean newData = false;
|
|
|
|
|
2020-05-20 19:33:05 +02:00
|
|
|
|
|
|
|
// Set backlight
|
2020-05-20 23:06:03 +02:00
|
|
|
boolean light = 0;
|
2020-05-20 19:33:05 +02:00
|
|
|
int timeout = 10000;
|
|
|
|
|
|
|
|
void toggleBacklight(int value = 128) {
|
|
|
|
if (light) {
|
|
|
|
analogWrite(5, 0);
|
|
|
|
light = 0;
|
|
|
|
} else {
|
|
|
|
analogWrite(5, value);
|
|
|
|
light = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set variables for menu
|
|
|
|
int programLenght;
|
|
|
|
int currentProgram = 0;
|
2020-05-20 20:22:41 +02:00
|
|
|
char programName[numChars] = {0};
|
2020-05-20 23:06:03 +02:00
|
|
|
boolean menuUpdate = true;
|
|
|
|
float volume = -1;
|
2020-05-20 19:33:05 +02:00
|
|
|
|
|
|
|
// Draw screen
|
2020-05-20 23:06:03 +02:00
|
|
|
void drawMenu() {
|
2020-05-20 19:33:05 +02:00
|
|
|
display.clearDisplay();
|
|
|
|
display.setFont(&Picopixel);
|
|
|
|
|
|
|
|
// Check for connection
|
2020-05-20 23:06:03 +02:00
|
|
|
if (connected && dataReady)
|
2020-05-20 19:33:05 +02:00
|
|
|
{
|
|
|
|
// Draw top bar
|
|
|
|
display.setCursor(0, 4);
|
|
|
|
display.print((String)(currentProgram + 1) + "/" + (String)(programLenght));
|
|
|
|
|
|
|
|
display.setCursor(73, 4);
|
|
|
|
display.print("USB");
|
|
|
|
}
|
|
|
|
|
|
|
|
display.drawLine(0, 6, 84, 6, BLACK);
|
|
|
|
display.setFont(NULL);
|
|
|
|
|
|
|
|
// Check for connection
|
2020-05-20 23:06:03 +02:00
|
|
|
if (connected && dataReady) {
|
2020-05-20 19:33:05 +02:00
|
|
|
// Program name
|
2020-05-20 23:06:03 +02:00
|
|
|
drawName();
|
2020-05-20 19:33:05 +02:00
|
|
|
|
|
|
|
// Volume Bar
|
2020-05-20 23:06:03 +02:00
|
|
|
drawBar();
|
2020-05-20 19:33:05 +02:00
|
|
|
} else {
|
|
|
|
// Blink connection message if not connected
|
|
|
|
if (time % 500 == 0)
|
|
|
|
{
|
2020-05-20 23:06:03 +02:00
|
|
|
blink = (!blink) ? true : false;
|
2020-05-20 19:33:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 23:06:03 +02:00
|
|
|
if (!blink)
|
2020-05-20 19:33:05 +02:00
|
|
|
{
|
|
|
|
display.setCursor(9, 10);
|
|
|
|
display.print("Waiting for");
|
2020-05-20 23:06:03 +02:00
|
|
|
if (!connected)
|
|
|
|
{
|
|
|
|
display.setCursor(12, 18);
|
|
|
|
display.print("Connection");
|
|
|
|
} else if (!dataReady)
|
|
|
|
{
|
|
|
|
display.setCursor(30, 18);
|
|
|
|
display.print("Data");
|
|
|
|
}
|
2020-05-20 19:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bottom Bar
|
|
|
|
display.drawLine(0, 38, 84, 38, BLACK);
|
2020-05-20 23:06:03 +02:00
|
|
|
if (connected && dataReady)
|
2020-05-20 19:33:05 +02:00
|
|
|
{
|
|
|
|
display.setCursor(0, 40);
|
2020-05-20 23:06:03 +02:00
|
|
|
if (editing)
|
2020-05-20 19:33:05 +02:00
|
|
|
{
|
|
|
|
display.print(" - OK + ");
|
|
|
|
} else {
|
|
|
|
display.print(" < OK > ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
display.display();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bar position
|
|
|
|
const int pos[2] = { 4, 26 };
|
|
|
|
|
|
|
|
// Draw volume bar and text
|
2020-05-20 23:06:03 +02:00
|
|
|
void drawBar() {
|
2020-05-20 19:33:05 +02:00
|
|
|
// Set fonts and size
|
|
|
|
display.setFont(&Picopixel);
|
|
|
|
display.setCursor(4, pos[1] - 2);
|
|
|
|
display.print("Volume:");
|
|
|
|
|
|
|
|
// Blink percentage if editing
|
2020-05-20 23:06:03 +02:00
|
|
|
if (editing && time % 500 == 0)
|
2020-05-20 19:33:05 +02:00
|
|
|
{
|
2020-05-20 23:06:03 +02:00
|
|
|
blink = (!blink) ? 1 : 0;
|
2020-05-20 19:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Center percentage
|
2020-05-20 23:06:03 +02:00
|
|
|
if (volume == 0) {
|
2020-05-20 19:33:05 +02:00
|
|
|
display.setCursor(39, pos[1] - 2);
|
2020-05-20 23:06:03 +02:00
|
|
|
} else if (volume == 100) {
|
2020-05-20 19:33:05 +02:00
|
|
|
display.setCursor(36, pos[1] - 2);
|
|
|
|
} else {
|
|
|
|
display.setCursor(37, pos[1] - 2);
|
|
|
|
}
|
|
|
|
|
2020-05-20 23:06:03 +02:00
|
|
|
if (!blink)
|
2020-05-20 19:33:05 +02:00
|
|
|
{
|
2020-05-20 23:06:03 +02:00
|
|
|
display.print((String)round(volume) + "%");
|
2020-05-20 19:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw volume bar
|
|
|
|
display.drawRect(pos[0], pos[1], 76, 8, BLACK);
|
|
|
|
display.drawLine(pos[0], pos[1] + 8, pos[0], pos[1] + 9, BLACK);
|
|
|
|
display.drawPixel(pos[0] + (76 / 4) * 1 - 1, pos[1] + 8, BLACK);
|
|
|
|
display.drawLine(pos[0] + (76 / 4) * 2 - 1, pos[1] + 8, pos[0] + (76 / 4) * 2 - 1, pos[1] + 9, 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);
|
|
|
|
|
2020-05-20 23:06:03 +02:00
|
|
|
display.fillRect(pos[0], pos[1], round(volume / 100 * 76), 8, BLACK);
|
2020-05-20 19:33:05 +02:00
|
|
|
display.setFont(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw program name in the middle of the display or scroll it
|
2020-05-20 23:06:03 +02:00
|
|
|
void drawName() {
|
|
|
|
int width = 0;
|
|
|
|
for (size_t i = 0; i < sizeof(programName); i++)
|
2020-05-20 19:33:05 +02:00
|
|
|
{
|
2020-05-20 23:06:03 +02:00
|
|
|
if (programName[i] != NULL)
|
2020-05-20 19:33:05 +02:00
|
|
|
{
|
2020-05-20 23:06:03 +02:00
|
|
|
width = width + 6;
|
2020-05-20 19:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 23:06:03 +02:00
|
|
|
if (width > 84)
|
2020-05-20 19:33:05 +02:00
|
|
|
{
|
2020-05-20 23:06:03 +02:00
|
|
|
// TODO: Make the text scroll if the name is too long
|
|
|
|
} else {
|
|
|
|
int x = (84 / 2) - (width / 2);
|
|
|
|
display.setCursor(x, 10);
|
2020-05-20 19:33:05 +02:00
|
|
|
}
|
2020-05-20 23:06:03 +02:00
|
|
|
display.print(programName);
|
2020-05-20 19:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void processData() {
|
|
|
|
if (newData == true) {
|
|
|
|
strcpy(tempChars, receivedChars);
|
|
|
|
|
2020-05-20 19:54:05 +02:00
|
|
|
char * strtokIndx;
|
2020-05-20 19:33:05 +02:00
|
|
|
|
2020-05-20 19:54:05 +02:00
|
|
|
strtokIndx = strtok(tempChars,",");
|
|
|
|
strcpy(command, strtokIndx);
|
2020-05-20 19:33:05 +02:00
|
|
|
|
2020-05-20 19:54:05 +02:00
|
|
|
strtokIndx = strtok(NULL, ",");
|
2020-05-20 20:22:41 +02:00
|
|
|
|
|
|
|
if (strcmp("HELLO", command) == 0)
|
|
|
|
{
|
2020-05-20 23:06:03 +02:00
|
|
|
connected = true;
|
2020-05-20 20:22:41 +02:00
|
|
|
Serial.println("<OK>");
|
2020-05-21 23:15:54 +02:00
|
|
|
Serial.println("<COUNT>");
|
|
|
|
Serial.println("<SWITCH," + (String)currentProgram + ">");
|
2020-05-20 20:22:41 +02:00
|
|
|
}
|
|
|
|
else if (strcmp("COUNT", command) == 0)
|
|
|
|
{
|
2020-05-20 23:06:03 +02:00
|
|
|
programLenght = atoi(strtokIndx);
|
|
|
|
Serial.println("<OK>");
|
|
|
|
}
|
|
|
|
else if (strcmp("CURRENT", command) == 0)
|
|
|
|
{
|
|
|
|
currentProgram = atoi(strtokIndx);
|
|
|
|
Serial.println("<OK>");
|
2020-05-20 20:22:41 +02:00
|
|
|
}
|
|
|
|
else if (strcmp("NAME", command) == 0)
|
|
|
|
{
|
2020-05-20 23:06:03 +02:00
|
|
|
strcpy(programName, strtokIndx);
|
|
|
|
Serial.println("<OK>");
|
2020-05-20 20:22:41 +02:00
|
|
|
}
|
|
|
|
else if (strcmp("GETVOL", command) == 0)
|
|
|
|
{
|
2020-05-20 23:06:03 +02:00
|
|
|
volume = atoi(strtokIndx);
|
|
|
|
Serial.println("<OK>");
|
2020-05-20 19:54:05 +02:00
|
|
|
}
|
2020-05-20 19:33:05 +02:00
|
|
|
|
|
|
|
newData = false;
|
2020-05-20 23:06:03 +02:00
|
|
|
dataReady = true;
|
2020-05-20 19:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 19:54:05 +02:00
|
|
|
|
|
|
|
processData();
|
2020-05-20 19:33:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 23:06:03 +02:00
|
|
|
// 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--;
|
|
|
|
}
|
2020-05-21 23:15:54 +02:00
|
|
|
Serial.println("<SWITCH," + (String)currentProgram + ">");
|
2020-05-20 23:06:03 +02:00
|
|
|
} 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++;
|
|
|
|
}
|
2020-05-21 23:15:54 +02:00
|
|
|
Serial.println("<SWITCH," + (String)currentProgram + ">");
|
2020-05-20 23:06:03 +02:00
|
|
|
} else {
|
|
|
|
if (volume != 100)
|
|
|
|
{
|
|
|
|
Serial.println("<SETVOL," + (String)(volume + 1) + ">");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
position = newPosition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (button.pressed())
|
|
|
|
{
|
|
|
|
editing = (editing) ? 0 : 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:33:05 +02:00
|
|
|
// Init arduino
|
|
|
|
void setup() {
|
|
|
|
// Start serial connection
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
|
|
|
// Init button
|
|
|
|
button.begin();
|
|
|
|
|
|
|
|
// Init display
|
|
|
|
display.begin();
|
|
|
|
display.setContrast(50);
|
|
|
|
toggleBacklight();
|
|
|
|
display.clearDisplay();
|
|
|
|
display.display();
|
|
|
|
|
|
|
|
// TODO: Remove testing variables
|
|
|
|
currentProgram = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// Check time
|
|
|
|
time = millis();
|
2020-05-20 23:06:03 +02:00
|
|
|
menuUpdate = (time % 1000 == 0) ? true : false;
|
|
|
|
|
|
|
|
if (programLenght == 0 || volume == -1 || strcmp("", programName) == 0)
|
|
|
|
{
|
|
|
|
dataReady = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!editing && connected && dataReady)
|
|
|
|
{
|
|
|
|
blink = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-20 19:33:05 +02:00
|
|
|
|
|
|
|
// Check for serial data or commands
|
|
|
|
checkSerial();
|
|
|
|
|
|
|
|
// Check if serial connection is established
|
2020-05-21 23:15:54 +02:00
|
|
|
if (connected && dataReady)
|
2020-05-20 19:33:05 +02:00
|
|
|
{
|
|
|
|
// Draw menu every second or every 0.1 second when editing
|
2020-05-20 23:06:03 +02:00
|
|
|
if (menuUpdate || (editing && time % 100 == 0)) {
|
|
|
|
menuUpdate = false;
|
|
|
|
drawMenu();
|
2020-05-20 19:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check input
|
|
|
|
checkInput(time);
|
|
|
|
|
|
|
|
} else {
|
2020-05-20 23:06:03 +02:00
|
|
|
if (menuUpdate)
|
2020-05-20 19:33:05 +02:00
|
|
|
{
|
|
|
|
// Draw menu with "Waiting for connection", name and volume are ignored bij drawMenu()
|
2020-05-20 23:06:03 +02:00
|
|
|
menuUpdate = false;
|
|
|
|
drawMenu();
|
2020-05-20 19:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|