Skip to content

Commit d4786d1

Browse files
thinkyheadptoal
authored andcommitted
🎨 Laser Ammeter followup (MarlinFirmware#22079)
Followup to MarlinFirmware#21835
1 parent f76b8d2 commit d4786d1

13 files changed

+90
-116
lines changed

Marlin/Configuration_adv.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -3285,15 +3285,6 @@
32853285
//#define AIR_ASSIST_PIN 44 // Override the default Air Assist pin
32863286
#endif
32873287

3288-
//
3289-
// Laser I2C Ammeter (High precision INA226 low/high side module)
3290-
//
3291-
//#define I2C_AMMETER
3292-
#if ENABLED(I2C_AMMETER)
3293-
#define I2C_AMMETER_IMAX .1 // Calibration value for the expected current range in Amps (use float e.g. 1.0)
3294-
#define I2C_AMMETER_SHUNT_RESISTOR .1 // Calibration shunt resistor value in ohms
3295-
#endif
3296-
32973288
//#define SPINDLE_SERVO // A servo converting an angle to spindle power
32983289
#ifdef SPINDLE_SERVO
32993290
#define SPINDLE_SERVO_NR 0 // Index of servo used for spindle control
@@ -3426,8 +3417,18 @@
34263417
#define SPINDLE_LASER_POWERDOWN_DELAY 50 // (ms) Delay to allow the spindle to stop
34273418

34283419
#endif
3420+
3421+
//
3422+
// Laser I2C Ammeter (High precision INA226 low/high side module)
3423+
//
3424+
//#define I2C_AMMETER
3425+
#if ENABLED(I2C_AMMETER)
3426+
#define I2C_AMMETER_IMAX 0.1 // (Amps) Calibration value for the expected current range
3427+
#define I2C_AMMETER_SHUNT_RESISTOR 0.1 // (Ohms) Calibration shunt resistor value
3428+
#endif
3429+
34293430
#endif
3430-
#endif
3431+
#endif // SPINDLE_FEATURE || LASER_FEATURE
34313432

34323433
/**
34333434
* Synchronous Laser Control with M106/M107

Marlin/src/HAL/STM32/eeprom_flash.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
// Better: "utility/stm32_eeprom.h", but only after updating stm32duino to 2.0.0
3232
// Use EEPROM.h for compatibility, for now.
33-
#include <EEPROM.h>
33+
#include <EEPROM.h>
3434

3535
/**
3636
* The STM32 HAL supports chips that deal with "pages" and some with "sectors" and some that

Marlin/src/feature/ammeter.cpp

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/**
22
* Marlin 3D Printer Firmware
33
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
44
*
@@ -23,27 +23,32 @@
2323
#include "../inc/MarlinConfig.h"
2424

2525
#if ENABLED(I2C_AMMETER)
26-
#include "ammeter.h"
2726

28-
INA226 ina;
27+
#include "ammeter.h"
2928

30-
Ammeter ammeter;
29+
#ifndef I2C_AMMETER_IMAX
30+
#define I2C_AMMETER_IMAX 0.500 // Calibration range 500 Milliamps
31+
#endif
3132

32-
float Ammeter::scale;
33-
float Ammeter::current;
33+
INA226 ina;
3434

35-
void Ammeter::init() {
36-
ina.begin();
37-
ina.configure(INA226_AVERAGES_16, INA226_BUS_CONV_TIME_1100US, INA226_SHUNT_CONV_TIME_1100US, INA226_MODE_SHUNT_BUS_CONT);
38-
ina.calibrate(I2C_AMMETER_SHUNT_RESISTOR,I2C_AMMETER_IMAX);
39-
}
35+
Ammeter ammeter;
4036

41-
float Ammeter::read() {
42-
scale = 1;
43-
current = ina.readShuntCurrent();
44-
if (current <= .0001) current = 0; // Cleanup lsb bit amplification errors
45-
if (current < .1) scale = 1000;
46-
return current * scale;
47-
}
37+
float Ammeter::scale;
38+
float Ammeter::current;
4839

49-
#endif //I2C_AMMETER
40+
void Ammeter::init() {
41+
ina.begin();
42+
ina.configure(INA226_AVERAGES_16, INA226_BUS_CONV_TIME_1100US, INA226_SHUNT_CONV_TIME_1100US, INA226_MODE_SHUNT_BUS_CONT);
43+
ina.calibrate(I2C_AMMETER_SHUNT_RESISTOR, I2C_AMMETER_IMAX);
44+
}
45+
46+
float Ammeter::read() {
47+
scale = 1;
48+
current = ina.readShuntCurrent();
49+
if (current <= 0.0001f) current = 0; // Clean up least-significant-bit amplification errors
50+
if (current < 0.1f) scale = 1000;
51+
return current * scale;
52+
}
53+
54+
#endif // I2C_AMMETER

Marlin/src/feature/ammeter.h

+4-9
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,14 @@
2626
#include <Wire.h>
2727
#include <INA226.h>
2828

29-
#ifndef I2C_AMMETER_IMAX
30-
#define I2C_AMMETER_IMAX .500 // Calibration range 500 Milli Amps
31-
#endif
32-
3329
class Ammeter {
3430
private:
35-
static float scale;
31+
static float scale;
3632

3733
public:
38-
static float current;
39-
static void init();
40-
static float read();
41-
34+
static float current;
35+
static void init();
36+
static float read();
4237
};
4338

4439
extern Ammeter ammeter;

Marlin/src/feature/spindle_laser.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ void SpindleLaser::init() {
7979
OUT_WRITE(AIR_ASSIST_PIN, !AIR_ASSIST_ACTIVE); // Init Air Assist OFF
8080
#endif
8181
#if ENABLED(I2C_AMMETER)
82-
ammeter.init(); // Init I2C Ammeter
82+
ammeter.init(); // Init I2C Ammeter
8383
#endif
84-
8584
}
8685

8786
#if ENABLED(SPINDLE_LASER_PWM)

Marlin/src/inc/Conditionals_LCD.h

+4
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@
405405

406406
#endif
407407

408+
#if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && DISABLED(NO_LCD_DETECT)
409+
#define DETECT_I2C_LCD_DEVICE 1
410+
#endif
411+
408412
#ifndef STD_ENCODER_PULSES_PER_STEP
409413
#if ENABLED(TOUCH_SCREEN)
410414
#define STD_ENCODER_PULSES_PER_STEP 2

Marlin/src/lcd/HD44780/marlinui_HD44780.cpp

+9-22
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@
6868

6969
#elif EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008)
7070

71-
LCD_CLASS lcd(LCD_I2C_ADDRESS
72-
#ifdef DETECT_DEVICE
73-
, 1
74-
#endif
75-
);
71+
LCD_CLASS lcd(LCD_I2C_ADDRESS OPTARG(DETECT_I2C_LCD_DEVICE, 1));
7672

7773
#elif ENABLED(LCD_I2C_TYPE_PCA8574)
7874

@@ -380,11 +376,7 @@ void MarlinUI::init_lcd() {
380376
}
381377

382378
bool MarlinUI::detected() {
383-
return (true
384-
#if EITHER(LCD_I2C_TYPE_MCP23017, LCD_I2C_TYPE_MCP23008) && defined(DETECT_DEVICE)
385-
&& lcd.LcdDetected() == 1
386-
#endif
387-
);
379+
return TERN1(DETECT_I2C_LCD_DEVICE, lcd.LcdDetected() == 1);
388380
}
389381

390382
#if HAS_SLOW_BUTTONS
@@ -602,10 +594,11 @@ FORCE_INLINE void _draw_cooler_status(const char prefix, const bool blink) {
602594
FORCE_INLINE void _draw_ammeter_status() {
603595
lcd_put_u8str(" ");
604596
ammeter.read();
605-
if (ammeter.current <= .999) {
606-
lcd_put_u8str(ftostr3ns(ammeter.current));
597+
if (ammeter.current <= 0.999f) {
598+
lcd_put_u8str(ui16tostr3rj(uint16_t(ammeter.current * 1000 + 0.5f)));
607599
lcd_put_u8str("mA");
608-
} else {
600+
}
601+
else {
609602
lcd_put_u8str(ftostr12ns(ammeter.current));
610603
lcd_put_wchar('A');
611604
}
@@ -847,15 +840,9 @@ void MarlinUI::draw_status_screen() {
847840
#endif
848841
#endif
849842

850-
#if HAS_COOLER
851-
_draw_cooler_status('*', blink);
852-
#endif
853-
#if ENABLED(LASER_COOLANT_FLOW_METER)
854-
_draw_flowmeter_status();
855-
#endif
856-
#if ENABLED(I2C_AMMETER)
857-
_draw_ammeter_status();
858-
#endif
843+
TERN_(HAS_COOLER, _draw_cooler_status('*', blink));
844+
TERN_(LASER_COOLANT_FLOW_METER, _draw_flowmeter_status());
845+
TERN_(I2C_AMMETER, _draw_ammeter_status());
859846

860847
#endif // LCD_WIDTH >= 20
861848

Marlin/src/lcd/dogm/dogm_Statusscreen.h

+29-31
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,13 @@
110110
//
111111
// Laser Ammeter
112112
//
113-
#if !STATUS_AMMETER_WIDTH && ENABLED(I2C_AMMETER)
114-
#include "status/ammeter.h"
115-
#endif
116-
#ifndef STATUS_AMMETER_WIDTH
117-
#define STATUS_AMMETER_WIDTH 0
118-
#endif
119-
#ifndef STATUS_AMMETER_BYTEWIDTH
120-
#define STATUS_AMMETER_BYTEWIDTH BW(STATUS_AMMETER_WIDTH)
113+
#if ENABLED(I2C_AMMETER)
114+
#if !STATUS_AMMETER_WIDTH
115+
#include "status/ammeter.h"
116+
#endif
117+
#ifndef STATUS_AMMETER_WIDTH
118+
#define STATUS_AMMETER_WIDTH 0
119+
#endif
121120
#endif
122121

123122
//
@@ -614,30 +613,29 @@
614613
#endif
615614
#endif
616615

617-
#if ENABLED(I2C_AMMETER)
618-
#if STATUS_AMMETER_WIDTH
619-
620-
#ifndef STATUS_AMMETER_X
621-
#define STATUS_AMMETER_X (LCD_PIXEL_WIDTH - (STATUS_AMMETER_BYTEWIDTH + STATUS_FLOWMETER_BYTEWIDTH + STATUS_FAN_BYTEWIDTH + STATUS_CUTTER_BYTEWIDTH + STATUS_COOLER_BYTEWIDTH) * 8)
622-
#endif
623-
624-
#ifndef STATUS_AMMETER_HEIGHT
625-
#define STATUS_AMMETER_HEIGHT(S) (sizeof(status_ammeter_bmp1) / (STATUS_AMMETER_BYTEWIDTH))
626-
#endif
627-
628-
#ifndef STATUS_AMMETER_Y
629-
#define STATUS_AMMETER_Y(S) (18 - STATUS_AMMETER_HEIGHT(S))
630-
#endif
631-
632-
#ifndef STATUS_AMMETER_TEXT_X
633-
#define STATUS_AMMETER_TEXT_X (STATUS_AMMETER_X + 7)
634-
#endif
635-
636-
static_assert(
637-
sizeof(status_ammeter_bmp1) == (STATUS_AMMETER_BYTEWIDTH) * STATUS_AMMETER_HEIGHT(0),
638-
"Status ammeter bitmap (status_ammeter_bmp1) dimensions don't match data."
639-
);
616+
//
617+
// I2C Laser Ammeter
618+
//
619+
#if ENABLED(I2C_AMMETER) && STATUS_AMMETER_WIDTH
620+
#ifndef STATUS_AMMETER_BYTEWIDTH
621+
#define STATUS_AMMETER_BYTEWIDTH BW(STATUS_AMMETER_WIDTH)
640622
#endif
623+
#ifndef STATUS_AMMETER_X
624+
#define STATUS_AMMETER_X (LCD_PIXEL_WIDTH - (STATUS_AMMETER_BYTEWIDTH + STATUS_FLOWMETER_BYTEWIDTH + STATUS_FAN_BYTEWIDTH + STATUS_CUTTER_BYTEWIDTH + STATUS_COOLER_BYTEWIDTH) * 8)
625+
#endif
626+
#ifndef STATUS_AMMETER_HEIGHT
627+
#define STATUS_AMMETER_HEIGHT(S) (sizeof(status_ammeter_bmp1) / (STATUS_AMMETER_BYTEWIDTH))
628+
#endif
629+
#ifndef STATUS_AMMETER_Y
630+
#define STATUS_AMMETER_Y(S) (18 - STATUS_AMMETER_HEIGHT(S))
631+
#endif
632+
#ifndef STATUS_AMMETER_TEXT_X
633+
#define STATUS_AMMETER_TEXT_X (STATUS_AMMETER_X + 7)
634+
#endif
635+
static_assert(
636+
sizeof(status_ammeter_bmp1) == (STATUS_AMMETER_BYTEWIDTH) * STATUS_AMMETER_HEIGHT(0),
637+
"Status ammeter bitmap (status_ammeter_bmp1) dimensions don't match data."
638+
);
641639
#endif
642640

643641
//

Marlin/src/lcd/dogm/status/ammeter.h

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Marlin 3D Printer Firmware
3-
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
3+
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
44
*
55
* Based on Sprinter and grbl.
66
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
@@ -24,11 +24,10 @@
2424
//
2525
// lcd/dogm/status/ammeter.h - Status Screen Laser Ammeter bitmaps
2626
//
27-
#if ENABLED(I2C_AMMETER)
2827

29-
#define STATUS_AMMETER_WIDTH 20
28+
#define STATUS_AMMETER_WIDTH 20
3029

31-
const unsigned char status_ammeter_bmp_mA[] PROGMEM = {
30+
const unsigned char status_ammeter_bmp_mA[] PROGMEM = {
3231
B00000000,B11111100,B00000000,
3332
B00000011,B00000011,B00000000,
3433
B00000100,B00000000,B10000000,
@@ -46,7 +45,7 @@
4645
B00000100,B00000000,B10000000,
4746
B00000011,B00000011,B00000000,
4847
B00000000,B11111100,B00000000
49-
};
48+
};
5049

5150
const unsigned char status_ammeter_bmp_A[] PROGMEM = {
5251
B00000000,B11111100,B00000000,
@@ -67,5 +66,3 @@ const unsigned char status_ammeter_bmp_A[] PROGMEM = {
6766
B00000011,B00000011,B00000000,
6867
B00000000,B11111100,B00000000,
6968
};
70-
71-
#endif

Marlin/src/lcd/dogm/status_screen_DOGM.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ FORCE_INLINE void _draw_centered_temp(const celsius_t temp, const uint8_t tx, co
207207

208208
#if DO_DRAW_AMMETER
209209
FORCE_INLINE void _draw_centered_current(const float current, const uint8_t tx, const uint8_t ty) {
210-
const char *str = ftostr31ns(current);
210+
const char *str = ftostr31ns(current);
211211
const uint8_t len = str[0] != ' ' ? 3 : str[1] != ' ' ? 2 : 1;
212212
lcd_put_u8str(tx - len * (INFO_FONT_WIDTH) / 2 + 1, ty, &str[3-len]);
213213
}
@@ -697,7 +697,7 @@ void MarlinUI::draw_status_screen() {
697697
const uint8_t ammetery = STATUS_AMMETER_Y(status_ammeter_bmp_mA),
698698
ammeterh = STATUS_AMMETER_HEIGHT(status_ammeter_bmp_mA);
699699
if (PAGE_CONTAINS(ammetery, ammetery + ammeterh - 1))
700-
u8g.drawBitmapP(STATUS_AMMETER_X, ammetery, STATUS_AMMETER_BYTEWIDTH, ammeterh, (ammeter.current < .1) ? status_ammeter_bmp_mA : status_ammeter_bmp_A);
700+
u8g.drawBitmapP(STATUS_AMMETER_X, ammetery, STATUS_AMMETER_BYTEWIDTH, ammeterh, (ammeter.current < 0.1f) ? status_ammeter_bmp_mA : status_ammeter_bmp_A);
701701
#endif
702702

703703
// Heated Bed

Marlin/src/libs/numtostr.cpp

-9
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,6 @@ const char* ftostr41ns(const_float_t f) {
217217
return &conv[2];
218218
}
219219

220-
// Convert unsigned float to string with 123 format
221-
const char* ftostr3ns(const_float_t f) {
222-
const long i = UINTFLOAT(f, 3);
223-
conv[4] = DIGIMOD(i, 100);
224-
conv[5] = DIGIMOD(i, 10);
225-
conv[6] = DIGIMOD(i, 1);
226-
return &conv[4];
227-
}
228-
229220
// Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format
230221
const char* ftostr42_52(const_float_t f) {
231222
if (f <= -10 || f >= 100) return ftostr52(f); // -23.45 / 123.45

Marlin/src/libs/numtostr.h

-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ const char* ftostr31ns(const_float_t x);
7474
// Convert unsigned float to string with 123.4 format
7575
const char* ftostr41ns(const_float_t x);
7676

77-
// Convert unsigned float to string with 123 format
78-
const char* ftostr3ns(const_float_t x);
79-
8077
// Convert signed float to fixed-length string with 12.34 / _2.34 / -2.34 or -23.45 / 123.45 format
8178
const char* ftostr42_52(const_float_t x);
8279

buildroot/tests/mega2560

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ opt_set MOTHERBOARD BOARD_RAMPS_14_EFB EXTRUDERS 0 LCD_LANGUAGE en TEMP_SENSOR_C
193193
MANUAL_FEEDRATE '{ 50*60, 50*60, 4*60 }' \
194194
AXIS_RELATIVE_MODES '{ false, false, false }'
195195
opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER SDSUPPORT EEPROM_SETTINGS EEPROM_BOOT_SILENT EEPROM_AUTO_INIT \
196-
LASER_FEATURE AIR_EVACUATION AIR_EVACUATION_PIN AIR_ASSIST AIR_ASSIST_PIN LASER_COOLANT_FLOW_METER
196+
LASER_FEATURE AIR_EVACUATION AIR_EVACUATION_PIN AIR_ASSIST AIR_ASSIST_PIN LASER_COOLANT_FLOW_METER I2C_AMMETER
197197

198198
exec_test $1 $2 "REPRAP MEGA2560 RAMPS | Laser Feature | Air Evacuation | Air Assist | Cooler | Flowmeter | 44780 LCD " "$3"
199199

0 commit comments

Comments
 (0)