Skip to content

Commit ec9f9ec

Browse files
committed
Added error handling for AMS external reader and allowed for 0-length hex colors when reading tags (use-case being when clearing all settings from a slot).
1 parent db01628 commit ec9f9ec

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

firmware/bambu.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ namespace bambulabs
123123
}
124124
}
125125

126-
if (doc_in["color_hex"].as<std::string>().length() != 6 && doc_in["color_hex"].as<std::string>().length() != 8) {
126+
if (doc_in["color_hex"].as<std::string>().length() > 0 && doc_in["color_hex"].as<std::string>().length() != 6 && doc_in["color_hex"].as<std::string>().length() != 8) {
127127
ESP_LOGE("bambu", "Invalid color_hex length (expected 6 or 8 characters)");
128128
return {};
129129
}
@@ -133,7 +133,7 @@ namespace bambulabs
133133
print["command"] = "ams_filament_setting";
134134
print["ams_id"] = ams_id;
135135
print["tray_id"] = ams_tray;
136-
if (doc_in["color_hex"].as<std::string>().length() == 6) {
136+
if (doc_in["color_hex"].as<std::string>().length() > 0 && doc_in["color_hex"].as<std::string>().length() == 6) {
137137
print["tray_color"] = doc_in["color_hex"].as<std::string>() + "FF";
138138
}
139139
else{

firmware/common.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ packages:
132132
mqtt_bambu_lan: !include conf.d/mqtt_bambu_lan.yaml
133133
filament: !include conf.d/filament.yaml
134134
bambu_printer: !include conf.d/bambu_printer.yaml
135-
pn532_rfid-solo: !include conf.d/pn532_rfid-solo.yaml # Comment if using an AMS with 4 LEDs; make sure you uncomment below also
136-
# pn532_rfid-ams-external: !include conf.d/pn532_rfid-ams-external.yaml # Uncomment if using an AMS with 4 LEDs; make sure you comment above also
135+
#pn532_rfid-solo: !include conf.d/pn532_rfid-solo.yaml # Comment if using an AMS with 4 LEDs; make sure you uncomment below also
136+
pn532_rfid-ams-external: !include conf.d/pn532_rfid-ams-external.yaml # Uncomment if using an AMS with 4 LEDs; make sure you comment above also
137137
automation: !include conf.d/automation.yaml
138138
ota: !include conf.d/ota.yaml
139139
#update: !include conf.d/update.yaml

firmware/conf.d/pn532_rfid-ams-external.yaml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
---
2-
esphome:
3-
on_boot:
4-
then:
5-
- component.suspend: tap_timer
6-
72
substitutions:
83
spi_data_rate: 200kHz
94
# spi_data_rate: 1Mhz #TODO: decrease this to increase reliability for long cable runs
@@ -18,6 +13,10 @@ globals:
1813
type: int
1914
restore_value: no
2015
initial_value: '0'
16+
- id: global_error
17+
type: bool
18+
restore_value: no
19+
initial_value: 'false'
2120

2221
interval:
2322
- id: tap_timer
@@ -81,14 +80,15 @@ pn532_spi:
8180
- delay: 500ms
8281
- lambda: |-
8382
id(tap_timer).setup();
84-
if(id(global_tap_counter) == 5){
85-
id(set_led_rainbow).execute();
86-
} else{
87-
for(int i = 0; i < id(global_tap_counter); i++) {
88-
id(set_led_green).execute(i);
89-
}
83+
if(id(global_error) == false) {
84+
if(id(global_tap_counter) == 5){
85+
id(set_led_rainbow).execute();
86+
} else{
87+
for(int i = 0; i < id(global_tap_counter); i++) {
88+
id(set_led_green).execute(i);
89+
}
90+
}
9091
}
91-
9292
- delay: 500ms
9393
- light.turn_on:
9494
id: neopixel_light
@@ -99,6 +99,7 @@ pn532_spi:
9999
- if:
100100
condition:
101101
and:
102+
- lambda: 'return id(global_error) == false;'
102103
- lambda: 'return id(global_tap_counter) > 0;'
103104
- ${mqtt}.connected
104105
- lambda: 'return id(filament_raw_data0).state != "";'
@@ -113,6 +114,9 @@ pn532_spi:
113114
# payload: !lambda "return bambulabs::generate_mqtt_payload( id(filament_raw_data0).state, 255, 254 );"
114115
payload: !lambda "return bambulabs::generate_mqtt_payload( id(filament_raw_data0).state, id(global_tap_counter) == 5 ? 255 : 0, id(global_tap_counter) == 5 ? 254: id(global_tap_counter) - 1 );"
115116
- lambda: 'id(filament_raw_data0).publish_state("");'
117+
- globals.set:
118+
id: global_error
119+
value: 'false'
116120

117121

118122
on_tag:
@@ -122,6 +126,7 @@ pn532_spi:
122126
state: ON
123127
- lambda: |-
124128
bool is_valid_openspool = false;
129+
id(global_error) = false;
125130
std::string payload;
126131
127132
if (tag.has_ndef_message()) {
@@ -176,6 +181,7 @@ pn532_spi:
176181
} else {
177182
id(tap_timer).stop_poller();
178183
id(tap_timer).setup();
184+
id(global_error) = true;
179185
for(int i = 0; i < 4; i++) {
180186
id(set_led_red).execute(i);
181187
}

firmware/rpipicow.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ esphome:
66
platform_packages:
77
- framework-arduinopico @ https://github.com/earlephilhower/arduino-pico.git
88
board_build.filesystem_size: 0.5m
9+
on_boot:
10+
then:
11+
- component.suspend: tap_timer # Remove this if only using the spool holder; it's for AMS-external only
912

1013
rp2040:
1114
board: rpipicow

0 commit comments

Comments
 (0)