add digital output pinmap support

This commit is contained in:
Siwat Sirichai 2023-12-28 19:01:37 +07:00
parent 83ed233dd4
commit 2fd37fe66d
7 changed files with 63 additions and 30 deletions

View file

@ -2,7 +2,11 @@
DigitalOutputCard::DigitalOutputCard(uint8_t address) {
this->address = address;
// load default pin map
for (int i = 0; i < 16; i++) {
this->pinMap[i] = i;
this->virtualPinMap[i] = i;
}
}
// Instantiate the card with the specified position on the dip switch
DigitalOutputCard::DigitalOutputCard(bool bit0, bool bit1, bool bit2, bool bit3, bool bit4) {
@ -25,18 +29,24 @@ bool DigitalOutputCard::begin() {
}
// Set the output to the specified state
void DigitalOutputCard::digitalWrite(uint8_t pin, bool state) {
this->pwm.setPin(pin, state ? 4095 : 0);
this->pwm.setPin(virtualPinMap[pin], state ? 4095 : 0);
this->state_buffer[pin] = state;
this->value_buffer[pin] = state ? 4095 : 0;
if(change_callback != NULL) {
change_callback(pin, state, state ? 4095 : 0);
}
}
// Set the output to the specified pwm value
void DigitalOutputCard::analogWrite(uint8_t pin, uint16_t value) {
// If value is greater than 4095, set it to 4095
if (value > 4095) value = 4095;
// Set the pwm value
this->pwm.setPin(pin, value);
this->pwm.setPin(virtualPinMap[pin], value);
this->state_buffer[pin] = value > 0;
this->value_buffer[pin] = value;
if(change_callback != NULL) {
change_callback(pin, value > 0, value);
}
}
// Dummy loop function
@ -59,29 +69,34 @@ uint8_t DigitalOutputCard::getType() {
}
void DigitalOutputCard::setState(uint8_t pin, bool state) {
Serial.print("Setting state of pin ");
Serial.print(pin);
Serial.print(" to ");
Serial.println(state);
this-> state_buffer[pin] = state;
Serial.print("New state of pin ");
Serial.print(pin);
Serial.print(" is ");
Serial.println(state_buffer[pin]*value_buffer[pin]);
this->pwm.setPin(pin, state*value_buffer[pin]);
if (change_callback != NULL) {
change_callback(pin, state, value_buffer[pin]);
}
}
void DigitalOutputCard::setValue(uint8_t pin, uint16_t value) {
Serial.print("Setting value of pin ");
Serial.print(pin);
Serial.print(" to ");
Serial.println(value);
// If value is greater than 4095, set it to 4095
if (value > 4095) value = 4095;
this-> value_buffer[pin] = value;
Serial.print("New value of pin ");
Serial.print(pin);
Serial.print(" is ");
Serial.println(value_buffer[pin]*state_buffer[pin]);
this->pwm.setPin(pin, state_buffer[pin]*value);
if (change_callback != NULL) {
change_callback(pin, state_buffer[pin], value);
}
}
void DigitalOutputCard::registerChangeCallback(std::function<void(uint8_t, bool, uint16_t)> callback) {
this->change_callback = callback;
}
void DigitalOutputCard::deregisterChangeCallback() {
this->change_callback = NULL;
}
void DigitalOutputCard::loadPinMap(uint8_t pinMap[16]) {
for(int i = 0; i < 16; i++) {
this->pinMap[i] = pinMap[i];
this->virtualPinMap[pinMap[i]] = i;
}
}