Files
GravityHW/software/GToE/GToE.ino
2023-05-08 01:50:59 +03:00

468 lines
14 KiB
C++

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RotaryEncoder.h>
#include <FlexiTimer2.h>
#include <EEPROM.h>
#define SCREEN_ADDRESS 0x3C
#define PPQN 24
#define PULSE_LENGTH 12 //ms (with 12 ms you can't get higher than 208bpm)
#define MAXBPM 200 //250 at 24ppqn with 5ms pulse will be 50/50 square wave
#define MINBPM 20
#define SCREEN_TIMEOUT 30000 //30 sec
#define INPUT_PIN 2 //needs to be an interrupt pin
#define ENC_BTN_PIN 14
#define ENC_D1_PIN 17
#define ENC_D2_PIN 4
#define START_STOP_BTN_PIN 5
#define ANALOGUE_INPUT_1_PIN A2
#define ANALOGUE_INPUT_2_PIN A1
const int outsPins[6] = {6, 11, 7, 10, 8, 9};
const int clockModes[18] = {-24, -16, -12, -8, -6, -4, -3, -2, 1, 2, 3, 4, 5, 6, 7, 8, 16, 32}; //positive - divide, negative - multiply, 0 - off
unsigned int bpm = 130;
struct channel {
unsigned int mode;
unsigned int random;
bool modulationChannel; //0 - A1, 1 - A2
int modulationRange;
unsigned int offset;
};
channel channels[6] = { //array of channel settings
{ 8, 0, 0, 0, 0 },
{ 8, 0, 0, 0, 0 },
{ 8, 0, 0, 0, 0 },
{ 8, 0, 0, 0, 0 },
{ 8, 0, 0, 0, 0 },
{ 8, 0, 0, 0, 0 }
};
int channelPulseCount[6];
int channelPulsesPerCycle[6];
int playingModes[6]; //actual channel modes array updated from channels each beat
unsigned int pulsePeriod;
bool isPlaying = false;
bool pulseCounted = false;
unsigned int tickCount = 0;
unsigned int pulseCount = 0;
unsigned int masterClockMode = 0; // 0 - internal, 1 - external 24ppqn, 2 - external beat
bool externalPulseCounted = true;
unsigned long lastExtPulseTime;
unsigned long newExtPulseTime;
unsigned int displayTab = 0;
unsigned int displayTabOld;
unsigned int insideTab = 0;
bool playBtnPushed = false;
int a1Input = 0;
int a2Input = 0;
int encPositionOld = 0;
unsigned long encPressedTime;
unsigned long encReleasedTime;
bool encPressRegistered;
unsigned long lastInteractionTime;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
RotaryEncoder encoder(ENC_D1_PIN, ENC_D2_PIN, RotaryEncoder::LatchMode::TWO03);
const unsigned char splash_logo [] PROGMEM = {
0x00, 0x1f, 0xfc, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x00, 0x00, 0x01, 0xff, 0xff, 0xc0, 0x00, 0x03,
0xfc, 0x1f, 0xe0, 0x00, 0x07, 0xe0, 0x03, 0xf0, 0x00, 0x0f, 0x80, 0x00, 0xf8, 0x00, 0x1f, 0x00,
0x00, 0x7c, 0x00, 0x1e, 0x00, 0x00, 0x3c, 0x00, 0x3c, 0x00, 0x00, 0x1e, 0x00, 0x3c, 0x00, 0x00,
0x1e, 0x00, 0x78, 0x0c, 0x18, 0x0f, 0x00, 0x78, 0x1e, 0x3c, 0x0f, 0x00, 0x70, 0x1e, 0x3c, 0x07,
0x00, 0xf0, 0x1e, 0x3c, 0x07, 0x80, 0xf0, 0x1e, 0x3c, 0x07, 0x80, 0xf0, 0x1e, 0x3c, 0x07, 0x80,
0xf0, 0x1e, 0x3c, 0x07, 0x80, 0xf0, 0x0c, 0x18, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0x0c, 0x18, 0x10, 0x00, 0x0f, 0x1e, 0x3c, 0x78, 0x00, 0x0f, 0x1e,
0x3c, 0x78, 0x00, 0x1e, 0x0c, 0x18, 0x3c, 0x00, 0x1e, 0x00, 0x00, 0x3c, 0x00, 0x3c, 0x00, 0x00,
0x1e, 0x00, 0x3c, 0x00, 0x00, 0x1e, 0x00, 0x78, 0x00, 0x00, 0x0f, 0x00, 0x78, 0x00, 0x00, 0x0f,
0x00, 0xf0, 0x00, 0x00, 0x07, 0x80, 0xf0, 0x00, 0x00, 0x07, 0x80, 0xf0, 0x00, 0x00, 0x07, 0x80,
0x78, 0x00, 0x00, 0x0f, 0x00, 0x7c, 0x00, 0x00, 0x1f, 0x00, 0x3e, 0x00, 0x00, 0x3e, 0x00, 0x3f,
0xff, 0xff, 0xfe, 0x00, 0x1f, 0xff, 0xff, 0xfc, 0x00, 0x07, 0xff, 0xff, 0xf0, 0x00, 0x01, 0xff,
0xff, 0xc0, 0x00
};
void setup() {
//Serial.begin(9600);
//check last bit in eeprom to know if settings were stored
if (EEPROM.read(1023) == 'K') {
EEPROM.get(0, bpm);
EEPROM.get(sizeof(int), channels);
} else {
saveState();
EEPROM.write(1023, 'K');
}
pinMode(ENC_BTN_PIN, INPUT_PULLUP);
pinMode(START_STOP_BTN_PIN, INPUT_PULLUP);
pinMode(START_STOP_BTN_PIN, ANALOGUE_INPUT_1_PIN);
pinMode(INPUT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INPUT_PIN), externalClock, RISING);
for (int i=0; i<6; i++) {
pinMode(outsPins[i], OUTPUT);
}
calculateCycles();
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
//Splash screen
display.clearDisplay();
display.drawBitmap(48, 18, splash_logo, 33, 39, 1);
display.setCursor(90,1);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.println(F("V:0.1a")); //Version
display.display();
delay(800);
updateScreen();
updateTiming();
FlexiTimer2::set(1, 1.0/1000, clock); // 1.0/1000 = 1ms period. If other than 1ms updateTiming() might need tweaking
FlexiTimer2::start();
}
void loop() {
checkInputs();
if ((millis() - lastInteractionTime) > SCREEN_TIMEOUT) {
display.clearDisplay();
display.display();
}
}
void clock() {
if (isPlaying) {
// Action on each pulse
if (tickCount == 0 && !pulseCounted) {
sendTriggers();
pulseCounted = true;
}
//this part gets the Pulse and Ticks ticking
//it's placed after the triggers to avoid problems on the start (when pulseCount==0)
if (tickCount < pulsePeriod) {
tickCount++;
} else {
tickCount = 0;
if (pulseCount < (PPQN-1)) { //-1 is here to avoid extra IF to reset to 0
pulseCount++;
} else {
pulseCount = 0;
}
}
//pulse and beat count reset
if (tickCount == 0) {
pulseCounted = false;
}
// pull low all outputs after set pulse length
if (tickCount >= PULSE_LENGTH) {
for (int i = 0; i<6; i++) {
digitalWrite(outsPins[i], LOW);
}
}
}
}
void sendTriggers() {
//switching modes on the beat and resetting channel clock
if (pulseCount == 0) {
for (int i = 0; i<6; i++) {
if (playingModes[i] != clockModes[channels[i].mode]) {
playingModes[i] = clockModes[channels[i].mode];
if (playingModes[i] > 0) {
channelPulsesPerCycle[i] = (playingModes[i] * PPQN) - 1;
} else {
channelPulsesPerCycle[i] = (PPQN / abs(playingModes[i])) - 1;
}
channelPulseCount[i] = 0;
}
}
}
//multiplier
for (int i = 0; i<6; i++) {
if (channelPulseCount[i] == channels[i].offset) { //Pulse on 0
if (channels[i].random == 0 || random(10) > channels[i].random) { //random
digitalWrite(outsPins[i], HIGH);
}
}
if (channelPulseCount[i] < channelPulsesPerCycle[i]) {
channelPulseCount[i]++;
} else {
channelPulseCount[i] = 0;
}
}
}
void calculateCycles() {
for (int i = 0; i<6; i++) {
if (playingModes[i] > 0) {
channelPulsesPerCycle[i] = (playingModes[i] * PPQN) - 1;
} else {
channelPulsesPerCycle[i] = (PPQN / abs(playingModes[i])) - 1;
}
}
}
void externalClock() {
externalPulseCounted = false;
}
void updateTiming() {
if (masterClockMode == 0) {
pulsePeriod = 60000 / (bpm * PPQN);
} else if (masterClockMode == 2) { //for external beat clock
pulsePeriod = (newExtPulseTime - lastExtPulseTime) / PPQN;
}
}
void resetClocks() {
for (int i = 0; i<6; i++) {
channelPulseCount[i] = 0;
digitalWrite(outsPins[i], LOW); //to avoid stuck leds
}
}
void saveState() {
EEPROM.put(0, bpm);
EEPROM.put(sizeof(int), channels);
}
void checkInputs() {
//encoder button
if (!digitalRead(ENC_BTN_PIN) && !encPressRegistered) {
encPressRegistered = true;
encPressedTime = millis();
} else if (digitalRead(ENC_BTN_PIN) && encPressRegistered) {
encPressRegistered = false;
encReleasedTime = millis();
if (encReleasedTime - encPressedTime < 500) { // press shorter than .5s switches tabs
if (insideTab == 0) {
displayTabOld = displayTab;
displayTab++;
if (displayTab>6) {
displayTab = 0;
}
} else if (insideTab < 3) {
insideTab ++;
} else {
insideTab = 1;
}
updateScreen();
} else if (encReleasedTime - encPressedTime < 2000 && displayTab != 0) { // longer press (<2s) and switches random mode, longer than 2s presses are ignored
if (insideTab == 0) {
insideTab = 1;
} else {
insideTab = 0;
}
updateScreen();
}
else if (encReleasedTime - encPressedTime < 2000 && displayTab == 0) { // switch to external clock mode
if (masterClockMode < 2) {
masterClockMode++;
} else {
masterClockMode = 0;
}
updateScreen();
}
}
//encoder
encoder.tick();
int encPosition = encoder.getPosition();
if (encPositionOld != encPosition) {
int change = encPositionOld - encPosition;
if (displayTab == 0 && masterClockMode == 0) {
bpm = bpm + change;
if (bpm > MAXBPM) {
bpm = MAXBPM;
} else if (bpm < MINBPM) {
bpm = MINBPM;
}
updateTiming();
} else if (displayTab != 0 && insideTab == 0) {
channels[displayTab-1].mode = channels[displayTab-1].mode - change;
if (channels[displayTab-1].mode == 65535) { //65535 is 0-1 for unsigned vars
channels[displayTab-1].mode = 0;
} else if (channels[displayTab-1].mode > (sizeof(clockModes)/sizeof(int)) - 1) {
channels[displayTab-1].mode = (sizeof(clockModes)/sizeof(int)) - 1;
}
} else if (displayTab != 0 && insideTab == 1) { //random
channels[displayTab-1].random = channels[displayTab-1].random + change;
if (channels[displayTab-1].random > 9 || channels[displayTab-1].random < 0) {
channels[displayTab-1].random = 0;
}
} else if (displayTab != 0 && insideTab == 2) { //modulation
channels[displayTab-1].modulationRange = channels[displayTab-1].modulationRange + change;
if (channels[displayTab-1].modulationRange > 6 || channels[displayTab-1].modulationRange < -6) {
channels[displayTab-1].modulationChannel = !channels[displayTab-1].modulationChannel;
channels[displayTab-1].modulationRange = 0;
}
} else if (displayTab != 0 && insideTab == 3) { //offset
channels[displayTab-1].offset = channels[displayTab-1].offset + change;
if (channels[displayTab-1].offset >= channelPulsesPerCycle[displayTab-1]) {
channels[displayTab-1].offset = channelPulsesPerCycle[displayTab-1];
} else if (channels[displayTab-1].offset <= 0 ) {
channels[displayTab-1].offset = 0;
}
}
updateScreen();
encPositionOld = encPosition;
}
//play button
if (!digitalRead(START_STOP_BTN_PIN) && !playBtnPushed) {
isPlaying = !isPlaying;
resetClocks();
playBtnPushed = true;
saveState();
updateScreen(); //to wake up the screen if turned off
} else if (digitalRead(START_STOP_BTN_PIN) && playBtnPushed) {
playBtnPushed = false;
}
//modulations
a1Input = analogRead(ANALOGUE_INPUT_1_PIN);
a2Input = analogRead(ANALOGUE_INPUT_2_PIN);
}
void updateScreen() {
display.clearDisplay();
lastInteractionTime = millis(); //not sure if it's a right place for this, but should do for now
//Tabs
display.drawRect(0, 0, 128, 2, SSD1306_WHITE);
display.setCursor(0,2);
display.setTextSize(1);
if (displayTab == 0) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
display.print(F(" "));
display.setTextColor(SSD1306_WHITE);
display.print(F(" bpm "));
} else {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
display.print(F(" bpm"));
}
for (int i = 1; i <= 6; i++) {
if (displayTab == i) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
display.print(" ");
display.setTextColor(SSD1306_WHITE);
display.print(" ");
display.print(i);
display.print(" ");
} else {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
display.print(" ");
display.print(i);
}
}
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
display.fillRect(108, 2, 20, 8, SSD1306_WHITE);
display.println();
display.println();
display.fillRect(0, 10, 128, 2, SSD1306_WHITE);
//Content
display.setCursor(4,16);
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
if (displayTab == 0 && masterClockMode == 0) {
display.print(bpm);
display.println(F("bpm"));
} else if (displayTab == 0 && masterClockMode == 1) {
display.println(F("EXT-24"));
} else if (displayTab == 0 && masterClockMode == 2) {
display.println(F("EXT-B"));
} else {
if (clockModes[channels[displayTab-1].mode] == 0) {
display.print(F("OFF"));
} else if (clockModes[channels[displayTab-1].mode]>0) {
display.print(F("/"));
display.print(abs(clockModes[channels[displayTab-1].mode]));
} else {
display.print(F("x"));
display.print(abs(clockModes[channels[displayTab-1].mode]));
}
}
display.println();
display.setTextSize(1);
display.println();
//Extra params
display.setCursor(58,16);
display.setTextSize(1);
if (displayTab != 0) {
if (insideTab == 1) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
} else {
display.setTextColor(SSD1306_WHITE);
}
display.print(F(" RND:"));
if (channels[displayTab-1].random > 0) {
display.print(channels[displayTab-1].random);
display.print(F("0% "));
} else {
display.print(F("Off "));
}
if (insideTab == 2) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
} else {
display.setTextColor(SSD1306_WHITE);
}
display.setCursor(58,30);
display.print(F(" MOD:"));
if (channels[displayTab-1].modulationChannel && channels[displayTab-1].modulationRange != 0) {
display.print(F("A2 "));
if (channels[displayTab-1].modulationRange > 0) {
display.print(F("+"));
}
display.print(channels[displayTab-1].modulationRange);
display.print(F(" "));
} else if (!channels[displayTab-1].modulationChannel && channels[displayTab-1].modulationRange != 0) {
display.print(F("A1 "));
if (channels[displayTab-1].modulationRange > 0) {
display.print(F("+"));
}
display.print(channels[displayTab-1].modulationRange);
display.print(F(" "));
} else {
display.print(F("Off "));
}
if (insideTab == 3) {
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
} else {
display.setTextColor(SSD1306_WHITE);
}
display.setCursor(58,44);
display.print(F(" OFT:"));
display.print(channels[displayTab-1].offset);
display.print(F(" "));
}
display.display();
}