Ver código fonte

✨ Add M3426 A<addr> parameter (#24130)

Co-authored-by: Scott Lahteine <github@thinkyhead.com>
Stephen Hawes 2 anos atrás
pai
commit
81a6834876
Nenhuma conta vinculada ao e-mail do autor do commit

+ 3
- 3
Marlin/src/feature/adc/adc_mcp3426.cpp Ver arquivo

@@ -34,7 +34,7 @@
34 34
 #include "adc_mcp3426.h"
35 35
 
36 36
 // Read the ADC value from MCP342X on a specific channel
37
-int16_t MCP3426::ReadValue(uint8_t channel, uint8_t gain) {
37
+int16_t MCP3426::ReadValue(uint8_t channel, uint8_t gain, uint8_t address) {
38 38
   Error = false;
39 39
 
40 40
   #if PINS_EXIST(I2C_SCL, I2C_SDA) && DISABLED(SOFT_I2C_EEPROM)
@@ -44,7 +44,7 @@ int16_t MCP3426::ReadValue(uint8_t channel, uint8_t gain) {
44 44
 
45 45
   Wire.begin(); // No address joins the BUS as the master
46 46
 
47
-  Wire.beginTransmission(I2C_ADDRESS(MCP342X_ADC_I2C_ADDRESS));
47
+  Wire.beginTransmission(I2C_ADDRESS(address));
48 48
 
49 49
   // Continuous Conversion Mode, 16 bit, Channel 1, Gain x4
50 50
   // 26 = 0b00011000
@@ -75,7 +75,7 @@ int16_t MCP3426::ReadValue(uint8_t channel, uint8_t gain) {
75 75
   uint8_t buffer[len] = {};
76 76
 
77 77
   do {
78
-    Wire.requestFrom(I2C_ADDRESS(MCP342X_ADC_I2C_ADDRESS), len);
78
+    Wire.requestFrom(I2C_ADDRESS(address), len);
79 79
     if (Wire.available() != len) {
80 80
       Error = true;
81 81
       return 0;

+ 1
- 4
Marlin/src/feature/adc/adc_mcp3426.h Ver arquivo

@@ -29,12 +29,9 @@
29 29
 #include <stdint.h>
30 30
 #include <Wire.h>
31 31
 
32
-// Address of MCP342X chip
33
-#define MCP342X_ADC_I2C_ADDRESS 104
34
-
35 32
 class MCP3426 {
36 33
   public:
37
-    int16_t ReadValue(uint8_t channel, uint8_t gain);
34
+    int16_t ReadValue(uint8_t channel, uint8_t gain, uint8_t address);
38 35
     bool Error;
39 36
 };
40 37
 

+ 10
- 5
Marlin/src/gcode/feature/adc/M3426.cpp Ver arquivo

@@ -28,6 +28,8 @@
28 28
 
29 29
 #include "../../../feature/adc/adc_mcp3426.h"
30 30
 
31
+#define MCP3426_BASE_ADDR (0b1101 << 3)
32
+
31 33
 /**
32 34
  * M3426: Read 16 bit (signed) value from I2C MCP3426 ADC device
33 35
  *
@@ -36,12 +38,15 @@
36 38
  *  M3426 I<byte-2 value in base 10> 0 or 1, invert reply
37 39
  */
38 40
 void GcodeSuite::M3426() {
39
-  uint8_t channel = parser.byteval('C', 1),       // Select the channel 1 or 2
40
-             gain = parser.byteval('G', 1);
41
-  const bool inverted = parser.byteval('I') == 1;
41
+  uint8_t channel = parser.byteval('C', 1), // Channel 1 or 2
42
+             gain = parser.byteval('G', 1), // Gain 1, 2, 4, or 8
43
+          address = parser.byteval('A', 3); // Address 0-7 (or 104-111)
44
+  const bool inverted = parser.boolval('I');
45
+
46
+  if (address <= 7) address += MCP3426_BASE_ADDR;
42 47
 
43
-  if (channel <= 2 && (gain == 1 || gain == 2 || gain == 4 || gain == 8)) {
44
-    int16_t result = mcp3426.ReadValue(channel, gain);
48
+  if (WITHIN(channel, 1, 2) && (gain == 1 || gain == 2 || gain == 4 || gain == 8) && WITHIN(address, MCP3426_BASE_ADDR, MCP3426_BASE_ADDR + 7)) {
49
+    int16_t result = mcp3426.ReadValue(channel, gain, address);
45 50
 
46 51
     if (mcp3426.Error == false) {
47 52
       if (inverted) {

Carregando…
Cancelar
Salvar