From 8fbb833de9309c53abb73dba7d33b76f0068c61a Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Fri, 8 Sep 2017 14:47:47 -0500 Subject: [PATCH] Encapsulate RGB(W) LEDs --- Marlin/src/Makefile | 2 +- Marlin/src/Marlin.cpp | 104 +------- Marlin/src/feature/Max7219_Debug_LEDs.cpp | 237 ++++++++++++++++++ .../feature/{leds => }/Max7219_Debug_LEDs.h | 58 ++--- .../src/feature/leds/Max7219_Debug_LEDs.cpp | 237 ------------------ Marlin/src/feature/leds/blinkm.cpp | 10 +- Marlin/src/feature/leds/blinkm.h | 9 +- Marlin/src/feature/leds/leds.cpp | 73 ++++++ Marlin/src/feature/leds/leds.h | 53 ++++ Marlin/src/feature/leds/neopixel.cpp | 72 ++++++ Marlin/src/feature/leds/neopixel.h | 38 +++ Marlin/src/feature/leds/pca9632.cpp | 3 +- Marlin/src/feature/leds/pca9632.h | 4 +- Marlin/src/gcode/queue.cpp | 4 + Marlin/src/gcode/temperature/M109.cpp | 4 + 15 files changed, 529 insertions(+), 379 deletions(-) create mode 100644 Marlin/src/feature/Max7219_Debug_LEDs.cpp rename Marlin/src/feature/{leds => }/Max7219_Debug_LEDs.h (64%) delete mode 100644 Marlin/src/feature/leds/Max7219_Debug_LEDs.cpp create mode 100644 Marlin/src/feature/leds/leds.cpp create mode 100644 Marlin/src/feature/leds/leds.h create mode 100644 Marlin/src/feature/leds/neopixel.cpp create mode 100644 Marlin/src/feature/leds/neopixel.h diff --git a/Marlin/src/Makefile b/Marlin/src/Makefile index cb63852fa..c45ab5bab 100644 --- a/Marlin/src/Makefile +++ b/Marlin/src/Makefile @@ -310,7 +310,7 @@ CXXSRC = WMath.cpp WString.cpp Print.cpp Marlin.cpp \ module/temperature.cpp sd/cardreader.cpp module/configuration_store.cpp \ HAL/watchdog.cpp SPI.cpp HAL/servo.cpp Tone.cpp lcd/ultralcd.cpp libs/digipot_mcp4451.cpp \ feature/dac/dac_mcp4728.cpp libs/vector_3.cpp libs/least_squares_fit.cpp module/endstops.cpp libs/stopwatch.cpp core/utility.cpp \ - module/printcounter.cpp libs/nozzle.cpp core/serial.cpp gcode/parser.cpp libs/Max7219_Debug_LEDs.cpp + module/printcounter.cpp libs/nozzle.cpp core/serial.cpp gcode/parser.cpp feature/Max7219_Debug_LEDs.cpp ifeq ($(NEOPIXEL), 1) CXXSRC += Adafruit_NeoPixel.cpp endif diff --git a/Marlin/src/Marlin.cpp b/Marlin/src/Marlin.cpp index 25cb73444..38801990f 100644 --- a/Marlin/src/Marlin.cpp +++ b/Marlin/src/Marlin.cpp @@ -71,19 +71,11 @@ #endif #if ENABLED(MAX7219_DEBUG) - #include "feature/leds/Max7219_Debug_LEDs.h" + #include "feature/Max7219_Debug_LEDs.h" #endif -#if ENABLED(NEOPIXEL_RGBW_LED) - #include -#endif - -#if ENABLED(BLINKM) - #include "feature/leds/blinkm.h" -#endif - -#if ENABLED(PCA9632) - #include "feature/leds/pca9632.h" +#if HAS_COLOR_LEDS + #include "feature/leds/leds.h" #endif #if HAS_SERVOS @@ -492,96 +484,6 @@ void servo_init() { #endif -#if HAS_COLOR_LEDS - - #if ENABLED(NEOPIXEL_RGBW_LED) - - Adafruit_NeoPixel pixels(NEOPIXEL_PIXELS, NEOPIXEL_PIN, NEO_GRBW + NEO_KHZ800); - - void set_neopixel_color(const uint32_t color) { - for (uint16_t i = 0; i < pixels.numPixels(); ++i) - pixels.setPixelColor(i, color); - pixels.show(); - } - - void setup_neopixel() { - pixels.setBrightness(255); // 0 - 255 range - pixels.begin(); - pixels.show(); // initialize to all off - - #if ENABLED(NEOPIXEL_STARTUP_TEST) - delay(2000); - set_neopixel_color(pixels.Color(255, 0, 0, 0)); // red - delay(2000); - set_neopixel_color(pixels.Color(0, 255, 0, 0)); // green - delay(2000); - set_neopixel_color(pixels.Color(0, 0, 255, 0)); // blue - delay(2000); - #endif - set_neopixel_color(pixels.Color(0, 0, 0, 255)); // white - } - - #endif // NEOPIXEL_RGBW_LED - - void set_led_color( - const uint8_t r, const uint8_t g, const uint8_t b - #if ENABLED(RGBW_LED) || ENABLED(NEOPIXEL_RGBW_LED) - , const uint8_t w = 0 - #if ENABLED(NEOPIXEL_RGBW_LED) - , bool isSequence = false - #endif - #endif - ) { - - #if ENABLED(NEOPIXEL_RGBW_LED) - - const uint32_t color = pixels.Color(r, g, b, w); - static uint16_t nextLed = 0; - - if (!isSequence) - set_neopixel_color(color); - else { - pixels.setPixelColor(nextLed, color); - pixels.show(); - if (++nextLed >= pixels.numPixels()) nextLed = 0; - return; - } - - #endif - - #if ENABLED(BLINKM) - - // This variant uses i2c to send the RGB components to the device. - SendColors(r, g, b); - - #endif - - #if ENABLED(RGB_LED) || ENABLED(RGBW_LED) - - // This variant uses 3 separate pins for the RGB components. - // If the pins can do PWM then their intensity will be set. - WRITE(RGB_LED_R_PIN, r ? HIGH : LOW); - WRITE(RGB_LED_G_PIN, g ? HIGH : LOW); - WRITE(RGB_LED_B_PIN, b ? HIGH : LOW); - analogWrite(RGB_LED_R_PIN, r); - analogWrite(RGB_LED_G_PIN, g); - analogWrite(RGB_LED_B_PIN, b); - - #if ENABLED(RGBW_LED) - WRITE(RGB_LED_W_PIN, w ? HIGH : LOW); - analogWrite(RGB_LED_W_PIN, w); - #endif - - #endif - - #if ENABLED(PCA9632) - // Update I2C LED driver - PCA9632_SetColor(r, g, b); - #endif - } - -#endif // HAS_COLOR_LEDS - #if HAS_WORKSPACE_OFFSET || ENABLED(DUAL_X_CARRIAGE) /** diff --git a/Marlin/src/feature/Max7219_Debug_LEDs.cpp b/Marlin/src/feature/Max7219_Debug_LEDs.cpp new file mode 100644 index 000000000..f2502303c --- /dev/null +++ b/Marlin/src/feature/Max7219_Debug_LEDs.cpp @@ -0,0 +1,237 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * This module is off by default, but can be enabled to facilitate the display of + * extra debug information during code development. It assumes the existence of a + * Max7219 LED Matrix. A suitable device can be obtained on eBay similar to this: + * http://www.ebay.com/itm/191781645249 for under $2.00 including shipping. + * + * Just connect up +5v and GND to give it power, then connect up the pins assigned + * in Configuration_adv.h. For example, on the Re-ARM you could use: + * + * #define MAX7219_CLK_PIN 77 + * #define MAX7219_DIN_PIN 78 + * #define MAX7219_LOAD_PIN 79 + * + * Max7219_init() is called automatically at startup, and then there are a number of + * support functions available to control the LEDs in the 8x8 grid. + * + * void Max7219_init(); + * void Max7219_PutByte(uint8_t data); + * void Max7219(uint8_t reg, uint8_t data); + * void Max7219_LED_On(uint8_t row, uint8_t col); + * void Max7219_LED_Off(uint8_t row, uint8_t col); + * void Max7219_LED_Toggle(uint8_t row, uint8_t col); + * void Max7219_Clear_Row(uint8_t row); + * void Max7219_Clear_Column(uint8_t col); + * void Max7219_Set_Row(uint8_t row, uint8_t val); + * void Max7219_Set_Column(uint8_t col, uint8_t val); + * void Max7219_idle_tasks(); + */ + +#include "../inc/MarlinConfig.h" + +#if ENABLED(MAX7219_DEBUG) + +#include "Max7219_Debug_LEDs.h" + +#include "../module/planner.h" +#include "../module/stepper.h" +#include "../Marlin.h" + +static uint8_t LEDs[8] = { 0 }; + +void Max7219_PutByte(uint8_t data) { + for (uint8_t i = 8; i--;) { + WRITE(MAX7219_CLK_PIN, LOW); // tick + WRITE(MAX7219_DIN_PIN, (data & 0x80) ? HIGH : LOW); // send 1 or 0 based on data bit + WRITE(MAX7219_CLK_PIN, HIGH); // tock + data <<= 1; + } +} + +void Max7219(const uint8_t reg, const uint8_t data) { + WRITE(MAX7219_LOAD_PIN, LOW); // begin + Max7219_PutByte(reg); // specify register + Max7219_PutByte(data); // put data + WRITE(MAX7219_LOAD_PIN, LOW); // and tell the chip to load the data + WRITE(MAX7219_LOAD_PIN, HIGH); +} + +void Max7219_LED_Set(const uint8_t row, const uint8_t col, const bool on) { + if (row > 7 || col > 7) return; + if (TEST(LEDs[row], col) == on) return; // if LED is already on/off, leave alone + if (on) SBI(LEDs[row], col); else CBI(LEDs[row], col); + Max7219(8 - row, LEDs[row]); +} + +void Max7219_LED_On(const uint8_t row, const uint8_t col) { + Max7219_LED_Set(row, col, true); +} + +void Max7219_LED_Off(const uint8_t row, const uint8_t col) { + Max7219_LED_Set(row, col, false); +} + +void Max7219_LED_Toggle(const uint8_t row, const uint8_t col) { + if (row > 7 || col > 7) return; + if (TEST(LEDs[row], col)) + Max7219_LED_Off(row, col); + else + Max7219_LED_On(row, col); +} + +void Max7219_Clear_Column(const uint8_t col) { + if (col > 7) return; + LEDs[col] = 0; + Max7219(8 - col, LEDs[col]); +} + +void Max7219_Clear_Row(const uint8_t row) { + if (row > 7) return; + for (uint8_t c = 0; c <= 7; c++) + Max7219_LED_Off(c, row); +} + +void Max7219_Set_Row(const uint8_t row, const uint8_t val) { + if (row > 7) return; + for (uint8_t b = 0; b <= 7; b++) + if (TEST(val, b)) + Max7219_LED_On(7 - b, row); + else + Max7219_LED_Off(7 - b, row); +} + +void Max7219_Set_Column(const uint8_t col, const uint8_t val) { + if (col > 7) return; + LEDs[col] = val; + Max7219(8 - col, LEDs[col]); +} + +void Max7219_init() { + uint8_t i, x, y; + + SET_OUTPUT(MAX7219_DIN_PIN); + SET_OUTPUT(MAX7219_CLK_PIN); + + OUT_WRITE(MAX7219_LOAD_PIN, HIGH); + + //initiation of the max 7219 + Max7219(max7219_reg_scanLimit, 0x07); + Max7219(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) + Max7219(max7219_reg_shutdown, 0x01); // not in shutdown mode + Max7219(max7219_reg_displayTest, 0x00); // no display test + Max7219(max7219_reg_intensity, 0x01 & 0x0F); // the first 0x0F is the value you can set + // range: 0x00 to 0x0F + for (i = 0; i <= 7; i++) { // empty registers, turn all LEDs off + LEDs[i] = 0x00; + Max7219(i + 1, 0); + } + + for (x = 0; x <= 7; x++) // Do an aesthetically pleasing pattern to fully test + for (y = 0; y <= 7; y++) { // the Max7219 module and LEDs. First, turn them + Max7219_LED_On(x, y); // all on. + delay(3); + } + + for (x = 0; x <= 7; x++) // Now, turn them all off. + for (y = 0; y <= 7; y++) { + Max7219_LED_Off(x, y); + delay(3); // delay() is OK here. Max7219_init() is only called from + } // setup() and nothing is running yet. + + delay(150); + + for (x = 8; x--;) // Now, do the same thing from the opposite direction + for (y = 0; y <= 7; y++) { + Max7219_LED_On(x, y); + delay(2); + } + + for (x = 8; x--;) + for (y = 0; y <= 7; y++) { + Max7219_LED_Off(x, y); + delay(2); + } +} + +/** +* These are sample debug features to demonstrate the usage of the 8x8 LED Matrix for debug purposes. +* There is very little CPU burden added to the system by displaying information within the idle() +* task. +* +* But with that said, if your debugging can be facilitated by making calls into the library from +* other places in the code, feel free to do it. The CPU burden for a few calls to toggle an LED +* or clear a row is not very significant. +*/ +void Max7219_idle_tasks() { + #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE) + static int debug_cnt = 0; + if (debug_cnt++ > 100) { + Max7219_LED_Toggle(7, 7); + debug_cnt = 0; + } + #endif + + #ifdef MAX7219_DEBUG_STEPPER_HEAD + Max7219_Clear_Row(MAX7219_DEBUG_STEPPER_HEAD); + Max7219_Clear_Row(MAX7219_DEBUG_STEPPER_HEAD + 1); + if ( planner.block_buffer_head < 8) + Max7219_LED_On( planner.block_buffer_head, MAX7219_DEBUG_STEPPER_HEAD); + else + Max7219_LED_On( planner.block_buffer_head-8, MAX7219_DEBUG_STEPPER_HEAD+1); + #endif + + #ifdef MAX7219_DEBUG_STEPPER_TAIL + Max7219_Clear_Row(MAX7219_DEBUG_STEPPER_TAIL); + Max7219_Clear_Row(MAX7219_DEBUG_STEPPER_TAIL + 1); + if ( planner.block_buffer_tail < 8) + Max7219_LED_On( planner.block_buffer_tail, MAX7219_DEBUG_STEPPER_TAIL ); + else + Max7219_LED_On( planner.block_buffer_tail-8, MAX7219_DEBUG_STEPPER_TAIL+1 ); + #endif + + #ifdef MAX7219_DEBUG_STEPPER_QUEUE + static int16_t last_depth = 0; + int16_t current_depth = planner.block_buffer_head - planner.block_buffer_tail; + if (current_depth != last_depth) { // usually, no update will be needed. + if (current_depth < 0) current_depth += BLOCK_BUFFER_SIZE; + NOMORE(current_depth, BLOCK_BUFFER_SIZE); + NOMORE(current_depth, 16); // if the BLOCK_BUFFER_SIZE is greater than 16, two lines + // of LEDs is enough to see if the buffer is draining + + const uint8_t st = min(current_depth, last_depth), + en = max(current_depth, last_depth); + if (current_depth < last_depth) + for (uint8_t i = st; i <= en; i++) // clear the highest order LEDs + Max7219_LED_Off(i >> 1, MAX7219_DEBUG_STEPPER_QUEUE + (i & 1)); + else + for (uint8_t i = st; i <= en; i++) // set the highest order LEDs + Max7219_LED_On(i >> 1, MAX7219_DEBUG_STEPPER_QUEUE + (i & 1)); + + last_depth = current_depth; + } + #endif +} + +#endif // MAX7219_DEBUG diff --git a/Marlin/src/feature/leds/Max7219_Debug_LEDs.h b/Marlin/src/feature/Max7219_Debug_LEDs.h similarity index 64% rename from Marlin/src/feature/leds/Max7219_Debug_LEDs.h rename to Marlin/src/feature/Max7219_Debug_LEDs.h index 71a5124e3..5349023c1 100644 --- a/Marlin/src/feature/leds/Max7219_Debug_LEDs.h +++ b/Marlin/src/feature/Max7219_Debug_LEDs.h @@ -53,36 +53,36 @@ #ifndef __MAX7219_DEBUG_LEDS_H__ #define __MAX7219_DEBUG_LEDS_H__ - // - // define max7219 registers - // - #define max7219_reg_noop 0x00 - #define max7219_reg_digit0 0x01 - #define max7219_reg_digit1 0x02 - #define max7219_reg_digit2 0x03 - #define max7219_reg_digit3 0x04 - #define max7219_reg_digit4 0x05 - #define max7219_reg_digit5 0x06 - #define max7219_reg_digit6 0x07 - #define max7219_reg_digit7 0x08 +// +// define max7219 registers +// +#define max7219_reg_noop 0x00 +#define max7219_reg_digit0 0x01 +#define max7219_reg_digit1 0x02 +#define max7219_reg_digit2 0x03 +#define max7219_reg_digit3 0x04 +#define max7219_reg_digit4 0x05 +#define max7219_reg_digit5 0x06 +#define max7219_reg_digit6 0x07 +#define max7219_reg_digit7 0x08 - #define max7219_reg_intensity 0x0A - #define max7219_reg_displayTest 0x0F - #define max7219_reg_decodeMode 0x09 - #define max7219_reg_scanLimit 0x0B - #define max7219_reg_shutdown 0x0C +#define max7219_reg_intensity 0x0A +#define max7219_reg_displayTest 0x0F +#define max7219_reg_decodeMode 0x09 +#define max7219_reg_scanLimit 0x0B +#define max7219_reg_shutdown 0x0C - void Max7219_init(); - void Max7219_PutByte(uint8_t data); - void Max7219(const uint8_t reg, const uint8_t data); - void Max7219_LED_Set(const uint8_t row, const uint8_t col, const bool on); - void Max7219_LED_On(const uint8_t row, const uint8_t col); - void Max7219_LED_Off(const uint8_t row, const uint8_t col); - void Max7219_LED_Toggle(const uint8_t row, const uint8_t col); - void Max7219_Clear_Row(const uint8_t row); - void Max7219_Clear_Column(const uint8_t col); - void Max7219_Set_Row(const uint8_t row, const uint8_t val); - void Max7219_Set_Column(const uint8_t col, const uint8_t val); - void Max7219_idle_tasks(); +void Max7219_init(); +void Max7219_PutByte(uint8_t data); +void Max7219(const uint8_t reg, const uint8_t data); +void Max7219_LED_Set(const uint8_t row, const uint8_t col, const bool on); +void Max7219_LED_On(const uint8_t row, const uint8_t col); +void Max7219_LED_Off(const uint8_t row, const uint8_t col); +void Max7219_LED_Toggle(const uint8_t row, const uint8_t col); +void Max7219_Clear_Row(const uint8_t row); +void Max7219_Clear_Column(const uint8_t col); +void Max7219_Set_Row(const uint8_t row, const uint8_t val); +void Max7219_Set_Column(const uint8_t col, const uint8_t val); +void Max7219_idle_tasks(); #endif // __MAX7219_DEBUG_LEDS_H__ diff --git a/Marlin/src/feature/leds/Max7219_Debug_LEDs.cpp b/Marlin/src/feature/leds/Max7219_Debug_LEDs.cpp deleted file mode 100644 index 379c11416..000000000 --- a/Marlin/src/feature/leds/Max7219_Debug_LEDs.cpp +++ /dev/null @@ -1,237 +0,0 @@ -/** - * Marlin 3D Printer Firmware - * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] - * - * Based on Sprinter and grbl. - * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -/** - * This module is off by default, but can be enabled to facilitate the display of - * extra debug information during code development. It assumes the existence of a - * Max7219 LED Matrix. A suitable device can be obtained on eBay similar to this: - * http://www.ebay.com/itm/191781645249 for under $2.00 including shipping. - * - * Just connect up +5v and GND to give it power, then connect up the pins assigned - * in Configuration_adv.h. For example, on the Re-ARM you could use: - * - * #define MAX7219_CLK_PIN 77 - * #define MAX7219_DIN_PIN 78 - * #define MAX7219_LOAD_PIN 79 - * - * Max7219_init() is called automatically at startup, and then there are a number of - * support functions available to control the LEDs in the 8x8 grid. - * - * void Max7219_init(); - * void Max7219_PutByte(uint8_t data); - * void Max7219(uint8_t reg, uint8_t data); - * void Max7219_LED_On(uint8_t row, uint8_t col); - * void Max7219_LED_Off(uint8_t row, uint8_t col); - * void Max7219_LED_Toggle(uint8_t row, uint8_t col); - * void Max7219_Clear_Row(uint8_t row); - * void Max7219_Clear_Column(uint8_t col); - * void Max7219_Set_Row(uint8_t row, uint8_t val); - * void Max7219_Set_Column(uint8_t col, uint8_t val); - * void Max7219_idle_tasks(); - */ - -#include "../../inc/MarlinConfig.h" - -#if ENABLED(MAX7219_DEBUG) - - #include "Max7219_Debug_LEDs.h" - - #include "../../module/planner.h" - #include "../../module/stepper.h" - #include "../../Marlin.h" - - static uint8_t LEDs[8] = { 0 }; - - void Max7219_PutByte(uint8_t data) { - for (uint8_t i = 8; i--;) { - WRITE(MAX7219_CLK_PIN, LOW); // tick - WRITE(MAX7219_DIN_PIN, (data & 0x80) ? HIGH : LOW); // send 1 or 0 based on data bit - WRITE(MAX7219_CLK_PIN, HIGH); // tock - data <<= 1; - } - } - - void Max7219(const uint8_t reg, const uint8_t data) { - WRITE(MAX7219_LOAD_PIN, LOW); // begin - Max7219_PutByte(reg); // specify register - Max7219_PutByte(data); // put data - WRITE(MAX7219_LOAD_PIN, LOW); // and tell the chip to load the data - WRITE(MAX7219_LOAD_PIN, HIGH); - } - - void Max7219_LED_Set(const uint8_t row, const uint8_t col, const bool on) { - if (row > 7 || col > 7) return; - if (TEST(LEDs[row], col) == on) return; // if LED is already on/off, leave alone - if (on) SBI(LEDs[row], col); else CBI(LEDs[row], col); - Max7219(8 - row, LEDs[row]); - } - - void Max7219_LED_On(const uint8_t row, const uint8_t col) { - Max7219_LED_Set(row, col, true); - } - - void Max7219_LED_Off(const uint8_t row, const uint8_t col) { - Max7219_LED_Set(row, col, false); - } - - void Max7219_LED_Toggle(const uint8_t row, const uint8_t col) { - if (row > 7 || col > 7) return; - if (TEST(LEDs[row], col)) - Max7219_LED_Off(row, col); - else - Max7219_LED_On(row, col); - } - - void Max7219_Clear_Column(const uint8_t col) { - if (col > 7) return; - LEDs[col] = 0; - Max7219(8 - col, LEDs[col]); - } - - void Max7219_Clear_Row(const uint8_t row) { - if (row > 7) return; - for (uint8_t c = 0; c <= 7; c++) - Max7219_LED_Off(c, row); - } - - void Max7219_Set_Row(const uint8_t row, const uint8_t val) { - if (row > 7) return; - for (uint8_t b = 0; b <= 7; b++) - if (TEST(val, b)) - Max7219_LED_On(7 - b, row); - else - Max7219_LED_Off(7 - b, row); - } - - void Max7219_Set_Column(const uint8_t col, const uint8_t val) { - if (col > 7) return; - LEDs[col] = val; - Max7219(8 - col, LEDs[col]); - } - - void Max7219_init() { - uint8_t i, x, y; - - SET_OUTPUT(MAX7219_DIN_PIN); - SET_OUTPUT(MAX7219_CLK_PIN); - - OUT_WRITE(MAX7219_LOAD_PIN, HIGH); - - //initiation of the max 7219 - Max7219(max7219_reg_scanLimit, 0x07); - Max7219(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) - Max7219(max7219_reg_shutdown, 0x01); // not in shutdown mode - Max7219(max7219_reg_displayTest, 0x00); // no display test - Max7219(max7219_reg_intensity, 0x01 & 0x0F); // the first 0x0F is the value you can set - // range: 0x00 to 0x0F - for (i = 0; i <= 7; i++) { // empty registers, turn all LEDs off - LEDs[i] = 0x00; - Max7219(i + 1, 0); - } - - for (x = 0; x <= 7; x++) // Do an aesthetically pleasing pattern to fully test - for (y = 0; y <= 7; y++) { // the Max7219 module and LEDs. First, turn them - Max7219_LED_On(x, y); // all on. - delay(3); - } - - for (x = 0; x <= 7; x++) // Now, turn them all off. - for (y = 0; y <= 7; y++) { - Max7219_LED_Off(x, y); - delay(3); // delay() is OK here. Max7219_init() is only called from - } // setup() and nothing is running yet. - - delay(150); - - for (x = 8; x--;) // Now, do the same thing from the opposite direction - for (y = 0; y <= 7; y++) { - Max7219_LED_On(x, y); - delay(2); - } - - for (x = 8; x--;) - for (y = 0; y <= 7; y++) { - Max7219_LED_Off(x, y); - delay(2); - } - } - -/** - * These are sample debug features to demonstrate the usage of the 8x8 LED Matrix for debug purposes. - * There is very little CPU burden added to the system by displaying information within the idle() - * task. - * - * But with that said, if your debugging can be facilitated by making calls into the library from - * other places in the code, feel free to do it. The CPU burden for a few calls to toggle an LED - * or clear a row is not very significant. - */ - void Max7219_idle_tasks() { - #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE) - static int debug_cnt = 0; - if (debug_cnt++ > 100) { - Max7219_LED_Toggle(7, 7); - debug_cnt = 0; - } - #endif - - #ifdef MAX7219_DEBUG_STEPPER_HEAD - Max7219_Clear_Row(MAX7219_DEBUG_STEPPER_HEAD); - Max7219_Clear_Row(MAX7219_DEBUG_STEPPER_HEAD + 1); - if ( planner.block_buffer_head < 8) - Max7219_LED_On( planner.block_buffer_head, MAX7219_DEBUG_STEPPER_HEAD); - else - Max7219_LED_On( planner.block_buffer_head-8, MAX7219_DEBUG_STEPPER_HEAD+1); - #endif - - #ifdef MAX7219_DEBUG_STEPPER_TAIL - Max7219_Clear_Row(MAX7219_DEBUG_STEPPER_TAIL); - Max7219_Clear_Row(MAX7219_DEBUG_STEPPER_TAIL + 1); - if ( planner.block_buffer_tail < 8) - Max7219_LED_On( planner.block_buffer_tail, MAX7219_DEBUG_STEPPER_TAIL ); - else - Max7219_LED_On( planner.block_buffer_tail-8, MAX7219_DEBUG_STEPPER_TAIL+1 ); - #endif - - #ifdef MAX7219_DEBUG_STEPPER_QUEUE - static int16_t last_depth = 0; - int16_t current_depth = planner.block_buffer_head - planner.block_buffer_tail; - if (current_depth != last_depth) { // usually, no update will be needed. - if (current_depth < 0) current_depth += BLOCK_BUFFER_SIZE; - NOMORE(current_depth, BLOCK_BUFFER_SIZE); - NOMORE(current_depth, 16); // if the BLOCK_BUFFER_SIZE is greater than 16, two lines - // of LEDs is enough to see if the buffer is draining - - const uint8_t st = min(current_depth, last_depth), - en = max(current_depth, last_depth); - if (current_depth < last_depth) - for (uint8_t i = st; i <= en; i++) // clear the highest order LEDs - Max7219_LED_Off(i >> 1, MAX7219_DEBUG_STEPPER_QUEUE + (i & 1)); - else - for (uint8_t i = st; i <= en; i++) // set the highest order LEDs - Max7219_LED_On(i >> 1, MAX7219_DEBUG_STEPPER_QUEUE + (i & 1)); - - last_depth = current_depth; - } - #endif - } - -#endif // MAX7219_DEBUG diff --git a/Marlin/src/feature/leds/blinkm.cpp b/Marlin/src/feature/leds/blinkm.cpp index b3a2f61d0..30a465c67 100644 --- a/Marlin/src/feature/leds/blinkm.cpp +++ b/Marlin/src/feature/leds/blinkm.cpp @@ -25,20 +25,20 @@ * Created by Tim Koster, August 21 2013. */ -#include "../../Marlin.h" +#include "../../inc/MarlinConfig.h" #if ENABLED(BLINKM) #include "blinkm.h" -void SendColors(byte red, byte grn, byte blu) { +void blinkm_set_led_color(const byte r, const byte g, const byte b) { Wire.begin(); Wire.beginTransmission(0x09); Wire.write('o'); //to disable ongoing script, only needs to be used once Wire.write('n'); - Wire.write(red); - Wire.write(grn); - Wire.write(blu); + Wire.write(r); + Wire.write(g); + Wire.write(b); Wire.endTransmission(); } diff --git a/Marlin/src/feature/leds/blinkm.h b/Marlin/src/feature/leds/blinkm.h index ed2ad79bb..5c7b1307a 100644 --- a/Marlin/src/feature/leds/blinkm.h +++ b/Marlin/src/feature/leds/blinkm.h @@ -25,7 +25,12 @@ * Created by Tim Koster, August 21 2013. */ +#ifndef __BLINKM_H__ +#define __BLINKM_H__ + #include "Arduino.h" -#include "Wire.h" +#include + +void blinkm_set_led_color(const uint8_t r, const uint8_t g, const uint8_t b); -void SendColors(byte red, byte grn, byte blu); +#endif // __BLINKM_H__ diff --git a/Marlin/src/feature/leds/leds.cpp b/Marlin/src/feature/leds/leds.cpp new file mode 100644 index 000000000..1f422657f --- /dev/null +++ b/Marlin/src/feature/leds/leds.cpp @@ -0,0 +1,73 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Marlin RGB LED general support + */ + +#include "../../inc/MarlinConfig.h" + +#if HAS_COLOR_LEDS + +#include "leds.h" + +void set_led_color( + const uint8_t r, const uint8_t g, const uint8_t b + #if ENABLED(RGBW_LED) || ENABLED(NEOPIXEL_RGBW_LED) + , const uint8_t w + #if ENABLED(NEOPIXEL_RGBW_LED) + , bool isSequence + #endif + #endif +) { + + #if ENABLED(NEOPIXEL_RGBW_LED) + if (neopixel_set_led_color(r, g, b, w, isSequence)) + return; + #endif + + #if ENABLED(BLINKM) + blinkm_set_led_color(r, g, b); // Use i2c to send the RGB components to the device. + #endif + + #if ENABLED(RGB_LED) || ENABLED(RGBW_LED) + // This variant uses 3 separate pins for the RGB components. + // If the pins can do PWM then their intensity will be set. + WRITE(RGB_LED_R_PIN, r ? HIGH : LOW); + WRITE(RGB_LED_G_PIN, g ? HIGH : LOW); + WRITE(RGB_LED_B_PIN, b ? HIGH : LOW); + analogWrite(RGB_LED_R_PIN, r); + analogWrite(RGB_LED_G_PIN, g); + analogWrite(RGB_LED_B_PIN, b); + + #if ENABLED(RGBW_LED) + WRITE(RGB_LED_W_PIN, w ? HIGH : LOW); + analogWrite(RGB_LED_W_PIN, w); + #endif + #endif + + #if ENABLED(PCA9632) + pca9632_set_led_color(r, g, b); // Update I2C LED driver + #endif +} + +#endif // HAS_COLOR_LEDS diff --git a/Marlin/src/feature/leds/leds.h b/Marlin/src/feature/leds/leds.h new file mode 100644 index 000000000..c271744fe --- /dev/null +++ b/Marlin/src/feature/leds/leds.h @@ -0,0 +1,53 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Marlin general RGB LED support + */ + +#ifndef __LEDS_H__ +#define __LEDS_H__ + +#if ENABLED(NEOPIXEL_RGBW_LED) + #include + #include "neopixel.h" +#endif + +#if ENABLED(BLINKM) + #include "blinkm.h" +#endif + +#if ENABLED(PCA9632) + #include "pca9632.h" +#endif + +void set_led_color( + const uint8_t r, const uint8_t g, const uint8_t b + #if ENABLED(RGBW_LED) || ENABLED(NEOPIXEL_RGBW_LED) + , const uint8_t w = 0 + #if ENABLED(NEOPIXEL_RGBW_LED) + , bool isSequence = false + #endif + #endif +); + +#endif // __LEDS_H__ diff --git a/Marlin/src/feature/leds/neopixel.cpp b/Marlin/src/feature/leds/neopixel.cpp new file mode 100644 index 000000000..d854098c2 --- /dev/null +++ b/Marlin/src/feature/leds/neopixel.cpp @@ -0,0 +1,72 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Marlin RGB LED general support + */ + +#include "../../inc/MarlinConfig.h" + +#if ENABLED(NEOPIXEL_RGBW_LED) + +#include "neopixel.h" + +Adafruit_NeoPixel pixels(NEOPIXEL_PIXELS, NEOPIXEL_PIN, NEO_GRBW + NEO_KHZ800); + +void set_neopixel_color(const uint32_t color) { + for (uint16_t i = 0; i < pixels.numPixels(); ++i) + pixels.setPixelColor(i, color); + pixels.show(); +} + +void setup_neopixel() { + pixels.setBrightness(255); // 0 - 255 range + pixels.begin(); + pixels.show(); // initialize to all off + + #if ENABLED(NEOPIXEL_STARTUP_TEST) + delay(2000); + set_neopixel_color(pixels.Color(255, 0, 0, 0)); // red + delay(2000); + set_neopixel_color(pixels.Color(0, 255, 0, 0)); // green + delay(2000); + set_neopixel_color(pixels.Color(0, 0, 255, 0)); // blue + delay(2000); + #endif + set_neopixel_color(pixels.Color(0, 0, 0, 255)); // white +} + +bool neopixel_set_led_color(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t w, const bool isSequence) { + const uint32_t color = pixels.Color(r, g, b, w); + static uint16_t nextLed = 0; + if (!isSequence) + set_neopixel_color(color); + else { + pixels.setPixelColor(nextLed, color); + pixels.show(); + if (++nextLed >= pixels.numPixels()) nextLed = 0; + return true; + } + return false; +} + +#endif // NEOPIXEL_RGBW_LED diff --git a/Marlin/src/feature/leds/neopixel.h b/Marlin/src/feature/leds/neopixel.h new file mode 100644 index 000000000..76ca7fabc --- /dev/null +++ b/Marlin/src/feature/leds/neopixel.h @@ -0,0 +1,38 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * Neopixel support + */ + +#ifndef __NEOPIXEL_H__ +#define __NEOPIXEL_H__ + +#include +#include + +void setup_neopixel(); +bool neopixel_set_led_color(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t w, const bool isSequence); + +extern Adafruit_NeoPixel pixels; + +#endif // __NEOPIXEL_H__ diff --git a/Marlin/src/feature/leds/pca9632.cpp b/Marlin/src/feature/leds/pca9632.cpp index 8699cebf9..e7bc46cb2 100644 --- a/Marlin/src/feature/leds/pca9632.cpp +++ b/Marlin/src/feature/leds/pca9632.cpp @@ -35,7 +35,6 @@ #define PCA9632_MODE2_VALUE 0b00010101 //(DIMMING, INVERT, CHANGE ON STOP,TOTEM) #define PCA9632_LEDOUT_VALUE 0b00101010 - /* Register addresses */ #define PCA9632_MODE1 0x00 #define PCA9632_MODE2 0x01 @@ -98,7 +97,7 @@ static void PCA9632_WriteAllRegisters(const byte addr, const byte regadd, const } #endif -void PCA9632_SetColor(const byte r, const byte g, const byte b) { +void pca9632_set_led_color(const byte r, const byte g, const byte b) { if (!PCA_init) { PCA_init = 1; Wire.begin(); diff --git a/Marlin/src/feature/leds/pca9632.h b/Marlin/src/feature/leds/pca9632.h index b8c78f065..b3ddfcd6e 100644 --- a/Marlin/src/feature/leds/pca9632.h +++ b/Marlin/src/feature/leds/pca9632.h @@ -29,8 +29,8 @@ #define __PCA9632_H__ #include "Arduino.h" -#include "Wire.h" +#include -void PCA9632_SetColor(const byte r, const byte g, const byte b); +void pca9632_set_led_color(const byte r, const byte g, const byte b); #endif // __PCA9632_H__ diff --git a/Marlin/src/gcode/queue.cpp b/Marlin/src/gcode/queue.cpp index 6af72c949..09c49933a 100644 --- a/Marlin/src/gcode/queue.cpp +++ b/Marlin/src/gcode/queue.cpp @@ -32,6 +32,10 @@ #include "../module/planner.h" #include "../Marlin.h" +#if HAS_COLOR_LEDS + #include "../feature/leds/leds.h" +#endif + /** * GCode line number handling. Hosts may opt to include line numbers when * sending commands to Marlin, and lines will be checked for sequentiality. diff --git a/Marlin/src/gcode/temperature/M109.cpp b/Marlin/src/gcode/temperature/M109.cpp index a3740d47a..1254ff4b1 100644 --- a/Marlin/src/gcode/temperature/M109.cpp +++ b/Marlin/src/gcode/temperature/M109.cpp @@ -34,6 +34,10 @@ #include "../../module/motion.h" #endif +#if HAS_COLOR_LEDS + #include "../../feature/leds/leds.h" +#endif + /** * M109: Sxxx Wait for extruder(s) to reach temperature. Waits only when heating. * Rxxx Wait for extruder(s) to reach temperature. Waits when heating and cooling.