#include <Arduino.h>
#include "usbh_common.h"
#include "sa_c4.h"
#include "SPI.h"
#include "ILI9341_t3.h"
#include "font_Arial.h"
#include <Encoder.h>
// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC);
Encoder myEnc(18, 19); //encoder 1
Encoder myEnc2(20, 21); // encoder 2
// Important! Do not access device until this has been set
static int deviceReady = 0;
int driver_callback (uint32_t msg, intptr_t *value1, uint32_t value2)
{
if (msg == USBD_MSG_DEVICEREADY){
printf("\r\n - C4 Synth ready - \r\n\r\n");
deviceReady = 1;
}
return 1;
}
void setup ()
{
//////////////////////////////////////////////////////////
pinMode(4, INPUT_PULLUP);
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setRotation(2);
/////////////////////////////////////////////////////////
Serial.begin(2000000);
//while (!Serial); // wait for Arduino Serial Monitor wont run without opening serial monitor
printf("Source Audio C4 demo\r\n");
usb_start(AS_PID_C4SYNTH);
SaC4_setCallbackFunc(driver_callback);
}
long oldPosition = -999; // encoder 1
long oldPosition2 = -999; // encoder 2
void loop ()
{
usb_task();
if (!deviceReady){
delay(10);
return;
}
else if (deviceReady == 1)
{
long newPosition = myEnc.read(); //encoder 1
long myPosition = newPosition / 4; //encoder 1
long newPosition2 = myEnc2.read(); // encoder 2
long myPosition2 = newPosition2 / 4; // encoder 2
// encoder 1
if (newPosition != oldPosition)
{
oldPosition = newPosition;
if (newPosition == myPosition * 4)
{
Serial.println(newPosition);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK); tft.setTextSize(3); //white text with black background
if(myPosition > 254)
myPosition = 254;
if(myPosition < 0)
myPosition = 0;
util_setCtrlValue("output", myPosition);
tft.setCursor(0, 100);
if (myPosition < 10)
{
tft.print(" ");
tft.setCursor(0, 100);
tft.print(myPosition);
}
else if (myPosition >= 10)
{
tft.print(" ");
tft.setCursor(0, 100);
tft.print(myPosition);
}
}
}
// encoder 2
if (newPosition2 != oldPosition2)
{
oldPosition2 = newPosition2;
if (newPosition2 == myPosition2 * 4)
{
//Serial.println(newPosition);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK); tft.setTextSize(3);
if(myPosition2 > 254)
myPosition2 = 254;
if(myPosition2 < 0)
myPosition2 = 0;
util_setCtrlValue("master_depth", myPosition2);
tft.setCursor(0, 150);
if (myPosition2 < 10)
{
tft.print(" ");
tft.setCursor(0, 150);
tft.print(myPosition2);
}
else if (myPosition2 >= 10)
{
tft.print(" ");
tft.setCursor(0, 150);
tft.print(myPosition2);
}
}
}
// Read the "output" value of the currently selected preset and update the encoder logic which should in turn update the number on LCD
if (digitalRead(4) == HIGH)
{}
else
{
//util_getCtrlValue("output");
//??????
//newPosition ==
}
}
delay(30);
}