RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
DigitalCounter.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 java.util.ArrayList;
22
23import robotCore.Device.PinType;
24
25//import robot.Robot;
26
36public class DigitalCounter {
37 private int m_inputNo = -1;
38 private int m_zero = 0;
39 private int m_pin = -1;
40 private static int m_nextInput = 0;
41 private Device m_device = null;
42 private static ArrayList<DigitalCounter> m_list = new ArrayList<DigitalCounter>();
43 private DigitalCounter m_counter = null;
44 int m_addr = Device.k_i2cDefAddr;
45
49 public DigitalCounter(int pin) {
50 m_device = Device.getInstance();
51
52 if (!m_device.isPinValid(m_addr, pin, PinType.InterruptPin)) {
53 throw new TwoWire.HardwareException(String.format("DigitalCounter: Invalid pin: %d", pin));
54 }
55
56 DigitalCounter counter = find(pin);
57
58 if (counter != null) {
59 m_counter = counter;
60 m_pin = pin;
61 m_zero = m_counter.getRaw();
62 return;
63 }
64
65 m_inputNo = m_nextInput++;
66
67 m_device.sendCommand(m_addr, TwoWire.k_createCounter, (byte) m_inputNo, (byte) pin);
68
69 m_list.add(this);
70 }
71
72 private DigitalCounter find(int pin) {
73 for (int i = 0; i < m_list.size(); i++) {
74 DigitalCounter counter = m_list.get(i);
75
76 if (counter.m_pin == pin) {
77 return (counter);
78 }
79 }
80
81 return (null);
82 }
83
84 private int getRaw() {
85 if (m_counter != null) {
86 return (m_counter.getRaw());
87 }
88
89 return (m_device.getInt(m_addr, TwoWire.k_getDigitalCount, (byte) m_inputNo));
90 }
91
97 public int get() {
98 return (getRaw() - m_zero);
99 }
100
105 public void reset() {
106 m_zero = getRaw();
107 }
108
115
117 return new DigitalCounter(m_pin);
118 }
119
120 // Arduino Interface
121
122 // private void initArduino(int pin)
123 // {
124 // m_arduino = ArduinoConnection.getInstance();
125
126 // DigitalCounter counter = find(pin);
127
128 // if (counter == null)
129 // {
130 // m_counter = counter;
131 // m_pin = pin;
132 // m_zero = m_counter.getRaw();
133 // return;
134 // }
135
136 // // if (m_nextInput < m_arduino.GetMaxDigitalCounters())
137 // {
138 // m_inputNo = m_nextInput++;
139
140 // m_arduino.sendCommand(String.format("c%dc %d", m_inputNo, pin));
141 // }
142
143 // m_list.add(this);
144 // }
145
146 // private void setCount(int count)
147 // {
148 // m_count = count;
149 // }
150
151 // private int getRawArduino()
152 // {
153 // return(m_count);
154 // }
155
156 // //! @cond PRIVATE
157 // public static void command(String args)
158 // {
159 // int a[] = RobotBase.parseIntegers(args, 2);
160
161 // // Logger.Log("DigitalCounter", 1, String.format("command: %s, id=%d, v=%d",
162 // args, a[0], a[1]));
163
164 // if (a != null)
165 // {
166 // for (int i = 0 ; i < m_list.size() ; i++)
167 // {
168 // DigitalCounter counter = m_list.get(i);
169
170 // // Logger.Log("DigitalCounter", 1, String.format("command: %d: id=%d", i,
171 // counter.m_inputNo));
172
173 // if (a[0] == counter.m_inputNo)
174 // {
175 // counter.setCount(a[1]);
176 // }
177 // }
178
179 // }
180 // }
181 // //! @endcond
182}
The DigitalCounter class provides a way to count the number of transitions for a specific pin.