2021-04-21 11:15:39 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-04-26 00:34:15 +00:00
|
|
|
'''
|
2021-04-21 11:15:39 +00:00
|
|
|
|
2021-04-26 00:34:15 +00:00
|
|
|
Neopixel GPIO D18 #(-> pin 12)
|
|
|
|
Ground pin 9
|
|
|
|
5 V pin 4
|
2021-04-21 11:15:39 +00:00
|
|
|
|
2021-04-26 00:34:15 +00:00
|
|
|
Modes :
|
2021-04-21 11:15:39 +00:00
|
|
|
|
2021-04-26 00:34:15 +00:00
|
|
|
-1 cls
|
|
|
|
0 Scappy
|
|
|
|
1 Rainbow
|
2021-04-21 11:15:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Simple test for NeoPixels on Raspberry Pi
|
|
|
|
import time
|
|
|
|
import board
|
|
|
|
import neopixel
|
|
|
|
|
|
|
|
|
|
|
|
# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18
|
|
|
|
# NeoPixels must be connected to D10, D12, D18 or D21 to work.
|
2021-04-26 00:34:15 +00:00
|
|
|
pixel_pin = board.D18 #(-> pin 12)
|
2021-04-21 11:15:39 +00:00
|
|
|
|
|
|
|
# The number of NeoPixels
|
2021-04-26 00:34:15 +00:00
|
|
|
num_pixels = 14
|
|
|
|
|
|
|
|
palette = [(0,0,0),
|
|
|
|
(255,0,0),
|
|
|
|
(0,255,0),
|
|
|
|
(0,0,255),
|
|
|
|
(255,255,0),
|
|
|
|
(0,255,255),
|
|
|
|
(255,0,255),
|
|
|
|
(192,192,192),
|
|
|
|
(128,128,128),
|
|
|
|
(128,0,0),
|
|
|
|
(128,128,0),
|
|
|
|
(0,128,0),
|
|
|
|
(128,0,128),
|
|
|
|
(0,128,128),
|
|
|
|
(0,0,128)
|
|
|
|
]
|
|
|
|
|
|
|
|
nbcolor = len(palette)
|
|
|
|
#print(nbcolor, "colors")
|
|
|
|
#print(palette)
|
2021-04-21 11:15:39 +00:00
|
|
|
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
|
|
|
|
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
|
|
|
|
ORDER = neopixel.GRB
|
|
|
|
|
|
|
|
pixels = neopixel.NeoPixel(
|
|
|
|
pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
|
|
|
|
)
|
|
|
|
|
2021-04-26 00:34:15 +00:00
|
|
|
# mode 0 : scapy
|
|
|
|
mode = 0
|
2021-04-21 11:15:39 +00:00
|
|
|
|
|
|
|
def wheel(pos):
|
|
|
|
# Input a value 0 to 255 to get a color value.
|
|
|
|
# The colours are a transition r - g - b - back to r.
|
|
|
|
if pos < 0 or pos > 255:
|
|
|
|
r = g = b = 0
|
|
|
|
elif pos < 85:
|
|
|
|
r = int(pos * 3)
|
|
|
|
g = int(255 - pos * 3)
|
|
|
|
b = 0
|
|
|
|
elif pos < 170:
|
|
|
|
pos -= 85
|
|
|
|
r = int(255 - pos * 3)
|
|
|
|
g = 0
|
|
|
|
b = int(pos * 3)
|
|
|
|
else:
|
|
|
|
pos -= 170
|
|
|
|
r = 0
|
|
|
|
g = int(pos * 3)
|
|
|
|
b = int(255 - pos * 3)
|
|
|
|
return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)
|
|
|
|
|
2021-04-26 00:34:15 +00:00
|
|
|
# rainbow demo
|
2021-04-21 11:15:39 +00:00
|
|
|
def rainbow_cycle(wait):
|
|
|
|
|
2021-04-26 00:34:15 +00:00
|
|
|
if mode == 1:
|
|
|
|
for j in range(255):
|
|
|
|
for i in range(num_pixels):
|
|
|
|
pixel_index = (i * 256 // num_pixels) + j
|
|
|
|
pixels[i] = wheel(pixel_index & 255)
|
|
|
|
pixels.show()
|
|
|
|
time.sleep(wait)
|
|
|
|
|
|
|
|
# rainbow mode
|
|
|
|
def rainbow_mode(wait = 0.001):
|
|
|
|
|
|
|
|
if mode == 1:
|
|
|
|
for j in range(255):
|
|
|
|
for i in range(num_pixels):
|
|
|
|
pixel_index = (i * 256 // num_pixels) + j
|
|
|
|
pixels[i] = wheel(pixel_index & 255)
|
|
|
|
pixels.show()
|
|
|
|
time.sleep(wait)
|
|
|
|
|
|
|
|
def cls():
|
|
|
|
|
|
|
|
print('Cls')
|
|
|
|
mode = -1
|
|
|
|
pixels.fill((0, 0, 0))
|
|
|
|
pixels.show()
|
|
|
|
|
|
|
|
def ledscls():
|
|
|
|
|
|
|
|
print('Cls')
|
|
|
|
pixels.fill((0, 0, 0))
|
|
|
|
pixels.show()
|
|
|
|
|
|
|
|
def display(colors):
|
|
|
|
|
|
|
|
print("Incoming generated colors :",colors)
|
|
|
|
for pixel in range(num_pixels):
|
|
|
|
#print(pixel, colors[pixel], palette[colors[pixel]] )
|
|
|
|
pixels[pixel] = palette[colors[pixel]]
|
|
|
|
#print("pixels array :",pixels)
|
|
|
|
pixels.show()
|
2021-04-21 11:15:39 +00:00
|
|
|
|
2021-04-26 00:34:15 +00:00
|
|
|
|
|
|
|
def demo():
|
|
|
|
global mode
|
|
|
|
|
|
|
|
ledscls()
|
2021-04-21 11:15:39 +00:00
|
|
|
# Comment this line out if you have RGBW/GRBW NeoPixels
|
2021-04-26 00:34:15 +00:00
|
|
|
print("Demo.")
|
2021-04-21 11:15:39 +00:00
|
|
|
pixels.fill((255, 0, 0))
|
|
|
|
# Uncomment this line if you have RGBW/GRBW NeoPixels
|
|
|
|
# pixels.fill((255, 0, 0, 0))
|
|
|
|
pixels.show()
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# Comment this line out if you have RGBW/GRBW NeoPixels
|
|
|
|
pixels.fill((0, 255, 0))
|
|
|
|
# Uncomment this line if you have RGBW/GRBW NeoPixels
|
|
|
|
# pixels.fill((0, 255, 0, 0))
|
|
|
|
pixels.show()
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# Comment this line out if you have RGBW/GRBW NeoPixels
|
|
|
|
pixels.fill((0, 0, 255))
|
|
|
|
# Uncomment this line if you have RGBW/GRBW NeoPixels
|
|
|
|
# pixels.fill((0, 0, 255, 0))
|
|
|
|
pixels.show()
|
|
|
|
time.sleep(1)
|
|
|
|
|
2021-04-26 00:34:15 +00:00
|
|
|
mode = 1
|
2021-04-21 11:15:39 +00:00
|
|
|
rainbow_cycle(0.001) # rainbow cycle with 1ms delay per step
|
2021-04-26 00:34:15 +00:00
|
|
|
|
|
|
|
ledscls()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
demo()
|