CanGrow/include/Output/Output_I2C_01_MCP4725.h

45 lines
1.5 KiB
C

/*
*
* include/Output/OutputI2C_01_MCP4725.h - sensor header for I2C Output MCP4725 sensor
*
*
*
*/
#include <Adafruit_MCP4725.h>
#define OUTPUT_I2C_01_NAME "MCP4725"
const byte Output_I2C_01_MCP4725_Addr[] = { 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67 };
Adafruit_MCP4725 MCP4725[sizeof(Output_I2C_01_MCP4725_Addr)];
const byte Output_I2C_01_MCP4725_Ports = 1;
void Output_I2C_01_MCP4725_Update(const byte AddrId, const byte PortId, const byte Value) {
/* Update Output Port of I2C Module */
const static char LogLoc[] PROGMEM = "[Output:I2C:01_MCP4725:Update]";
/* 'Value' , which comes from outputState[], is byte, so 0-255. So we need to map this to the MCPs 0-4095 */
MCP4725[AddrId].setVoltage(map(Value, 0, 255, 0, 4095), false);
#ifdef DEBUG
Log.verbose(F("%s 0x%x Port %d, Value %d, MCP4725_Value %d" CR), LogLoc, Output_I2C_01_MCP4725_Addr[AddrId], PortId, Value, map(Value, 0, 255, 0, 4095));
#endif
}
bool Output_I2C_01_MCP4725_Init(const byte AddrId, const byte PortId) {
/* Initialize I2C Module, return true when successful */
const static char LogLoc[] PROGMEM = "[Output:I2C:01_MCP4725:Init]";
bool returnCode;
if(MCP4725[AddrId].begin(Output_I2C_01_MCP4725_Addr[AddrId])) {
Log.notice(F("%s found at addr 0x%x" CR), LogLoc, Output_I2C_01_MCP4725_Addr[AddrId]);
Output_I2C_01_MCP4725_Update(AddrId, PortId, 50);
returnCode = true;
} else {
Log.error(F("%s FAILED! Not found at addr 0x%x" CR), LogLoc, Output_I2C_01_MCP4725_Addr[AddrId]);
returnCode = false;
}
return returnCode;
}