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

Devt #1034

Merged
merged 21 commits into from
Oct 1, 2023
Merged

Devt #1034

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
52 changes: 52 additions & 0 deletions FluidNC/esp32/StartupLog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2023 - Mitch Bradley
// Use of this source code is governed by a GPLv3 license that can be found in the LICENSE file.

#include "src/StartupLog.h"
#include "src/Protocol.h" // send_line()
#include <sstream>

// The startup log is stored in RTC RAM that is preserved across
// resets. That lets us show the previous startup log if the
// system panics and resets.

// The size is limited by the size of RTC RAM minus system usage thereof
static const size_t _maxlen = 7000;
static RTC_NOINIT_ATTR char _messages[_maxlen];
static RTC_NOINIT_ATTR size_t _len;
static bool _paniced;

void StartupLog::init() {
if (esp_reset_reason() == ESP_RST_PANIC) {
_paniced = true;
} else {
_paniced = false;
_len = 0;
}
}
size_t StartupLog::write(uint8_t data) {
if (_paniced || _len >= _maxlen) {
return 0;
}
_messages[_len++] = (char)data;
return 1;
}
void StartupLog::dump(Channel& out) {
if (_paniced) {
log_error_to(out, "Showing startup log from previous panic");
}
for (size_t i = 0; i < _len;) {
std::string line;
while (i < _len) {
char c = _messages[i++];
if (c == '\n') {
break;
}
line += c;
}
log_to(out, line);
}
}

StartupLog::~StartupLog() {}

StartupLog startupLog;
10 changes: 7 additions & 3 deletions FluidNC/src/CoolantControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ void CoolantControl::write(CoolantState state) {
bool pinState = state.Mist;
_mist.synchronousWrite(pinState);
}

_previous_state = state;
}

// Directly called by coolant_init(), coolant_set_state(), which can be at
Expand All @@ -68,11 +70,13 @@ void CoolantControl::stop() {
// parser program end, and g-code parser CoolantControl::sync().

void CoolantControl::set_state(CoolantState state) {
if (sys.abort) {
return; // Block during abort.
if (sys.abort || (_previous_state.Mist == state.Mist && _previous_state.Flood == state.Flood)) {
return; // Block during abort or if no change
}
write(state);
delay_msec(_delay_ms, DwellMode::SysSuspend);

if (state.Mist || state.Flood) // ignore delay on turn off
delay_msec(_delay_ms, DwellMode::SysSuspend);
}

void CoolantControl::off() {
Expand Down
2 changes: 2 additions & 0 deletions FluidNC/src/CoolantControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class CoolantControl : public Configuration::Configurable {

uint32_t _delay_ms = 0;

CoolantState _previous_state = {};

void write(CoolantState state);

public:
Expand Down
1 change: 0 additions & 1 deletion FluidNC/src/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ std::map<Error, const char*> ErrorNames = {
{ Error::AuthenticationFailed, "Authentication failed!" },
{ Error::Eol, "End of line" },
{ Error::Eof, "End of file" },
{ Error::Reset, "System Reset" },
{ Error::AnotherInterfaceBusy, "Another interface is busy" },
{ Error::BadPinSpecification, "Bad Pin Specification" },
{ Error::JogCancelled, "Jog Cancelled" },
Expand Down
1 change: 0 additions & 1 deletion FluidNC/src/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ enum class Error : uint8_t {
AuthenticationFailed = 110,
Eol = 111,
Eof = 112, // Not necessarily an error
Reset = 113,
AnotherInterfaceBusy = 120,
JogCancelled = 130,
BadPinSpecification = 150,
Expand Down
3 changes: 0 additions & 3 deletions FluidNC/src/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1620,9 +1620,6 @@ Error gc_execute_line(char* line) {
// As far as the parser is concerned, the position is now == target. In reality the
// motion control system might still be processing the action and the real tool position
// in any intermediate location.
if (sys.abort) {
return Error::Reset;
}
if (gc_update_pos == GCUpdatePos::Target) {
copyAxes(gc_state.position, gc_block.values.xyz);
} else if (gc_update_pos == GCUpdatePos::System) {
Expand Down
1 change: 1 addition & 0 deletions FluidNC/src/Machine/MachineConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace Machine {
handler.section("user_outputs", _userOutputs);

handler.section("oled", _oled);
handler.section("status_outputs", _stat_out);

Spindles::SpindleFactory::factory(handler, _spindles);

Expand Down
2 changes: 2 additions & 0 deletions FluidNC/src/Machine/MachineConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "../Stepper.h"
#include "../Config.h"
#include "../OLED.h"
#include "../Status_outputs.h"
#include "Axes.h"
#include "SPIBus.h"
#include "I2CBus.h"
Expand Down Expand Up @@ -74,6 +75,7 @@ namespace Machine {
Start* _start = nullptr;
Parking* _parking = nullptr;
OLED* _oled = nullptr;
Status_Outputs* _stat_out = nullptr;
Spindles::SpindleList _spindles;

UartChannel* _uart_channels[MAX_N_UARTS] = { nullptr };
Expand Down
24 changes: 11 additions & 13 deletions FluidNC/src/Machine/Macros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
#include "src/System.h" // sys
#include "src/Machine/MachineConfig.h" // config

Macro::Macro(const char* name) : _name(name) {}

void MacroEvent::run(void* arg) {
config->_macros->_macro[_num].run();
int n = int(arg);
if (sys.state != State::Idle) {
log_error("Macro can only be used in idle state");
return;
}
config->_macros->_macro[n].run();
}

Macro Macros::_startup_line[n_startup_lines] = { "startup_line0", "startup_line1" };
Macro Macros::_macro[n_macros] = { "macro0", "macro1", "macro2", "macro3", "macro4" };
Macro Macros::_after_homing { "after_homing" };
Macro Macros::_after_reset { "after_reset" };
Macro Macros::_after_unlock { "after_unlock" };
Macro Macros::_startup_line[n_startup_lines] = { { "startup_line0" }, { "startup_line1" } };
Macro Macros::_macro[n_macros] = { { "macro0" }, { "macro1" }, { "macro2" }, { "macro3" } };
Macro Macros::_after_homing = { "after_homing" };
Macro Macros::_after_reset = { "after_reset" };
Macro Macros::_after_unlock = { "after_unlock" };

MacroEvent macro0Event { 0 };
MacroEvent macro1Event { 1 };
Expand Down Expand Up @@ -51,11 +54,6 @@ Cmd findOverride(std::string name) {
}

bool Macro::run() {
if (sys.state != State::Idle) {
log_error("Macro can only be used in idle state");
return false;
}

const std::string& s = _gcode;
if (_gcode == "") {
return true;
Expand Down
24 changes: 12 additions & 12 deletions FluidNC/src/Machine/Macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ namespace Machine {
class Macro {
public:
std::string _gcode;
const char* _name;
Macro(const char* name);
std::string _name;
Macro(const char* name) : _name(name) {}
bool run();
};

class Macros : public Configuration::Configurable {
public:
static const int n_startup_lines = 2;
static const int n_macros = 5;
static const int n_macros = 4;

static Macro _macro[n_macros];
static Macro _startup_line[n_startup_lines];
Expand All @@ -50,15 +50,15 @@ namespace Machine {
// TODO: We could validate the startup lines

void group(Configuration::HandlerBase& handler) override {
handler.item(_startup_line[0]._name, _startup_line[0]._gcode);
handler.item(_startup_line[1]._name, _startup_line[1]._gcode);
handler.item(_macro[0]._name, _macro[0]._gcode);
handler.item(_macro[1]._name, _macro[1]._gcode);
handler.item(_macro[2]._name, _macro[2]._gcode);
handler.item(_macro[3]._name, _macro[3]._gcode);
handler.item(_after_homing._name, _after_homing._gcode);
handler.item(_after_reset._name, _after_reset._gcode);
handler.item(_after_unlock._name, _after_unlock._gcode);
handler.item(_startup_line[0]._name.c_str(), _startup_line[0]._gcode);
handler.item(_startup_line[1]._name.c_str(), _startup_line[1]._gcode);
handler.item(_macro[0]._name.c_str(), _macro[0]._gcode);
handler.item(_macro[1]._name.c_str(), _macro[1]._gcode);
handler.item(_macro[2]._name.c_str(), _macro[2]._gcode);
handler.item(_macro[3]._name.c_str(), _macro[3]._gcode);
handler.item(_after_homing._name.c_str(), _after_homing._gcode);
handler.item(_after_reset._name.c_str(), _after_reset._gcode);
handler.item(_after_unlock._name.c_str(), _after_unlock._gcode);
}

~Macros() {}
Expand Down
8 changes: 7 additions & 1 deletion FluidNC/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ void setup() {
uartInit(); // Setup serial port
Uart0.println(); // create some white space after ESP32 boot info

StartupLog::init();

// Setup input polling loop after loading the configuration,
// because the polling may depend on the config
allChannels.init();
Expand All @@ -47,7 +49,7 @@ void setup() {
// Load settings from non-volatile storage
settings_init(); // requires config

log_info("FluidNC " << git_info);
log_info("FluidNC " << git_info << " " << git_url);
log_info("Compiled with ESP32 SDK:" << esp_get_idf_version());

if (localfs_mount()) {
Expand Down Expand Up @@ -96,6 +98,10 @@ void setup() {
config->_oled->init();
}

if (config->_stat_out) {
config->_stat_out->init();
}

config->_stepping->init(); // Configure stepper interrupt timers

plan_init();
Expand Down
5 changes: 1 addition & 4 deletions FluidNC/src/Motors/TrinamicUartDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ namespace MotorDrivers {

void TrinamicUartDriver::init() {
_uart = config->_uarts[_uart_num];
if (!_uart) {
log_error("TMC Driver missing uart" << _uart_num << " section");
return;
}
Assert(_uart, "TMC Driver missing uart%d section", _uart_num);

_cs_pin.setAttr(Pin::Attr::Output);
}
Expand Down
5 changes: 1 addition & 4 deletions FluidNC/src/Pin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "Pins/I2SOPinDetail.h"
#include "Pins/ErrorPinDetail.h"
#include "string_util.h"
#include <stdio.h> // snprintf()

Pins::PinDetail* Pin::undefinedPin = new Pins::VoidPinDetail();
Pins::PinDetail* Pin::errorPin = new Pins::ErrorPinDetail("unknown");
Expand Down Expand Up @@ -113,9 +112,7 @@ Pin Pin::create(std::string_view str) {
}
} catch (const AssertionFailed& ex) { // We shouldn't get here under normal circumstances.
log_error("ERR: " << str << " - " << ex.what());
char buf[255];
snprintf(buf, 255, "ERR: %s - %s", str, ex.what());
Assert(false, buf);
Assert(false, "");
// return Pin(new Pins::ErrorPinDetail(str.str()));
}
}
Expand Down
27 changes: 7 additions & 20 deletions FluidNC/src/Pins/DebugPinDetail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,15 @@

#include "../UartChannel.h"
#include <esp32-hal.h> // millis()
#include <cstdio> // vsnprintf
#include <cstdarg>

namespace Pins {
inline void WriteSerial(const char* format, ...) {
char buf[50];
va_list arg;
va_list copy;
va_start(arg, format);
va_copy(copy, arg);
size_t len = vsnprintf(buf, 50, format, arg);
va_end(copy);
log_msg_to(Uart0, buf);
va_end(arg);
}

// I/O:
void DebugPinDetail::write(int high) {
if (high != int(_isHigh)) {
_isHigh = bool(high);
if (shouldEvent()) {
WriteSerial("Write %s < %d", toString(), high);
log_msg_to(Uart0, "Write " << toString() << " < " << high);
}
}
_implementation->write(high);
Expand All @@ -35,7 +22,7 @@ namespace Pins {
int DebugPinDetail::read() {
auto result = _implementation->read();
if (shouldEvent()) {
WriteSerial("Read %s > %d", toString(), result);
log_msg_to(Uart0, "Read " << toString() << " > " << result);
}
return result;
}
Expand Down Expand Up @@ -63,10 +50,10 @@ namespace Pins {
if (value.has(PinAttributes::InitialOn)) {
buf[n++] = '+';
}
buf[n++] = 0;
buf[n++] = '\0';

if (shouldEvent()) {
WriteSerial("Set pin attr %s = %s", toString(), buf);
log_msg_to(Uart0, "Set pin attr " << toString() << " = " << buf);
}
_implementation->setAttr(value);
}
Expand All @@ -76,7 +63,7 @@ namespace Pins {
void DebugPinDetail::CallbackHandler::handle(void* arg) {
auto handler = static_cast<CallbackHandler*>(arg);
if (handler->_myPin->shouldEvent()) {
WriteSerial("Received ISR on %s", handler->_myPin->toString());
log_msg_to(Uart0, "Received ISR on " << handler->_myPin->toString());
}
handler->callback(handler->argument);
}
Expand All @@ -88,7 +75,7 @@ namespace Pins {
_isrHandler.callback = callback;

if (shouldEvent()) {
WriteSerial("Attaching interrupt to pin %s, mode %d", toString(), mode);
log_msg_to(Uart0, "Attaching interrupt to pin " << toString() << ", mode " << mode);
}
_implementation->attachInterrupt(_isrHandler.handle, &_isrHandler, mode);
}
Expand All @@ -109,7 +96,7 @@ namespace Pins {
} else if (_eventCount == 10) {
_lastEvent = time;
++_eventCount;
WriteSerial("Suppressing events...");
log_msg_to(Uart0, "Suppressing events...");
return false;
} else {
_lastEvent = time;
Expand Down
4 changes: 2 additions & 2 deletions FluidNC/src/Pins/GPIOPinDetail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace Pins {
PinCapabilities::UART;

case 5:
case 9:
case 10:
case 16:
case 17:
case 18:
Expand Down Expand Up @@ -62,8 +64,6 @@ namespace Pins {
case 6: // SPI flash integrated
case 7:
case 8:
case 9:
case 10:
case 11:
return PinCapabilities::Reserved;

Expand Down
2 changes: 1 addition & 1 deletion FluidNC/src/ProcessSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ static Error showChannelInfo(const char* value, WebUI::AuthenticationLevel auth_
}

static Error showStartupLog(const char* value, WebUI::AuthenticationLevel auth_level, Channel& out) {
startupLog.dump(out);
StartupLog::dump(out);
return Error::Ok;
}

Expand Down
1 change: 1 addition & 0 deletions FluidNC/src/Report.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const char* state_name();

extern const char* grbl_version;
extern const char* git_info;
extern const char* git_url;

// Callout to custom code
void display_init();
Expand Down
Loading