ESPMegaPRO-v3-SDK/Template Project/lib/ESPMegaPRO/AnalogCard.cpp

130 lines
2.9 KiB
C++
Raw Normal View History

2023-12-27 16:15:11 +00:00
#include <AnalogCard.hpp>
2023-12-27 16:41:58 +00:00
AnalogCard::AnalogCard() : dac0(DAC0_ADDRESS),
dac1(DAC1_ADDRESS),
dac2(DAC2_ADDRESS),
2023-12-27 19:18:21 +00:00
dac3(DAC3_ADDRESS),
analogInputBankA(),
analogInputBankB()
2023-12-27 16:41:58 +00:00
{
}
void AnalogCard::dacWrite(uint8_t pin, uint16_t value)
2023-12-28 17:39:11 +00:00
{
this->setDACState(pin, value > 0);
this->setDACValue(pin, value);
}
void AnalogCard::setDACState(uint8_t pin, bool state)
{
this->dac_state[pin] = state;
this->sendDataToDAC(pin, this->dac_value[pin]*state);
if (this->dac_change_callback != NULL)
{
this->dac_change_callback(pin, state, this->dac_value[pin]);
}
}
void AnalogCard::setDACValue(uint8_t pin, uint16_t value)
{
this->dac_value[pin] = value;
this->sendDataToDAC(pin, value*this->dac_state[pin]);
if (this->dac_change_callback != NULL)
{
this->dac_change_callback(pin, this->dac_state[pin], value);
}
}
uint16_t AnalogCard::getDACValue(uint8_t pin)
{
return this->dac_value[pin];
}
bool AnalogCard::getDACState(uint8_t pin)
{
return this->dac_state[pin];
}
void AnalogCard::sendDataToDAC(uint8_t pin, uint16_t value)
2023-12-27 16:41:58 +00:00
{
switch (pin)
{
case 0:
this->dac0.writeDAC(value);
break;
case 1:
this->dac1.writeDAC(value);
break;
case 2:
this->dac2.writeDAC(value);
break;
case 3:
this->dac3.writeDAC(value);
break;
2023-12-27 16:15:11 +00:00
}
}
2023-12-27 16:41:58 +00:00
uint16_t AnalogCard::analogRead(uint8_t pin)
{
if (pin >= 0 && pin <= 3)
{
2023-12-27 16:15:11 +00:00
return this->analogInputBankA.readADC_SingleEnded(pin);
2023-12-27 16:41:58 +00:00
}
else if (pin >= 4 && pin <= 7)
{
2023-12-27 16:15:11 +00:00
return this->analogInputBankB.readADC_SingleEnded(pin - 4);
}
2023-12-28 12:11:12 +00:00
return 65535;
2023-12-27 16:15:11 +00:00
}
2023-12-27 19:18:21 +00:00
bool AnalogCard::begin()
2023-12-27 16:41:58 +00:00
{
2023-12-27 19:18:21 +00:00
if (!this->dac0.begin())
{
Serial.println("Card Analog ERROR: Failed to install DAC0");
return false;
}
if (!this->dac1.begin())
{
Serial.println("Card Analog ERROR: Failed to install DAC1");
return false;
}
if (!this->dac2.begin())
{
Serial.println("Card Analog ERROR: Failed to install DAC2");
return false;
}
if (!this->dac3.begin())
{
Serial.println("Card Analog ERROR: Failed to install DAC3");
return false;
}
if (!this->analogInputBankA.begin())
{
Serial.println("Card Analog ERROR: Failed to install analog input bank A");
return false;
}
if (!this->analogInputBankB.begin())
{
Serial.println("Card Analog ERROR: Failed to install analog input bank B");
return false;
}
return true;
}
void AnalogCard::loop()
{
2023-12-28 04:41:20 +00:00
}
uint8_t AnalogCard::getType()
{
return CARD_TYPE_ANALOG;
2023-12-28 17:39:11 +00:00
}
void AnalogCard::registerDACChangeCallback(std::function<void(uint8_t, bool, uint16_t)> callback)
{
this->dac_change_callback = callback;
}
void AnalogCard::deregisterDACChangeCallback()
{
this->dac_change_callback = NULL;
2023-12-27 16:15:11 +00:00
}