87 lines
1.7 KiB
C++
87 lines
1.7 KiB
C++
// Out.cpp
|
|
#include "Arduino.h"
|
|
#include "Out.h"
|
|
#include "globals.h"
|
|
|
|
Out::Out(byte pin) {
|
|
this->pin = pin;
|
|
state = LOW;
|
|
|
|
divideMode = 1; // 1 divison | 0 multiplication
|
|
modifier = 1; // divide mode modifier (4x, /32, etc)
|
|
div = 1; // cycles needed before a pulse based on divide mode and modifier
|
|
cycle = 0; // how many cycles have passed since last pulse
|
|
divString = ""; // string for screen .. probably does not belong here
|
|
|
|
dur = 0; // how long pulse is on
|
|
width = 50; // pulse width
|
|
len = 0; // max len a pulse can be on, as determined by width
|
|
|
|
p = 100; // probability of a pulse
|
|
|
|
|
|
pinMode(pin, OUTPUT);
|
|
}
|
|
|
|
int Out::getState() {
|
|
return state;
|
|
}
|
|
|
|
void Out::setLen(unsigned long currentPeriod) {
|
|
len = (unsigned long)((double)currentPeriod * (width / 100.0) / 1000.0);
|
|
}
|
|
|
|
void Out::setDiv(int modifier, byte divide = 1) {
|
|
if (divide == 1) {
|
|
div = ppqn * modifier;
|
|
divString = "/" + modifier;
|
|
} else {
|
|
div = ppqn / modifier;
|
|
divString = "x" + modifier;
|
|
}
|
|
divideMode = divide;
|
|
this->modifier = modifier;
|
|
};
|
|
|
|
void Out::setWidth(int newWidth) {
|
|
width = newWidth;
|
|
if (divideMode == 1) {
|
|
len = (unsigned long)((double)(minute / BPM) * (width / 100.0) / 1000.0);
|
|
} else {
|
|
len = (unsigned long)((double)(minute / BPM / modifier) * (width / 100.0) / 1000.0);
|
|
}
|
|
};
|
|
|
|
void Out::setP(int prob) {
|
|
this->p = prob;
|
|
}
|
|
|
|
void Out::turnOn() {
|
|
cycle += 1;
|
|
byte pRes = 1;
|
|
|
|
if (cycle == div) {
|
|
if (p < 100) {
|
|
long r = random(1, 101);
|
|
if (r > p) {
|
|
pRes = 0;
|
|
}
|
|
}
|
|
|
|
if (pRes == 1) {
|
|
state = HIGH;
|
|
digitalWrite(pin, state);
|
|
dur = millis();
|
|
}
|
|
cycle = 0;
|
|
};
|
|
}
|
|
|
|
void Out::turnOff() {
|
|
if (state == HIGH && millis() - dur >= len) {
|
|
state = LOW;
|
|
digitalWrite(pin, state);
|
|
dur = 0;
|
|
};
|
|
}
|
|
|