RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
DigitalInput.java
1/*
2 * Copyright (C) 2021 John H. Gaby
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Contact: robotics@gabysoft.com
17 */
18
19package robotCore;
20
21import robotCore.TwoWire.HardwareException;
22
23//import robot.Robot;
24
33public class DigitalInput
34{
35 private int m_pin;
36 private int m_addr;
37 private Device m_device = null;
38 // private ArduinoConnection m_arduino = null;
39
40 public void init(int pin, int addr)
41 {
42 // if (RobotBase.TW)
43 // {
44 initTW(pin, addr);
45 // }
46 // else
47 // {
48 // initArduino(pin);
49 // }
50 }
51
52 private void initTW(int pin, int addr)
53 {
54 m_device = Device.getInstance();
55
56// Logger.log("DigitalInput", 1, String.format("DigitalInput=%d", pin));
57
58 if (!m_device.isPinValid(addr, pin, Device.PinType.InputPin)) {
59 throw new HardwareException(String.format("Invalid input pin: %d", pin));
60 }
61
62 if (m_device.checkUsedPin(addr, pin)) {
63 throw new HardwareException(String.format("Pin %d is in use", pin));
64 }
65
66 m_pin = pin;
67 m_addr = addr;
68
69 m_device.setUsedPin(m_addr, pin);
70 }
71
75 public DigitalInput(int pin)
76 {
77 init(pin, Device.k_mainTwoWireId);
78 }
79
80 public DigitalInput(int pin, int addr)
81 {
82 init(pin, addr);
83 }
84
90 public boolean get()
91 {
92 // if (RobotBase.TW)
93 // {
94 return(getTW());
95 // }
96 // else
97 // {
98 // return(getArduino());
99 // }
100 }
101
102 private boolean getTW()
103 {
104 return(m_device.getByte(m_addr, TwoWire.k_digitalRead, (byte) m_pin) == 1);
105 }
106
107 // Arduino Interface
108
109 // private static int m_inputs = 0;
110 // private static int m_nextInput = 0;
111 // private int m_inputNo = -1;
112
113 // //! @cond PRIVATE
114 // public static void command(String args)
115 // {
116 // int a[] = RobotBase.parseIntegers(args, 1);
117
118 // if (a != null)
119 // {
120
121 // m_inputs = a[0];
122
123 // }
124 // }
125 // //! @endcond
126
127 // private void initArduino(int pin)
128 // {
129 // m_arduino = ArduinoConnection.getInstance();
130
131 // m_inputNo = m_nextInput++;
132
133 // m_arduino.sendCommand(String.format("d%dc %d", m_inputNo, pin));
134 // }
135
136 // public boolean getArduino()
137 // {
138 // if (m_inputNo >= 0)
139 // {
140 // return(((m_inputs >> m_inputNo) & 1) != 0);
141 // }
142
143 // return(false);
144 // }
145}