Giter Club home page Giter Club logo

Comments (14)

samelhusseini avatar samelhusseini commented on June 3, 2024

I've implemented these animations in Typescript.
@xobs, @jieq
We have two options here, we can keep the animations as blocks that they need to drag into forever loops. These animations are blocking and need their own thread, luckily we can add as many forever loops as we want (thread limit dependent).

Here's what the code looks like, anything inside a function can be wrapped into a block.

Something that would make this a little more flexible is to add a duration for which the user wants to run the animation, say 500ms. The animation would run for that long and then continue with the code execution. This would mean they don't necessarily need to create a forever loop (thread) for each if they want to program something sequentially.

loops.forever(function () {
    heartbeat(AnalogPin.A0, 1);
})

loops.forever(function () {
    heartbeat(AnalogPin.A1);
})

loops.forever(function () {
    twinkle(AnalogPin.A3);
})

loops.forever(function () {
    twinkle(AnalogPin.A4, 2);
})

loops.forever(function () {
    blink(AnalogPin.A5, 4);
})

function heartbeat(pin: AnalogPin, tempo = 12) {
    let current = 0;
    if (tempo > 12)
        tempo = 12;

    current = fade_to(current, 0xc0, 2, pin, 1);
    current = fade_to(current, 0x4, 2, pin, 1);
    loops.pause(80); // fastest rate
    loops.pause((13 - tempo) * 15);
    //delay(180);
    current = fade_to(current, 0xff, 2, pin, 1);
    current = fade_to(current, 0x00, 2, pin, 1);
    loops.pause(214); // fastest rate
    loops.pause((13 - tempo) * 37);
    //delay(420);
}

function twinkle(pin: AnalogPin, tempo = 5) {
    let current = 128;
    while (1) {
        current = fade_to(current, Math.randomRange(0, 255), tempo, pin, 3);
    }
}

function blink(pin: AnalogPin, tempo = 4) {
    fade_to(0, 255, tempo, pin, 7);
    fade_to(255, 0, tempo, pin, 7);
}

function fade_to(current: number, target: number, rate: number, pin: AnalogPin, pause: number) {
    while (Math.abs(current - target) > rate) {
        pins.analogWrite(pin, current);
        current = current + ((target - current) > 0 ? rate : - rate);
        loops.pause(pause);
    }
    return current;
}

from pxt-chibitronics.

xobs avatar xobs commented on June 3, 2024

This looks good.

We use threads all the time, and in fact the preloaded program spawns a thread for each LED, so such an approach is just fine.

We may want to reduce the amount of per-thread overhead (currently 400 bytes I believe), but that's a thing we can look into later.

from pxt-chibitronics.

samelhusseini avatar samelhusseini commented on June 3, 2024

So, wrap the functions in blocks and keep it up the user to place them in forever loops?
or do we want to do the threading ourselves?

from pxt-chibitronics.

xobs avatar xobs commented on June 3, 2024

That would be my approach, unless @jieq has any other ideas.

from pxt-chibitronics.

samelhusseini avatar samelhusseini commented on June 3, 2024

@jieq what are your thoughts on this design? Also would you want it under a separate category or under Lights? One block with dropdown or multiple blocks?
and besides Blinky, Heartbeat and Twinkle. Any other animations come to mind?

from pxt-chibitronics.

pelikhan avatar pelikhan commented on June 3, 2024

+1 for 1 block with dropdown (maybe with animated gif grid :)). A few core transition animation will get you a long way.

from pxt-chibitronics.

samelhusseini avatar samelhusseini commented on June 3, 2024

Also, should we make them a package to reduce standard payload? or make them part of core?

from pxt-chibitronics.

pelikhan avatar pelikhan commented on June 3, 2024

from pxt-chibitronics.

samelhusseini avatar samelhusseini commented on June 3, 2024

@jieq what do you think of this block design:

screen shot 2017-10-06 at 12 18 06 am

from pxt-chibitronics.

pelikhan avatar pelikhan commented on June 3, 2024

from pxt-chibitronics.

jieq avatar jieq commented on June 3, 2024

Hey! I just tried out the effects too and they look so good! I love the dropdown and the illustrations look great :)

I noticed that I couldn't pause or stop the twinkle pattern, whereas I was able to put the heartbeat and blink into a sequence (there seems to be a clear "stop" to the animation cycle).

For example in this code, we don't ever make it out of the twinkle effect:
screen shot 2017-10-06 at 11 35 08 am

Is there some way to make all of the effects have a finite cycle with the same cycle duration (e.g. 1 second)? Also, I noticed that when the effects end, they complete on a dim but not all the way off. Is it possible to make it turn off at the end of the cycle? As a workaround, I just put in an "off" block for now!

from pxt-chibitronics.

samelhusseini avatar samelhusseini commented on June 3, 2024

Yes, I noticed that this morning. the Twinkle animation was in a while loop, and so I made them all have a finite animation. (Not time based, but we could calibrate them so that they all go for the same amount of time). see: c884a72

and yes it is possible to make it turn off, however what if the animation doesn't have an OFF as part of it. like the twinkle (which is all random) that might look weird if you place it in a forever loop. It will have an OFF every x seconds.

from pxt-chibitronics.

samelhusseini avatar samelhusseini commented on June 3, 2024

(As for the dim issue at the end, looking into that now)

from pxt-chibitronics.

jieq avatar jieq commented on June 3, 2024

For turning off in the end: in this case, I think it's fine that we ask people to use a custom "turn off block" so that we don't have blips in the regular animation. Thanks!

from pxt-chibitronics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.