Skip to content

Commit

Permalink
Merge pull request #1 from Tom-Hirschberger/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Tom-Hirschberger authored Oct 5, 2020
2 parents 1d3db4f + 2302757 commit aaca3be
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 6 deletions.
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ The strip can be controlled either via hardware or software spi. The preferred m
**If you want to use an WS281X strip like i.e. WS2812B you can not use the build in sound output of the Raspberry Pi because both machnism use PWM. The installation guide will show you how to disable the kernel module of the on-board sound card!**

## Wiring ##
The strip uses 5V spi lanes but the Raspberry uses 3.3V. We need to use an level converter to get rid of this problem. Connecting the strip directly to the Pi may cause harm to it.

The buttons will be connected with a 2kOhm pull down resistor each. The resistors will pull down the gpio to 0V if the buttons are not pressed. When the buttons are pressed the gpio pins will be connected to the 3.3V lane which results in an high flag.
The strip uses 5V spi lanes but the Raspberry uses 3.3V. We need to use an level converter to get rid of this problem. Connecting the strip directly to the Pi may cause harm to it!

There are different possibilities to wire the buttons. I will show three examples.

### Wiring WS2801 ###
This example uses the hardware spi pins to connect the led strip.
Expand All @@ -27,6 +26,31 @@ This example uses the GPIO21. 10, 12 and 18 are supported, also.

![Wiring WS281X](https://github.com/Tom-Hirschberger/PythonLedControl/raw/master/ledcontrol-WS281X.png "Wiring WS281X")

### Wiring the Buttons with pull-down resistors ###
GPIO pins do not have an inital default state. To get one we connect an resistor to the ground and to the pin. The GPIO then is low and will be set to high if the switch is pressed and the connection to the VCC pin is closed.

![Two buttons pull-down](https://github.com/Tom-Hirschberger/PythonLedControl/raw/master/ledcontrol-buttons-pulldown.png "Wiring the buttons with pull-down resistors")

You need to set "LED_BTN_TRIGGER_ON_HIGH=1" in the environment file to get this setup work (default)!

### Wiring the Buttons with pull-up resistors ###
GPIO pins do not have an inital default state. To get one we connect an resistor to the VCC and to the pin. The GPIO then is high and will be set to low if the switch is pressed and the connection to the ground pin is closed.

This setup is more robust against influnces of other electronical devices like frigerators or microwaves.

![Two buttons pull-up](https://github.com/Tom-Hirschberger/PythonLedControl/raw/master/ledcontrol-buttons-pullup.png "Wiring the buttons with pull-up resistors")

You need to set "LED_BTN_TRIGGER_ON_HIGH=0" in the environment file to get this setup work!

### Wiring the Buttons with pull-up resistors and capacitors###
GPIO pins do not have an inital default state. To get one we connect an resistor to the VCC and to the pin. The GPIO then is high and will be set to low if the switch is pressed and the connection to the ground pin is closed.

This setup is more robust against influnces of other electronical devices like frigerators or microwaves than the one which only uses pull-up resistors.

![Two buttons pull-up with capacitors](https://github.com/Tom-Hirschberger/PythonLedControl/raw/master/ledcontrol-buttons-pullup-withCapacitors.png "Wiring the buttons with pull-up resistors")

You need to set "LED_BTN_TRIGGER_ON_HIGH=0" in the environment file to get this setup work!

## Installation ##
### General###
```
Expand Down Expand Up @@ -118,6 +142,8 @@ Open the file /home/pi/ledcontrol/ledcontrol.env in your favorit editor. In exam
LED_BTN_TWO_GPIO=27
#How many milliseconds should be waited before another press of the same button is accepted?
LED_BTN_DEBOUNCE_DELAY=300
#Should the button be triggerd on a high (default) or low value of the gpio
LED_BTN_TRIGGER_ON_HIGH=1
#How many leds does the connected led stripe have?
LED_MAX_LEDS=160
Expand Down Expand Up @@ -159,6 +185,13 @@ Open the file /home/pi/ledcontrol/ledcontrol.env in your favorit editor. In exam
LED_PONG_RESULT_DELAY_DURING=2
#How many seconds should the final result be displayed (fraction support!)?
LED_PONG_RESULT_DELAY_AFTER=5
#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.
LED_PUBLISH_STATUS_AFTER_EVERY_CONFIG_CHANGE=0
#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.
LED_PUBLISH_STATUS_IF_TOGGLED=1
#Should the status be published at the script start?
LED_PUBLISH_STATUS_AT_START=1
```

### MQTT Messages ###
Expand Down
Binary file modified ledcontrol-WS2801.fzz
Binary file not shown.
Binary file modified ledcontrol-WS2801.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ledcontrol-WS2813.fzz
Binary file not shown.
Binary file modified ledcontrol-WS2813.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ledcontrol-WS281X.fzz
Binary file not shown.
Binary file modified ledcontrol-WS281X.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ledcontrol-buttons-pulldown.fzz
Binary file not shown.
Binary file added ledcontrol-buttons-pulldown.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ledcontrol-buttons-pullup-withCapacitors.fzz
Binary file not shown.
Binary file added ledcontrol-buttons-pullup-withCapacitors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ledcontrol-buttons-pullup.fzz
Binary file not shown.
Binary file added ledcontrol-buttons-pullup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion ledcontrol.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ LED_BTN_ONE_GPIO=17
LED_BTN_TWO_GPIO=27
#How many milliseconds should be waited before another press of the same button is accepted?
LED_BTN_DEBOUNCE_DELAY=300
#Should the button be triggerd on a high (default) or low value of the gpio
LED_BTN_TRIGGER_ON_HIGH=1

#How many leds does the connected led stripe have?
LED_MAX_LEDS=160
Expand Down Expand Up @@ -75,6 +77,7 @@ LED_PONG_RESULT_DELAY_AFTER=5

#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.
LED_PUBLISH_STATUS_AFTER_EVERY_CONFIG_CHANGE=0

#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.
LED_PUBLISH_STATUS_IF_TOGGLED=1
#Should the status be published at the script start?
LED_PUBLISH_STATUS_AT_START=1
15 changes: 13 additions & 2 deletions ledcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
DEFAULT_BTN_ONE_GPIO=17
DEFAULT_BTN_TWO_GPIO=27
DEFAULT_BTN_DEBOUNCE_DELAY=300
DEFAULT_BTN_TRIGGER_ON_HIGH=True
DEFAULT_PONG_BTN_DELAY=2.0
DEFAULT_PONG_MAX_WINS=2
DEFAULT_PONG_TOLERANCE=2
Expand All @@ -44,6 +45,7 @@
DEFAULT_COLOR_B=255
DEFAULT_PUBLISH_STATUS_AFTER_EVERY_CONFIG_CHANGE=False
DEFAULT_PUBLISH_STATUS_IF_TOGGLED=True
DEFAULT_PUBLISH_STATUS_AT_START=True

#set the gpio mode to use the gpio numbering and not the pin numbering
GPIO.setmode(GPIO.BCM)
Expand Down Expand Up @@ -105,6 +107,7 @@ def get_board_pin(gpio_nr):
btn_one_gpio = sys_var_to_var("LED_BTN_ONE_GPIO", DEFAULT_BTN_ONE_GPIO)
btn_two_gpio = sys_var_to_var("LED_BTN_TWO_GPIO", DEFAULT_BTN_TWO_GPIO)
btn_debounce = sys_var_to_var("LED_BTN_DEBOUNCE_DELAY", DEFAULT_BTN_DEBOUNCE_DELAY)
btn_trigger_on_high = sys_var_to_var("LED_BTN_TRIGGER_ON_HIGH", DEFAULT_BTN_TRIGGER_ON_HIGH)

reverse_mode = False
cur_pixel = 0
Expand Down Expand Up @@ -152,6 +155,7 @@ def get_board_pin(gpio_nr):

publish_status_after_every_config_change = sys_var_to_var("LED_PUBLISH_STATUS_AFTER_EVERY_CONFIG_CHANGE", DEFAULT_PUBLISH_STATUS_AFTER_EVERY_CONFIG_CHANGE)
publish_status_if_toggled = sys_var_to_var("LED_PUBLISH_STATUS_IF_TOGGLED", DEFAULT_PUBLISH_STATUS_IF_TOGGLED)
publish_status_at_start = sys_var_to_var("LED_PUBLISH_STATUS_AT_START", DEFAULT_PUBLISH_STATUS_AT_START)

led_gpio_mode = False
if led_gpio_pin > 0:
Expand Down Expand Up @@ -639,12 +643,19 @@ def do_cleanup(signum, frame):
GPIO.setup(btn_one_gpio, GPIO.IN)
GPIO.setup(btn_two_gpio, GPIO.IN)

GPIO.add_event_detect(btn_one_gpio, GPIO.RISING, callback=callback_one, bouncetime = btn_debounce)
GPIO.add_event_detect(btn_two_gpio, GPIO.RISING, callback=callback_two, bouncetime = btn_debounce)
if btn_trigger_on_high == True:
trigger_on = GPIO.RISING
else:
trigger_on = GPIO.FALLING

GPIO.add_event_detect(btn_one_gpio, trigger_on, callback=callback_one, bouncetime = btn_debounce)
GPIO.add_event_detect(btn_two_gpio, trigger_on, callback=callback_two, bouncetime = btn_debounce)

#only if mqtt should be used we activate the clients
if mqtt_active:
connect_mqtt_client()
if publish_status_at_start:
publish_current_status()

try:
while not stop_now:
Expand Down

0 comments on commit aaca3be

Please sign in to comment.