Skip to content

Commit

Permalink
Rebase merge and add ready() method
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchBradley committed Nov 19, 2023
1 parent b1f5344 commit 7cf191e
Show file tree
Hide file tree
Showing 22 changed files with 259 additions and 194 deletions.
96 changes: 38 additions & 58 deletions FluidNC/src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,19 @@ void Channel::autoReportGCodeState() {
}
void Channel::autoReport() {
if (_reportInterval) {
auto limitState = limits_get_state();
auto probeState = config->_probe->get_state();
if (_reportWco || sys.state != _lastState || limitState != _lastLimits || probeState != _lastProbe ||
if (probeState != _lastProbe) {
report_recompute_pin_string();
}
if (_reportWco || sys.state != _lastState || probeState != _lastProbe || _lastPinString != report_pin_string ||
(motionState() && (int32_t(xTaskGetTickCount()) - _nextReportTime) >= 0)) {
if (_reportWco) {
report_wco_counter = 0;
}
_reportWco = false;
_lastState = sys.state;
_lastLimits = limitState;
_lastProbe = probeState;
_reportWco = false;
_lastState = sys.state;
_lastProbe = probeState;
_lastPinString = report_pin_string;

_nextReportTime = xTaskGetTickCount() + _reportInterval;
report_realtime_status(*this);
Expand Down Expand Up @@ -140,20 +142,12 @@ Channel* Channel::pollLine(char* line) {
if (ch < 0) {
break;
}
if (_last_rt_cmd == Cmd::PinLow) {
try {
auto event_pin = _events.at(ch);
_pin_values[ch] = false;
event_pin->trigger(false);
} catch (std::exception& ex) {}
_last_rt_cmd = Cmd::None;
continue;
}
if (_last_rt_cmd == Cmd::PinHigh) {
if (_last_rt_cmd == Cmd::PinLow || _last_rt_cmd == Cmd::PinHigh) {
bool isHigh = _last_rt_cmd == Cmd::PinHigh;
try {
auto event_pin = _events.at(ch);
_pin_values[ch] = true;
event_pin->trigger(true);
auto event_pin = _events.at(ch);
*_pin_values[ch] = isHigh;
event_pin->trigger(isHigh);
} catch (std::exception& ex) {}
_last_rt_cmd = Cmd::None;
continue;
Expand All @@ -170,12 +164,13 @@ Channel* Channel::pollLine(char* line) {
}

if (realtimeOkay(ch)) {
if (is_extended_realtime_command(ch)) {
_last_rt_cmd = static_cast<Cmd>(ch);
continue;
}
if (is_realtime_command(ch)) {
execute_realtime_command(static_cast<Cmd>(ch), *this);
auto cmd = static_cast<Cmd>(ch);
if (cmd == Cmd::PinLow || cmd == Cmd::PinHigh) {
_last_rt_cmd = cmd;
} else {
execute_realtime_command(cmd, *this);
}
continue;
}
}
Expand All @@ -193,34 +188,29 @@ Channel* Channel::pollLine(char* line) {
return nullptr;
}

void Channel::setAttr(int index, Pins::PinAttributes attr) {
_pin_attributes[index] = _pin_attributes[index] | attr;
void Channel::setAttr(int index, bool* value, const std::string& attrString) {
if (value) {
_pin_values[index] = value;
}
while (_ackwait) {
pollLine(NULL);
delay_ms(10);
}
log_msg_to(*this, attrString);
_ackwait = true;
log_debug(attrString);
}
Pins::PinAttributes Channel::getAttr(int index) const {
try {
return _pin_attributes.at(index);
} catch (std::exception& ex) { return Pins::PinAttributes::None; }

void Channel::out(const std::string& s) {
log_msg_to(*this, s);
// _channel->_ackwait = true;
log_debug(s);
}

void Channel::out(int index, int value) {
if (value == _pin_values[index]) {
return;
void Channel::ready() {
if (!_pin_values.empty()) {
out("GET: io.*");
}
_pin_values[index] = value;
std::string s = "[MSG:SET: io.";
s += std::to_string(index);
s += "=";
s += std::to_string(value);
s += "]";

if (!sendCtrlCmd(s.c_str(), true)) {// send it out
// do something about the NAK
}

//log_info(s.c_str());
}
int Channel::in(int index) {
return _pin_values[index];
}

void Channel::registerEvent(uint8_t code, EventPin* obj) {
Expand All @@ -242,13 +232,3 @@ void Channel::ack(Error status) {
msg << static_cast<int>(status);
}
}

// send a command and optionally wait for an ACK
bool Channel::sendCtrlCmd(std::string cmd, bool need_Ack) { // return false is command was NAK'd
println(cmd.c_str());
if (need_Ack) {
// need some code here
return true;
}
return true;
}
59 changes: 30 additions & 29 deletions FluidNC/src/Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,48 @@ class Channel : public Stream {
static const int maxLine = 255;

protected:
const char* _name;
std::string _name;
char _line[maxLine];
size_t _linelen;
bool _addCR = false;
char _lastWasCR = false;
bool _controller = false; // the device is a pin extender
bool _addCR = false;
char _lastWasCR = false;

std::queue<uint8_t> _queue;

uint32_t _reportInterval = 0;
int32_t _nextReportTime = 0;

gc_modal_t _lastModal;
uint8_t _lastTool;
float _lastSpindleSpeed;
float _lastFeedRate;
State _lastState;
MotorMask _lastLimits;
bool _lastProbe;
gc_modal_t _lastModal;
uint8_t _lastTool;
float _lastSpindleSpeed;
float _lastFeedRate;
State _lastState;
MotorMask _lastLimits;
bool _lastProbe;
std::string _lastPinString;

bool _reportWco = true;
CoordIndex _reportNgc = CoordIndex::End;

Cmd _last_rt_cmd;

std::map<int, EventPin*> _events;
std::map<int, int> _pin_values;
std::map<int, Pins::PinAttributes> _pin_attributes;
std::map<int, EventPin*> _events;
std::map<int, bool*> _pin_values;

public:
Channel(const char* name, bool addCR = false) : _name(name), _linelen(0), _addCR(addCR) {}
Channel(const char* name, int num, bool addCR = false) {
_name = name;
_name += std::to_string(num), _linelen = 0, _addCR = addCR;
}
virtual ~Channel() = default;

virtual void handle() {};
virtual Channel* pollLine(char* line);
virtual void ack(Error status);
const char* name() { return _name; }
bool _ackwait = false;

virtual void handle() {};
virtual Channel* pollLine(char* line);
virtual void ack(Error status);
const std::string& name() { return _name; }

// rx_buffer_available() is the number of bytes that can be sent without overflowing
// a reception buffer, even if the system is busy. Channels that can handle external
Expand Down Expand Up @@ -113,22 +118,18 @@ class Channel : public Stream {
void notifyWco() { _reportWco = true; }
void notifyNgc(CoordIndex coord) { _reportNgc = coord; }

int peek() override { return -1; }
int read() override { return -1; }
int available() override { return _queue.size(); }
bool isController() { return _controller; }
bool sendCtrlCmd(std::string s, bool need_Ack);
int peek() override { return -1; }
int read() override { return -1; }
int available() override { return _queue.size(); }

uint32_t setReportInterval(uint32_t ms);
uint32_t getReportInterval() { return _reportInterval; }
virtual void autoReport();
void autoReportGCodeState();

// Pin extender functions
virtual void out(int index, int value);
virtual int in(int index);
virtual void setAttr(int index, Pins::PinAttributes value);
virtual Pins::PinAttributes getAttr(int index) const;

virtual void registerEvent(uint8_t code, EventPin* obj);
void out(const std::string& s);
void setAttr(int index, bool* valuep, const std::string& s);
void ready();
void registerEvent(uint8_t code, EventPin* obj);
};
1 change: 0 additions & 1 deletion FluidNC/src/HashFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ void HashFS::rehash_file(const std::filesystem::path& path) {
delete_file(path);
} else {
localFsHashes[path.filename()] = hash;
log_debug(path.filename() << " hash " << hash);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions FluidNC/src/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ extern bool atMsgLevel(MsgLevel level);
// Note: these '{'..'}' scopes are here for a reason: the destructor should flush.

// #define log_bare(prefix, x) { LogStream ss(prefix); ss << x; }
#define log_msg(x) { LogStream ss("[MSG: "); ss << x; }
#define log_msg(x) { LogStream ss("[MSG:"); ss << x; }
#define log_verbose(x) if (atMsgLevel(MsgLevelVerbose)) { LogStream ss("[MSG:VRB: "); ss << x; }
#define log_debug(x) if (atMsgLevel(MsgLevelDebug)) { LogStream ss("[MSG:DBG: "); ss << x; }
#define log_info(x) if (atMsgLevel(MsgLevelInfo)) { LogStream ss("[MSG:INFO: "); ss << x; }
#define log_warn(x) if (atMsgLevel(MsgLevelWarning)) { LogStream ss("[MSG:WARN: "); ss << x; }
#define log_error(x) if (atMsgLevel(MsgLevelError)) { LogStream ss("[MSG:ERR: "); ss << x; }
#define log_fatal(x) { LogStream ss("[MSG:FATAL: "); ss << x; Assert(false, "A fatal error occurred."); }

#define log_msg_to(out, x) { LogStream ss(out, "[MSG: "); ss << x; }
#define log_msg_to(out, x) { LogStream ss(out, "[MSG:"); ss << x; }
#define log_verbose_to(out, x) if (atMsgLevel(MsgLevelVerbose)) { LogStream ss(out, "[MSG:VRB: "); ss << x; }
#define log_debug_to(out, x) if (atMsgLevel(MsgLevelDebug)) { LogStream ss(out, "[MSG:DBG: "); ss << x; }
#define log_info_to(out, x) if (atMsgLevel(MsgLevelInfo)) { LogStream ss(out, "[MSG:INFO: "); ss << x; }
Expand Down
3 changes: 3 additions & 0 deletions FluidNC/src/Machine/EventPin.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include "EventPin.h"
#include "src/Report.h"

#include "src/Protocol.h" // protocol_send_event

EventPin::EventPin(Event* event, const char* legend) : _event(event), _legend(legend) {}

void EventPin::trigger(bool active) {
update(active);
log_debug(_legend << " " << active);
if (active) {
protocol_send_event(_event, this);
}
report_recompute_pin_string();
}
1 change: 0 additions & 1 deletion FluidNC/src/Machine/LimitPin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ namespace Machine {
}

void LimitPin::update(bool value) {
log_debug(_legend << " " << value);
if (value) {
if (Homing::approach() || (sys.state != State::Homing && _pHardLimits)) {
_pLimited = value;
Expand Down
4 changes: 2 additions & 2 deletions FluidNC/src/Machine/MachineConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ namespace Machine {
handler.section("uart1", _uarts[1], 1);
handler.section("uart2", _uarts[2], 2);

handler.section("uart_channel1", _uart_channels[1]);
handler.section("uart_channel2", _uart_channels[2]);
handler.section("uart_channel1", _uart_channels[1], 1);
handler.section("uart_channel2", _uart_channels[2], 2);

handler.section("i2so", _i2so);

Expand Down
3 changes: 0 additions & 3 deletions FluidNC/src/Pin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ const char* Pin::parse(std::string_view pin_str, Pins::PinDetail*& pinImplementa
return nullptr;
}
if (string_util::equal_ignore_case(prefix, "i2so")) {
if (config->_i2so == nullptr) {
return "i2so: section must be defined before using i2so pins";
}
pinImplementation = new Pins::I2SOPinDetail(static_cast<pinnum_t>(pin_number), parser);
return nullptr;
}
Expand Down
Loading

0 comments on commit 7cf191e

Please sign in to comment.