Skip to content

Commit

Permalink
Added gcode handler - M707. Added gcode modes (P1-4): temperature, al…
Browse files Browse the repository at this point in the history
…ert, wipe, theather chase.
  • Loading branch information
imrahil committed Apr 2, 2016
1 parent 3fc9ef7 commit 5f4263b
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 82 deletions.
9 changes: 8 additions & 1 deletion src/ArduinoAVR/Repetier/Commands.cpp
Expand Up @@ -56,7 +56,7 @@ void Commands::commandLoop() {
Printer::defaultLoopActions();

#ifdef NEOPIXEL_LEDS
WS2812::handle_temperature();
WS2812::workLoop();
#endif
}
}
Expand Down Expand Up @@ -2364,6 +2364,13 @@ break;
else
GCode::resetFatalError();
break;

case 707: // WS2812 handler
if(com->hasP())
WS2812::handleCommand((uint8_t)com->P);
else
WS2812::handleCommand(0);
break;
default:
if(!EVENT_UNHANDLED_M_CODE(com) && Printer::debugErrors()) {
Com::printF(Com::tUnknownCommand);
Expand Down
2 changes: 1 addition & 1 deletion src/ArduinoAVR/Repetier/Printer.cpp
Expand Up @@ -775,7 +775,7 @@ void Printer::setup()
HAL::hwSetup();

#ifdef NEOPIXEL_LEDS
WS2812::setup();
WS2812::init();
#endif

#ifdef ANALYZER
Expand Down
217 changes: 147 additions & 70 deletions src/ArduinoAVR/Repetier/WS2812.cpp
Expand Up @@ -2,75 +2,96 @@
#include "Repetier.h"

Adafruit_NeoPixel WS2812::ext_neopixel = Adafruit_NeoPixel(EXT_NUM_LEDS, EXT_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel WS2812::bed_neopixel = Adafruit_NeoPixel(BED_NUM_LEDS, BED_PIN, NEO_GRB + NEO_KHZ800);

uint32_t WS2812::stat_update = 0;
uint8_t WS2812::currentMode = 1;
uint8_t WS2812::alertMode = 0;
uint8_t WS2812::pixelCount = 0;

uint32_t WS2812::previousMillis = 0;

uint16_t WS2812::neo_r = 0;
uint16_t WS2812::neo_g = 0;
uint16_t WS2812::neo_b = 255;
uint16_t WS2812::c_diff = 0;
uint32_t WS2812::bed_c;
uint32_t WS2812::ext_c;
uint32_t WS2812::last_bed_c;
uint32_t WS2812::last_ext_c;
float WS2812::curr_ext_temp = 0.0;
float WS2812::target_ext_temp = 0.0;
float WS2812::curr_bed_temp = 0;
float WS2812::target_bed_temp = 0;

void WS2812::setup()
{
bed_neopixel.begin();
bed_neopixel.show();
void WS2812::init() {
ext_neopixel.begin();
ext_neopixel.show();

currentMode = 1;
}

void WS2812::handle_temperature() {
if(HAL::timeInMilliseconds() > stat_update) {
stat_update += 500;
// SET Bed LED
if(heatedBedController.currentTemperatureC > 0) {
if(heatedBedController.currentTemperatureC > heatedBedController.targetTemperatureC && heatedBedController.targetTemperatureC > 0) {
curr_bed_temp = heatedBedController.targetTemperatureC;
} else {
curr_bed_temp = heatedBedController.currentTemperatureC;
}
}
target_bed_temp = heatedBedController.targetTemperatureC;
void WS2812::workLoop() {
switch (currentMode) {
case 1:
handleTemperature();
break;
case 2:
handleAlert();
break;
case 3:
handleColorWipe();
break;
case 4:
handleTheaterChase();
break;
}
}

if(curr_bed_temp >= 30.0) {
if(target_bed_temp > 0) { //Heating Up
c_diff = (curr_bed_temp - 30)*255/(target_bed_temp-30);
if(c_diff>255) {c_diff = 255;}
} else { //Cooling Down
c_diff = (curr_bed_temp-30)*6;
if(c_diff>255) {c_diff = 255;}
}
neo_r = c_diff;
neo_b = 255 - c_diff;
} else { //Temp is less than 30.0
neo_r = 0;
neo_b = 255;
}
last_bed_c = bed_c;
bed_c = bed_neopixel.Color(neo_r, neo_g, neo_b);
if(bed_c != last_bed_c) { //only update if there's a change
for(uint16_t i=0; i<bed_neopixel.numPixels(); i++) {
bed_neopixel.setPixelColor(i, bed_c);
bed_neopixel.show();
}
}
// gcode M707 - available commands:
// 0 - reset LED strip
// 1 - display temperature
// 2 - simple alert mode (flashing red and white)
// 3 - color wipe
// 4 - theater chase (flashing every second pixel)
void WS2812::handleCommand(uint8_t command) {
previousMillis = 0;
alertMode = 0;
ext_c = 0;

ext_neopixel.clear();

switch (command) {
case 0:
init();
Com::printFLN(PSTR("WS2812 - reseting leds"));
break;
case 1:
currentMode = 1;
Com::printFLN(PSTR("WS2812 - display temperature mode"));
break;
case 2:
currentMode = 2;
Com::printFLN(PSTR("WS2812 - alert mode"));
break;
case 3:
currentMode = 3;
pixelCount = 1;
Com::printFLN(PSTR("WS2812 - color wipe"));
break;
case 4:
currentMode = 4;
Com::printFLN(PSTR("WS2812 - theater chase"));
break;
}
}

// SET Extruder LED
if(Extruder::current->tempControl.currentTemperatureC > 0) {
if(Extruder::current->tempControl.currentTemperatureC > tempController[Extruder::current->id]->targetTemperatureC && tempController[Extruder::current->id]->targetTemperatureC > 0) {
curr_ext_temp = tempController[Extruder::current->id]->targetTemperatureC;
} else {
curr_ext_temp = Extruder::current->tempControl.currentTemperatureC;
}
}
void WS2812::handleTemperature() {
if(HAL::timeInMilliseconds() - previousMillis >= tempLoopInterval) {
previousMillis = HAL::timeInMilliseconds();

curr_ext_temp = Extruder::current->tempControl.currentTemperatureC;
target_ext_temp = tempController[Extruder::current->id]->targetTemperatureC;

// SET Extruder LED
if(curr_ext_temp > 0)
if(curr_ext_temp > target_ext_temp && target_ext_temp > 0)
curr_ext_temp = target_ext_temp;

if(curr_ext_temp > 30.0) {
if(target_ext_temp > 0) {
c_diff = (curr_ext_temp - 30)*255/(target_ext_temp-30);
Expand All @@ -85,28 +106,84 @@ void WS2812::handle_temperature() {
neo_r = 0;
neo_b = 255;
}

last_ext_c = ext_c;
ext_c = ext_neopixel.Color(neo_r, neo_g, neo_b);
if(ext_c != last_ext_c) {
for(uint16_t i=0; i<ext_neopixel.numPixels(); i++) {
ext_neopixel.setPixelColor(i, ext_c);
ext_neopixel.show();
}
}

if(ext_c != last_ext_c)
colorFill(ext_c);
}
}
void WS2812::handle_alert() {
while(1) {
colorWipe(ext_neopixel.Color(255, 0, 0), 50); // Red
colorWipe(ext_neopixel.Color(0, 255, 0), 50); // Green
colorWipe(ext_neopixel.Color(0, 0, 255), 50); // Blue
}

void WS2812::handleAlert() {
if(HAL::timeInMilliseconds() - previousMillis >= alertLoopInterval) {
previousMillis = HAL::timeInMilliseconds();

if (alertMode == 0)
colorFill(ext_neopixel.Color(255, 255, 255)); // White
else
colorFill(ext_neopixel.Color(255, 0, 0)); // Red

alertMode += 1;

if (alertMode > 1)
alertMode = 0;
}
}

void WS2812::handleColorWipe() {
if(HAL::timeInMilliseconds() - previousMillis >= wipeLoopInterval) {
previousMillis = HAL::timeInMilliseconds();

if (alertMode == 0)
colorWipe(ext_neopixel.Color(255, 0, 0)); // Red
else if (alertMode == 1)
colorWipe(ext_neopixel.Color(0, 255, 0)); // Green
else
colorWipe(ext_neopixel.Color(0, 0, 255)); // Blue

pixelCount += 1;

if (pixelCount >= ext_neopixel.numPixels()) {
pixelCount = 0;

alertMode += 1;
if (alertMode > 2)
alertMode = 0;
}
}
}

void WS2812::handleTheaterChase() {
if(HAL::timeInMilliseconds() - previousMillis >= theatherChaseLoopInterval) {
previousMillis = HAL::timeInMilliseconds();

uint32_t c1 = (alertMode == 0) ? ext_neopixel.Color(255, 0, 0) : 0;
uint32_t c2 = (alertMode == 0) ? 0 : ext_neopixel.Color(255, 0, 0);

for (uint16_t i=0; i < ext_neopixel.numPixels(); i+=2) {
ext_neopixel.setPixelColor(i, c1);
ext_neopixel.setPixelColor(i+1, c2);
}
ext_neopixel.show();

alertMode += 1;

if (alertMode > 1)
alertMode = 0;
}
}

void WS2812::colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<ext_neopixel.numPixels(); i++) {
ext_neopixel.setPixelColor(i, c);
ext_neopixel.show();
delay(wait);
}

void WS2812::colorFill(uint32_t c) {
for(uint16_t i=0; i < ext_neopixel.numPixels(); i++) {
ext_neopixel.setPixelColor(i, c);
}

ext_neopixel.show();
}

void WS2812::colorWipe(uint32_t c) {
ext_neopixel.setPixelColor(pixelCount, c);
ext_neopixel.show();
}
30 changes: 20 additions & 10 deletions src/ArduinoAVR/Repetier/WS2812.h
Expand Up @@ -13,26 +13,36 @@ class WS2812
{
public:
static Adafruit_NeoPixel ext_neopixel;
static Adafruit_NeoPixel bed_neopixel;

static void setup();
static void handle_temperature();
static void handle_alert();
static void colorWipe(uint32_t c, uint8_t wait);

static void init();
static void workLoop();
static void handleCommand(uint8_t command);
static void handleTemperature();
static void handleAlert();
static void handleColorWipe();
static void handleTheaterChase();

private:
static const uint16_t tempLoopInterval = 500;
static const uint16_t alertLoopInterval = 500;
static const uint16_t wipeLoopInterval = 50;
static const uint16_t theatherChaseLoopInterval = 100;
static uint32_t previousMillis;

static uint8_t currentMode;
static uint8_t alertMode;
static uint8_t pixelCount;
static uint32_t stat_update;
static uint16_t neo_r;
static uint16_t neo_g;
static uint16_t neo_b;
static uint16_t c_diff;
static uint32_t bed_c;
static uint32_t ext_c;
static uint32_t last_bed_c;
static uint32_t last_ext_c;
static float curr_ext_temp;
static float target_ext_temp;
static float curr_bed_temp;
static float target_bed_temp;

static void colorFill(uint32_t c);
static void colorWipe(uint32_t c);
};
#endif // WS2812_H_INCLUDED

0 comments on commit 5f4263b

Please sign in to comment.