wip with light and mqtt working
This commit is contained in:
45
lib/pin/pin.cpp
Normal file
45
lib/pin/pin.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#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) {
|
||||
ledcSetup(ledChannel, ledFrequency, 8); // Setup LEDC for PWM with 8-bit resolution
|
||||
ledcAttachPin(pinNumber, ledChannel); // Attach the pin to the LEDC channel
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
// analogWrite(pinNumber, level); // Use analogWrite for PWM control
|
||||
}
|
||||
|
||||
bool Pin::read() {
|
||||
if (!output) {
|
||||
return digitalRead(pinNumber);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int Pin::getPinNumber() const {
|
||||
return pinNumber;
|
||||
}
|
||||
|
||||
bool Pin::isOutput() const {
|
||||
return output;
|
||||
}
|
||||
18
lib/pin/pin.h
Normal file
18
lib/pin/pin.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
class Pin {
|
||||
public:
|
||||
Pin(int pinNumber, bool isOutput = true, bool isLed = false, uint32_t ledFrequency = 5000, uint8_t ledChannel = 0);
|
||||
void setHigh();
|
||||
void setLow();
|
||||
void setLedLevel(uint32_t level);
|
||||
bool read();
|
||||
int getPinNumber() const;
|
||||
bool isOutput() const;
|
||||
|
||||
private:
|
||||
uint8_t ledChannel = 0; // LED channel for PWM
|
||||
uint8_t pinNumber;
|
||||
bool output;
|
||||
bool isLed = false; // Flag to indicate if this pin is used for LED control
|
||||
};
|
||||
Reference in New Issue
Block a user