Ledborg commandline

Hello,

I have the script bellow.

#!/usr/bin/env python

# Import library functions we need
import time
import wiringpi2 as wiringpi
wiringpi.wiringPiSetup()
import sys

print sys.argv[1]
print sys.argv[2]
print sys.argv[3]

# Setup software PWMs on the GPIO pins
PIN_RED = 0
PIN_GREEN = 2
PIN_BLUE = 3
LED_MAX = 100
wiringpi.softPwmCreate(PIN_RED,   0, LED_MAX)
wiringpi.softPwmCreate(PIN_GREEN, 0, LED_MAX)
wiringpi.softPwmCreate(PIN_BLUE,  0, LED_MAX)
wiringpi.softPwmWrite(PIN_RED,    int(sys.argv[1]))
wiringpi.softPwmWrite(PIN_GREEN,  int(sys.argv[2]))
wiringpi.softPwmWrite(PIN_BLUE,   int(sys.argv[3]))
time.sleep( 2.0 )

___________________________________________________________
want to change the collors by the commandline.

However when the script starts everything is fine.
but after the 2 seconds sleep there is a strange behavior.

when i start with all the colors on. there is only blue left.
if i choose a red/green mix there will be no color left on the end of the script.

it would be the best to only change the colors (so remove the first part that set all colors to 0)

all help would be welcome.

piborg's picture

Unfortunately this is a limitation of how the software PWM in wiringpi works.
Put simply it need the script to continue running so that it can maintain the colour on the LedBorg.

One option is to only use the simple on/off controls as shown in the more basic examples: https://www.piborg.org/ledborg/lesson/1
This should maintain the colour after the script ends, but it will only allow you to have 7 colours plus off.

The other option is a bit more tricky and involves two scripts.
What you would have to do is have a script running constantly which controls the LedBorg when it is sent a new colour.
You will then need a second script which takes the command line values and sends them to the first script.

There is a simple example of passing data between scripts using UDP here:
https://wiki.python.org/moin/UdpCommunication
The very first example can be used for the command line script, simply fill in the correct data into MESSAGE.
The second example is how you then receive the message, you would use the contents of DATA after the recvfrom call to set the LedBorg colour.

Hey thanks for the help.
I am not really a python engineer but with your suggestion and some help from google i created the following 2 scripts.

the deamon

#!/usr/bin/env python

# Import library functions we need
import time
import socket
import wiringpi2 as wiringpi
wiringpi.wiringPiSetup()

# Setup software PWMs on the GPIO pins
PIN_RED = 0
PIN_GREEN = 2
PIN_BLUE = 3
LED_MAX = 100


wiringpi.softPwmCreate(PIN_RED,   0, LED_MAX)
wiringpi.softPwmCreate(PIN_GREEN, 0, LED_MAX)
wiringpi.softPwmCreate(PIN_BLUE,  0, LED_MAX)
UDP_IP = "127.0.0.1"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, # Internet
                socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
        data, addr = sock.recvfrom(1024)
        print data
        sdata= data.split(" ")
        print int(sdata[0])
        print int(sdata[1])
        print int(sdata[2])
        wiringpi.softPwmWrite(PIN_RED,    int(sdata[0]))
        wiringpi.softPwmWrite(PIN_GREEN,  int(sdata[1]))
        wiringpi.softPwmWrite(PIN_BLUE,   int(sdata[2]))
        time.sleep( 1.0 )

wiringpi.softPwmWrite(PIN_RED,   0)
wiringpi.softPwmWrite(PIN_GREEN, 0)
wiringpi.softPwmWrite(PIN_BLUE,  0)
time.sleep( 0.1 )
print exit

the client script needs 3 arguments (RGB)
script.py 10 20 30
where Red = 10%
Green = 20%
Blue = 30%

#!/usr/bin/env python

# Import library functions we need
import sys
import socket

print sys.argv[1]
print sys.argv[2]
print sys.argv[3]

UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE = sys.argv[1]+" "+sys.argv[2]+" "+sys.argv[3]

print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT
print "message:", MESSAGE

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.sendto(str(MESSAGE), (UDP_IP, UDP_PORT))

Hope this will also help someone else.

Again thanks for your help.

Subscribe to Comments for "Ledborg commandline"