Re: [WS281x](SK6812 driver for the Omega2? [Solved])
Hi.
In the last days I played a bit with Python to get the WS281x working wirth Luz' module.
As I want to use HLS color schema (it allows to change the luminosity of the color by only one number), I programmed a function HLS to RGB color string:
def hls2rgb(h,l,s):
r, g, b = colorsys.hls_to_rgb(h,l,s)
shr = str(hex(int(r * 255.0)))
shg = str(hex(int(g * 255.0)))
shb = str(hex(int(b * 255.0)))
# print ([int(255.0*r), int(255.0*g), int(255.0*b)])
if len(shr) == 3:
shr = shr[0:2] + str(0) + shr[2]
if len(shg) == 3:
shg = shg[0:2] + str(0) + shg[2]
if len(shb) == 3:
shb = shb[0:2] + str(0) + shb[2]
col_led_string = shr[2:4].decode('hex') +shg[2:4].decode('hex') + shb[2:4].decode('hex')
By using the following additional function, it allows using a 2 dimensional color matrix which is converted in the corresponding hex color string needed for the module. (It might be useful and easier to use a matrix to handle LED chains instead of a string of hexadigitals)
def hls2rgb_hexstring(hls_matrix):
dim1 = len(hls_matrix)
try:
dim2t = len(hls_matrix[0])
except:
dim2t = 0
if dim2t == 0:
dim2 = dim1
dim1 = 1
else:
dim2 = dim2t
if dim1 == 1:
h = hls_matrix[0]/360.0
l = hls_matrix[1]/100.0
s = hls_matrix[2]/100.0
col_led_string = hls2rgb(h,l,s)
return col_led_string
else:
col_led_string=""
for i in range(0,dim1):
h = hls_matrix[i][0]/360.0
l = hls_matrix[i][1]/100.0
s = hls_matrix[i][2]/100.0
col_led_string = col_led_string + hls2rgb(h,l,s)
return col_led_string
To display a color gradient among several LED's (here 40) one has to run this code, which is using both function above:
color_start = 0
color_end = 359
nr_led = 40
lum = 2
sat = 100
colorMatrix = []
for i in range(color_start,color_end, abs(color_end-color_start)/nr_led):
colorMatrix.append([i,lum,sat])
#print [i,lum, sat]
print colorMatrix
with open('/dev/ledchain0','w') as export:
export.write(hls2rgb_hexstring(colorMatrix))