Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend RGB LED with Printer Events #6240

Merged
merged 6 commits into from
Apr 11, 2017

Conversation

thinkyhead
Copy link
Member

@thinkyhead thinkyhead commented Apr 4, 2017

Rebase and squash of #5395 by @boweeble

  • RGB_LED_STRIP was not needed as a separate feature. The only difference was that the led strip "required" PWM pins, while RGB_LED would work with either PWM or 5V digital.
  • Adjustments have been made from a suggestion by @Tannoo that color transition (while waiting for heating) proportionally using the current temperature when the loop starts as the low bound.

It is still possible to add software PWM for those cases where one or more pins isn't hardware-PWM-capable, but we need a way to determine at compile-time that a pin is PWM capable (and available for use because it's not associated with a timer).

r = 255;
g = 0;
set_led_color(r, g, b);
set_led_color(255, 0, 0);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@boweeble I assume this is the correct fix?

@thinkyhead thinkyhead force-pushed the rebuild_rgb_led branch 2 times, most recently from 90a0b3c to 684384c Compare April 4, 2017 23:49
@thinkyhead thinkyhead added the Needs: Work More work is needed label Apr 5, 2017
@thinkyhead thinkyhead force-pushed the rebuild_rgb_led branch 2 times, most recently from f82ebf0 to b396486 Compare April 5, 2017 00:44
@ghost
Copy link

ghost commented Apr 5, 2017

The RGB strip does not have to be pwm pins. The TIP's don't care. Can be digital, but transitions won't happen.

@ghost
Copy link

ghost commented Apr 5, 2017

I just got through testing this.

Brings back memories of the issues I worked through on this.

@thinkyhead
Copy link
Member Author

Right, PWM is truly optional. I'll modify the notes.

@Tannoo
Copy link
Contributor

Tannoo commented Apr 7, 2017

I'm curious. Why submit a new feature that needs work when getting ready for a release?
I had this completely working with the current RCBugFix (and still do).

@thinkyhead
Copy link
Member Author

thinkyhead commented Apr 8, 2017

@Tannoo I was using this as an example while working on a video explaining how to use git for the developers of Marlin. So, then I've been trying to polish it up since. And actually, it doesn't amount to a new feature after all. It's ending up being more like just changing the notes on the existing RGB_LED feature. And, well, I guess there is PRINTER_EVENT_LEDS where it changes the color of the LED at certain points….

I don't know if this would get merged before the release. Do you think it looks innocuous enough to merge?

How's things with you? Working on anything interesting lately?

@Tannoo
Copy link
Contributor

Tannoo commented Apr 8, 2017

I would not merge this as it is incomplete and doesn't doo all of what's advertised.

I am ok. Currently trying to track down something I noticed this afternoon on my display. Not sure if it's something I did or not.
The X and Y are not reporting the accurate position anymore. Used to report X.XX.. Now, it's just X. Also, when going above 99.9, it shows 00.0. At 110.0, it shows 10.0.

I've gone back several of my backups ago and it's still the same. So, I'm not sure when that broke.

@Tannoo
Copy link
Contributor

Tannoo commented Apr 8, 2017

I will dig into it more in the am.

@thinkyhead
Copy link
Member Author

thinkyhead commented Apr 8, 2017

I would not merge this as it is incomplete and doesn't doo all of what's advertised.

I generally agree. All it now amounts to is PRINTER_EVENT_LEDS. I will update the title to reflect that change.

This PR doesn't need to worry about PWM versus non-PWM, since both will work with the right pins. Obviously we can't prevent someone hooking up an LED strip wrong (without power MOSFETs) so I think the warnings are enough in support of LED Strips. The only thing really left to do with this is to make sure the PRINTER_EVENT_LEDS color-change points make sense. Obviously not a big priority. I will definitely use this for one of my projects, though, and may add more color-changing events for that.

@thinkyhead
Copy link
Member Author

The X and Y are not reporting the accurate position anymore. Used to report X.XX.. Now, it's just X. Also, when going above 99.9, it shows 00.0. At 110.0, it shows 10.0.

I'll flash the latest RCBugFix on my machine and take a look. I assume you're using a graphical display…? I'll be testing with character.

@Tannoo
Copy link
Contributor

Tannoo commented Apr 8, 2017

Yes. Reprap full graphic.

@Tannoo
Copy link
Contributor

Tannoo commented Apr 8, 2017

I already have the led things working on mine. Printer events for homing, leveling, printing, filament change events, and done homing.

I have tested it with a single rgb led, an rgb strip, an rgbw strip, and an addressable rgb strip.

The only thing I couldn't test yet is the blinkm.

@thinkyhead
Copy link
Member Author

thinkyhead commented Apr 8, 2017

  • Patched to correct the "violet-to-red" color transition.
  • Moved cleanup/formatting commits over to the existing cleanup PR

@thinkyhead thinkyhead force-pushed the rebuild_rgb_led branch 3 times, most recently from b04afcb to b79bafe Compare April 8, 2017 23:57
@Tannoo
Copy link
Contributor

Tannoo commented Apr 11, 2017

float starting_temp_b = (thermalManager.degBed()) - 2;

I'm not sure if I need the -2 anymore.

@thinkyhead
Copy link
Member Author

thinkyhead commented Apr 11, 2017

This sets the initial starting color that otherwise might not be set.

The initial color setting in this PR is guaranteed by the initialized values of old_red and old_blue being highly unlikely to match the first calculated values of red and blue.

@Tannoo
Copy link
Contributor

Tannoo commented Apr 11, 2017

You know, I may have just run into pure luck that it's been working for me. lol

I will change it to something that is guaranteed.

@thinkyhead
Copy link
Member Author

thinkyhead commented Apr 11, 2017

You know, I may have just run into pure luck that it's been working for me.

That was my working assumption. 😉

@Tannoo
Copy link
Contributor

Tannoo commented Apr 11, 2017

I will have to test what you have done here without my minglings.

The initial color setting in this PR is guaranteed by the initialized values of old_red and old_blue being highly unlikely to match the first calculated values of red and blue.

I have been trying to implement this into mine, and all I get is a slower flashing of red and purple.
I've changed (r == starting_temp_b) to (r == 0) and it doesn't turn on for quite a while. It doesn't make sense.

@@ -6195,11 +6251,16 @@ inline void gcode_M109() {

target_extruder = active_extruder; // for print_heaterstates

#if ENABLED(PRINTER_EVENT_LEDS)
const float start_temp = thermalManager.degTargetBed();
uint8_t old_red = 255;
Copy link
Contributor

@Tannoo Tannoo Apr 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const float start_temp = thermalManager.degBed();
const uint8_t old_red = 0;

Copy link
Member Author

@thinkyhead thinkyhead Apr 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right… thermalManager.degBed is better!

Since old_red is altered below, it can't be const. And it should be 255 to start, because this ensures the color will be set on the first iteration (because the initial red will be closer to 0).

bool wants_to_cool = false;
wait_for_heatup = true;
millis_t now, next_temp_ms = 0, next_cool_check_ms = 0;

KEEPALIVE_STATE(NOT_BUSY);

#if ENABLED(PRINTER_EVENT_LEDS)
const float start_temp = thermalManager.degTargetHotend(target_extruder);
uint8_t old_blue = 0;
Copy link
Contributor

@Tannoo Tannoo Apr 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const float start_temp = thermalManager.degHotend(target_extruder);
const uint8_t old_blue = 255

Copy link
Member Author

@thinkyhead thinkyhead Apr 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right… thermalManager.degHotend is better!

Since old_blue is altered below, it can't be const. And it should be 0 to start, because this ensures the color will be set on the first iteration (because the initial blue will be closer to 255).

@Tannoo
Copy link
Contributor

Tannoo commented Apr 11, 2017

I got a copy of your repo and got this to compile, then got the transitions working.
I cannot create a PR against your branch, it gives me "validation error". So... maybe merge this and I'll put it against RCBugFix.

@Tannoo
Copy link
Contributor

Tannoo commented Apr 11, 2017

Simple changes and not that many to make it work. I'll just comment them in the code.

const uint8_t blue = map(constrain(temp, start_temp, target_temp), start_temp, target_temp, 255, 0);
if (blue != old_blue) set_led_color(255, 0, (old_blue = blue));
}
#endif

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    #if ENABLED(PRINTER_EVENT_LEDS)
      // Gradually change LED strip from violet to red as nozzle heats up
      if (wait_for_heatup && !wants_to_cool) {
        const uint8_t blue = map(constrain(temp, start_temp, target_temp), start_temp, target_temp, 255, 0);
        if (blue == old_blue) set_led_color(255, 0, 255);   //Purple to start
        if (blue != old_blue) set_led_color(255, 0, blue);  //Start transitioning to Red
      }
    #endif

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd generally reduce those two lines down to one:

set_led_color(255, 0, blue == old_blue ? 255 : blue);

…but they are incorrect. First of all, old_blue is not being set to blue when it differs. Secondly, with that correction made, blue == old_blue will evaluate true most of the time.

if (red != old_red) set_led_color((old_red = red), 0, 255);
}
}
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

      #if ENABLED(PRINTER_EVENT_LEDS)
        // Gradually change LED strip from blue to violet as bed heats up
        if (wait_for_heatup && !wants_to_cool) {
          const uint8_t red = map(constrain(temp, start_temp, target_temp), start_temp, target_temp, 0, 255);
          if (red == old_red) set_led_color(0, 0, 255);     //Blue to start
          if (red != old_red) set_led_color(red, 0, 255);   //Start transitioning to Purple
        }
      #endif

@Tannoo
Copy link
Contributor

Tannoo commented Apr 11, 2017

It works fine with those changes.

@thinkyhead
Copy link
Member Author

I cannot create a PR against your branch

Trying from the web interface or from Github Desktop? It should be easier from the web interface. If you open your fork's page, the link to create a PR will be at the top.

@thinkyhead
Copy link
Member Author

thinkyhead commented Apr 11, 2017

It works fine with those changes.

Only by kismet. The code you pasted will simply always pass the test red != old_red once red changes, so it is merely setting the color on every loop once the value of red changes. This is because you are not updating old_red. The purpose of the old_red and old_blue values is to track when red or blue has changed, so the LED will only be updated as necessary. By initializing old_red and old_blue to the final values of red and blue, we ensure the first test passes and the color is always set on the first iteration. This is a common programming pattern.

Yep, I do dream in code. I started programming in 1978 in Sinclair BASIC. By 1984 I was programming 6502 Assembler. In 1988 I was programming 680x0 Assembler, writing Amiga games. But I must admit, I didn't start programming in C++ until around 1997. Of course, now I eat new languages for breakfast. 😜

@thinkyhead
Copy link
Member Author

thinkyhead commented Apr 11, 2017

I presume that for an RGBW LED we just need to add one more pin and treat all gray values as varying intensities of white. For example, if the color given is 128, 128, 128 the color output should be R=0 G=0 B=0 W=128. Unless we want to mix other colors with white….

@thinkyhead
Copy link
Member Author

thinkyhead commented Apr 11, 2017

  • Patched to support RGBW LEDs.

@thinkyhead thinkyhead merged commit 6d5400d into MarlinFirmware:RCBugFix Apr 11, 2017
@thinkyhead thinkyhead deleted the rebuild_rgb_led branch April 11, 2017 20:17
@Tannoo
Copy link
Contributor

Tannoo commented Apr 12, 2017

I presume that for an RGBW LED we just need to add one more pin and treat all gray values as white. For example, if the color given is 128, 128, 128 the color output should be R=0 G=0 B=0 W=128.

That's the idea. set_led_color(r, g, b, w).

@Tannoo
Copy link
Contributor

Tannoo commented Apr 12, 2017

It's back to flickering again....WTH?

I think it's due to your code is too efficient and it's executing too fast.

@nesalkiN
Copy link

So, no ws2812b support as of now? :'(

@Tannoo
Copy link
Contributor

Tannoo commented Apr 13, 2017

Not officially, yet.

Although, my branch has it currently working.

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

Successfully merging this pull request may close these issues.

3 participants