Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active December 10, 2016 07:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/621ce40c56f02b8303d5fb66424fbd6c to your computer and use it in GitHub Desktop.
Save creationix/621ce40c56f02b8303d5fb66424fbd6c to your computer and use it in GitHub Desktop.
Sample bright-lang program with JS-style syntax. Easier on my son who recently learned JS.
length = 60 // Our strip has 60 LEDs
bpp = 3 // We are using normal ws2812b lights that expect 3 bytes of color.
delay = 33 // Delay between animation loops in ms (defaults to 100)
sprites = 4 // Total number of sprites to spawn at startup
fizz = 3 // How many sprites to update per animation loop (defaults to all)
// This is called once per animation loop before updating sprites.
loop {
fade(20)
}
// This is called for each sprite when it's spawned.
// Variables created here are local to the sprite.
spawn {
p:length = rand // Random starting position
h:768 = rand // Random starting hue
l = rand(50) + 50 // Semi-random lifetime
}
// This is called once per sprite update
// you can access global variables as well as sprite local variables.
update {
p += 1 // Move 1 pixel forward
h += 7 // Slightly change hue
hue(p, h, 100) // Draw new position
l -= 1 // Decrement lifetime.
if l <= 0 {
explode // When lifetime reaches zero, explode!
}
}
// draw gradient that color shifts while fading away.
explode {
b = 255
loop i:14 {
b -= 18
hue(p + i, h, b)
hue(p - i, h, b)
}
respawn // And then restart the sprite.
}
@creationix
Copy link
Author

creationix commented Dec 9, 2016

Function scope is very different than JS and much simpler.

  • Functions are globally scoped and don't take parameters. They are simply subroutines.
  • Some functions have sprite scope (local variables to a particular sprite instance)
  • Other functions have global scope (can only access global variables)
  • There is no function scope. All functions with sprite scope share the same set of sprite variables.
  • A function cannot be run in both scopes. Static analysis in the compiler will enforce this and infer the scope of each function.
  • There is no block scope. Loop variables are global or sprite local (depending on calling scope)

Variables are all integers.

  • Normally they are signed 32 bit integers.
  • You can declare them with a limit at which point they will be unsigned integers constrained to a modulus.

@joliveros
Copy link

What kind of gear do I need to run this?

Thanks for sharing. This is so nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment