View Javadoc
1   /*
2    * Copyright © 2016 Greg Chabala
3    *
4    * This file is part of brick-control-lab.
5    *
6    * brick-control-lab is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   *
11   * brick-control-lab is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with brick-control-lab.  If not, see http://www.gnu.org/licenses/.
18   */
19  package org.chabala.brick.controllab;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import java.util.Collections;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  import static org.chabala.brick.controllab.Protocol.STOP_RELEASED;
29  
30  /**
31   * Handle for stop button on a specific control lab instance. Obtain via {@link ControlLab#getStopButton()}.
32   */
33  public class StopButton {
34      private final Logger log = LoggerFactory.getLogger(getClass());
35      private final Set<StopButtonListener> stopButtonListeners;
36      private boolean stopDepressed = false;
37  
38      StopButton(InputManager inputManager) {
39          stopButtonListeners = Collections.synchronizedSet(new HashSet<>(2));
40          inputManager.setStopButtonCallback(this::processStopButton);
41      }
42  
43      /**
44       * Attach a listener for {@link StopButtonEvent}s.
45       *
46       * <p>Multiple listeners are allowed. A listener instance will only be registered
47       * once even if it is added multiple times.
48       * @param listener listener to add
49       */
50      public void addListener(StopButtonListener listener) {
51          stopButtonListeners.add(listener);
52      }
53  
54      /**
55       * Remove a listener for {@link StopButtonEvent}s.
56       * @param listener listener to remove
57       */
58      public void removeListener(StopButtonListener listener) {
59          stopButtonListeners.remove(listener);
60      }
61  
62      /**
63       * Returns current status of stop button.
64       * @return true if the stop button is in depressed state, false otherwise
65       */
66      public boolean isStopDepressed() {
67          return stopDepressed;
68      }
69  
70      @SuppressWarnings("squid:S2629")
71      private void processStopButton(byte b) {
72          if (STOP_RELEASED != b) {
73              if (!stopDepressed) {
74                  StopButtonEvent event = new StopButtonEvent(this, b);
75                  log.info("Stop button depressed {}", String.format("0x%02X", b));
76                  synchronized (stopButtonListeners) {
77                      stopButtonListeners.forEach(l -> l.stopButtonPressed(event));
78                  }
79                  stopDepressed = true;
80              }
81          } else {
82              if (stopDepressed) {
83                  StopButtonEvent event = new StopButtonEvent(this, b);
84                  log.info("Stop button released {}", String.format("0x%02X", b));
85                  synchronized (stopButtonListeners) {
86                      stopButtonListeners.forEach(l -> l.stopButtonReleased(event));
87                  }
88                  stopDepressed = false;
89              }
90          }
91      }
92  }