Digipots refactor / cleanup (#19690)
This commit is contained in:
@ -21,5 +21,13 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
void digipot_i2c_set_current(const uint8_t channel, const float current);
|
||||
void digipot_i2c_init();
|
||||
//
|
||||
// Header for MCP4018 and MCP4451 current control i2c devices
|
||||
//
|
||||
class DigipotI2C {
|
||||
public:
|
||||
static void init();
|
||||
static void set_current(const uint8_t channel, const float current);
|
||||
};
|
||||
|
||||
DigipotI2C digipot_i2c;
|
||||
|
@ -24,6 +24,8 @@
|
||||
|
||||
#if ENABLED(DIGIPOT_MCP4018)
|
||||
|
||||
#include "digipot.h"
|
||||
|
||||
#include <Stream.h>
|
||||
#include <SlowSoftI2CMaster.h> // https://github.com/stawel/SlowSoftI2CMaster
|
||||
|
||||
@ -68,7 +70,7 @@ static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = {
|
||||
#endif
|
||||
};
|
||||
|
||||
static void i2c_send(const uint8_t channel, const byte v) {
|
||||
static void digipot_i2c_send(const uint8_t channel, const byte v) {
|
||||
if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
|
||||
pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS_A) << 1) | I2C_WRITE);
|
||||
pots[channel].i2c_write(v);
|
||||
@ -77,12 +79,12 @@ static void i2c_send(const uint8_t channel, const byte v) {
|
||||
}
|
||||
|
||||
// This is for the MCP4018 I2C based digipot
|
||||
void digipot_i2c_set_current(const uint8_t channel, const float current) {
|
||||
void DigipotI2C::set_current(const uint8_t channel, const float current) {
|
||||
const float ival = _MIN(_MAX(current, 0), float(DIGIPOT_MCP4018_MAX_VALUE));
|
||||
i2c_send(channel, current_to_wiper(ival));
|
||||
digipot_i2c_send(channel, current_to_wiper(ival));
|
||||
}
|
||||
|
||||
void digipot_i2c_init() {
|
||||
void DigipotI2C::init() {
|
||||
LOOP_L_N(i, DIGIPOT_I2C_NUM_CHANNELS) pots[i].i2c_init();
|
||||
|
||||
// Init currents according to Configuration_adv.h
|
||||
@ -94,7 +96,7 @@ void digipot_i2c_init() {
|
||||
#endif
|
||||
;
|
||||
LOOP_L_N(i, COUNT(digipot_motor_current))
|
||||
digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
|
||||
set_current(i, pgm_read_float(&digipot_motor_current[i]));
|
||||
}
|
||||
|
||||
#endif // DIGIPOT_MCP4018
|
||||
|
@ -24,6 +24,8 @@
|
||||
|
||||
#if ENABLED(DIGIPOT_MCP4451)
|
||||
|
||||
#include "digipot.h"
|
||||
|
||||
#include <Stream.h>
|
||||
#include <Wire.h>
|
||||
|
||||
@ -61,7 +63,7 @@ static void digipot_i2c_send(const byte addr, const byte a, const byte b) {
|
||||
}
|
||||
|
||||
// This is for the MCP4451 I2C based digipot
|
||||
void digipot_i2c_set_current(const uint8_t channel, const float current) {
|
||||
void DigipotI2C::set_current(const uint8_t channel, const float current) {
|
||||
// These addresses are specific to Azteeg X3 Pro, can be set to others.
|
||||
// In this case first digipot is at address A0=0, A1=0, second one is at A0=0, A1=1
|
||||
const byte addr = channel < 4 ? DIGIPOT_I2C_ADDRESS_A : DIGIPOT_I2C_ADDRESS_B; // channel 0-3 vs 4-7
|
||||
@ -75,7 +77,7 @@ void digipot_i2c_set_current(const uint8_t channel, const float current) {
|
||||
digipot_i2c_send(addr, addresses[channel & 0x3], current_to_wiper(_MIN(float(_MAX(current, 0)), DIGIPOT_I2C_MAX_CURRENT)));
|
||||
}
|
||||
|
||||
void digipot_i2c_init() {
|
||||
void DigipotI2C::init() {
|
||||
#if MB(MKS_SBASE)
|
||||
configure_i2c(16); // Set clock_option to 16 ensure I2C is initialized at 400kHz
|
||||
#else
|
||||
@ -90,7 +92,7 @@ void digipot_i2c_init() {
|
||||
#endif
|
||||
;
|
||||
LOOP_L_N(i, COUNT(digipot_motor_current))
|
||||
digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
|
||||
set_current(i, pgm_read_float(&digipot_motor_current[i]));
|
||||
}
|
||||
|
||||
#endif // DIGIPOT_MCP4451
|
||||
|
Reference in New Issue
Block a user