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.sensor;
20  
21  /**
22   * Touch sensor.
23   */
24  public class TouchSensor implements SensorValue {
25  
26      private SensorValue sensorValue;
27  
28      public TouchSensor(SensorValue sensorValue) {
29          this.sensorValue = sensorValue;
30      }
31  
32      @Override
33      public int getAnalogValue() {
34          return sensorValue.getAnalogValue();
35      }
36  
37      @Override
38      public int getStatusCode() {
39          return sensorValue.getStatusCode();
40      }
41  
42      public String touchStatus() {
43          switch (getStatusCode()) {
44              case 0:
45                  return "depressed";
46              case 8:
47                  return "released";
48              case 24:
49                  return "releasing";
50              case 16:
51                  return "depressing";
52              default:
53                  return "";
54          }
55      }
56  
57      @Override
58      public String toString() {
59          return "TouchSensor{" +
60                  "value=" + String.format("0x%02X", getAnalogValue()) +
61                  ", status=" + getStatusCode() +
62                  ", touch=" + touchStatus() +
63                  '}';
64      }
65  }