Files
GravityHW/Software/Gravity/Gravity.ino

494 lines
15 KiB
C++

#include <Wire.h>
//#include <Adafruit_GFX.h>
//#include <Adafruit_SSD1306.h>
#include <RotaryEncoder.h>
#include <FlexiTimer2.h>
#include <EEPROM.h>
#include <U8g2lib.h>
#define VERSION "0.9b"
#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 600000 //Turn display off after 5 min
/* Rev 1 Config
#define ENC_BTN_PIN 14
#define ENC_D1_PIN 17
#define ENC_D2_PIN 4
#define START_STOP_BTN_PIN 5
#define EXT_INPUT_PIN 2 //needs to be an interrupt pin
#define ANALOGUE_INPUT_1_PIN A2
#define ANALOGUE_INPUT_2_PIN A1
const int outsPins[6] = {6, 11, 7, 10, 8, 9};
*/
// Rev 2 and 3 Config
#define ENC_BTN_PIN 14
#define ENC_D1_PIN 17
#define ENC_D2_PIN 4
#define START_STOP_BTN_PIN 5
#define SHIFT_BTN_PIN 12
#define EXT_INPUT_PIN 2 //needs to be an interrupt pin
#define ANALOGUE_INPUT_1_PIN A7
#define ANALOGUE_INPUT_2_PIN A6
const byte outsPins[6] = { 7, 8, 10, 6, 9, 11 };
const int subDivs[17] = { -24, -12, -8, -6, -4, -3, -2, 1, 2, 3, 4, 5, 6, 7, 8, 16, 32 }; //positive - divide, negative - multiply, 0 - off
byte bpm = 130;
byte bpmModulationChannel = 200; //0 - CV1, 1 - CV2, 255 - OFF
byte bpmModulationRange = 0;
struct channel {
byte mode; //0 - CLK, 1 - RND, 2 - SEQ
byte subDiv;
byte CV1Target; //0 - Off, 1 - Subdiv, 2 - RND, 3 - SeqPattern
byte CV1Value;
byte CV2Target;
byte CV2Value;
byte offset;
byte random;
byte seqPattern;
};
channel channels[6] = { //array of channel settings
{ 0, 7, 0, 3, 0, 3, 0, 0, 0 },
{ 0, 7, 0, 3, 0, 3, 0, 0, 0 },
{ 0, 7, 0, 3, 0, 3, 0, 0, 0 },
{ 0, 7, 0, 3, 0, 3, 0, 0, 0 },
{ 0, 7, 0, 3, 0, 3, 0, 0, 0 },
{ 0, 7, 0, 3, 0, 3, 0, 0, 0 }
};
bool seqA1[16] = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0};
bool seqA2[16] = {0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0};
bool seqA3[16] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
bool seqA4[16] = {1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0};
bool seqA5[16] = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0};
bool seqA6[16] = {0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0};
bool seqA7[16] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
bool seqA8[16] = {1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0};
bool seqB1[16] = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0};
bool seqB2[16] = {0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0};
bool seqB3[16] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
bool seqB4[16] = {1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0};
bool seqB5[16] = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0};
bool seqB6[16] = {0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0};
bool seqB7[16] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
bool seqB8[16] = {1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0};
bool *currentSeq;
byte currentStep = 0;
byte memCode = 'a'; //Change to different letter if you changed the data structure
unsigned int channelPulseCount[6];
unsigned int channelPulsesPerCycle[6];
byte sixteenthPulseCount = 0;
int playingModes[6]; //actual channel modes array updated from channels object on each beat
unsigned int pulsePeriod;
bool isPlaying = false;
unsigned int tickCount = 0;
unsigned int pulseCount = 0;
byte masterClockMode = 0; // 0 - internal, 1 - external 24ppqn, 2 - external beat
unsigned long lastExtPulseTime;
unsigned long newExtPulseTime;
bool needPulseReset[6] = { true, true, true, true, true, true };
byte displayTab = 0;
bool insideTab = false;
byte menuItem = 0;
byte lastMenuItem = 3;
byte displayScreen = 0; //0 - main, 1 - sequencer, 2 - settings
bool playBtnPushed = false;
bool shiftBtnPushed = false;
int a1Input = 0;
int a2Input = 0;
int encPositionOld = 0;
unsigned long encPressedTime;
unsigned long encReleasedTime;
bool encPressRegistered;
//unsigned long lastInteractionTime; // used for display timeout
U8G2_SSD1306_128X64_NONAME_2_HW_I2C u8g2(U8G2_R2, SCL, SDA, U8X8_PIN_NONE);
RotaryEncoder encoder(ENC_D1_PIN, ENC_D2_PIN, RotaryEncoder::LatchMode::TWO03);
const uint8_t velvetscreen[597] U8G2_FONT_SECTION("velvetscreen") =
"I\0\2\2\3\3\3\3\4\5\5\0\0\5\0\5\0\0\363\0\0\2\70!\6)\251\254\0\42\6\23"
"\317$\25#\12-\351UC\345\241*\0$\11,\331\215\24e\32\11%\12-\351\250\244r\245\222\0"
"&\11-\351\351*\311\250\2'\5\21\257\10(\6*\271\251\62)\10*\271\304\224\24\0*\11-\351"
"V\325jF\0+\7\33\313\245\225\0,\5\21\251\10-\5\13\315\14.\5\11\251\4/\7$\331\307"
"\66\0\60\10,\331\251h&\5\61\7+\311\310\326\0\62\11,\331l\224T\36\1\63\11,\331l\224"
"\64\32\11\64\10,\331D\71t\1\65\11,\331\14\275\321H\0\66\11,\331\251\274bR\0\67\10,"
"\331\214\34\353\10\70\11,\331\251\230TL\12\71\11,\331\251\230vR\0:\6\31\253\244\0;\6!"
"\251$\1<\7+\311\246\272\0=\6\33\313\354\1>\7+\311\344\252\4\77\11,\331l\224\64\216\0"
"@\12-\351\255J\215\14\27\0A\10,\331\251\70\246\14B\11,\331\254\70R\34\2C\11,\331\251"
"\250cR\0D\10,\331\254h\216\4E\10,\331\14=\364\10F\10,\331\14\275r\6G\10,\331"
"\215\234f\32H\10,\331D\71\246\14I\6)\251\14\1J\10,\331\327\62)\0K\11,\331D\225"
"d*\3L\7,\331\344\366\10M\11-\351\344\265\222\326\1N\11-\351\344\251\222\334\1O\10,\331"
"\251h&\5P\11,\331\254\70R\316\0Q\10,\331\251h\305\24R\10,\331\254\70R\63S\11,"
"\331\215\214\64\32\11T\7+\311\254\330\2U\10,\331DgR\0V\12-\351d\235\312\224#\0W"
"\12-\351d\225TR]\0X\11,\331D\231T\224\1Y\12-\351\344Tg\224Q\4Z\7+\311"
"\314T\16[\6*\271\254J\134\11$\331d\224QF\1]\6*\271\250j^\5\23\317\65_\6\14"
"\331\214\0o\7[\333\214\64\2p\11-\351-\265\227Z\0q\7-\351\35_\13r\10+\311D\65"
"T\4s\7-\351\334\366\30t\7-\351\374G\0u\10-\351\334*\351\61v\5\211\335\4w\12-"
"\351\255J\215\234\26\0x\6\33\311\244\16\0\0\0\4\377\377\0";
void setup() {
//Serial.begin(9600);
//check last bit in eeprom to know if the correct settings were stored
if (EEPROM.read(1023) == memCode) {
int addr = 0;
EEPROM.get(addr, bpm);
addr = addr + sizeof(bpm);
EEPROM.get(addr, bpmModulationChannel);
addr = addr + sizeof(bpmModulationChannel);
EEPROM.get(addr, bpmModulationRange);
addr = addr + sizeof(bpmModulationRange);
EEPROM.get(addr, masterClockMode);
addr = addr + sizeof(masterClockMode);
EEPROM.get(addr, channels);
addr = addr + sizeof(channels);
EEPROM.get(addr, seqA1);
addr = addr + sizeof(seqA1);
EEPROM.get(addr, seqA2);
addr = addr + sizeof(seqA2);
EEPROM.get(addr, seqA3);
addr = addr + sizeof(seqA3);
EEPROM.get(addr, seqA4);
addr = addr + sizeof(seqA4);
EEPROM.get(addr, seqA5);
addr = addr + sizeof(seqA5);
EEPROM.get(addr, seqA6);
addr = addr + sizeof(seqA6);
EEPROM.get(addr, seqA7);
addr = addr + sizeof(seqA7);
EEPROM.get(addr, seqA8);
addr = addr + sizeof(seqA8);
EEPROM.get(addr, seqB1);
addr = addr + sizeof(seqB1);
EEPROM.get(addr, seqB2);
addr = addr + sizeof(seqB2);
EEPROM.get(addr, seqB3);
addr = addr + sizeof(seqB3);
EEPROM.get(addr, seqB4);
addr = addr + sizeof(seqB4);
EEPROM.get(addr, seqB5);
addr = addr + sizeof(seqB5);
EEPROM.get(addr, seqB6);
addr = addr + sizeof(seqB6);
EEPROM.get(addr, seqB7);
addr = addr + sizeof(seqB7);
EEPROM.get(addr, seqB8);
} else {
saveState();
EEPROM.write(1023, memCode);
}
pinMode(ENC_BTN_PIN, INPUT_PULLUP);
pinMode(START_STOP_BTN_PIN, INPUT_PULLUP);
pinMode(SHIFT_BTN_PIN, INPUT_PULLUP);
pinMode(EXT_INPUT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(EXT_INPUT_PIN), externalClock, FALLING);
for (byte i = 0; i < 6; i++) {
pinMode(outsPins[i], OUTPUT);
}
u8g2.begin();
updateScreen();
calculateCycles();
calculateBPMTiming();
FlexiTimer2::set(1, 1.0 / 1000, clock); // 1.0/1000 = 1ms period. If other than 1ms calculateBPMTiming() might need tweaking
FlexiTimer2::start();
}
void loop() {
checkInputs();
}
void clock() {
if (isPlaying) {
// Action on each pulse
if (tickCount == 0) {
sendTriggers();
}
//this part gets the Pulse and Ticks ticking
//it's placed after the triggers to avoid problems on the start (when pulseCount==0)
tickCount++;
if (masterClockMode == 0) {
if (tickCount >= pulsePeriod) {
tickCount = 0;
if (pulseCount < (PPQN - 1)) { //-1 is here to avoid extra IF to reset to 0
pulseCount++;
} else {
pulseCount = 0;
}
if (bpmModulationRange != 0) {
calculateBPMTiming();
}
}
}
// pull low all outputs after set pulse length
if (tickCount >= PULSE_LENGTH) {
for (byte i = 0; i < 6; i++) {
digitalWrite(outsPins[i], LOW);
}
}
}
}
void externalClock() {
lastExtPulseTime = newExtPulseTime;
newExtPulseTime = millis();
if (masterClockMode == 1) { // EXT-24
//reset cycles if there was no pulses for a while
if ((newExtPulseTime - lastExtPulseTime) > 125) { //125ms is 20bpm
for (byte i = 0; i < 6; i++) {
channelPulseCount[i] = 0;
}
}
if (!isPlaying) {
isPlaying = true;
}
tickCount = 0; //to make things happen in the main clock function
if (pulseCount < (PPQN - 1)) {
pulseCount++;
} else {
pulseCount = 0;
}
}
}
void sendTriggers() {
for (byte i = 0; i < 6; i++) {
if (playingModes[i] != subDivs[channels[i].subDiv]) {
needPulseReset[i] = true;
}
}
//16th notes for sequencer
if (sixteenthPulseCount == 0) {
for (byte i = 0; i < 6; i++) {
if (channels[i].seqPattern == 0) {
currentSeq = seqA1;
} else if (channels[i].seqPattern == 1) {
currentSeq = seqA2;
} else if (channels[i].seqPattern == 2) {
currentSeq = seqA3;
} else if (channels[i].seqPattern == 3) {
currentSeq = seqA4;
} else if (channels[i].seqPattern == 4) {
currentSeq = seqA5;
} else if (channels[i].seqPattern == 5) {
currentSeq = seqA6;
} else if (channels[i].seqPattern == 6) {
currentSeq = seqA7;
} else if (channels[i].seqPattern == 7) {
currentSeq = seqA8;
} else if (channels[i].seqPattern == 8) {
currentSeq = seqB1;
} else if (channels[i].seqPattern == 9) {
currentSeq = seqB2;
} else if (channels[i].seqPattern == 10) {
currentSeq = seqB3;
} else if (channels[i].seqPattern == 11) {
currentSeq = seqB4;
} else if (channels[i].seqPattern == 12) {
currentSeq = seqB5;
} else if (channels[i].seqPattern == 13) {
currentSeq = seqB6;
} else if (channels[i].seqPattern == 14) {
currentSeq = seqB7;
} else if (channels[i].seqPattern == 15) {
currentSeq = seqB8;
}
if (channels[i].mode == 2 && channelPulseCount[i] == 0 && currentSeq[currentStep]) {
digitalWrite(outsPins[i], HIGH);
}
}
}
if (sixteenthPulseCount < (PPQN / 4) - 1) {
sixteenthPulseCount++;
} else {
sixteenthPulseCount = 0;
if (currentStep < 15) {
currentStep ++;
} else {
currentStep = 0;
}
}
//switching modes on the beat and resetting channel clock
if (pulseCount == 0) {
calculateCycles();
for (byte i = 0; i < 6; i++) {
if (needPulseReset[i] == true) {
channelPulseCount[i] = 0;
needPulseReset[i] = false;
}
}
}
//multiplier
for (byte i = 0; i < 6; i++) {
//RND modulation
byte randMod = 0;
if (channels[i].CV1Target == 2) {
randMod = randMod + a1Input;
}
if (channels[i].CV2Target == 2) {
randMod = randMod + a2Input;
}
if (channels[i].CV1Target == 2 || channels[i].CV2Target == 2) {
randMod = map(randMod, 0, 1023, -5, +5);
}
byte randAmount = channels[i].random + randMod;
if (randAmount > 100) {
randAmount = 0;
} else if (randAmount > 10) {
randAmount = 10;
}
if ((channels[i].mode == 0 && channelPulseCount[i] == channels[i].offset) //CLK with offset
|| (channels[i].mode == 1 && channelPulseCount[i] == 0 && (random(10) + 1) > randAmount) //RND
) {
digitalWrite(outsPins[i], HIGH);
}
if (channelPulseCount[i] < channelPulsesPerCycle[i]) {
channelPulseCount[i]++;
} else {
channelPulseCount[i] = 0;
}
}
}
void calculateCycles() {
for (byte i = 0; i < 6; i++) {
if (channels[i].CV1Target != 1 && channels[i].CV2Target != 1) {
playingModes[i] = subDivs[channels[i].subDiv];
} else if (channels[i].CV1Target == 1) { //subdiv modulation happens here
int mod;
mod = a1Input;
mod = map(mod, 0, 1023, (channels[i].CV1Value * -1), channels[i].CV1Value);
playingModes[i] = subDivs[channels[i].subDiv - mod]; //subtracting because the innitial array is backwards
} else if (channels[i].CV2Target == 1) {
int mod;
mod = a2Input;
mod = map(mod, 0, 1023, (channels[i].CV2Value * -1), channels[i].CV2Value);
playingModes[i] = subDivs[channels[i].subDiv - mod];
}
if (playingModes[i] > 0 && channels[i].mode != 2) {
channelPulsesPerCycle[i] = (playingModes[i] * PPQN) - 1;
} else if (playingModes[i] <= 0 && channels[i].mode != 2) {
channelPulsesPerCycle[i] = (PPQN / abs(playingModes[i])) - 1;
} else if (channels[i].mode == 2) { //Sequencer plays 1/16th
channelPulsesPerCycle[i] = (PPQN / 4) - 1;
}
if (channels[i].offset > channelPulsesPerCycle[i]) {
channels[i].offset = channelPulsesPerCycle[i];
}
}
}
void calculateBPMTiming() {
int mod = 0;
if (masterClockMode == 0) { //Internal clock
if (bpmModulationRange != 0 && bpmModulationChannel == 0) {
mod = map(a1Input, 0, 1023, bpmModulationRange * -10, bpmModulationRange * 10);
} else if (bpmModulationRange != 0 && bpmModulationChannel == 1) {
mod = map(a2Input, 0, 1023, bpmModulationRange * -10, bpmModulationRange * 10);
}
pulsePeriod = 60000 / ((bpm + mod) * PPQN);
} else if (masterClockMode == 2) { //for external beat clock
pulsePeriod = (newExtPulseTime - lastExtPulseTime) / PPQN;
} else if (masterClockMode == 3) { //for ext 1/16 clock (hardcoded)
pulsePeriod = (newExtPulseTime - lastExtPulseTime) / 6;
}
}
void resetClocks() {
for (byte i = 0; i < 6; i++) {
channelPulseCount[i] = 0;
digitalWrite(outsPins[i], LOW); //to avoid stuck leds
}
pulseCount = 0;
tickCount = 0;
}
void saveState() {
int addr = 0;
EEPROM.put(addr, bpm);
addr = addr + sizeof(bpm);
EEPROM.put(addr, bpmModulationChannel);
addr = addr + sizeof(bpmModulationChannel);
EEPROM.put(addr, bpmModulationRange);
addr = addr + sizeof(bpmModulationRange);
EEPROM.put(addr, masterClockMode);
addr = addr + sizeof(masterClockMode);
EEPROM.put(addr, channels);
addr = addr + sizeof(channels);
EEPROM.put(addr, seqA1);
addr = addr + sizeof(seqA1);
EEPROM.put(addr, seqA2);
addr = addr + sizeof(seqA2);
EEPROM.put(addr, seqA3);
addr = addr + sizeof(seqA3);
EEPROM.put(addr, seqA4);
addr = addr + sizeof(seqA4);
EEPROM.put(addr, seqA5);
addr = addr + sizeof(seqA5);
EEPROM.put(addr, seqA6);
addr = addr + sizeof(seqA6);
EEPROM.put(addr, seqA7);
addr = addr + sizeof(seqA7);
EEPROM.put(addr, seqA8);
addr = addr + sizeof(seqA8);
EEPROM.put(addr, seqB1);
addr = addr + sizeof(seqB1);
EEPROM.put(addr, seqB2);
addr = addr + sizeof(seqB2);
EEPROM.put(addr, seqB3);
addr = addr + sizeof(seqB3);
EEPROM.put(addr, seqB4);
addr = addr + sizeof(seqB4);
EEPROM.put(addr, seqB5);
addr = addr + sizeof(seqB5);
EEPROM.put(addr, seqB6);
addr = addr + sizeof(seqB6);
EEPROM.put(addr, seqB7);
addr = addr + sizeof(seqB7);
EEPROM.put(addr, seqB8);
}