Files
smart-rgb-esp32/lib/pin/pin.cpp

54 lines
1.2 KiB
C++
Raw Permalink Normal View History

2025-08-03 00:34:05 +02:00
#include <Arduino.h>
#include "pin.h"
Pin::Pin(int pinNumber, bool isOutput, bool isLed, uint32_t ledFrequency, uint8_t ledChannel)
: pinNumber(pinNumber), output(isOutput), isLed(isLed), ledChannel(ledChannel) {
pinMode(pinNumber, isOutput ? OUTPUT : INPUT);
if (isLed) {
2025-08-27 17:30:39 +02:00
ledcSetup(ledChannel, ledFrequency, ledResolutionBits); // Setup LEDC for PWM with 8-bit resolution
2025-08-03 00:34:05 +02:00
ledcAttachPin(pinNumber, ledChannel); // Attach the pin to the LEDC channel
}
}
2025-08-27 17:30:39 +02:00
const uint8_t Pin::getLedResolutionBits() const {
return ledResolutionBits;
}
2025-08-03 00:34:05 +02:00
void Pin::setHigh() {
if (output) {
digitalWrite(pinNumber, HIGH);
}
}
void Pin::setLow() {
if (output) {
digitalWrite(pinNumber, LOW);
}
}
void Pin::setLedLevel(uint32_t level) {
if (output && isLed) {
ledcWrite(ledChannel, level);
2025-08-27 17:30:39 +02:00
ledLevel = level;
2025-08-03 00:34:05 +02:00
}
2025-08-27 17:30:39 +02:00
}
uint32_t Pin::getLedLevel() const {
return ledLevel;
2025-08-03 00:34:05 +02:00
}
bool Pin::read() {
if (!output) {
return digitalRead(pinNumber);
}
return false;
}
int Pin::getPinNumber() const {
return pinNumber;
}
bool Pin::isOutput() const {
return output;
}