clock_master/Out.cpp
2026-01-18 15:49:28 -05:00

79 lines
1.3 KiB
C++

// Out.cpp
#include "Arduino.h"
#include "Out.h"
#include "globals.h"
Out::Out(byte pin) {
this->pin = pin;
state = LOW;
divideMode = 1;
modifier = 1;
div = 1;
divString = "";
cycle = 0;
dur = 0;
width = 50;
// this needs to dynamically change with width
len = 0;
pinMode(pin, OUTPUT);
}
void Out::setLen(unsigned long currentPeriod) {
len = (unsigned long)((double)currentPeriod * (width / 100.0) / 1000.0);
}
void Out::turnOn() {
cycle += 1;
if (cycle == div) {
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;
};
}
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) {
Serial.println(modifier);
Serial.println(divideMode);
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);
}
Serial.println(len);
Serial.println("=========");
};
int Out::getState() {
return state;
}