M3426 to read i2c MCP3426 ADC (#23184)

This commit is contained in:
Stuart Pittaway
2021-12-04 23:44:10 +00:00
committed by Scott Lahteine
parent 05b57278d4
commit c0addd1d33
9 changed files with 234 additions and 5 deletions

View File

@ -0,0 +1,63 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "../../../inc/MarlinConfig.h"
#if ENABLED(HAS_MCP3426_ADC)
#include "../../gcode.h"
#include "../../../feature/adc/adc_mcp3426.h"
/**
* M3426: Read 16 bit (signed) value from I2C MCP3426 ADC device
*
* M3426 C<byte-1 value in base 10> channel 1 or 2
* M3426 G<byte-1 value in base 10> gain 1, 2, 4 or 8
* M3426 I<byte-2 value in base 10> 0 or 1, invert reply
*/
void GcodeSuite::M3426() {
uint8_t channel = parser.byteval('C', 1), // Select the channel 1 or 2
gain = parser.byteval('G', 1);
const bool inverted = parser.byteval('I') == 1;
if (channel <= 2 && (gain == 1 || gain == 2 || gain == 4 || gain == 8)) {
int16_t result = mcp3426.ReadValue(channel, gain);
if (mcp3426.Error == false) {
if (inverted) {
// Should we invert the reading (32767 - ADC value) ?
// Caters to end devices that expect values to increase when in reality they decrease.
// e.g., A pressure sensor in a vacuum when the end device expects a positive pressure.
result = INT16_MAX - result;
}
//SERIAL_ECHOPGM(STR_OK);
SERIAL_ECHOLNPGM("V:", result, " C:", channel, " G:", gain, " I:", inverted ? 1 : 0);
}
else
SERIAL_ERROR_MSG("MCP342X i2c error");
}
else
SERIAL_ERROR_MSG("MCP342X Bad request");
}
#endif // HAS_MCP3426_ADC

View File

@ -1044,6 +1044,10 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
case 7219: M7219(); break; // M7219: Set LEDs, columns, and rows
#endif
#if ENABLED(HAS_MCP3426_ADC)
case 3426: M3426(); break; // M3426: Read MCP3426 ADC (over i2c)
#endif
default: parser.unknown_command_warning(); break;
}
break;

View File

@ -296,6 +296,7 @@
* M917 - L6470 tuning: Find minimum current thresholds. (Requires at least one _DRIVER_TYPE L6470)
* M918 - L6470 tuning: Increase speed until max or error. (Requires at least one _DRIVER_TYPE L6470)
* M951 - Set Magnetic Parking Extruder parameters. (Requires MAGNETIC_PARKING_EXTRUDER)
* M3426 - Read MCP3426 ADC over I2C. (Requires HAS_MCP3426_ADC)
* M7219 - Control Max7219 Matrix LEDs. (Requires MAX7219_GCODE)
*
*** SCARA ***
@ -1199,6 +1200,10 @@ private:
static void M1004();
#endif
#if ENABLED(HAS_MCP3426_ADC)
static void M3426();
#endif
#if ENABLED(MAX7219_GCODE)
static void M7219();
#endif