63 lines
1 KiB
C++
63 lines
1 KiB
C++
// Out.cpp
|
|
#include "Arduino.h"
|
|
#include "Out.h"
|
|
#include "globals.h"
|
|
|
|
Out::Out(byte pin) {
|
|
this->pin = pin;
|
|
state = LOW;
|
|
|
|
div = 1;
|
|
cycle = 0;
|
|
|
|
dur = 0;
|
|
width = 50;
|
|
// this needs to dynamically change with width
|
|
|
|
len = 0;
|
|
|
|
pinMode(pin, OUTPUT);
|
|
}
|
|
|
|
void Out::setLen(unsigned long currentPeriod) {
|
|
// Fix the integer division by using floating-point math temporarily
|
|
// The result 'len' is likely a duration in milliseconds, so keep it an integer type
|
|
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) {
|
|
Serial.println(len);
|
|
state = LOW;
|
|
digitalWrite(pin, state);
|
|
dur = 0;
|
|
};
|
|
}
|
|
|
|
void Out::setDiv(int newDiv) {
|
|
div = newDiv;
|
|
};
|
|
|
|
void Out::setWidth(int newWidth) {
|
|
width = newWidth;
|
|
len = (unsigned long)((double)period * (width / 100.0) / 1000.0);
|
|
|
|
};
|
|
|
|
int Out::getState() {
|
|
return state;
|
|
}
|
|
|