11package ev3dev .utils ;
22
3+ import java .io .DataInputStream ;
34import java .io .File ;
45import java .io .IOException ;
56import java .io .InputStream ;
@@ -110,7 +111,7 @@ public static int readInteger(final String filePath) {
110111 }
111112
112113public static float readFloat (final String filePath ) {
113- return Float . parseFloat ( readString ( filePath ) );
114+ return ( float ) readS16CustomChannel ( /* filePath*/ );
114115 }
115116
116117/**
@@ -161,22 +162,8 @@ public static boolean writeBytes(final String path, final byte[] value) {
161162 }
162163
163164//Skipping much of the boilerplate between InputStream and Channels. 12 ms, but 6 ms with the static path!
164- //static float fetchSampleCustomChannel() {
165- // return Float.parseFloat(readStringCustomChannel());
166- //}
167-
168- //Static Setter to test the idea from @dwalend
169- /*
170- public static void setPathString(String pathString) {
171- Sysfs2.pathString = pathString;
172- }
173-
174- public static String pathString = "";
175165
176- private static final byte[] buffer = new byte[8];
177- static final Path staticPath = Paths.get(pathString);
178- */
179- public static String readStringCustomChannel (Path path ) {
166+ static String readStringCustomChannel (Path path ) {
180167final byte []buffer =new byte [8 ];
181168try {
182169try (InputStream in =customInputStream (path )) {
@@ -194,4 +181,30 @@ static InputStream customInputStream(final Path path) throws IOException {
194181return Channels .newInputStream (rbc );
195182 }
196183
184+ static int readS16CustomChannel () {
185+ final Path usePath =Paths .get ("/sys/class/lego-sensor/sensor0/bin_data" );
186+
187+ try {
188+ try (DataInputStream in =dataInputStream (usePath )) {
189+ // return Short.reverseBytes(in.readShort()); //Nope 1 = 8193
190+ // return in.readShort(); //Nope 1 = 288
191+ int ch1 =in .read ();
192+ int ch2 =in .read ();
193+
194+ System .out .println (ch1 +" " +ch2 );
195+
196+ return ch1 <<8 |ch2 &0x00FF ;
197+
198+ }
199+ }catch (IOException e ) {
200+ throw new RuntimeException ("Problem reading path: " +usePath ,e );
201+ }
202+ }
203+
204+ static DataInputStream dataInputStream (final Path path )throws IOException {
205+ ReadableByteChannel rbc =Files .newByteChannel (path );
206+ return new DataInputStream (Channels .newInputStream (rbc ));
207+ }
208+
209+
197210}