migrate callbacks from std::vector to std::map
This commit is contained in:
parent
c635427d64
commit
5f6e586945
11 changed files with 143 additions and 178 deletions
|
@ -7,16 +7,15 @@ DigitalOutputCard::DigitalOutputCard(uint8_t address) : change_callbacks(){
|
|||
this->pinMap[i] = i;
|
||||
this->virtualPinMap[i] = i;
|
||||
}
|
||||
this->framBinded = false;
|
||||
this->callbacks_handler_index = 0;
|
||||
}
|
||||
// Instantiate the card with the specified position on the dip switch
|
||||
DigitalOutputCard::DigitalOutputCard(bool bit0, bool bit1, bool bit2, bool bit3, bool bit4) {
|
||||
this->address = 0x20;
|
||||
if (bit0) this->address += 1;
|
||||
if (bit1) this->address += 2;
|
||||
if (bit2) this->address += 4;
|
||||
if (bit3) this->address += 8;
|
||||
if (bit4) this->address += 16;
|
||||
DigitalOutputCard::DigitalOutputCard(bool bit0, bool bit1, bool bit2, bool bit3, bool bit4) :
|
||||
DigitalOutputCard(0x20+bit0+bit1*2+bit2*4+bit3*8+bit4*16)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
// Initialize the card
|
||||
bool DigitalOutputCard::begin() {
|
||||
|
@ -36,8 +35,9 @@ void DigitalOutputCard::digitalWrite(uint8_t pin, bool state) {
|
|||
this->saveStateToFRAM();
|
||||
this->savePinValueToFRAM(pin);
|
||||
}
|
||||
for (int i = 0; i < change_callbacks.size(); i++) {
|
||||
change_callbacks[i](pin, state, state ? 4095 : 0);
|
||||
for (const auto& callback : change_callbacks)
|
||||
{
|
||||
callback.second(pin, state, state ? 4095 : 0);
|
||||
}
|
||||
}
|
||||
// Set the output to the specified pwm value
|
||||
|
@ -52,8 +52,9 @@ void DigitalOutputCard::analogWrite(uint8_t pin, uint16_t value) {
|
|||
}
|
||||
this->state_buffer[pin] = value > 0;
|
||||
this->value_buffer[pin] = value;
|
||||
for (int i = 0; i < change_callbacks.size(); i++) {
|
||||
change_callbacks[i](pin, value > 0, value);
|
||||
for (const auto& callback : change_callbacks)
|
||||
{
|
||||
callback.second(pin, value > 0, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,8 +83,8 @@ void DigitalOutputCard::setState(uint8_t pin, bool state) {
|
|||
if(this->framAutoSave) {
|
||||
this->saveStateToFRAM();
|
||||
}
|
||||
for(int i = 0; i < change_callbacks.size(); i++) {
|
||||
change_callbacks[i](pin, state, value_buffer[pin]);
|
||||
for(const auto& callback : change_callbacks) {
|
||||
callback.second(pin, state, value_buffer[pin]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -95,23 +96,19 @@ void DigitalOutputCard::setValue(uint8_t pin, uint16_t value) {
|
|||
if (this->framAutoSave) {
|
||||
this->savePinValueToFRAM(pin);
|
||||
}
|
||||
for (int i = 0; i < change_callbacks.size(); i++) {
|
||||
change_callbacks[i](pin, state_buffer[pin], value);
|
||||
for (const auto& callback : change_callbacks)
|
||||
{
|
||||
callback.second(pin, state_buffer[pin], value);
|
||||
}
|
||||
}
|
||||
|
||||
void DigitalOutputCard::registerChangeCallback(std::function<void(uint8_t, bool, uint16_t)> callback) {
|
||||
this->change_callbacks.push_back(callback);
|
||||
this->change_callbacks[this->callbacks_handler_index++] = callback;
|
||||
}
|
||||
|
||||
// void DigitalOutputCard::deregisterChangeCallback(std::function<void(uint8_t, bool, uint16_t)> callback) {
|
||||
// for(int i = 0; i < change_callbacks.size(); i++) {
|
||||
// if(change_callbacks[i].target<void(uint8_t, bool, uint16_t)>() == callback.target<void(uint8_t, bool, uint16_t)>()) {
|
||||
// change_callbacks.erase(change_callbacks.begin()+i);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
void DigitalOutputCard::deregisterChangeCallback(uint8_t handler) {
|
||||
this->change_callbacks.erase(handler);
|
||||
}
|
||||
|
||||
void DigitalOutputCard::loadPinMap(uint8_t pinMap[16]) {
|
||||
for(int i = 0; i < 16; i++) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue