I2C Output was not finished implemented, MCP4725 driver too. Fixed now.

This commit is contained in:
DeltaLima 2025-03-16 20:38:04 +01:00
parent 3d5d633da5
commit 238dc34b95
2 changed files with 27 additions and 14 deletions
Arduino/CanGrow/include

View file

@ -46,7 +46,7 @@ OutputI2C_Index OutputI2Cindex[] {
const byte OutputI2Cindex_length = 2;
byte Output_I2C_Addr_Init_Update(const byte OutputI2CindexId, const byte AddrId, const byte PortId, const byte mode, const byte Value = 0) {
byte Output_I2C_Addr_Init_Update(const byte OutputI2CindexId, const byte AddrId, const byte PortId, const byte Mode, const bool Invert = false, const byte Value = 0) {
const static char LogLoc[] PROGMEM = "[Output:I2C:Addr_Init_Update]";
/* Multi purpose function.
*
@ -56,19 +56,26 @@ byte Output_I2C_Addr_Init_Update(const byte OutputI2CindexId, const byte AddrId,
* 2 - update i2 module data
*/
byte Dummy_Addr[] = { 0x42, 0x69 };
/* invert Value if set, save it to Value_tmp */
byte Value_tmp = Value;
if(Invert == true)
/* Value comes from outputState[] which is only type byte (max 255) */
Value_tmp = 255 - Value;
switch(OutputI2CindexId) {
/* I2C Output module 01 */
case 1:
switch(mode) {
case 0:
switch(Mode) {
case OUPUT_I2C_AIU_MODE_ADDR:
return Output_I2C_01_MCP4725_Addr[AddrId];
break;
case 1:
case OUPUT_I2C_AIU_MODE_INIT:
return Output_I2C_01_MCP4725_Init(AddrId, PortId);
break;
case 2:
Output_I2C_01_MCP4725_Update(AddrId, PortId, Value);
case OUPUT_I2C_AIU_MODE_UPDATE:
Output_I2C_01_MCP4725_Update(AddrId, PortId, Value_tmp);
break;
}
break;
@ -76,7 +83,7 @@ byte Output_I2C_Addr_Init_Update(const byte OutputI2CindexId, const byte AddrId,
/* 02 - dummy*/
case 2:
switch(mode) {
switch(Mode) {
case 0:
return Dummy_Addr[AddrId];
break;
@ -216,8 +223,11 @@ bool Output_Webcall_Init_Update(const byte OutputId, const bool Value = false) {
}
bool Output_Check_PWM(const byte OutputId) {
/* when we verify output is PWM */
if( ((config.system.output.type[OutputId] == OUTPUT_TYPE_GPIO) && (config.system.output.gpio_pwm[OutputId] == true) && (GPIOindex[config.system.output.type[OutputId]].note != NO_PWM)) ) {
/* when we verify output is GPIO PWM OR */
if(((config.system.output.type[OutputId] == OUTPUT_TYPE_GPIO) && (config.system.output.gpio_pwm[OutputId] == true) && (GPIOindex[config.system.output.type[OutputId]].note != NO_PWM)) ||
/* Output is type I2C */
(config.system.output.type[OutputId] == OUTPUT_TYPE_I2C)
) {
/* return true */
return true;
} else {
@ -413,7 +423,7 @@ void Output_Init() {
OutputI2Cindex[config.system.output.i2c_type[i]].name, config.system.output.i2c_type[i],
config.system.output.enabled[i], config.system.output.gpio_pwm[i], config.system.output.invert[i]);
#endif
outputStatus[i] = Output_I2C_Addr_Init_Update(config.system.output.i2c_type[i], config.system.output.i2c_addr[i], config.system.output.i2c_port[i], OUPUT_I2C_AIU_MODE_INIT);
outputStatus[i] = Output_I2C_Addr_Init_Update(config.system.output.i2c_type[i], config.system.output.i2c_addr[i], config.system.output.i2c_port[i], OUPUT_I2C_AIU_MODE_INIT, config.system.output.invert[i]);
break;
case OUTPUT_TYPE_WEB:
@ -466,7 +476,7 @@ void Output_Update() {
* I2C
* *****/
case OUTPUT_TYPE_I2C:
Output_I2C_Addr_Init_Update(config.system.output.i2c_type[i], config.system.output.i2c_addr[i], config.system.output.i2c_port[i], OUPUT_I2C_AIU_MODE_UPDATE, outputState[i]);
Output_I2C_Addr_Init_Update(config.system.output.i2c_type[i], config.system.output.i2c_addr[i], config.system.output.i2c_port[i], OUPUT_I2C_AIU_MODE_UPDATE, config.system.output.invert[i], outputState[i]);
break;
/*******

View file

@ -19,11 +19,14 @@ 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]";
const short MaxRawValue = 4095;
short RawValue = (MaxRawValue * Value)/100;
//const short MaxRawValue = 4095;
//byte RawValue = map(); // (MaxRawValue * Value)/100;
/* '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, RawValue %d" CR), LogLoc, Output_I2C_01_MCP4725_Addr[AddrId], PortId, Value, RawValue);
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
}