Skip to content

Commit d55875c

Browse files
committed
Update the ams and slot calculation for rotary encoder
1 parent 00f3671 commit d55875c

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

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

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,54 @@ sensor:
5151
on_clockwise:
5252
then:
5353
- lambda: |-
54-
if(id(ams_number).state == 0) {
55-
id(ams_number).publish_state(1);
56-
} else if (id(slot_number).state == 4) {
57-
if (id(ams_number).state == id(ams_units).state) {
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) {
5870
id(ams_number).publish_state(0);
59-
} else {
60-
id(ams_number).publish_state(id(ams_number).state + 1);
61-
}
62-
id(slot_number).publish_state(1);
71+
id(slot_number).publish_state(1);
6372
} else {
64-
id(slot_number).publish_state(id(slot_number).state + 1);
73+
id(ams_number).publish_state((current_slot - 1) / 4 + 1);
74+
id(slot_number).publish_state((current_slot - 1) % 4 + 1);
6575
}
6676
- script.execute: update_selected_slot
6777
on_anticlockwise:
6878
then:
6979
- lambda: |-
70-
if(id(ams_number).state == 0) {
71-
id(ams_number).publish_state(id(ams_units).state);
72-
id(slot_number).publish_state(4);
73-
} else if (id(slot_number).state == 1) {
74-
id(ams_number).publish_state(id(ams_number).state - 1);
75-
id(slot_number).publish_state(4);
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;
7687
} else {
77-
id(slot_number).publish_state(id(slot_number).state - 1);
88+
current_slot = (id(ams_number).state - 1) * 4 + id(slot_number).state;
7889
}
7990
80-
- script.execute: update_leds
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+
}
81102
- script.execute: update_selected_slot
82103

83104
binary_sensor:

0 commit comments

Comments
 (0)