Thorsten Kimmeskamp
Published © GPL3+

Calliope mini - Kaminfeuer

Simuliere ein Kaminfeuer auf dem Display deines Calliope mini!

AdvancedFull instructions provided1 hour200

Things used in this project

Hardware components

Calliope mini
Calliope mini
×1
Mülltüte (durchsichtig)
×1

Story

Read more

Code

kaminfeuer

JavaScript
// Kaminfeuer fuer den Calliope mini, basierend auf: https://github.com/bbcmicrobit/micropython/blob/master/examples/flame_simulation.py

let minBrightness = 2
let maxBrightness = 8

let displayWidth = 5
let displayHeight = 5

let mask = [
[ 10, 33, 66, 33, 10 ],
[ 33, 75, 88, 75, 33 ],
[ 50, 88, 90, 88, 50 ],
[ 60, 95, 100, 95, 60 ],
[ 88, 100, 100, 100, 88 ]]
	
let grid = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]

let line = [0, 0, 0, 0, 0]
	
let percent = 0
let percentIncrement = 25
let sleeptime = 15

function generateLine() {
    for (let x = 0; x < displayWidth; x++) {
       line[x] = randint(minBrightness, maxBrightness)
    }
}

function shiftUp() {
    for (let y = 0; y < displayHeight - 1 ; y++) {
        for (let x = 0; x < displayWidth; x++) {
            grid[y][x] = grid[y + 1][x]
        }
    }
    for (let x = 0; x < displayWidth; x++) {
        grid[displayHeight - 1][x] = line[x]
    }
}
	
function interpolateFrame() {
    for (let y = 0; y < displayHeight - 1; y++) {
        for (let x = 0; x < displayWidth; x++) {
            let newval = mask[y][x] * ((100-percent)*grid[y][x] + percent*grid[y+1][x]) / 10000.0
            led.plotBrightness(x, y, (newval > 1 ? Math.pow(2, newval) : 0))
        }
    }
    for (let x = 0; x < displayWidth; x++) {
        let newval = mask[displayHeight-1][x] * ((100-percent)*grid[displayHeight-1][x] + percent*line[x]) / 10000.0
        led.plotBrightness(x, displayHeight - 1, (newval > 1 ? Math.pow(2, newval) : 0))
    }
}

	
generateLine()

basic.forever(function () {
    while (percent <= 100) {
        interpolateFrame()
        basic.pause(sleeptime)
        percent += percentIncrement
    }
    percent = 0
    shiftUp()
    generateLine()
})

Credits

Thorsten Kimmeskamp
19 projects • 13 followers

Comments