Files
GravityHW/software/GToE/GToE.ino

340 lines
8.9 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 5
#define MAXBPM 250 //250 at 24ppqn with 5ms pulse will be 50/50 square wave
#define MINBPM 20
#define INPUT_CONNECTED_PIN 1
#define INPUT_PIN 2 //needs to be an interrupt pin
#define ENC_BTN_PIN 17
#define ENC_D1_PIN 4
#define ENC_D2_PIN 3
#define START_STOP_BTN_PIN 14
#define ANALOGUE_INPUT_1_PIN A1
#define ANALOGUE_INPUT_2_PIN A1
const int outsPins[6] = {5, 6, 7, 8, 9, 10};
const int outsModes[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
int outsModeIndex[6] = {8, 9, 10, 7, 6, 6}; //10 - /1, 9 - off
bool outsRandom[6] = {0,0,0,0,0,1};
bool outsMod[6] = {0,0,0,0,0,1}; //0 - A1, 1 - A2
int outsPeriods[6];
int outsClocksCounts[6];
int outsModesPlay[6]; //actual channel modes array updated from outsModeIndex each beat
bool clockMode;
bool clockModeOld;
int inputMode; //clock, reset, start/stop
int clockCount = 0;
int bpm = 130;
int pulseClockCount = 0;
int pulseCount = 0;
int pulsePeriod;
bool isPlaying = 0;
int needToResetChannel;
bool beatCounted = false;
bool pulseCounted = false;
int displayTab = 0;
int displayTabOld;
bool needToChangeTab = 0;
bool buttonPushed = false;
int a1Input = 0;
int a2Input = 0;
bool changesSaved;
int encPositionOld = 0;
unsigned long encPressedTime;
unsigned long encReleasedTime;
bool encPressRegistered;
Adafruit_SSD1306 display(128, 64, &Wire, -1);
RotaryEncoder encoder(ENC_D1_PIN, ENC_D2_PIN, RotaryEncoder::LatchMode::TWO03);
void setup() {
Serial.begin(9600);
pinMode(INPUT_CONNECTED_PIN, INPUT_PULLUP);
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); //probably will need interrupt
for (int i=0; i<6; i++) {
pinMode(outsPins[i], OUTPUT);
}
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
updateScreen();
updatePeriod();
FlexiTimer2::set(1, 1.0/1000, internalClock); // 1.0/1000 = 1ms period
FlexiTimer2::start();
}
void loop() {
checkInputs();
}
void internalClock() {
if (isPlaying) {
// Action on each pulse
if (pulseClockCount == 0 && !pulseCounted) {
//modulation
for (int i = 0; i<6; i++) {
if (!outsMod[i]) {
outsModesPlay[i] = outsModes[outsModeIndex[i] - a1Input];
} else {
outsModesPlay[i] = outsModes[outsModeIndex[i] - a2Input];
}
}
//divider
if (pulseCount == 0 && !beatCounted) {
for (int i = 0; i<6; i++) {
outsModesPlay[i] = outsModes[outsModeIndex[i]]; //updated here to prevent sync problems for multipliers
/*if (a1Input != 0 && i == 5) { //TESTING THE MODULATION ON 6th CHANNEL
outsModesPlay[i] = outsModes[outsModeIndex[i] - a1Input];
}*/
if (outsModesPlay[i] > 0) {
if (outsClocksCounts[i] == 0) { //Pulse on 0
if (outsRandom[i] == 0 || (outsRandom[i] == 1 && random(2))) { //random
digitalWrite(outsPins[i], HIGH);
}
}
if (outsClocksCounts[i] < (outsModesPlay[i] - 1)) {
outsClocksCounts[i]++;
} else {
outsClocksCounts[i] = 0;
}
}
}
beatCounted = true;
}
//multiplier
for (int i = 0; i<6; i++) {
if (outsModesPlay[i] < 0) {
if (outsClocksCounts[i] == 0) { //Pulse on 0
if (outsRandom[i] == 0 || (outsRandom[i] == 1 && random(2))) { //random
digitalWrite(outsPins[i], HIGH);
}
}
if (outsClocksCounts[i] < (PPQN / abs(outsModesPlay[i])) - 1) {
outsClocksCounts[i]++;
} else {
outsClocksCounts[i] = 0;
}
}
}
pulseCounted = true;
}
//internal pulse
if (pulseClockCount == 0) {
pulseCount++;
beatCounted = false;
pulseCounted = false;
}
if (pulseClockCount < pulsePeriod) {
pulseClockCount++;
} else {
pulseClockCount = 0;
}
if (pulseCount >= PPQN) {
pulseCount = 0;
}
// pull low all outputs after set pulse length
if (pulseClockCount >= PULSE_LENGTH) {
for (int i = 0; i<6; i++) {
digitalWrite(outsPins[i], LOW);
}
}
}
}
void updatePeriod() {
pulsePeriod = 60000 / (bpm * PPQN);
}
void resetClocks() {
for (int i = 0; i<6; i++) {
outsClocksCounts[i] = 0;
}
}
void checkInputs() {
//input jack switcch
clockMode = digitalRead(INPUT_CONNECTED_PIN);
if (clockMode != clockModeOld) {
updateScreen();
clockModeOld = clockMode;
}
//encoder button
if (!digitalRead(ENC_BTN_PIN) && !encPressRegistered) {
encPressRegistered = true;
encPressedTime = millis();
} else if (digitalRead(ENC_BTN_PIN) && encPressRegistered) {
encPressRegistered = false;
encReleasedTime = millis();
Serial.println(encReleasedTime - encPressedTime);
if (encReleasedTime - encPressedTime < 500) { // press shorter than .5s switches tabs
displayTabOld = displayTab;
displayTab++;
if (displayTab>6) {
displayTab = 0;
}
needToChangeTab = 0;
updateScreen();
} else if (encReleasedTime - encPressedTime < 2000 && displayTab != 0) { // longer press (<2s) and switches random mode, longer than 2s presses are ignored
outsRandom[displayTab-1] = !outsRandom[displayTab-1];
updateScreen();
}
}
//encoder
encoder.tick();
int encPosition = encoder.getPosition();
if (encPositionOld != encPosition) {
int change = encPositionOld - encPosition;
if (displayTab == 0) {
bpm = bpm + change;
if (bpm > MAXBPM) {
bpm = MAXBPM;
} else if (bpm < MINBPM) {
bpm = MINBPM;
}
updatePeriod();
} else {
outsModeIndex[displayTab-1] = outsModeIndex[displayTab-1] - change;
if (outsModeIndex[displayTab-1] < 0) {
outsModeIndex[displayTab-1] = 0;
} else if (outsModeIndex[displayTab-1] > (sizeof(outsModes)/sizeof(int)) - 1) {
outsModeIndex[displayTab-1] = (sizeof(outsModes)/sizeof(int)) - 1;
}
needToResetChannel = displayTab-1;
}
updateScreen();
encPositionOld = encPosition;
}
//play button
if (!digitalRead(START_STOP_BTN_PIN) && !buttonPushed) {
isPlaying = !isPlaying;
resetClocks();
buttonPushed = true;
} else if (digitalRead(START_STOP_BTN_PIN) && buttonPushed) {
buttonPushed = false;
}
//modulations
a1Input = analogRead(ANALOGUE_INPUT_1_PIN);
a1Input = map (a1Input, 0, 1023, 0, 4);
a2Input = analogRead(ANALOGUE_INPUT_2_PIN);
Serial.println(a2Input);
a2Input = map (a2Input, 0, 1023, 0, 4);
}
void updateScreen() {
display.clearDisplay();
//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.println(F(" "));
display.fillRect(108, 2, 20, 8, SSD1306_WHITE);
display.println();
display.println();
display.fillRect(0, 10, 128, 2, SSD1306_WHITE);
//Content
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
if (displayTab == 0) {
display.print(bpm);
display.println(F("bpm"));
} else {
if (outsModes[outsModeIndex[displayTab-1]] == 0) {
display.print(F(" OFF"));
} else if (outsModes[outsModeIndex[displayTab-1]]>0) {
display.print(F(" /"));
display.print(abs(outsModes[outsModeIndex[displayTab-1]]));
} else {
display.print(F(" x"));
display.print(abs(outsModes[outsModeIndex[displayTab-1]]));
}
}
display.println();
display.setTextSize(1);
display.println();
//Extra params
display.setTextSize(1);
display.print(F("RND:"));
if (displayTab != 0) {
if (outsRandom[displayTab-1]) {
display.print(F("On"));
} else {
display.print(F("Off"));
}
display.setCursor(64,50);
display.print(F("MOD:"));
if (outsMod[displayTab-1]) {
display.print(F("A2 "));
} else {
display.print(F("A1 "));
}
}
display.display();
}