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

M308: Generic Heater Control #254

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/GCodes/GCodes2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2381,6 +2381,109 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply)
result = SetHeaterModel(gb, reply);
break;

case 308: // Set generic heater state
{
/*
* M308 Generic Heater Control
* Pnnn Heater number
* Snnn Active Temp
* Rnnn Standby Temp
* Tnnn State: 0 = off, 1 = standby 2 = on.
*/
Heat& heat = reprap.GetHeat();
bool seen = false;
uint8_t currentHeater = 0;

if (gb.Seen('P')) {
currentHeater = gb.GetUIValue();
if (currentHeater >= NumHeaters)
{
reply.printf("Invalid heater index '%d'", currentHeater);
result = GCodeResult::error;
break;
}
} else {
reply.copy("P parameter must be specified");
result = GCodeResult::error;
break;
}

const Heat::HeaterStatus currentHeaterStatus = heat.GetStatus(currentHeater);
if (currentHeaterStatus > Heat::HS_active) {
reply.printf("Heater is currently in %s state",
heat.HeaterStatusToString(currentHeaterStatus));
result = GCodeResult::error;
break;
}

Tool* tool = reprap.GetCurrentTool();
ToolState toolState = tool->GetState();
int toolHeater = tool->GetHeaterAssignedToTool(currentHeater);

// Active temperature
if (gb.Seen('S'))
{
seen = true;
if (toolHeater >= 0) {
tool->SetToolHeaterActiveTemperature(toolHeater, gb.GetFValue());
} else {
heat.SetActiveTemperature(currentHeater, gb.GetFValue());
}
}

if (gb.Seen('R'))
{
seen = true;
if (toolHeater >= 0) {
tool->SetToolHeaterStandbyTemperature(toolHeater, gb.GetFValue());
} else {
heat.SetStandbyTemperature(currentHeater, gb.GetFValue());
}
}

if (gb.Seen('T'))
{
seen = true;
const int8_t state = gb.GetIValue();
if (state < Heat::HS_off || state > Heat::HS_active) {
reply.copy("T parameter must be 0(off), 1(standby) or 2(active)");
result = GCodeResult::error;
break;
}

if ((reprap.GetPrintMonitor().IsPrinting() || IsPausing() || IsPaused() || IsResuming())
&& toolState == ToolState::active && toolHeater >= 0) {
reply.printf("The state of active tool %d can't be changed while printing, pausing or resuming",
tool->Number());
result = GCodeResult::error;
break;
}

switch(state) {
case Heat::HS_off:
heat.SwitchOff(currentHeater);
break;
case Heat::HS_standby:
heat.Standby(currentHeater, nullptr);
break;
case Heat::HS_active:
heat.Activate(currentHeater);
break;
}
}

if (!seen)
{
reply.printf("Heater %d State: '%s', Active temp: %.1f" DEGREE_SYMBOL \
", Standby temp: %.1f" DEGREE_SYMBOL ", Current temp: %.1f" DEGREE_SYMBOL,
currentHeater, heat.HeaterStatusToString(currentHeaterStatus),
(double)heat.GetActiveTemperature(currentHeater),
(double)heat.GetStandbyTemperature(currentHeater),
(double)heat.GetTemperature(currentHeater));
}
}
break;

case 350: // Set/report microstepping
{
bool interp = (gb.Seen('I') && gb.GetIValue() > 0);
Expand Down
18 changes: 18 additions & 0 deletions src/Heating/Heat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,4 +743,22 @@ bool Heat::WriteBedAndChamberTempSettings(FileStore *f) const
return (buf.strlen() == 0) || f->Write(buf.c_str());
}

const char *Heat::HeaterStatusToString(HeaterStatus status) const
{
switch(status) {
case HeaterStatus::HS_off:
return "Off";
case HeaterStatus::HS_standby:
return "Standby";
case HeaterStatus::HS_active:
return "Active";
case HeaterStatus::HS_fault:
return "Fault";
case HeaterStatus::HS_tuning:
return "Tuning";
default:
return "unknown";
}
}

// End
2 changes: 2 additions & 0 deletions src/Heating/Heat.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class Heat

void SuspendHeaters(bool sus); // Suspend the heaters to conserve power

const char *HeaterStatusToString(HeaterStatus status) const;

private:
Heat(const Heat&); // Private copy constructor to prevent copying

Expand Down
13 changes: 13 additions & 0 deletions src/Tools/Tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,17 @@ void Tool::IterateHeaters(std::function<void(int)> f) const
}
}

int Tool::GetHeaterAssignedToTool(int8_t global_heater) const
{
for (size_t i = 0; i < heaterCount; i++)
{
if (heaters[i] == global_heater)
{
return i;
}
}

return -1;
}

// End
2 changes: 2 additions & 0 deletions src/Tools/Tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class Tool
void IterateExtruders(std::function<void(unsigned int)> f) const;
void IterateHeaters(std::function<void(int)> f) const;

int GetHeaterAssignedToTool(int8_t global_heater) const;

friend class RepRap;

protected:
Expand Down