• ...LED with changing as mentioned in Post #3: Some Fun... bangle.js 'project' - click clip (link) at the bottom.

    Since with the constructed Software LED - as outlined in the conversation about Bangle.js (and emulator), where did you leave my LED, LED1, LEDX? - you can set any color, you can make it even a neopixel like thing that changes color over time... see Reusing some RGB LED interior lighting modules.

    I changed the code only slightly: instead of using pins and writing pwm (analog), I store the rgb values in pr, pg, pb variables. Then, in intervals, I set the color of the led and set the led itself... which updates the color of the led... ;-)

    Caution: 20 times per second I change the color and fill a circle of 80 pixels in diameter. I do not know if bangle can keep up with the redraw... after all, it does all the graphics and talks byte by byte to the display... Therefore: all your banglejs-ers out there, give the code a shot and report back...

    Interesting detail: seven (7) independent intervals are going on... try this with Arduino: would become quite a time_and_if_mess... Espruino's event drivenness is the right solution here. (I did though not adjust / update the pwm and delta pwm factors.... in other words: colors changes and speed of changes happen way more often that the LED is updated... To save some cpu cycles, the code could be inverted and new color(s) only calculated once per LED update - just before LED update...).

    // someBangleTriColors.js
    // (c)20191129 allObjects
    // using bangle to do some light show with a software LED / 'Neopixel'
    // sine modulated sine modulated r g b value changes // before pwm...
    var itpf =   2; // interval time pwm factor
    var itdf =   5; // interval time pwm delta factor
    var pi2  = Math.PI*2, rdd = pi2 / 90;
    var pr   =   0, pg   =   0, pb   =   0; // rgb // befgore pin for r, g and b
    var pwmr =   0, pwmg =   0, pwmb =   0; // pwm value of r, g and b
    var pwdr = rdd, pwdg = rdd, pwdb = rdd; // pwm delta (increment)
    var itpr =   7, itpg =  11, itpb =  17; // pwm interval time of r, g and b
    var iipr      , iipg      , iipb      ; // pwm interval id of r, g and b
    var pddr =   0, pddg =   0, pddb =   0; // pwm delta delta
    var itdr =  30, itdg =  37, itdb =  47; // pwm delta delta interval time
    var iidr      , iidg      , iidb      ; // pwm delta delta interval id of r, g, b 
    
    var led, ledIId, ledIT=50; // 20 times per sec 're'-light LED
    // --- LED as constructor to create any number, colored, sized LEDs
    let LED = function(x,y,s,c,f) { // constructor
      this.isOn = false;  // LED on / off
      this.x = x || 0; // left of LED bounding box
      this.y = y || 0; // top of LED bounding box
      this.s = s || 10; // size (diameter) };
      this.c = c || [1,0,0]; //  color as RGB array with values 0..1
      this.f = f || function(_) {
          g.fillCircle(_.x+_.s/2,_.y+_.s/2,_.s/2); }; }; // fill shape
    LED.prototype.set = function(v) { // set method
      v = ((this.isOn = (v === undefined || !!v))
          ?g.setColor(this.c[0],this.c[1],this.c[2]):g.setColor(0,0,0));
      this.f(this); }; // execute the function that fills the (default) shape
    LED.prototype.reset = function() { this.set(false); }; // reset method
    LED.prototype.toggle = function() { this.set( ! this.isOn); };
    
    function adpr() {
      pwmr = ((pwmr += pwdr) < pi2) ? pwmr : 0;
      pr = (1 + Math.sin(pwmr)) / 2;
    }
    function adpg() {
      pwmg = ((pwmg += pwdg) < pi2) ? pwmg : 0;
      pg = (1 + Math.sin(pwmg)) / 2;
    }
    function adpb() {
      pwmb = ((pwmb += pwdb) < pi2) ? pwmb : 0;
      pb =(1 + Math.sin(pwmb)) / 2;
    }
    function addr() {
      pddr = ((pddr += rdd) < pi2) ? pddr : 0;
      pwdr = rdd * ((1 + Math.sin(pddr)) / 2);
    }
    function addg() {
      pddg = ((pddg += rdd) < pi2) ? pddg : 0;
      pwdg = rdd * ((1 + Math.sin(pddg)) / 2);
    }
    function addb() {
      pddb = ((pddb += rdd) < pi2) ? pddb : 0;
      pwdb = rdd * ((1 + Math.sin(pddb)) / 2);
    }
    function goPWMs() {
      iipr = setInterval(adpr,itpr * itpf);
      iipg = setInterval(adpg,itpg * itpf);
      iipb = setInterval(adpb,itpb * itpf);  
    }
    function goDeltas() {
      iidr = setInterval(addr,itdr * itdf);
      iidg = setInterval(addg,itdg * itdf);
      iidb = setInterval(addb,itdb * itdf);  
    }
    
    function goLED() {
       var led = new LED(30,20,80,[0,0,0]);
       ledIId = setInterval(function(){
           led.c=[pr,pg,pb];
           led.set();
         },ledIT);
    }
    
    function onInit() {
       goPWMs();
       goDeltas();
       goLED();
    }
    setTimeout(onInit,999); // while dev'g; remove before upload for save()
    

    2 Attachments

About

Avatar for allObjects @allObjects started