Skip to content

Commit bcd24d6

Browse files
added functionality to only send the current configuration if the output status changed (configurable);
the status will not be published every time the configuration changes. This is only needed if more than one module controls the strip. If needed the publishing can be reactivated with a configuration switch.
1 parent 89fef60 commit bcd24d6

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ Switch the led strip on or off
164164

165165
"off" -> Switch all leds off
166166

167+
anything else -> Toggle the status
168+
167169
#### /raspled/get_status ####
168170
Trigger the script to send the current configuration als MQTT message to the topic "raspled/status". The message content will be in json format.
169171
```json5

ledcontrol.env.example

+6
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,9 @@ LED_PONG_BTN_DELAY=2
7272
LED_PONG_RESULT_DELAY_DURING=2
7373
#How many seconds should the final result be displayed (fraction support!)?
7474
LED_PONG_RESULT_DELAY_AFTER=5
75+
76+
#Should the status be published after every config change? This is only needed if you use two different sources to change the configuration of the strip and want the second source to be updated if the first source changes a value.
77+
LED_PUBLISH_STATUS_AFTER_EVERY_CONFIG_CHANGE=0
78+
79+
#Should the status be published if the output state changed? This feature is enabled because if one of the buttons gets pressed and you have an control source active the source gets informed that the output state changed.
80+
LED_PUBLISH_STATUS_IF_TOGGLED=1

ledcontrol.py

+24-6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
DEFAULT_COLOR_R=255
4343
DEFAULT_COLOR_G=255
4444
DEFAULT_COLOR_B=255
45+
DEFAULT_PUBLISH_STATUS_AFTER_EVERY_CONFIG_CHANGE=False
46+
DEFAULT_PUBLISH_STATUS_IF_TOGGLED=True
4547

4648
#set the gpio mode to use the gpio numbering and not the pin numbering
4749
GPIO.setmode(GPIO.BCM)
@@ -148,6 +150,9 @@ def get_board_pin(gpio_nr):
148150
spi_clk_gpio = sys_var_to_var("SPI_CLK_GPIO", DEFAULT_SPI_CLK_GPIO)
149151
spi_data_gpio = sys_var_to_var("SPI_DATA_GPIO", DEFAULT_SPI_DATA_GPIO)
150152

153+
publish_status_after_every_config_change = sys_var_to_var("LED_PUBLISH_STATUS_AFTER_EVERY_CONFIG_CHANGE", DEFAULT_PUBLISH_STATUS_AFTER_EVERY_CONFIG_CHANGE)
154+
publish_status_if_toggled = sys_var_to_var("LED_PUBLISH_STATUS_IF_TOGGLED", DEFAULT_PUBLISH_STATUS_IF_TOGGLED)
155+
151156
led_gpio_mode = False
152157
if led_gpio_pin > 0:
153158
import neopixel, board
@@ -385,7 +390,7 @@ def publish_current_status():
385390
global pong_max_wins, pong_wins_delay_after, pong_wins_delay_during
386391
global pong_color_r, pong_color_g, pong_color_b, pong_result_color_r, pong_result_color_g, pong_result_color_b
387392
global color_r, color_g, color_b
388-
global stripe_on, stripe_mode
393+
global stripe_on, stripe_mode, publish_status_if_toggled
389394

390395
if client != None and client.connected_flag:
391396
print("Will publish the current status!")
@@ -437,11 +442,19 @@ def toggle_leds(to_state = None):
437442
global stripe_on
438443
global pixels
439444

445+
state_changed = False
446+
440447
if to_state != None:
441448
if(to_state == False):
442-
stripe_on = True
449+
if stripe_on != True:
450+
stripe_on = True
451+
state_changed = True
443452
else:
444-
stripe_on = False
453+
if stripe_on != False:
454+
stripe_on = False
455+
state_changed = True
456+
else:
457+
state_changed = True
445458

446459
if stripe_on == False:
447460
for i in range(0,num_leds):
@@ -452,7 +465,9 @@ def toggle_leds(to_state = None):
452465
stripe_on = False
453466

454467
pixels.show()
455-
publish_current_status()
468+
469+
if state_changed and publish_status_if_toggled:
470+
publish_current_status()
456471

457472
#this function will init the pong mode
458473
def switch_to_pong_mode():
@@ -515,7 +530,7 @@ def apply_config(new_config={}):
515530
global pong_max_wins, pong_wins_delay_after, pong_wins_delay_during
516531
global pong_color_r, pong_color_g, pong_color_b, pong_result_color_r, pong_result_color_g, pong_result_color_b
517532
global color_r, color_g, color_b
518-
global stripe_mode, stripe_on
533+
global stripe_mode, stripe_on, publish_status_after_every_config_change
519534

520535
pong_options = new_config.get("pong", {})
521536

@@ -596,7 +611,10 @@ def apply_config(new_config={}):
596611
color_b = 0
597612

598613
if stripe_mode == 0:
599-
toggle_leds(stripe_on)
614+
toggle_leds(to_state = stripe_on)
615+
616+
if publish_status_after_every_config_change:
617+
publish_current_status()
600618

601619
#this function will be called if the script gets killed (sigterm, sigint)
602620
def do_cleanup(signum, frame):

0 commit comments

Comments
 (0)