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.chabala.brick.controllab.sensor.SensorEvent; 22 import org.chabala.brick.controllab.sensor.SensorListener; 23 24 /** 25 * Handle for an input port on a specific control lab instance. Obtain 26 * via {@link ControlLab#getInput(InputId)}. 27 */ 28 public class Input { 29 private final InputManager inputManager; 30 private final InputId inputId; 31 32 Input(InputManager inputManager, InputId inputId) { 33 this.inputManager = inputManager; 34 this.inputId = inputId; 35 } 36 37 /** 38 * Attach a listener for {@link SensorEvent}s. 39 * 40 * <p>Multiple listeners are allowed. A listener instance will only be registered 41 * once even if it is added multiple times. 42 * @param listener listener to add 43 */ 44 public void addListener(SensorListener listener) { 45 inputManager.addSensorListener(inputId, listener); 46 } 47 48 /** 49 * Remove a listener for {@link SensorEvent}s. 50 * @param listener listener to remove 51 */ 52 public void removeListener(SensorListener listener) { 53 inputManager.removeSensorListener(inputId, listener); 54 } 55 56 /** {@inheritDoc} */ 57 @Override 58 public String toString() { 59 return "Input{" + 60 "inputId=" + inputId + 61 '}'; 62 } 63 }