Flask backend to control ws2812 led stripes via rasperry pi
(install node, install ws2812 libary, install flask, ...)
From the very beginning:
- buy a raspberry pi and a sd card
- follow the instructions and install raspberrian
sudo apt-get updatecurl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -sudo apt-get install nodejs npm node-semver- git clone https://github.com/shark39/sharkled.git
- git submodule init && git submodule update
- sudo apt-get install scons python-dev swig
- cd rpi_ws281x
- sudo scons
- cd python
- sudo python setup.py build
- sudo python setupt.py install
- Since this library and the onboard Raspberry Pi audio both use the PWM, they cannot be used together. You will need to blacklist the Broadcom audio kernel module by creating a file /etc/modprobe.d/snd-blacklist.conf with
blacklist snd_bcm2835
- cd ../..
sudo pip install -r requirements.txt(17.sudo easy_install supervisor)
File Structure:
The python code is in the folder server.
If you want to start the server run sudo python server/api.py
Example Request:
TODO!!!
Code Overview
simplified code to get the main aspects of the code structure
| api.py
## contains all the endpoints
## for creating a new effect send a post request to /effect/...
@app.route("/effect/<name>", methods=['POST', 'OPTIONS'])
def effect(name):
## some processing
lid = ledmaster.add(name=name, parameters=postdata) ## get the indentifier of the effect
return {id: lid, name: name, parameters: ledmaster.getControllerParameters(lid)} ## return id and all parameters
| LedControl.py
## handles all the logic
class LEDMaster
manage all effects and send the output to the led stripes
| Some methods defined here:
|
| add(self, name, parameters={})
| adds or updates the controller indentified by name
|
| writeBuffer(self)
| this function is running in an extra thread
| execute the effect function of the LEDControllers and set the pixels
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| getDefaultParameters(effect)
|
| getEffects()
class LEDEffect(LEDController)
contains all the effects as methods with default values
| Some methods defined here:
|
| color(self, ts, pos, color=[1, 1, 1, 1], **kwargs)
| Description: set a solid color
| return len(pos) * [color]
|
| pulsate(self, ts, pos, interval=1000, wavelength=100, color=[1, 1, 1, 1], background=[0, 0, 0, 0], **kwargs)
| Description: generates a pulsating light
|