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  /**
22   * Identifiers for the power level of output ports on the control lab.
23   *
24   * <p>When an output is turned on without specifying a power level, the
25   * default power level is {@link #P8}.
26   */
27  public enum PowerLevel {
28      /**
29       * Power level 0, turns the output off, and resets the default power level
30       * back to {@link #P8}.
31       */
32      P0,
33      /** Power level 1, lowest power level. */
34      P1,
35      /** Power level 2. */
36      P2,
37      /** Power level 3. */
38      P3,
39      /** Power level 4. */
40      P4,
41      /** Power level 5. */
42      P5,
43      /** Power level 6. */
44      P6,
45      /** Power level 7. */
46      P7,
47      /** Power level 8, highest power level. This is the default when not specified. */
48      P8;
49  
50      private final byte code;
51  
52      /**
53       * Enumerates possible power level values for the change power level command.
54       */
55      PowerLevel() {
56          code = getCodeFromLevel(ordinal());
57      }
58  
59      /**
60       * The byte expected by the control lab for the change power level command.
61       * @return a byte relating to this power level
62       */
63      public byte getCode() {
64          return code;
65      }
66  
67      private static byte getCodeFromLevel(int level) {
68          if (level == 0) {
69              return (byte) 0b10010010;
70          } else {
71              return (byte) (0b10110000 + (level - 1 & 0x07));
72          }
73      }
74  }