Skip to content

Commit

Permalink
Reset overrides before starting a new stream (#2669)
Browse files Browse the repository at this point in the history
  • Loading branch information
breiler authored Jan 4, 2025
1 parent d6fc29f commit 7d9d6fc
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ protected AbstractOverrideManager(IController controller, ICommunicator communic
public void statusStringListener(ControllerStatus status) {
onControllerStatus(status);
}

@Override
public void streamComplete() {
resetAll();
}

@Override
public void streamCanceled() {
resetAll();
}

@Override
public void streamStarted() {
resetAll();
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public void setMessageService(MessageService messageService) {
// Not implemented
}

@Override
public void resetAll() {
// Not implemented
}

@Override
public List<Integer> getSliderSteps(OverrideType type) {
return List.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,9 @@ public interface IOverrideManager {
void setRadioTarget(OverrideType type, int value);

void setMessageService(MessageService messageService);

/**
* Resets the overrides
*/
void resetAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}

@Override
public void resetAll() {
sendOverrideCommand(Overrides.CMD_RAPID_OVR_RESET);
sendOverrideCommand(Overrides.CMD_FEED_OVR_RESET);
sendOverrideCommand(Overrides.CMD_SPINDLE_OVR_RESET);
}

@Override
public int getSliderDefault(OverrideType type) {
return switch (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This file is part of Universal Gcode Sender (UGS).
import static com.willwinder.universalgcodesender.model.CommunicatorState.COMM_CHECK;
import static com.willwinder.universalgcodesender.model.CommunicatorState.COMM_IDLE;
import static com.willwinder.universalgcodesender.model.CommunicatorState.COMM_SENDING;
import com.willwinder.universalgcodesender.model.Overrides;
import com.willwinder.universalgcodesender.model.PartialPosition;
import com.willwinder.universalgcodesender.model.UnitUtils;
import com.willwinder.universalgcodesender.services.MessageService;
Expand Down Expand Up @@ -717,22 +718,22 @@ public void cancelSendOnDoorStateShouldCancelCommandAndIssueReset() throws Excep

assertEquals(1, mgc.numCancelSendCalls);
assertEquals(0, mgc.numPauseSendCalls);
assertEquals(0, mgc.sentBytes.size());
assertEquals(3, mgc.sentBytes.size());

// First round we will store the last position
instance.rawResponseHandler("<Door|MPos:0.000,0.000,0.000|FS:0,0|Pn:XYZ>");

assertEquals(1, mgc.numCancelSendCalls);
assertEquals(0, mgc.numPauseSendCalls);
assertEquals(0, mgc.sentBytes.size());
assertEquals(3, mgc.sentBytes.size());

// Now we will do the actual cancel
instance.rawResponseHandler("<Door|MPos:0.000,0.000,0.000|FS:0,0|Pn:XYZ>");

assertEquals(2, mgc.numCancelSendCalls);
assertEquals(0, mgc.numPauseSendCalls);
assertEquals(1, mgc.sentBytes.size());
assertEquals(Byte.valueOf(GRBL_RESET_COMMAND), mgc.sentBytes.get(0));
assertEquals(4, mgc.sentBytes.size());
assertEquals(Byte.valueOf(GRBL_RESET_COMMAND), mgc.sentBytes.get(3));
}

private void sendStuff(GrblController instance) throws Exception {
Expand Down Expand Up @@ -1293,7 +1294,10 @@ public void errorInCheckModeSending() throws Exception {
assertEquals(COMM_SENDING, gc.getCommunicatorState());
assertEquals(ControllerState.CHECK, gc.getControllerStatus().getState());
assertFalse(gc.isPaused());
assertEquals(Byte.valueOf(GRBL_PAUSE_COMMAND), mgc.sentBytes.get(0));
assertEquals(GrblUtils.getOverrideForEnum(Overrides.CMD_RAPID_OVR_RESET, gc.getCapabilities()), mgc.sentBytes.get(0));
assertEquals(GrblUtils.getOverrideForEnum(Overrides.CMD_FEED_OVR_RESET, gc.getCapabilities()), mgc.sentBytes.get(1));
assertEquals(GrblUtils.getOverrideForEnum(Overrides.CMD_SPINDLE_OVR_RESET, gc.getCapabilities()), mgc.sentBytes.get(2));
assertEquals(Byte.valueOf(GRBL_PAUSE_COMMAND), mgc.sentBytes.get(3));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.willwinder.universalgcodesender.Capabilities;
import com.willwinder.universalgcodesender.CapabilitiesConstants;
import com.willwinder.universalgcodesender.GrblUtils;
import com.willwinder.universalgcodesender.IController;
import com.willwinder.universalgcodesender.communicator.ICommunicator;
import com.willwinder.universalgcodesender.listeners.ControllerListener;
import com.willwinder.universalgcodesender.listeners.ControllerState;
import com.willwinder.universalgcodesender.listeners.ControllerStatus;
import com.willwinder.universalgcodesender.listeners.ControllerStatusBuilder;
Expand All @@ -16,8 +18,12 @@
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import static org.mockito.ArgumentMatchers.anyByte;
import static org.mockito.ArgumentMatchers.eq;
import org.mockito.Captor;
import org.mockito.Mock;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -31,11 +37,16 @@ public class GrblOverrideManagerTest {
private IController controller;
@Mock
private ICommunicator communicator;

@Captor
private ArgumentCaptor<ControllerListener> controllerListenerCaptor;

private GrblOverrideManager overrideManager;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
doNothing().when(controller).addListener(controllerListenerCaptor.capture());
overrideManager = new GrblOverrideManager(controller, communicator, new MessageService());
}

Expand Down Expand Up @@ -153,6 +164,49 @@ public void hasSettledShouldReturnTrueWhenOverridePercentReachesTarget() {
assertTrue(overrideManager.hasSettled());
}

@Test
public void onStreamCanceledShouldResetOverrides() throws Exception {
mockControllerStatus(ControllerState.IDLE);
mockOverrideCapabilities();

ControllerListener controllerListener = controllerListenerCaptor.getValue();
controllerListener.streamCanceled();

assertOverridesResetted();
}

@Test
public void onStreamCompleteShouldResetOverrides() throws Exception {
mockControllerStatus(ControllerState.IDLE);
mockOverrideCapabilities();

ControllerListener controllerListener = controllerListenerCaptor.getValue();
controllerListener.streamComplete();

assertOverridesResetted();
}

@Test
public void onStreamStartedShouldResetOverrides() throws Exception {
mockControllerStatus(ControllerState.IDLE);
mockOverrideCapabilities();

ControllerListener controllerListener = controllerListenerCaptor.getValue();
controllerListener.streamStarted();

assertOverridesResetted();
}

private void assertOverridesResetted() throws Exception {
verify(communicator, times(1)).sendByteImmediately(eq(getOverrideCommand(Overrides.CMD_RAPID_OVR_RESET)));
verify(communicator, times(1)).sendByteImmediately(eq(getOverrideCommand(Overrides.CMD_FEED_OVR_RESET)));
verify(communicator, times(1)).sendByteImmediately(eq(getOverrideCommand(Overrides.CMD_SPINDLE_OVR_RESET)));
}

private byte getOverrideCommand(Overrides overrides) {
return GrblUtils.getOverrideForEnum(overrides, controller.getCapabilities());
}

private void mockControllerStatus(ControllerState controllerState) {
ControllerStatus controllerStatus = ControllerStatusBuilder.newInstance().setOverrides(new OverridePercents(100, 100, 100)).setState(controllerState).build();
when(controller.getControllerStatus()).thenReturn(controllerStatus);
Expand Down

0 comments on commit 7d9d6fc

Please sign in to comment.