Skip to content

Commit 0190b5c

Browse files
committed
Remove m_machineState in favor of m_lastStatusReport, MachineState to DeviceContext
1 parent d65c126 commit 0190b5c

8 files changed

Lines changed: 239 additions & 240 deletions

File tree

src/gpilot/core/communicator/communicator.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,8 @@ void Communicator::deinit()
151151

152152
void Communicator::resetStateVariables()
153153
{
154-
m_machineState = MachineState::Unknown;
155-
if (m_posTracker) m_posTracker->reset();
156154
m_deviceContext.reset();
155+
if (m_posTracker) m_posTracker->reset();
157156
}
158157

159158
// Called by CommandBuffer after a command response is fully processed.
@@ -348,14 +347,6 @@ AbstractConnection *Communicator::connection()
348347
return m_connection;
349348
}
350349

351-
void Communicator::setMachineStateAndEmitSignal(MachineState state)
352-
{
353-
if (m_machineState != state) {
354-
m_machineState = state;
355-
emit machineStateChanged(state);
356-
}
357-
}
358-
359350
bool Communicator::isMachineConfigurationReady() const
360351
{
361352
return m_deviceContext.hasPhysicalConfig();

src/gpilot/core/communicator/communicator.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "positiontracker.h"
1111
#include "core/state_behavior/abstractstatebehavior.h"
1212
#include "statebehaviormanager.h"
13-
#include "machinestatus.h"
1413
#include "overrides.h"
1514
#include "commandbuffer.h"
1615
#include "commandscanner.h"
@@ -56,7 +55,7 @@ class Communicator : public QObject
5655
AbstractConnection* connection();
5756
// void stopUpdatingState();
5857
// void startUpdatingState(int interval = -1);
59-
const MachineState& machineState() const { return m_machineState; }
58+
MachineState machineState() const { return m_deviceContext.machineState(); }
6059
DeviceContext& deviceContext() { return m_deviceContext; }
6160
const DeviceContext& deviceContext() const { return m_deviceContext; }
6261
void setMachineType(MachineType type) { m_deviceContext.setMachineType(type); }
@@ -88,8 +87,6 @@ class Communicator : public QObject
8887
CommandScanner *m_commandScanner = nullptr;
8988
CommunicatorApi *m_comApi;
9089

91-
// States
92-
MachineState m_machineState;
9390
StateBehaviorManager m_sbManager;
9491

9592
QTimer m_startTime;
@@ -121,7 +118,6 @@ class Communicator : public QObject
121118
int m_lastAlarmCode = 0;
122119

123120
bool execute(AbstractStateBehavior *statebehavior, bool force = false);
124-
void setMachineStateAndEmitSignal(MachineState);
125121
void processOffsetsVars(QStringList response);
126122
static bool dataIsFloating(QString data);
127123
void processStatus(QString line);

src/gpilot/core/communicator/communicator_processing_response.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,13 @@ void Communicator::processStatus(QString line)
159159
emit floodStateReceived(report->floodEnabled);
160160
}
161161

162+
m_deviceContext.setLastStatusReport(*report);
163+
162164
emit machineStatusReportReceived(*report);
163165
emit workPosChanged(m_posTracker->workPos());
164166

165167
m_posTracker->processNewToolPosition(
166-
m_machineState == MachineState::Check,
168+
m_deviceContext.machineState() == MachineState::Check,
167169
true
168170
);
169171

@@ -185,9 +187,9 @@ void Communicator::processMachineState(MachineState state)
185187

186188
// Update status
187189
AbstractStateBehavior* sb = m_sbManager.current();
188-
if (state != m_machineState) {
189-
// emit deviceStateChanged(state);
190+
if (state != m_deviceContext.machineState()) {
190191
sb->onMachineStateChanged(state);
192+
emit machineStateChanged(state);
191193
}
192194
sb->onMachineState(state);
193195

@@ -254,8 +256,6 @@ void Communicator::processMachineState(MachineState state)
254256
// }
255257
// }
256258

257-
// Store device state
258-
setMachineStateAndEmitSignal(state);
259259
}
260260

261261
void Communicator::processDeviceConfiguration(QStringList response)
@@ -739,8 +739,6 @@ void Communicator::processWelcomeMessageDetected(QString message)
739739
return;
740740
emit welcomeMessageReceived(message);
741741

742-
setMachineStateAndEmitSignal(MachineState::Unknown);
743-
744742
// m_streamer->reset();
745743
//m_form->fileCommandIndex() = 0;
746744

Lines changed: 1 addition & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -1,213 +1 @@
1-
// This file is a part of "G-Pilot GCode Sender" application.
2-
// Copyright 2015-2021 Hayrullin Denis Ravilevich
3-
// Copyright 2024 BTS
4-
5-
#ifndef MACHINESTATUS_H
6-
#define MACHINESTATUS_H
7-
8-
#include <QVector3D>
9-
#include <QString>
10-
#include <QDebug>
11-
#include "core/globals.h"
12-
13-
struct PinState
14-
{
15-
bool limitX = false;
16-
bool limitY = false;
17-
bool limitZ = false;
18-
bool limitA = false;
19-
bool limitB = false;
20-
bool probe = false;
21-
bool door = false;
22-
bool feedHold = false;
23-
bool reset = false;
24-
bool cycleStart = false;
25-
26-
QString raw;
27-
28-
static PinState parse(const QString& s)
29-
{
30-
PinState p;
31-
p.raw = s;
32-
for (QChar c : s) {
33-
switch (c.toLatin1()) {
34-
case 'X': p.limitX = true; break;
35-
case 'Y': p.limitY = true; break;
36-
case 'Z': p.limitZ = true; break;
37-
case 'A': p.limitA = true; break;
38-
case 'B': p.limitB = true; break;
39-
case 'P': p.probe = true; break;
40-
case 'D': p.door = true; break;
41-
case 'H': p.feedHold = true; break;
42-
case 'R': p.reset = true; break;
43-
case 'S': p.cycleStart = true; break;
44-
}
45-
}
46-
47-
return p;
48-
}
49-
50-
const QString& toString() const
51-
{
52-
return raw;
53-
}
54-
};
55-
56-
struct MachineStatusReport
57-
{
58-
// Main state
59-
MachineState state = MachineState::Unknown;
60-
61-
// Position data
62-
QVector3D machinePos;
63-
QVector3D workPos;
64-
QVector3D workOffset;
65-
bool hasMachinePos = false;
66-
bool hasWorkPos = false;
67-
bool hasWorkOffset = false;
68-
69-
// Feed and spindle
70-
int feedRate = 0;
71-
int spindleSpeed = 0;
72-
bool hasFeedSpindleSpeed = false;
73-
74-
// Overrides
75-
int feedOverride = 100;
76-
int rapidOverride = 100;
77-
int spindleOverride = 100;
78-
bool hasOverrides = false;
79-
80-
// Buffer status
81-
int bufferAvailable = 0;
82-
int bufferSize = 0;
83-
bool hasBufferStatus = false;
84-
85-
// Pin states
86-
PinState pinStates;
87-
bool hasPinStates = false;
88-
89-
// Accessory states (spindle, flood, mist)
90-
bool spindleEnabled = false;
91-
bool spindleCW = false;
92-
bool floodEnabled = false;
93-
bool mistEnabled = false;
94-
bool hasAccessoryState = false;
95-
96-
// Raw line for debugging
97-
QString rawLine;
98-
99-
QString toMarkdown() const
100-
{
101-
static const QMap<MachineState, QString> stateNames = {
102-
{ MachineState::Unknown, "Unknown" },
103-
{ MachineState::Idle, "Idle" },
104-
{ MachineState::Alarm, "Alarm" },
105-
{ MachineState::Run, "Run" },
106-
{ MachineState::Home, "Home" },
107-
{ MachineState::Hold0, "Hold:0" },
108-
{ MachineState::Hold1, "Hold:1" },
109-
{ MachineState::Queue, "Queue" },
110-
{ MachineState::Check, "Check" },
111-
{ MachineState::Door0, "Door" },
112-
{ MachineState::Door1, "Door (open)" },
113-
{ MachineState::Door2, "Door (hold)" },
114-
{ MachineState::Door3, "Door (resume)" },
115-
{ MachineState::Jog, "Jog" },
116-
{ MachineState::Sleep, "Sleep" },
117-
};
118-
119-
QStringList lines;
120-
121-
// Line 1: state
122-
lines << QString("**%1**").arg(stateNames.value(state, "Unknown"));
123-
124-
// Line 2: positions
125-
QStringList pos;
126-
if (hasWorkPos) {
127-
pos << QString("*WPos:* %1, %2, %3")
128-
.arg(workPos.x(), 0, 'f', 3)
129-
.arg(workPos.y(), 0, 'f', 3)
130-
.arg(workPos.z(), 0, 'f', 3);
131-
}
132-
if (hasMachinePos) {
133-
pos << QString("*MPos:* %1, %2, %3")
134-
.arg(machinePos.x(), 0, 'f', 3)
135-
.arg(machinePos.y(), 0, 'f', 3)
136-
.arg(machinePos.z(), 0, 'f', 3);
137-
}
138-
if (!pos.isEmpty()) { lines << pos.join(" "); }
139-
else { lines << "*No position data*"; }
140-
141-
// Line 3: feed/spindle + accessories
142-
QStringList motion;
143-
if (hasFeedSpindleSpeed) {
144-
motion << QString("*F:* %1 *S:* %2").arg(feedRate).arg(spindleSpeed);
145-
}
146-
if (hasAccessoryState) {
147-
if (spindleEnabled) { motion << (spindleCW ? "Spindle CW" : "Spindle CCW"); }
148-
if (floodEnabled) { motion << "Flood"; }
149-
if (mistEnabled) { motion << "Mist"; }
150-
}
151-
if (hasBufferStatus) {
152-
motion << QString("*Bf:* %1/%2").arg(bufferAvailable).arg(bufferSize);
153-
}
154-
if (!motion.isEmpty()) { lines << motion.join(" "); }
155-
else { lines << "*No feed/spindle data*"; }
156-
157-
// Line 4: overrides + pins
158-
QStringList extras;
159-
if (hasPinStates && !pinStates.raw.isEmpty()) {
160-
extras << QString("*Pins:* `%1`").arg(pinStates.toString());
161-
} else {
162-
extras << QString("*Pins:* none");
163-
}
164-
if (hasOverrides) {
165-
extras << QString("*Ov:* %1% / %2% / %3%").arg(feedOverride).arg(rapidOverride).arg(spindleOverride);
166-
}
167-
if (!extras.isEmpty()) { lines << extras.join(" "); }
168-
else { lines << "*No overrides/pin data*"; }
169-
170-
return lines.join("<br/>");
171-
}
172-
};
173-
174-
inline QDebug operator<<(QDebug debug, const MachineStatusReport &status)
175-
{
176-
QDebugStateSaver saver(debug);
177-
debug.nospace() << "MachineStatus(";
178-
debug << "state=" << static_cast<int>(status.state);
179-
180-
if (status.hasMachinePos) {
181-
debug << ", MPos=" << status.machinePos;
182-
}
183-
if (status.hasWorkPos) {
184-
debug << ", WPos=" << status.workPos;
185-
}
186-
if (status.hasWorkOffset) {
187-
debug << ", WCO=" << status.workOffset;
188-
}
189-
if (status.hasFeedSpindleSpeed) {
190-
debug << ", F=" << status.feedRate << ", S=" << status.spindleSpeed;
191-
}
192-
if (status.hasOverrides) {
193-
debug << ", Ov=" << status.feedOverride << "/" << status.rapidOverride << "/" << status.spindleOverride;
194-
}
195-
if (status.hasBufferStatus) {
196-
debug << ", Bf=" << status.bufferAvailable << "/" << status.bufferSize;
197-
}
198-
if (status.hasPinStates) {
199-
debug << ", Pn=" << status.pinStates.toString();
200-
}
201-
if (status.hasAccessoryState) {
202-
debug << ", A:";
203-
if (status.spindleEnabled) debug << (status.spindleCW ? "S" : "C");
204-
if (status.floodEnabled) debug << "F";
205-
if (status.mistEnabled) debug << "M";
206-
}
207-
208-
debug << ")";
209-
210-
return debug;
211-
}
212-
213-
#endif // MACHINESTATUS_H
1+
// Moved to core/machine/machinestatus.h

src/gpilot/core/communicator/statusreportprocessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <QString>
99
#include <QMap>
1010
#include <optional>
11-
#include "machinestatus.h"
11+
#include "core/machine/machinestatus.h"
1212
#include "core/globals.h"
1313

1414
class StatusReportProcessor

src/gpilot/core/machine/devicecontext.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <optional>
88
#include "physicalmachineconfiguration.h"
99
#include "modalstateparser.h"
10+
#include "machinestatus.h"
1011

1112
enum class MachineType {
1213
Unknown,
@@ -17,9 +18,10 @@ enum class MachineType {
1718
FluidNC,
1819
};
1920

20-
// Holds everything known about the connected device: its firmware type,
21-
// physical hardware configuration (from $$), and current modal state (from $G).
22-
// Populated during handshake; reset on disconnect.
21+
// Holds everything known about the connected device: firmware type,
22+
// physical hardware configuration (from $$), modal parser state (from $G),
23+
// and the last received status report (updated every status cycle).
24+
// Populated during the session; reset on disconnect.
2325
class DeviceContext
2426
{
2527
public:
@@ -30,20 +32,31 @@ class DeviceContext
3032
bool hasModalState() const { return m_modalState.has_value(); }
3133
const ModalState& modalState() const { return *m_modalState; }
3234

35+
MachineState machineState() const {
36+
return m_lastStatusReport.has_value() ? m_lastStatusReport->state
37+
: MachineState::Unknown;
38+
}
39+
40+
bool hasLastStatusReport() const { return m_lastStatusReport.has_value(); }
41+
const MachineStatusReport& lastStatusReport() const { return *m_lastStatusReport; }
42+
3343
void setMachineType(MachineType type) { m_machineType = type; }
3444
void setPhysicalConfig(PhysicalMachineConfiguration config) { m_physicalConfig = std::move(config); }
3545
void setModalState(ModalState state) { m_modalState = std::move(state); }
46+
void setLastStatusReport(MachineStatusReport report) { m_lastStatusReport = std::move(report); }
3647

3748
void reset() {
3849
m_machineType = MachineType::Unknown;
3950
m_physicalConfig.reset();
4051
m_modalState.reset();
52+
m_lastStatusReport.reset();
4153
}
4254

4355
private:
4456
MachineType m_machineType = MachineType::Unknown;
4557
std::optional<PhysicalMachineConfiguration> m_physicalConfig;
4658
std::optional<ModalState> m_modalState;
59+
std::optional<MachineStatusReport> m_lastStatusReport;
4760
};
4861

4962
#endif // DEVICECONTEXT_H

0 commit comments

Comments
 (0)