Skip to content

Commit e1c2961

Browse files
author
Your Name
committed
led-control
1 parent 31f79d3 commit e1c2961

File tree

12 files changed

+1548
-0
lines changed

12 files changed

+1548
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
package-lock.json

led-control/.gitignore

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (http://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+

led-control/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# led-control
2+
App controlling led strips on RPi
3+
4+
- Production: `leds.local`
5+
- Development: `localhost:3000`
6+
7+
## Install
8+
```
9+
git clone [email protected]:keymetrics/led-control.git && cd led-control && yarn
10+
```
11+
12+
## Deploy
13+
```
14+
pm2 deploy production
15+
```
16+
17+
## Example
18+
```
19+
curl -XPOST http://leds.local/color/0000FF
20+
```
21+
22+
## Color
23+
```http
24+
POST /color/:color
25+
```
26+
27+
## Tick
28+
```http
29+
POST /tick/:color
30+
```
31+
32+
## Led
33+
```http
34+
POST /led/:number/:color
35+
```
36+
37+
## Mods
38+
### Follow
39+
```http
40+
POST /follow
41+
```
42+
43+
### Sequence
44+
```http
45+
POST /sequence
46+
```

led-control/classes/Mode.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Parent class which will be inherited by the different modes
3+
*/
4+
module.exports = class Mode {
5+
constructor() {
6+
this.timeouts = []
7+
this.intervals = []
8+
}
9+
10+
clearAll() {
11+
// clear every timeout + interval here
12+
console.log('Clearing all timeouts + intervals')
13+
}
14+
15+
}

led-control/classes/YoutubeMode.js

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const Mode = require('./Mode')
2+
const fs = require('fs')
3+
const youtubedl = require('youtube-dl')
4+
const ffmpeg = require('fluent-ffmpeg')
5+
const { exec } = require('child_process')
6+
const config = require('../config/config.js')
7+
const leds = require('rpi-ws281x-native')
8+
const chalk = require('chalk')
9+
10+
function rgbToInt(r, g, b) {
11+
return r << 16 | g << 8 | b
12+
}
13+
14+
module.exports = class YoutubeMode extends Mode {
15+
constructor({ url }) {
16+
super()
17+
console.log('Initializing YoutubeMode')
18+
19+
console.log('Loading url', url)
20+
let video = youtubedl(url, ['--format=18'], { cwd: __dirname })
21+
video.on('info', function (info) {
22+
console.log('Download started')
23+
console.log('filename: ' + info.filename)
24+
console.log('size: ' + info.size)
25+
})
26+
video.pipe(fs.createWriteStream('myvideo.mp4'))
27+
video.on('end', function () {
28+
console.log('finished downloading!')
29+
let ff = ffmpeg(__dirname + '/../myvideo.mp4').noAudio().outputFormat('rawvideo')
30+
console.log('file loaded')
31+
console.log('Led Count = ' + config.ledCount)
32+
const ffmpegCommand = 'yes | ffmpeg -i myvideo.mp4 -filter:v "scale=1650:-1, crop=w=in_w:h=1:x=0:y=in_h/2:exact=1" -vcodec rawvideo -pix_fmt rgb24 out.rgb'
33+
exec(ffmpegCommand, (error, stdout, stderr) => {
34+
if (error) {
35+
console.error(`exec error: ${error}`);
36+
return;
37+
}
38+
console.log(`stdout: ${stdout}`);
39+
console.log(`stderr: ${stderr}`);
40+
console.log('ffmpeg command finished')
41+
// let file = fs.readFileSync(__dirname + '/../out.rgb')
42+
// let fileArray = new Uint32Array(file)
43+
// console.log(fileArray.length + ' <------- length')
44+
let frameNb = 0
45+
setInterval(() => {
46+
let readStream = fs.createReadStream(__dirname + '/../out.rgb', {start: frameNb * 1650, end: (3 + frameNb) * 1650 - 1})
47+
let data = ''
48+
let testChunk
49+
readStream.on('data', function(chunk) {
50+
console.log('chunk loaded')
51+
testChunk = new Uint32Array(chunk)
52+
console.log(testChunk.length)
53+
54+
// console.log(testChunk)
55+
// data += chunk
56+
}).on('end', function() {
57+
console.log('stream read ended')
58+
let frame = 0;
59+
let newArray = new Uint32Array(1650).map((val, index, array) => {
60+
let r = testChunk[index * 3]
61+
let g = testChunk[index * 3 + 1]
62+
let b = testChunk[index * 3 + 2]
63+
// console.log(chalk.rgb(r, g, b)(index))
64+
// if( r === 189 && g === 189 && b === 189) console.log(index)
65+
if (index === 1649) console.log(chalk.rgb(r, g, b)(index))
66+
if (index === 1650) console.log(`${r} ${g} ${b}`)
67+
if (index === 1651) console.log(`${r} ${g} ${b}`)
68+
// if (index === 1651) console.log(chalk.rgb(r, g, b)(index))
69+
// console.log(`${r} ${g} ${b}`)
70+
return rgbToInt(r, g, b)
71+
})
72+
console.log('Rendering Frame ' + frameNb)
73+
74+
leds.render(newArray)
75+
frameNb = frameNb +1
76+
// let p = newArray[newArray.length - 2]
77+
// console.log(chalk.rgb(p.r, p.g, p.b)('youpi'))
78+
// console.log('maimmamidaimi')
79+
})
80+
}, 40)
81+
})
82+
// ffmpeg -i myVideo.mp4 -vcodec rawvideo -filter:v "crop=in_w/:1:0:0" -pix_fmt yuv420p out.rgb
83+
// ffmpeg -i myVideo.mp4 filter:v "crop=in_w/:1:0:0" out.mp4
84+
// ffmpeg -i myVideo.mp4 -filter:v "crop=in_w:1:0:0" out.mp4
85+
// ffmpeg -i myVideo.mp4 -filter:v "crop=w=in_w:h=1:x=0:y=0:exact=1" -vcodec rawvideo out.rgb
86+
// ffmpeg -i myVideo.mp4 -filter:v "crop=w=in_w:h=1:x=0:y=0:exact=1" -vcodec rawvideo -pix_fmt rgb24 out.rgb
87+
// ffmpeg -i myVideo.mp4 -filter:v "crop=w=in_w:h=1:x=0:y=0:exact=1" -vcodec rawvideo -pix_fmt rgb32 out.rgb
88+
// ffmpeg -i myvideo.mp4 -filter:v scale=1650:-1 -filter:v "crop=w=in_w:h=1:x=0:y=0:exact=1" -vcodec rawvideo -pix_fmt rgb32 out.rgb
89+
// fmpeg -i myvideo.mp4 -filter:v "scale=1650:-1" -filter:v "crop=w=in_w:h=2:x=0:y=0:exact=1" -vcodec rawvideo -pix_fmt rgb24 out.rgb
90+
// yes | fmpeg -i myvideo.mp4 -filter:v "scale=1650:-1" -filter:v "crop=w=in_w:h=1:x=0:y=0:exact=1" -vcodec rawvideo -pix_fmt rgb24 out.rgb
91+
// .complexFilter([
92+
// // Duplicate video stream 3 times into streams a, b, and c
93+
// { filter: 'split', options: '3', outputs: ['a', 'b', 'c'] },
94+
95+
// // Create stream 'red' by cancelling green and blue channels from stream 'a'
96+
// { filter: 'lutrgb', options: { g: 0, b: 0 }, inputs: 'a', outputs: 'red' },
97+
98+
// // Create stream 'green' by cancelling red and blue channels from stream 'b'
99+
// { filter: 'lutrgb', options: { r: 0, b: 0 }, inputs: 'b', outputs: 'green' },
100+
101+
// // Create stream 'blue' by cancelling red and green channels from stream 'c'
102+
// { filter: 'lutrgb', options: { r: 0, g: 0 }, inputs: 'c', outputs: 'blue' },
103+
104+
// // Pad stream 'red' to 3x width, keeping the video on the left, and name output 'padded'
105+
// { filter: 'pad', options: { w: 'iw3', h: 'ih' }, inputs: 'red', outputs: 'padded' },
106+
107+
// // Overlay 'green' onto 'padded', moving it to the center, and name output 'redgreen'
108+
// { filter: 'overlay', options: { x: 'w', y: 0 }, inputs: ['padded', 'green'], outputs: 'redgreen' },
109+
110+
// // Overlay 'blue' onto 'redgreen', moving it to the right
111+
// { filter: 'overlay', options: { x: '2w', y: 0 }, inputs: ['redgreen', 'blue'] },
112+
// ])
113+
})
114+
}
115+
}

led-control/config/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
ledCount: 1650
3+
}

led-control/ecosystem.config.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = {
2+
apps: [{
3+
name: 'led-control',
4+
script: 'index.js',
5+
env: {
6+
NODE_ENV: 'development'
7+
},
8+
env_production: {
9+
NODE_ENV: 'production'
10+
}
11+
}],
12+
deploy: {
13+
production: {
14+
user: 'root',
15+
host: '192.168.1.11',
16+
ref: 'origin/master',
17+
repo: '[email protected]:keymetrics/led-control.git',
18+
path: '/home/pi/led-control/',
19+
'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env production'
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)