micropy-light/code/lib/light_modes.py

78 lines
2.2 KiB
Python
Raw Normal View History

2018-11-19 11:51:04 +01:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Modes for neopixels strip
Each function update the NeoPixel strip buffer (except update_neopixel() that
call them)
"""
2018-11-19 12:05:36 +01:00
__author__ = "arofarn"
__version__ = 0.1
2018-11-19 11:51:04 +01:00
import time
import urandom
def update_neopixel(mode, np_strp, col=(255, 255, 255), bri=50):
"""Update NeoPixel strip with one color
:param np_strp : NeoPixel object
:param mode : mode ;)
:param col : color [R, G, B] (default: [255, 255, 255])
:param bri : brightness 0-100 (default: 50)
"""
2020-04-21 17:56:30 +02:00
if bri > 100:
bri = 100
elif bri < 0:
bri = 0
bri_coef = bri/100
2018-11-19 11:51:04 +01:00
# Apply mode
MODES_LST[mode](np_strp, col)
# Apply brightness to the whole LEDs
2020-04-21 17:56:30 +02:00
np_strp.buf = bytearray([int(x * bri_coef) for x in np_strp.buf])
# Finally display color on LEDs
2018-11-19 11:51:04 +01:00
np_strp.write()
2020-04-21 17:56:30 +02:00
2018-11-19 11:51:04 +01:00
def fill_usr(np_strp, col):
"""Update NeoPixel strip with one color
:param np_strp : NeoPixel object
:param col : color [R, G, B]
"""
np_strp.fill(col)
def fill_white(np_strp, col=(255, 255, 255)):
"""Update NeoPixel strip with only white
:param np_strp : NeoPixel object
:param col : color [R, G, B] (ignored, default=(255, 255, 255))
"""
np_strp.fill((255, 255, 255))
def sparkles(np_strp, col):
2020-04-21 17:56:30 +02:00
"""Make Neopixel sparkle with user defined color!!!"""
2018-11-19 11:51:04 +01:00
np_strp.fill((0, 0, 0))
for _ in range(int(np_strp.n / 4)):
pix = int(urandom.getrandbits(8) / 256 * (np_strp.n))
2018-11-19 11:51:04 +01:00
np_strp[pix] = col
2020-04-21 17:56:30 +02:00
def christmas(np_strp, col):
"""Shine like christmas tree °<:oD
TODO !!!
"""
pass
2018-11-19 11:51:04 +01:00
MODES_LST = (fill_usr, fill_white, sparkles)