2023-12-31 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* @file AnalogCard.cpp
|
|
|
|
* @brief Implementation of the AnalogCard class.
|
|
|
|
*/
|
|
|
|
|
2023-12-27 16:15:11 +00:00
|
|
|
#include <AnalogCard.hpp>
|
2023-12-30 11:47:52 +00:00
|
|
|
#include "esp_log.h"
|
2023-12-27 16:15:11 +00:00
|
|
|
|
2023-12-31 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Default constructor for the AnalogCard class.
|
|
|
|
*/
|
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),
|
2023-12-31 06:29:11 +00:00
|
|
|
analogInputBankA(),
|
|
|
|
analogInputBankB(),
|
|
|
|
dac_change_callbacks()
|
2023-12-27 16:41:58 +00:00
|
|
|
{
|
2023-12-31 06:29:11 +00:00
|
|
|
this->handler_count = 0;
|
2023-12-27 16:41:58 +00:00
|
|
|
}
|
|
|
|
|
2023-12-31 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Writes a value to the specified DAC pin.
|
|
|
|
* @param pin The DAC pin to write to.
|
|
|
|
* @param value The value to write.
|
|
|
|
*/
|
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
|
|
|
{
|
2023-12-30 11:47:52 +00:00
|
|
|
ESP_LOGV("AnalogCard", "DAC Write: %d, %d", pin, value);
|
2023-12-28 17:39:11 +00:00
|
|
|
this->setDACState(pin, value > 0);
|
|
|
|
this->setDACValue(pin, value);
|
|
|
|
}
|
|
|
|
|
2023-12-31 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Sets the state of the specified DAC pin.
|
|
|
|
* @param pin The DAC pin to set the state of.
|
|
|
|
* @param state The state to set (true = on, false = off).
|
|
|
|
*/
|
2023-12-28 17:39:11 +00:00
|
|
|
void AnalogCard::setDACState(uint8_t pin, bool state)
|
|
|
|
{
|
2023-12-30 11:47:52 +00:00
|
|
|
ESP_LOGD("AnalogCard", "Setting DAC state: %d, %d", pin, state);
|
2023-12-28 17:39:11 +00:00
|
|
|
this->dac_state[pin] = state;
|
2023-12-31 06:29:11 +00:00
|
|
|
this->sendDataToDAC(pin, this->dac_value[pin] * state);
|
|
|
|
for (const auto& callback : this->dac_change_callbacks)
|
2023-12-28 17:39:11 +00:00
|
|
|
{
|
2023-12-31 06:29:11 +00:00
|
|
|
callback.second(pin, state, this->dac_value[pin]);
|
2023-12-28 17:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-31 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Sets the value of the specified DAC pin.
|
|
|
|
* @param pin The DAC pin to set the value of.
|
|
|
|
* @param value The value to set.
|
|
|
|
*/
|
2023-12-28 17:39:11 +00:00
|
|
|
void AnalogCard::setDACValue(uint8_t pin, uint16_t value)
|
|
|
|
{
|
2023-12-30 11:47:52 +00:00
|
|
|
ESP_LOGD("AnalogCard", "Setting DAC value: %d, %d", pin, value);
|
2023-12-28 17:39:11 +00:00
|
|
|
this->dac_value[pin] = value;
|
2023-12-31 06:29:11 +00:00
|
|
|
this->sendDataToDAC(pin, value * this->dac_state[pin]);
|
|
|
|
for (const auto& callback : this->dac_change_callbacks)
|
2023-12-28 17:39:11 +00:00
|
|
|
{
|
2023-12-31 06:29:11 +00:00
|
|
|
callback.second(pin, this->dac_state[pin], value);
|
2023-12-28 17:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-31 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Gets the value of the specified DAC pin.
|
|
|
|
* @param pin The DAC pin to get the value of.
|
|
|
|
* @return The value of the DAC pin.
|
|
|
|
*/
|
2023-12-28 17:39:11 +00:00
|
|
|
uint16_t AnalogCard::getDACValue(uint8_t pin)
|
|
|
|
{
|
|
|
|
return this->dac_value[pin];
|
|
|
|
}
|
|
|
|
|
2023-12-31 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Gets the state of the specified DAC pin.
|
|
|
|
* @param pin The DAC pin to get the state of.
|
|
|
|
* @return The state of the DAC pin (true = on, false = off).
|
|
|
|
*/
|
2023-12-28 17:39:11 +00:00
|
|
|
bool AnalogCard::getDACState(uint8_t pin)
|
|
|
|
{
|
|
|
|
return this->dac_state[pin];
|
|
|
|
}
|
|
|
|
|
2023-12-31 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Sends data to the specified DAC pin.
|
|
|
|
* @param pin The DAC pin to send data to.
|
|
|
|
* @param value The data to send.
|
|
|
|
* @note This function does not call the DAC change callbacks.
|
|
|
|
*/
|
2023-12-28 17:39:11 +00:00
|
|
|
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-31 18:38:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Reads the value from the specified analog pin.
|
|
|
|
* @param pin The analog pin to read from.
|
|
|
|
* @return The value read from the analog pin.
|
|
|
|
*/
|
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-31 18:38:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initializes the AnalogCard.
|
|
|
|
* @return True if initialization is successful, false otherwise.
|
|
|
|
*/
|
2023-12-27 19:18:21 +00:00
|
|
|
bool AnalogCard::begin()
|
2023-12-27 16:41:58 +00:00
|
|
|
{
|
2024-04-01 15:36:53 +00:00
|
|
|
bool success = false;
|
2023-12-27 19:18:21 +00:00
|
|
|
if (!this->dac0.begin())
|
|
|
|
{
|
2023-12-30 11:47:52 +00:00
|
|
|
ESP_LOGE("AnalogCard", "Card Analog ERROR: Failed to install DAC0");
|
2024-04-01 15:36:53 +00:00
|
|
|
// success = false;
|
2023-12-27 19:18:21 +00:00
|
|
|
}
|
|
|
|
if (!this->dac1.begin())
|
|
|
|
{
|
2023-12-30 11:47:52 +00:00
|
|
|
ESP_LOGE("AnalogCard", "Card Analog ERROR: Failed to install DAC1");
|
2024-04-01 15:36:53 +00:00
|
|
|
// success = false;
|
2023-12-27 19:18:21 +00:00
|
|
|
}
|
|
|
|
if (!this->dac2.begin())
|
|
|
|
{
|
2023-12-30 11:47:52 +00:00
|
|
|
ESP_LOGE("AnalogCard", "Card Analog ERROR: Failed to install DAC2");
|
2024-04-01 15:36:53 +00:00
|
|
|
// success = false;
|
2023-12-27 19:18:21 +00:00
|
|
|
}
|
|
|
|
if (!this->dac3.begin())
|
|
|
|
{
|
2023-12-30 11:47:52 +00:00
|
|
|
ESP_LOGE("AnalogCard", "Card Analog ERROR: Failed to install DAC3");
|
2024-04-01 15:36:53 +00:00
|
|
|
// success = false;
|
2023-12-27 19:18:21 +00:00
|
|
|
}
|
2024-04-01 15:34:40 +00:00
|
|
|
if (!this->analogInputBankA.begin(ANALOG_INPUT_BANK_A_ADDRESS))
|
2023-12-27 19:18:21 +00:00
|
|
|
{
|
2023-12-30 11:47:52 +00:00
|
|
|
ESP_LOGE("AnalogCard", "Card Analog ERROR: Failed to install analog input bank A");
|
2024-03-09 17:57:16 +00:00
|
|
|
success = false;
|
2023-12-27 19:18:21 +00:00
|
|
|
}
|
2024-04-01 15:34:40 +00:00
|
|
|
if (!this->analogInputBankB.begin(ANALOG_INPUT_BANK_B_ADDRESS))
|
2023-12-27 19:18:21 +00:00
|
|
|
{
|
2023-12-30 11:47:52 +00:00
|
|
|
ESP_LOGE("AnalogCard", "Card Analog ERROR: Failed to install analog input bank B");
|
2024-03-09 17:57:16 +00:00
|
|
|
success = false;
|
2023-12-27 19:18:21 +00:00
|
|
|
}
|
2024-03-09 17:57:16 +00:00
|
|
|
return success;
|
2023-12-27 19:18:21 +00:00
|
|
|
}
|
|
|
|
|
2023-12-31 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* @brief The main loop of the AnalogCard.
|
|
|
|
* @note This function does nothing.
|
|
|
|
*/
|
2023-12-27 19:18:21 +00:00
|
|
|
void AnalogCard::loop()
|
|
|
|
{
|
2023-12-28 04:41:20 +00:00
|
|
|
}
|
|
|
|
|
2023-12-31 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Gets the type of the AnalogCard.
|
|
|
|
* @return The type of the AnalogCard.
|
|
|
|
*/
|
2023-12-28 04:41:20 +00:00
|
|
|
uint8_t AnalogCard::getType()
|
|
|
|
{
|
|
|
|
return CARD_TYPE_ANALOG;
|
2023-12-28 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-31 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Registers a callback function to be called when the state or value of a DAC pin changes.
|
|
|
|
* @param callback The callback function to register.
|
|
|
|
* @return The handler ID of the registered callback.
|
|
|
|
*/
|
2023-12-31 06:29:11 +00:00
|
|
|
uint8_t AnalogCard::registerDACChangeCallback(std::function<void(uint8_t, bool, uint16_t)> callback)
|
2023-12-28 17:39:11 +00:00
|
|
|
{
|
2023-12-31 06:29:11 +00:00
|
|
|
this->dac_change_callbacks[this->handler_count] = callback;
|
|
|
|
return this->handler_count++;
|
2023-12-28 17:39:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-31 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Unregisters a previously registered DAC change callback.
|
|
|
|
* @param handler The handler ID of the callback to unregister.
|
|
|
|
*/
|
2023-12-31 06:41:48 +00:00
|
|
|
void AnalogCard::unregisterDACChangeCallback(uint8_t handler)
|
2023-12-31 06:29:11 +00:00
|
|
|
{
|
|
|
|
this->dac_change_callbacks.erase(handler);
|
|
|
|
}
|