Skip to content

Conversation

@markterrill
Copy link

Been working on a statuslight library to emulate how Particle do their status light with a glowing/fading light when the device is happy.

The pull request contains the code required to get it to smoothly work using the hardware functions on ESP32.

It has a goodie bag of mgos_pwm_fade_direction flags.

One key thing with ESP32 is knowing what pwm channels a pin was assigned. And for some reason I seemed to only be able to listen to the events in user code land. Anyways, have added MGOS_PWM_CHANNEL as an event.

Some user code for initialisation

if (mgos_pwm_rgb_led_init(&led, pinRed, pinGreen, pinBlue, frequency, false)) {
        LOG(LL_DEBUG, ("StatusLight::Initialise successfully init, turning on"));

        mgos_event_add_handler(MGOS_PWM_CHANNEL, mgos_pwm_rgb_channel_update_cb, &led); /* Used on esp32 to map gpio's to their ledc channel */

        LOG(LL_DEBUG, ("StatusLight::Initialise Turning status LED on"));
        mgos_pwm_rgb_led_set(&led, 255, 255, 255, 255);        
        vTaskDelay(50 / portTICK_PERIOD_MS); // lets wait for the channel events to fire
    } else {
        LOG(LL_DEBUG, ("StatusLight::Initialise FAILED init"));
    }

Some examples on how to change the LED:

// Determine pattern
    switch (singlelight.pattern){       
        case LIGHT_FADE:
            LOG(LL_DEBUG, ("StatusLight::apply fade"));
            mgos_pwm_rgb_fade_start(&led, 3000, FADE_LOOP, true, 250, 1);
            break;
        case LIGHT_FADESLOW:
            LOG(LL_DEBUG, ("StatusLight::apply fade slow"));
            mgos_pwm_rgb_fade_start(&led, 5000, FADE_LOOP, true, 250, 1);
            break;
        case LIGHT_BLINK:
            LOG(LL_DEBUG, ("StatusLight::apply blink"));
            mgos_pwm_rgb_blink_start(&led, 500);
            break;
        case LIGHT_BLINKRAPID:
            LOG(LL_DEBUG, ("StatusLight::apply rapid blink"));
            mgos_pwm_rgb_blink_start(&led, 200);
            break;
        case LIGHT_BLINKSLOW:
            LOG(LL_DEBUG, ("StatusLight::apply slow blink"));           
            mgos_pwm_rgb_blink_start(&led, 1300);
            break;
        case LIGHT_ON:
            LOG(LL_DEBUG, ("StatusLight::apply ON, stopping effects"));
            mgos_pwm_rgb_fade_stop(&led);
            mgos_pwm_rgb_blink_stop(&led);
            break;
        case LIGHT_OFF:
            LOG(LL_DEBUG, ("StatusLight::apply OFF, stopping effects"));
            mgos_pwm_rgb_fade_stop(&led);
            mgos_pwm_rgb_blink_stop(&led);
            break;
        default:
            LOG(LL_DEBUG, ("StatusLight::apply no pattern"));
    }

Let me know any questions, and if you are interested in the concept of a statuslight library for MOS.

@rojer
Copy link
Contributor

rojer commented Jun 25, 2020

hi Mark

thanks for contributing. a number of comments:

  1. for blinking, can we specify on and off time separately? to make it similar to mgos_gpio_blink.
  2. please format code with to conform to existing style. you can use clang-format (i just pushed .clang-format config for it).

@nliviu
Copy link

nliviu commented Jul 5, 2020

This PR breaks ESP8266 builds, unless some platform conditional compilation is implemented.

@markterrill
Copy link
Author

Hi guys, I've been fixing some watchdog issues.

...incidentally, what do you do for local development so you don't need to update a github repo and then reference that repo in your mos.yml? That's why there are the 'another brick' commits..
I know there is a flag for a local dir for mongoose core, but couldn't find any way of referencing a local directory for a library.

I've added on/off delays per Rojer's request.

I'll give some thought to ESP32 implementation. I can split mgos_pwm_rgb_blink_* in the respective implementation files. I don't have a 8266 to test on, but will give it a go.

@markterrill
Copy link
Author

Ignore my PWM github repo question, that was pretty stupid. I thought the libs/xxx folders were getting overwritten, but I can simply edit there as it won't be overwritten (unless I change the library location..)

@markterrill
Copy link
Author

Interesting date changes. Tried squashing commits. Key thing is this library has some worthwhile new features for hardware led fading, led blinking via exec process, common anode/cathode flag, etc.

@markterrill
Copy link
Author

Also latest commit fixes the IDF change to the ledc_fade_func_uninstall() function, they removed the argument.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants