Skip to content

Commit 1c6e54d

Browse files
committed
Move rotary encoder code to its own file
1 parent 8b15c1a commit 1c6e54d

File tree

3 files changed

+73
-69
lines changed

3 files changed

+73
-69
lines changed

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

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -32,75 +32,6 @@ script:
3232
3333
return bambulabs::generate_mqtt_payload(payload_data, ams_id, tray_id);
3434
35-
sensor:
36-
- platform: rotary_encoder
37-
id: encoder_sensor
38-
name: "Encoder Position"
39-
resolution: 2
40-
restore_mode: ALWAYS_ZERO
41-
pin_a:
42-
number: ${encoder_pin_a}
43-
mode:
44-
input: true
45-
pullup: true
46-
pin_b:
47-
number: ${encoder_pin_b}
48-
mode:
49-
input: true
50-
pullup: true
51-
on_clockwise:
52-
then:
53-
- lambda: |-
54-
// Calculate total slots (AMS 0 + all AMS units with 4 slots each)
55-
int total_slots = 1 + id(ams_units).state * 4;
56-
int current_slot;
57-
58-
// Get current absolute slot position
59-
if (id(ams_number).state == 0) {
60-
current_slot = 0;
61-
} else {
62-
current_slot = (id(ams_number).state - 1) * 4 + id(slot_number).state;
63-
}
64-
65-
// Move to next slot
66-
current_slot = (current_slot + 1) % total_slots;
67-
68-
// Convert back to AMS and slot
69-
if (current_slot == 0) {
70-
id(ams_number).publish_state(0);
71-
id(slot_number).publish_state(1);
72-
} else {
73-
id(ams_number).publish_state((current_slot - 1) / 4 + 1);
74-
id(slot_number).publish_state((current_slot - 1) % 4 + 1);
75-
}
76-
- script.execute: update_selected_slot
77-
on_anticlockwise:
78-
then:
79-
- lambda: |-
80-
// Calculate total slots (AMS 0 + all AMS units with 4 slots each)
81-
int total_slots = 1 + id(ams_units).state * 4;
82-
int current_slot;
83-
84-
// Get current absolute slot position
85-
if (id(ams_number).state == 0) {
86-
current_slot = 0;
87-
} else {
88-
current_slot = (id(ams_number).state - 1) * 4 + id(slot_number).state;
89-
}
90-
91-
// Move to previous slot
92-
current_slot = (current_slot + total_slots - 1) % total_slots;
93-
94-
// Convert back to AMS and slot
95-
if (current_slot == 0) {
96-
id(ams_number).publish_state(0);
97-
id(slot_number).publish_state(1);
98-
} else {
99-
id(ams_number).publish_state((current_slot - 1) / 4 + 1);
100-
id(slot_number).publish_state((current_slot - 1) % 4 + 1);
101-
}
102-
- script.execute: update_selected_slot
103-
10435
binary_sensor:
10536
- platform: template
10637
name: "NFC Tag Present"
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
script:
2+
- id: navigate_slots
3+
parameters:
4+
direction: int # 1 for clockwise, -1 for anticlockwise
5+
then:
6+
- lambda: |-
7+
// Calculate total slots (AMS 0 + all AMS units with 4 slots each)
8+
int total_slots = 1 + id(ams_units).state * 4;
9+
int current_slot;
10+
11+
// Get current absolute slot position
12+
if (id(ams_number).state == 0) {
13+
current_slot = 0;
14+
} else {
15+
current_slot = (id(ams_number).state - 1) * 4 + id(slot_number).state;
16+
}
17+
18+
// Move to next/previous slot based on direction
19+
if (direction > 0) {
20+
current_slot = (current_slot + 1) % total_slots;
21+
} else {
22+
current_slot = (current_slot + total_slots - 1) % total_slots;
23+
}
24+
25+
// Convert back to AMS and slot
26+
auto slot_call = id(slot_number).make_call();
27+
auto ams_call = id(ams_number).make_call();
28+
if (current_slot == 0) {
29+
slot_call.set_value(1);
30+
ams_call.set_value(0);
31+
slot_call.perform();
32+
ams_call.perform();
33+
} else {
34+
int ams = (current_slot - 1) / 4 + 1;
35+
int slot = (current_slot - 1) % 4 + 1;
36+
if (id(ams_number).state != ams) {
37+
ams_call.set_value(ams);
38+
ams_call.perform();
39+
}
40+
if (id(slot_number).state != slot) {
41+
slot_call.set_value(slot);
42+
slot_call.perform();
43+
}
44+
}
45+
- script.execute: update_selected_slot
46+
47+
sensor:
48+
- platform: rotary_encoder
49+
id: encoder_sensor
50+
name: "Encoder Position"
51+
resolution: 2
52+
restore_mode: ALWAYS_ZERO
53+
pin_a:
54+
number: ${encoder_pin_a}
55+
mode:
56+
input: true
57+
pullup: true
58+
pin_b:
59+
number: ${encoder_pin_b}
60+
mode:
61+
input: true
62+
pullup: true
63+
on_clockwise:
64+
then:
65+
- script.execute:
66+
id: navigate_slots
67+
direction: 1
68+
on_anticlockwise:
69+
then:
70+
- script.execute:
71+
id: navigate_slots
72+
direction: -1

firmware/esp32-s3-super-mini-manual-ams.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ packages:
1919
improv-serial: !include conf.d/improv-serial.yaml
2020
bambu_ams: !include conf.d/bambu_ams.yaml
2121
openspool-manual-ams: !include openspool-manual-ams.yaml
22+
encoder: !include conf.d/rotary-encoder-manual-ams.yaml # Comment out if you don't use the rotary encoder
2223
led-internal: !include conf.d/led-internal.yaml
2324
extra: !include conf.d/extra.yaml
2425
button: !include conf.d/button.yaml

0 commit comments

Comments
 (0)