SlideShare a Scribd company logo
1 of 48
Download to read offline
rpi-ws281x 
something with raspberry-pi, hardware-hacking, node.js and other stuff 
Martin Schuhfuss – @usefulthink
step0: demystify
this is what bits actually 
look like. 
(image from http://www.electrobob.com/ws2812-level-translator/)
rpi-ws281x
LED-Controller 
rpi-ws2811 
ws2812 
full-color LED with 
integrated controller
ws2812 
(image from 
green 
5mm 
‣ SMD5050 Package 
‣ ws2811 controller 
‣ three separate LEDs 
red 
blue 
HARDWARE 
controller
ws2811 
one-wire serial data-signal 
0 1.25μs 2.5μs 3.75μs 
5μs 
1 0 1 1 
+5V 
0V 
t 
wavelength λ pulsewidth 
frequency 800kHz 
wavelength 1.25μs
ws2811 
one-wire serial data-signal 
+5V 
0V 
350ns (±150ns) 800ns (±150ns) 
λ = 1.25μs (±600ns) 
(f = 800kHz) 
0 
t
ws2811 
one-wire serial data-signal 
+5V 
0V 
750ns (±150ns) 600ns (±150ns) 
λ = 1.25μs (±600ns) 
(f = 800kHz) 
1 
t
ws2811 
one-wire serial data-signal 
1 0 1 1 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 1 0 1 0 0 
b 2 8 0 f 4 
#80b2f4 
in HTML-Notation
ws2811 
one-wire serial data-signal 
LED 01 
Register 
#000000 
DIN DOUT 
LED 02 
Register 
#000000 
DIN DOUT 
LED 03 
Register 
#000000 
DIN DOUT 
+5V 
GND 
+5V 
GND 
+5V 
GND 
+5V 
GND
ws2811 
one-wire serial data-signal 
LED 01 
Register 
#ff8800 
DIN DOUT 
1 
#ff8800 
LED 02 
Register 
#000000 
DIN DOUT 
LED 03 
Register 
#000000 
DIN DOUT 
— — 
+5V 
GND 
+5V 
GND 
+5V 
GND 
+5V 
GND
ws2811 
one-wire serial data-signal 
LED 01 
Register 
#ff8800 
DIN DOUT 
2 
#00ffff 
LED 02 
Register 
#00ffff 
DIN DOUT 
LED 03 
Register 
#000000 
DIN DOUT 
#00ffff — 
+5V 
GND 
+5V 
GND 
+5V 
GND 
+5V 
GND
ws2811 
one-wire serial data-signal 
LED 01 
Register 
#ff8800 
DIN DOUT 
3 
#0000ff 
LED 02 
Register 
#ffff00 
DIN DOUT 
LED 03 
Register 
#0000ff 
DIN DOUT 
#0000ff #0000ff 
+5V 
GND 
+5V 
GND 
+5V 
GND 
+5V 
GND
ws2811 
one-wire serial data-signal 
LED 01 
Register 
#000000 
DIN DOUT 
4 
reset 
LED 02 
Register 
#000000 
DIN DOUT 
LED 03 
Register 
#000000 
DIN DOUT 
reset reset 
+5V 
GND 
+5V 
GND 
+5V 
GND 
+5V 
GND
neopixel etc. 
where to get your ws2812 
USA http://www.adafruit.com/category/168 
https://www.sparkfun.com/search/results?term=ws2812 
http://www.watterott.com/index.php?page=search&keywords=ws2812 
GERMANY 
OTHER 
INTERNATIONAL 
ebay, amazon (search "ws2812") 
direct from china: banggood.com, www.dx.com, alibaba.com
build your own :)
step1: control
Raspberry Pi 
Ethernet 
‣ Broadcom BCM2xxx SoC 
‣ 700MHz ARM CPU / 512MB RAM 
‣ SD-Card as Harddrive 
‣ 100MBit LAN / HDMI / USB / Audio 
‣ raspbian OS 
Analog-Audio USB 
HDMI 
micro-USB 
(power supply) 
Digital-Audio 
GPIO 
SD-Card 
Status-LEDs
Raspberry Pi 
‣ interface to external hardware 
‣ sensors, motors, anything 
‣ SPI / UART / I2C 
‣ controllable logic-pins 
GPIO
step2: drivers
problem 
the signal-level needs to be changed 
1.6 Million times per second. 
(image from http://www.electrobob.com/ws2812-level-translator/)
Arduino: no problem* 
‣ CPU running at 16MHz 
‣ 62 nanoseconds per instruction 
‣ lots of time™ in between level changes… 
* assembler-programming required :(
solution awesomeness 
awesomeness 
solutions 
programming it 
with arduino 
#hhjs 
sleep 
(based on a totally representative study with 1 participant)
Raspbian: problem 
‣ because multitasking. 
‣ when the process runs depends on lots 
of factors we can’t control
Raspberry Pi PWM 
‣ „serializer“-mode 
‣ bits in data-registers represent 
signal-level per PWM-cycle 
‣ only a few bytes in registers 
‣ needs DMA to transport data
Raspberry Pi PWM 
+5V 
0V 
1 0 0 bits written to 
(frequency: 2.4 MHz) 
0 
t 
PWM cycle: 416ns 
PWM-Registers
Raspberry Pi PWM 
+5V 
0V 
1 1 0 bits written to 
(frequency: 2.4 MHz) 
1 
t 
PWM cycle: 416ns 
PWM-Registers
Raspbian: problem 
‣ store prepared pixel-data in memory 
‣ let the DMA-Controller transfer data 
to PWM-Module 
‣ CPU or OS are not involved!
solution awesomeness 
awesomeness 
solutions 
arduino 
#hhjs 
sleep 
program it in C 
on raspberry 
build a server 
in C and control 
with node 
write a 
node-addon.
step3: node!
node-rpi-ws281x 
‣ based on a C-Library by Jeremy Garff 
‣ wrapper handles only type-checking and 
conversions between V8-types and C 
‣ simple JS-interface: 
ws281x = { 
init: function(numLeds, options) { … }, 
/** @param ledData {Uint32Array} */ 
render: function(ledData) { … }, 
reset: function() { … } 
}; 
C-Library source:
writing node-addons 
‣ first challenge: get it to compile. 
‣ hard to find documentation 
‣ solution was actually pretty simple 
// binding.gyp (some stuff left out) 
{ 
targets: [{ 
target_name: "rpi_ws281x", 
sources: ["./src/rpi-ws281x.cc"], 
dependencies: ["libws2811"] 
}, { 
target_name: "libws2811", 
type: "static_library", 
sources: [ … ], 
} 
] 
} 
(required reading: http://nethack4.org/blog/building-c.html)
writing node-addons 
no arguments / return undefined 
// rpi_ws281x.cc 
#include <node.h> 
#include <v8.h> 
using namespace v8; 
Handle<Value> Reset(const Arguments& args) { 
HandleScope scope; 
// ... binding-code here ... 
return scope.Close(Undefined()); 
} 
void initialize(Handle<Object> exports) { 
// exports.reset = function Reset() {}; 
exports->Set(String::NewSymbol("reset"), 
FunctionTemplate::New(Reset)->GetFunction()); 
} 
NODE_MODULE(rpi_ws281x, initialize)
writing node-addons 
accepting arguments/ throwing exceptions 
// rpi_ws281x.cc 
Handle<Value> Init(const Arguments& args) { 
HandleScope scope; 
if(args.Length() < 1 || !args[0]->IsNumber()) { 
ThrowException(Exception::TypeError( 
String::New("init(): argument 0 is not a number"))); 
return scope.Close(Undefined()); 
} 
int numLEDs = args[0]->Int32Value(); 
// ... the actual binding-code ... 
return scope.Close(Undefined()); 
}
writing node-addons 
lessons learned… 
‣ writing a simple node addon is not as 
complicated as it seems 
‣ i should write more C++ 
‣ most of the code to deal with arguments and 
type-checking 
‣ better understanding how V8 works
finally: Javascript
hello ws281x-native 
the javascript-side 
var ws281x = require('../lib/binding/ws281x-native'); 
var NUM_LEDS = 100, 
data = new Uint32Array(NUM_LEDS); 
ws281x.init(NUM_LEDS); 
data[42] = 0xff0000; 
ws281x.render(data); 
setTimeout(function() { ws281x.reset(); }, 2000);
hello ws281x-native 
of course it didn't end there 
‣ API feels a bit too low-level 
‣ numbers as colors are complicated 
‣ need to manually manage the data-array
matrix-API 
the javascript-side 
var ws281x = require('../lib/ws281x'), 
m = ws281x.createMatrix(10,10) 
Color = ws281x.Color; 
var offset = 0, 
c1 = new Color('red'), c2 = new Color('blue'); 
setInterval(function() { 
m.clear(); 
for(var i=0; i<10; i++) { 
var color = Color.mix(c1, c2, ((i % 10) / 10)); 
m.set(i%10, (i+offset)%10, color); 
} 
offset++; 
m.render(); 
}, 1000 / 30);
solution awesomeness 
awesomeness 
solutions 
arduino 
#hhjs 
sleep 
program it in C 
on raspberry 
build a server 
in C and control 
with node 
✔ 
write a 
node-addon.
solution awesomeness 
awesomeness 
solutions 
#hhjs 
sleep 
program it in C 
on raspberry 
build a server 
in C and control 
with node 
write a 
node-addon. 
use 
<canvas>
canvas-API 
‣ web-technology FTW! 
‣ has more stuff than we need. 
‣ can be rendered server-side with node-canvas 
‣ also works in the browser
canvas-API 
the javascript-side 
var ws281x = require('../lib/ws281x'), 
canvas = ws281x.createCanvas(10,10), 
ctx = canvas.ctx, 
Color = ws281x.Color; 
var c1 = new Color('red'), c2 = new Color('blue'); 
function rnd(max) { return (max || 1) * Math.random(); } 
function rndi(max) { return Math.round(rnd(max)); } 
setInterval(function() { 
var c = Color.mix(c1,c2, rnd()); 
ctx.clearRect(0,0,10,10); 
ctx.fillStyle = 'rgb(' + c.rgb.join(',') + ')'; 
ctx.fillRect(rndi(10)-2, rndi(10)-2, rndi(10), rndi(10)); 
canvas.render(); 
}, 1000/5);
more to come… 
‣ open-source everything [really soon] 
‣ browser-based IDE on the raspberry [in progress] 
‣ integrate with other hardware [planning] 
‣ improved 3d-printed casing [some day…]
final thoughts 
why would you want to do that? 
‣ do something you love. just for the fun. 
‣ understand how stuff works. 
‣ learn something new. 
‣ no deadlines (well, except you do a talk about it)
<3 
thank you! 
come to me if you have any questions… 
Martin Schuhfuss – @usefulthink

More Related Content

What's hot

Windows debugging sisimon
Windows debugging   sisimonWindows debugging   sisimon
Windows debugging sisimonSisimon Soman
 
From front-end to the hardware
From front-end to the hardwareFrom front-end to the hardware
From front-end to the hardwareHenri Cavalcante
 
Async Debugging - A Practical Guide to survive !
Async Debugging - A Practical Guide to survive !Async Debugging - A Practical Guide to survive !
Async Debugging - A Practical Guide to survive !Mirco Vanini
 
zkStudyClub: CirC and Compiling Programs to Circuits
zkStudyClub: CirC and Compiling Programs to CircuitszkStudyClub: CirC and Compiling Programs to Circuits
zkStudyClub: CirC and Compiling Programs to CircuitsAlex Pruden
 
Digital system design practical file
Digital system design practical fileDigital system design practical file
Digital system design practical fileArchita Misra
 
Virtual machines - how they work
Virtual machines - how they workVirtual machines - how they work
Virtual machines - how they workBartosz Sypytkowski
 
GTC16 - S6410 - Comparing OpenACC 2.5 and OpenMP 4.5
GTC16 - S6410 - Comparing OpenACC 2.5 and OpenMP 4.5GTC16 - S6410 - Comparing OpenACC 2.5 and OpenMP 4.5
GTC16 - S6410 - Comparing OpenACC 2.5 and OpenMP 4.5Jeff Larkin
 
maXbox Starter 39 GEO Maps Tutorial
maXbox Starter 39 GEO Maps TutorialmaXbox Starter 39 GEO Maps Tutorial
maXbox Starter 39 GEO Maps TutorialMax Kleiner
 
Dsd lab Practical File
Dsd lab Practical FileDsd lab Practical File
Dsd lab Practical FileSoumya Behera
 
Managing Large-scale Networks with Trigger
Managing Large-scale Networks with TriggerManaging Large-scale Networks with Trigger
Managing Large-scale Networks with Triggerjathanism
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2PVS-Studio
 
Programming at Compile Time
Programming at Compile TimeProgramming at Compile Time
Programming at Compile TimeemBO_Conference
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...Positive Hack Days
 
The CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitThe CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitAndrey Karpov
 
Event driven simulator
Event driven simulatorEvent driven simulator
Event driven simulatorSahil Abrol
 
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)Miguel Arroyo
 
All VLSI programs
All VLSI programsAll VLSI programs
All VLSI programsGouthaman V
 

What's hot (20)

Windows debugging sisimon
Windows debugging   sisimonWindows debugging   sisimon
Windows debugging sisimon
 
From front-end to the hardware
From front-end to the hardwareFrom front-end to the hardware
From front-end to the hardware
 
Async Debugging - A Practical Guide to survive !
Async Debugging - A Practical Guide to survive !Async Debugging - A Practical Guide to survive !
Async Debugging - A Practical Guide to survive !
 
zkStudyClub: CirC and Compiling Programs to Circuits
zkStudyClub: CirC and Compiling Programs to CircuitszkStudyClub: CirC and Compiling Programs to Circuits
zkStudyClub: CirC and Compiling Programs to Circuits
 
Digital system design practical file
Digital system design practical fileDigital system design practical file
Digital system design practical file
 
Virtual machines - how they work
Virtual machines - how they workVirtual machines - how they work
Virtual machines - how they work
 
Der perfekte 12c trigger
Der perfekte 12c triggerDer perfekte 12c trigger
Der perfekte 12c trigger
 
GTC16 - S6410 - Comparing OpenACC 2.5 and OpenMP 4.5
GTC16 - S6410 - Comparing OpenACC 2.5 and OpenMP 4.5GTC16 - S6410 - Comparing OpenACC 2.5 and OpenMP 4.5
GTC16 - S6410 - Comparing OpenACC 2.5 and OpenMP 4.5
 
maXbox Starter 39 GEO Maps Tutorial
maXbox Starter 39 GEO Maps TutorialmaXbox Starter 39 GEO Maps Tutorial
maXbox Starter 39 GEO Maps Tutorial
 
Dsd lab Practical File
Dsd lab Practical FileDsd lab Practical File
Dsd lab Practical File
 
Managing Large-scale Networks with Trigger
Managing Large-scale Networks with TriggerManaging Large-scale Networks with Trigger
Managing Large-scale Networks with Trigger
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
 
TVM VTA (TSIM)
TVM VTA (TSIM) TVM VTA (TSIM)
TVM VTA (TSIM)
 
Protecting C++
Protecting C++Protecting C++
Protecting C++
 
Programming at Compile Time
Programming at Compile TimeProgramming at Compile Time
Programming at Compile Time
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
 
The CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitThe CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGit
 
Event driven simulator
Event driven simulatorEvent driven simulator
Event driven simulator
 
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
Go Go Gadget! - An Intro to Return Oriented Programming (ROP)
 
All VLSI programs
All VLSI programsAll VLSI programs
All VLSI programs
 

Similar to Control RGB LED Strips with Raspberry Pi, Node.js and WS2812 Drivers

Getting Started with Raspberry Pi - DCC 2013.1
Getting Started with Raspberry Pi - DCC 2013.1Getting Started with Raspberry Pi - DCC 2013.1
Getting Started with Raspberry Pi - DCC 2013.1Tom Paulus
 
20081114 Friday Food iLabt Bart Joris
20081114 Friday Food iLabt Bart Joris20081114 Friday Food iLabt Bart Joris
20081114 Friday Food iLabt Bart Jorisimec.archive
 
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Low-level Shader Optimization for Next-Gen and DX11 by Emil PerssonLow-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Low-level Shader Optimization for Next-Gen and DX11 by Emil PerssonAMD Developer Central
 
Vectorization on x86: all you need to know
Vectorization on x86: all you need to knowVectorization on x86: all you need to know
Vectorization on x86: all you need to knowRoberto Agostino Vitillo
 
The internet of (lego) trains
The internet of (lego) trainsThe internet of (lego) trains
The internet of (lego) trainsGrzegorz Duda
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
Metasepi team meeting #19: ATS application on Arduino
Metasepi team meeting #19: ATS application on ArduinoMetasepi team meeting #19: ATS application on Arduino
Metasepi team meeting #19: ATS application on ArduinoKiwamu Okabe
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기Ji Hun Kim
 
Linux+sensor+device-tree+shell=IoT !
Linux+sensor+device-tree+shell=IoT !Linux+sensor+device-tree+shell=IoT !
Linux+sensor+device-tree+shell=IoT !Dobrica Pavlinušić
 
Day 20.1 configuringframerelay
Day 20.1 configuringframerelayDay 20.1 configuringframerelay
Day 20.1 configuringframerelayCYBERINTELLIGENTS
 
Introduction to Industrial Control Systems : Pentesting PLCs 101 (BlackHat Eu...
Introduction to Industrial Control Systems : Pentesting PLCs 101 (BlackHat Eu...Introduction to Industrial Control Systems : Pentesting PLCs 101 (BlackHat Eu...
Introduction to Industrial Control Systems : Pentesting PLCs 101 (BlackHat Eu...arnaudsoullie
 
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)Gavin Guo
 
MADlib Architecture and Functional Demo on How to Use MADlib/PivotalR
MADlib Architecture and Functional Demo on How to Use MADlib/PivotalRMADlib Architecture and Functional Demo on How to Use MADlib/PivotalR
MADlib Architecture and Functional Demo on How to Use MADlib/PivotalRPivotalOpenSourceHub
 
Building High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortBuilding High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortStefan Marr
 
Basics Of Embedded Systems
Basics Of Embedded SystemsBasics Of Embedded Systems
Basics Of Embedded Systemsarlabstech
 
Lesson 24. Phantom errors
Lesson 24. Phantom errorsLesson 24. Phantom errors
Lesson 24. Phantom errorsPVS-Studio
 
Scottish Ruby Conference 2010 Arduino, Ruby RAD
Scottish Ruby Conference 2010 Arduino, Ruby RADScottish Ruby Conference 2010 Arduino, Ruby RAD
Scottish Ruby Conference 2010 Arduino, Ruby RADlostcaggy
 

Similar to Control RGB LED Strips with Raspberry Pi, Node.js and WS2812 Drivers (20)

Getting Started with Raspberry Pi - DCC 2013.1
Getting Started with Raspberry Pi - DCC 2013.1Getting Started with Raspberry Pi - DCC 2013.1
Getting Started with Raspberry Pi - DCC 2013.1
 
20081114 Friday Food iLabt Bart Joris
20081114 Friday Food iLabt Bart Joris20081114 Friday Food iLabt Bart Joris
20081114 Friday Food iLabt Bart Joris
 
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Low-level Shader Optimization for Next-Gen and DX11 by Emil PerssonLow-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
 
Vectorization on x86: all you need to know
Vectorization on x86: all you need to knowVectorization on x86: all you need to know
Vectorization on x86: all you need to know
 
The internet of (lego) trains
The internet of (lego) trainsThe internet of (lego) trains
The internet of (lego) trains
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
Metasepi team meeting #19: ATS application on Arduino
Metasepi team meeting #19: ATS application on ArduinoMetasepi team meeting #19: ATS application on Arduino
Metasepi team meeting #19: ATS application on Arduino
 
Windbg랑 친해지기
Windbg랑 친해지기Windbg랑 친해지기
Windbg랑 친해지기
 
Dpdk applications
Dpdk applicationsDpdk applications
Dpdk applications
 
Linux+sensor+device-tree+shell=IoT !
Linux+sensor+device-tree+shell=IoT !Linux+sensor+device-tree+shell=IoT !
Linux+sensor+device-tree+shell=IoT !
 
Day 20.1 configuringframerelay
Day 20.1 configuringframerelayDay 20.1 configuringframerelay
Day 20.1 configuringframerelay
 
Lcd12 a64
Lcd12 a64Lcd12 a64
Lcd12 a64
 
Elliptics
EllipticsElliptics
Elliptics
 
Introduction to Industrial Control Systems : Pentesting PLCs 101 (BlackHat Eu...
Introduction to Industrial Control Systems : Pentesting PLCs 101 (BlackHat Eu...Introduction to Industrial Control Systems : Pentesting PLCs 101 (BlackHat Eu...
Introduction to Industrial Control Systems : Pentesting PLCs 101 (BlackHat Eu...
 
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
Spectre(v1%2 fv2%2fv4) v.s. meltdown(v3)
 
MADlib Architecture and Functional Demo on How to Use MADlib/PivotalR
MADlib Architecture and Functional Demo on How to Use MADlib/PivotalRMADlib Architecture and Functional Demo on How to Use MADlib/PivotalR
MADlib Architecture and Functional Demo on How to Use MADlib/PivotalR
 
Building High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortBuilding High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low Effort
 
Basics Of Embedded Systems
Basics Of Embedded SystemsBasics Of Embedded Systems
Basics Of Embedded Systems
 
Lesson 24. Phantom errors
Lesson 24. Phantom errorsLesson 24. Phantom errors
Lesson 24. Phantom errors
 
Scottish Ruby Conference 2010 Arduino, Ruby RAD
Scottish Ruby Conference 2010 Arduino, Ruby RADScottish Ruby Conference 2010 Arduino, Ruby RAD
Scottish Ruby Conference 2010 Arduino, Ruby RAD
 

Recently uploaded

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 

Recently uploaded (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 

Control RGB LED Strips with Raspberry Pi, Node.js and WS2812 Drivers

  • 1. rpi-ws281x something with raspberry-pi, hardware-hacking, node.js and other stuff Martin Schuhfuss – @usefulthink
  • 3. this is what bits actually look like. (image from http://www.electrobob.com/ws2812-level-translator/)
  • 5. LED-Controller rpi-ws2811 ws2812 full-color LED with integrated controller
  • 6. ws2812 (image from green 5mm ‣ SMD5050 Package ‣ ws2811 controller ‣ three separate LEDs red blue HARDWARE controller
  • 7. ws2811 one-wire serial data-signal 0 1.25μs 2.5μs 3.75μs 5μs 1 0 1 1 +5V 0V t wavelength λ pulsewidth frequency 800kHz wavelength 1.25μs
  • 8. ws2811 one-wire serial data-signal +5V 0V 350ns (±150ns) 800ns (±150ns) λ = 1.25μs (±600ns) (f = 800kHz) 0 t
  • 9. ws2811 one-wire serial data-signal +5V 0V 750ns (±150ns) 600ns (±150ns) λ = 1.25μs (±600ns) (f = 800kHz) 1 t
  • 10. ws2811 one-wire serial data-signal 1 0 1 1 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 1 0 1 0 0 b 2 8 0 f 4 #80b2f4 in HTML-Notation
  • 11. ws2811 one-wire serial data-signal LED 01 Register #000000 DIN DOUT LED 02 Register #000000 DIN DOUT LED 03 Register #000000 DIN DOUT +5V GND +5V GND +5V GND +5V GND
  • 12. ws2811 one-wire serial data-signal LED 01 Register #ff8800 DIN DOUT 1 #ff8800 LED 02 Register #000000 DIN DOUT LED 03 Register #000000 DIN DOUT — — +5V GND +5V GND +5V GND +5V GND
  • 13. ws2811 one-wire serial data-signal LED 01 Register #ff8800 DIN DOUT 2 #00ffff LED 02 Register #00ffff DIN DOUT LED 03 Register #000000 DIN DOUT #00ffff — +5V GND +5V GND +5V GND +5V GND
  • 14. ws2811 one-wire serial data-signal LED 01 Register #ff8800 DIN DOUT 3 #0000ff LED 02 Register #ffff00 DIN DOUT LED 03 Register #0000ff DIN DOUT #0000ff #0000ff +5V GND +5V GND +5V GND +5V GND
  • 15. ws2811 one-wire serial data-signal LED 01 Register #000000 DIN DOUT 4 reset LED 02 Register #000000 DIN DOUT LED 03 Register #000000 DIN DOUT reset reset +5V GND +5V GND +5V GND +5V GND
  • 16. neopixel etc. where to get your ws2812 USA http://www.adafruit.com/category/168 https://www.sparkfun.com/search/results?term=ws2812 http://www.watterott.com/index.php?page=search&keywords=ws2812 GERMANY OTHER INTERNATIONAL ebay, amazon (search "ws2812") direct from china: banggood.com, www.dx.com, alibaba.com
  • 19. Raspberry Pi Ethernet ‣ Broadcom BCM2xxx SoC ‣ 700MHz ARM CPU / 512MB RAM ‣ SD-Card as Harddrive ‣ 100MBit LAN / HDMI / USB / Audio ‣ raspbian OS Analog-Audio USB HDMI micro-USB (power supply) Digital-Audio GPIO SD-Card Status-LEDs
  • 20. Raspberry Pi ‣ interface to external hardware ‣ sensors, motors, anything ‣ SPI / UART / I2C ‣ controllable logic-pins GPIO
  • 22. problem the signal-level needs to be changed 1.6 Million times per second. (image from http://www.electrobob.com/ws2812-level-translator/)
  • 23. Arduino: no problem* ‣ CPU running at 16MHz ‣ 62 nanoseconds per instruction ‣ lots of time™ in between level changes… * assembler-programming required :(
  • 24. solution awesomeness awesomeness solutions programming it with arduino #hhjs sleep (based on a totally representative study with 1 participant)
  • 25. Raspbian: problem ‣ because multitasking. ‣ when the process runs depends on lots of factors we can’t control
  • 26. Raspberry Pi PWM ‣ „serializer“-mode ‣ bits in data-registers represent signal-level per PWM-cycle ‣ only a few bytes in registers ‣ needs DMA to transport data
  • 27. Raspberry Pi PWM +5V 0V 1 0 0 bits written to (frequency: 2.4 MHz) 0 t PWM cycle: 416ns PWM-Registers
  • 28. Raspberry Pi PWM +5V 0V 1 1 0 bits written to (frequency: 2.4 MHz) 1 t PWM cycle: 416ns PWM-Registers
  • 29. Raspbian: problem ‣ store prepared pixel-data in memory ‣ let the DMA-Controller transfer data to PWM-Module ‣ CPU or OS are not involved!
  • 30.
  • 31. solution awesomeness awesomeness solutions arduino #hhjs sleep program it in C on raspberry build a server in C and control with node write a node-addon.
  • 33. node-rpi-ws281x ‣ based on a C-Library by Jeremy Garff ‣ wrapper handles only type-checking and conversions between V8-types and C ‣ simple JS-interface: ws281x = { init: function(numLeds, options) { … }, /** @param ledData {Uint32Array} */ render: function(ledData) { … }, reset: function() { … } }; C-Library source:
  • 34. writing node-addons ‣ first challenge: get it to compile. ‣ hard to find documentation ‣ solution was actually pretty simple // binding.gyp (some stuff left out) { targets: [{ target_name: "rpi_ws281x", sources: ["./src/rpi-ws281x.cc"], dependencies: ["libws2811"] }, { target_name: "libws2811", type: "static_library", sources: [ … ], } ] } (required reading: http://nethack4.org/blog/building-c.html)
  • 35. writing node-addons no arguments / return undefined // rpi_ws281x.cc #include <node.h> #include <v8.h> using namespace v8; Handle<Value> Reset(const Arguments& args) { HandleScope scope; // ... binding-code here ... return scope.Close(Undefined()); } void initialize(Handle<Object> exports) { // exports.reset = function Reset() {}; exports->Set(String::NewSymbol("reset"), FunctionTemplate::New(Reset)->GetFunction()); } NODE_MODULE(rpi_ws281x, initialize)
  • 36. writing node-addons accepting arguments/ throwing exceptions // rpi_ws281x.cc Handle<Value> Init(const Arguments& args) { HandleScope scope; if(args.Length() < 1 || !args[0]->IsNumber()) { ThrowException(Exception::TypeError( String::New("init(): argument 0 is not a number"))); return scope.Close(Undefined()); } int numLEDs = args[0]->Int32Value(); // ... the actual binding-code ... return scope.Close(Undefined()); }
  • 37. writing node-addons lessons learned… ‣ writing a simple node addon is not as complicated as it seems ‣ i should write more C++ ‣ most of the code to deal with arguments and type-checking ‣ better understanding how V8 works
  • 39. hello ws281x-native the javascript-side var ws281x = require('../lib/binding/ws281x-native'); var NUM_LEDS = 100, data = new Uint32Array(NUM_LEDS); ws281x.init(NUM_LEDS); data[42] = 0xff0000; ws281x.render(data); setTimeout(function() { ws281x.reset(); }, 2000);
  • 40. hello ws281x-native of course it didn't end there ‣ API feels a bit too low-level ‣ numbers as colors are complicated ‣ need to manually manage the data-array
  • 41. matrix-API the javascript-side var ws281x = require('../lib/ws281x'), m = ws281x.createMatrix(10,10) Color = ws281x.Color; var offset = 0, c1 = new Color('red'), c2 = new Color('blue'); setInterval(function() { m.clear(); for(var i=0; i<10; i++) { var color = Color.mix(c1, c2, ((i % 10) / 10)); m.set(i%10, (i+offset)%10, color); } offset++; m.render(); }, 1000 / 30);
  • 42. solution awesomeness awesomeness solutions arduino #hhjs sleep program it in C on raspberry build a server in C and control with node ✔ write a node-addon.
  • 43. solution awesomeness awesomeness solutions #hhjs sleep program it in C on raspberry build a server in C and control with node write a node-addon. use <canvas>
  • 44. canvas-API ‣ web-technology FTW! ‣ has more stuff than we need. ‣ can be rendered server-side with node-canvas ‣ also works in the browser
  • 45. canvas-API the javascript-side var ws281x = require('../lib/ws281x'), canvas = ws281x.createCanvas(10,10), ctx = canvas.ctx, Color = ws281x.Color; var c1 = new Color('red'), c2 = new Color('blue'); function rnd(max) { return (max || 1) * Math.random(); } function rndi(max) { return Math.round(rnd(max)); } setInterval(function() { var c = Color.mix(c1,c2, rnd()); ctx.clearRect(0,0,10,10); ctx.fillStyle = 'rgb(' + c.rgb.join(',') + ')'; ctx.fillRect(rndi(10)-2, rndi(10)-2, rndi(10), rndi(10)); canvas.render(); }, 1000/5);
  • 46. more to come… ‣ open-source everything [really soon] ‣ browser-based IDE on the raspberry [in progress] ‣ integrate with other hardware [planning] ‣ improved 3d-printed casing [some day…]
  • 47. final thoughts why would you want to do that? ‣ do something you love. just for the fun. ‣ understand how stuff works. ‣ learn something new. ‣ no deadlines (well, except you do a talk about it)
  • 48. <3 thank you! come to me if you have any questions… Martin Schuhfuss – @usefulthink