Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1334b59

Browse files
authored
Merge pull request#781 from dwalend/channelRewriter
Channel rewriter
2 parents40bc3fe +d38df3b commit1334b59

File tree

5 files changed

+231
-50
lines changed

5 files changed

+231
-50
lines changed

‎src/main/java/ev3dev/actuators/ev3/EV3Led.java‎

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
importev3dev.hardware.EV3DevDevice;
44
importev3dev.hardware.EV3DevPlatform;
5-
importev3dev.utils.Sysfs;
5+
importev3dev.utils.DataChannelRewriter;
66
importlejos.hardware.LED;
77
importorg.slf4j.Logger;
88
importorg.slf4j.LoggerFactory;
99

10+
importjava.io.Closeable;
11+
importjava.io.IOException;
12+
1013
/**
1114
* This class provides methods for interacting with the LEDs on the EV3Brick.
1215
*
1316
* <p><i>Only EV3Bricks are supported.</i>
1417
*/
15-
publicclassEV3LedextendsEV3DevDeviceimplementsLED {
18+
publicclassEV3LedextendsEV3DevDeviceimplementsLED,Closeable {
1619

1720
/**
1821
* Directions of the LED.
@@ -21,16 +24,26 @@ public enum Direction {
2124
LEFT,
2225
RIGHT
2326
}
27+
28+
privatestaticfinalDirection[]directionArray = {Direction.LEFT,Direction.RIGHT};
2429

2530
privatestaticfinalLoggerlog =LoggerFactory.getLogger(EV3Led.class);
2631

32+
/**
33+
* @deprecated Use EV3LedDirection.LEFT instead.
34+
*/
35+
@Deprecated
2736
publicstaticfinalintLEFT =0;
37+
/**
38+
* @deprecated Use EV3Led.Direction.RIGHT instead.
39+
*/
40+
@Deprecated
2841
publicstaticfinalintRIGHT =1;
2942

3043
privatefinalDirectiondirection;
3144

32-
privatefinalStringLED_RED;
33-
privatefinalStringLED_GREEN;
45+
privatefinalDataChannelRewriterredWriter;
46+
privatefinalDataChannelRewritergreenWriter;
3447

3548
/**
3649
* Create an EV3LED object associated with the LED of the specified direction.
@@ -50,11 +63,11 @@ public EV3Led(final Direction direction) {
5063
this.direction =direction;
5164

5265
if (direction ==Direction.LEFT) {
53-
LED_RED =ev3DevProperties.getProperty("ev3.led.left.red");
54-
LED_GREEN =ev3DevProperties.getProperty("ev3.led.left.green");
66+
redWriter =newDataChannelRewriter(ev3DevProperties.getProperty("ev3.led.left.red"));
67+
greenWriter =newDataChannelRewriter(ev3DevProperties.getProperty("ev3.led.left.green"));
5568
}else {
56-
LED_RED =ev3DevProperties.getProperty("ev3.led.right.red");
57-
LED_GREEN =ev3DevProperties.getProperty("ev3.led.right.green");
69+
redWriter =newDataChannelRewriter(ev3DevProperties.getProperty("ev3.led.right.red"));
70+
greenWriter =newDataChannelRewriter(ev3DevProperties.getProperty("ev3.led.right.green"));
5871
}
5972
}
6073

@@ -65,22 +78,9 @@ public EV3Led(final Direction direction) {
6578
* @throws RuntimeException if LED feature is not supported on the current platform.
6679
* @deprecated Use {@link #EV3Led(Direction)} instead.
6780
*/
81+
@Deprecated
6882
publicEV3Led(finalintbutton) {
69-
checkPlatform();
70-
71-
if (button ==LEFT) {
72-
LED_RED =ev3DevProperties.getProperty("ev3.led.left.red");
73-
LED_GREEN =ev3DevProperties.getProperty("ev3.led.left.green");
74-
direction =Direction.LEFT;
75-
}elseif (button ==RIGHT) {
76-
LED_RED =ev3DevProperties.getProperty("ev3.led.right.red");
77-
LED_GREEN =ev3DevProperties.getProperty("ev3.led.right.green");
78-
direction =Direction.RIGHT;
79-
}else {
80-
log.error("You are not specifying any button.");
81-
thrownewIllegalArgumentException("You are not specifying any button.");
82-
}
83-
83+
this(directionArray[button]);
8484
}
8585

8686
/**
@@ -96,7 +96,6 @@ private void checkPlatform() {
9696
}
9797

9898
//TODO Add Enums for patterns
99-
10099
/**
101100
* Sets the pattern of light to be shown with this LED.
102101
*
@@ -109,19 +108,22 @@ private void checkPlatform() {
109108
*/
110109
@Override
111110
publicvoidsetPattern(finalintpattern) {
112-
//Off
111+
112+
finalStringoff =Integer.toString(0);
113+
finalStringon =Integer.toString(255);
114+
113115
if (pattern ==0) {
114-
Sysfs.writeInteger(LED_RED,0);
115-
Sysfs.writeInteger(LED_GREEN,0);
116+
greenWriter.writeString(off);
117+
redWriter.writeString(off);
116118
}elseif (pattern ==1) {
117-
Sysfs.writeInteger(LED_RED,0);
118-
Sysfs.writeInteger(LED_GREEN,255);
119+
greenWriter.writeString(on);
120+
redWriter.writeString(off);
119121
}elseif (pattern ==2) {
120-
Sysfs.writeInteger(LED_RED,255);
121-
Sysfs.writeInteger(LED_GREEN,0);
122+
greenWriter.writeString(off);
123+
redWriter.writeString(on);
122124
}elseif (pattern ==3) {
123-
Sysfs.writeInteger(LED_RED,255);
124-
Sysfs.writeInteger(LED_GREEN,255);
125+
greenWriter.writeString(on);
126+
redWriter.writeString(on);
125127
}elseif (pattern >3) {
126128
log.debug("This feature is not implemented");
127129
}
@@ -135,4 +137,12 @@ public void setPattern(final int pattern) {
135137
publicDirectiongetDirection() {
136138
returndirection;
137139
}
140+
141+
@Override
142+
publicvoidclose()throwsIOException {
143+
greenWriter.close();
144+
redWriter.close();
145+
}
146+
147+
138148
}

‎src/main/java/ev3dev/sensors/Battery.java‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,38 +67,37 @@ private Battery() {
6767
currentRereader =newDataChannelRereader(batteryPath +"/" +batteryEv3 +"/" +current);
6868
}
6969

70-
publicintgetVoltageMicroVolts() {
70+
publicintgetVoltageMicroVolt() {
7171
returnInteger.parseInt(voltageRereader.readString());
7272
}
7373

74+
/**
75+
* @return voltage of the battery in millivolts.
76+
*/
7477
@Override
7578
publicintgetVoltageMilliVolt() {
76-
returngetVoltageMicroVolts() /1000;
79+
returngetVoltageMicroVolt() /1000;
7780
}
7881

7982
/**
80-
* Returns voltage of the battery in microvolts.
81-
*
82-
* @return voltage
83+
* @return voltage of the battery in microvolts.
8384
*/
8485
publicfloatgetVoltage() {
85-
returngetVoltageMicroVolts() /1000000f;
86+
returngetVoltageMicroVolt() /1000000f;
8687
}
8788

8889
//TODO Review output
8990
//TODO Review units
9091

9192
/**
92-
* Returns the current of the battery in amps.
93-
*
94-
* @return current
93+
* @return current from the battery in amps, or Float.NaN if run on something other than EV3BRICK
9594
*/
9695
publicfloatgetBatteryCurrent() {
9796
if (CURRENT_PLATFORM.equals(EV3DevPlatform.EV3BRICK)) {
9897
returnFloat.parseFloat(currentRereader.readString());
9998
}else {
10099
LOGGER.warn("This method is not available for {} & {}",EV3DevPlatform.PISTORMS,EV3DevPlatform.BRICKPI);
101-
return-1f;
100+
returnFloat.NaN;
102101
}
103102
}
104103

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
packageev3dev.utils;
2+
3+
importjava.io.Closeable;
4+
importjava.io.IOException;
5+
importjava.nio.ByteBuffer;
6+
importjava.nio.channels.FileChannel;
7+
importjava.nio.charset.StandardCharsets;
8+
importjava.nio.file.Path;
9+
importjava.nio.file.Paths;
10+
importjava.nio.file.StandardOpenOption;
11+
12+
/**
13+
* Writer of streams that can rewrite the same channel for structured data of
14+
* known length. The focus of this class is on performance.
15+
*
16+
* @author David Walend
17+
*/
18+
publicclassDataChannelRewriterimplementsCloseable {
19+
20+
privatefinalPathpath;
21+
privatefinalByteBufferbyteBuffer;
22+
privatefinalFileChannelchannel;
23+
24+
/**
25+
* Create a DataChannelRewriter for path with a bufferLength byte buffer
26+
*
27+
* @param path path to the file to reread
28+
* @param bufferLength length of the buffer to hold the structure
29+
*/
30+
publicDataChannelRewriter(Pathpath,intbufferLength) {
31+
this.path =path;
32+
this.byteBuffer =ByteBuffer.allocate(bufferLength);
33+
try {
34+
this.channel =FileChannel.open(path,StandardOpenOption.WRITE);
35+
}catch (IOExceptione) {
36+
thrownewRuntimeException("While opening " +path,e);
37+
}
38+
}
39+
40+
/**
41+
* Create a DataChannelRewriter for pathString with the default 32-byte buffer.
42+
*
43+
* @param pathString Path to the file to reread
44+
*/
45+
publicDataChannelRewriter(StringpathString) {
46+
this(Paths.get(pathString),32);
47+
}
48+
49+
/**
50+
* @param string to write. A new line character
51+
*/
52+
publicsynchronizedvoidwriteString(Stringstring) {
53+
try {
54+
byteBuffer.clear();
55+
byteBuffer.put(string.getBytes(StandardCharsets.UTF_8));
56+
byteBuffer.put(((byte)'\n'));
57+
byteBuffer.flip();
58+
channel.truncate(0);
59+
channel.write(byteBuffer,0);
60+
channel.force(false);
61+
}catch (IOExceptione) {
62+
thrownewRuntimeException("Problem writing path: " +path,e);
63+
}
64+
}
65+
66+
publicPathgetPath() {
67+
returnpath;
68+
}
69+
70+
@Override
71+
publicsynchronizedvoidclose()throwsIOException {
72+
channel.close();
73+
}
74+
}
75+

‎src/test/java/ev3dev/actuators/ev3/EV3LedTest.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void constructorLeftTest() throws Exception {
3535

3636
finalFakeBatteryfakeBattery =newFakeBattery(EV3DevPlatform.EV3BRICK);
3737

38-
LEDled =newEV3Led(EV3Led.LEFT);
38+
@SuppressWarnings("deprecation")LEDled =newEV3Led(EV3Led.LEFT);
3939
led =newEV3Led(EV3Led.Direction.LEFT);
4040
}
4141

@@ -44,7 +44,7 @@ public void constructorRightTest() throws Exception {
4444

4545
finalFakeBatteryfakeBattery =newFakeBattery(EV3DevPlatform.EV3BRICK);
4646

47-
LEDled =newEV3Led(EV3Led.RIGHT);
47+
@SuppressWarnings("deprecation")LEDled =newEV3Led(EV3Led.RIGHT);
4848
led =newEV3Led(EV3Led.Direction.RIGHT);
4949
}
5050

@@ -56,7 +56,7 @@ public void usingLedOnEV3BrickPlatformTest() throws Exception {
5656

5757
finalFakeBatteryfakeBattery =newFakeBattery(EV3DevPlatform.BRICKPI);
5858

59-
LEDled =newEV3Led(EV3Led.LEFT);
59+
LEDled =newEV3Led(EV3Led.Direction.LEFT);
6060
}
6161

6262
@Test
@@ -66,7 +66,7 @@ public void badButtonTest() throws Exception {
6666

6767
finalFakeBatteryfakeBattery =newFakeBattery(EV3DevPlatform.EV3BRICK);
6868

69-
LEDled =newEV3Led(2);
69+
@SuppressWarnings("deprecation")LEDled =newEV3Led(4);
7070
}
7171

7272
@Test
@@ -85,7 +85,7 @@ public void leftLedPatternsTest() throws Exception {
8585
finalFakeBatteryfakeBattery =newFakeBattery(EV3DevPlatform.EV3BRICK);
8686
finalFakeLedfakeLed =newFakeLed(EV3DevPlatform.EV3BRICK);
8787

88-
LEDled =newEV3Led(EV3Led.LEFT);
88+
LEDled =newEV3Led(EV3Led.Direction.LEFT);
8989
led.setPattern(1);
9090
led.setPattern(2);
9191
led.setPattern(3);
@@ -104,7 +104,7 @@ public void rightLedPatternsTest() throws Exception {
104104
finalFakeBatteryfakeBattery =newFakeBattery(EV3DevPlatform.EV3BRICK);
105105
finalFakeLedfakeLed =newFakeLed(EV3DevPlatform.EV3BRICK);
106106

107-
LEDled =newEV3Led(EV3Led.RIGHT);
107+
@SuppressWarnings("deprecation")LEDled =newEV3Led(EV3Led.RIGHT);
108108
led.setPattern(1);
109109
led.setPattern(2);
110110
led.setPattern(3);
@@ -123,7 +123,7 @@ public void getDirectionTest() throws Exception {
123123
finalFakeBatteryfakeBattery =newFakeBattery(EV3DevPlatform.EV3BRICK);
124124
finalFakeLedfakeLed =newFakeLed(EV3DevPlatform.EV3BRICK);
125125

126-
EV3Ledled =newEV3Led(EV3Led.RIGHT);
126+
@SuppressWarnings("deprecation")EV3Ledled =newEV3Led(EV3Led.RIGHT);
127127
Assert.assertEquals(EV3Led.Direction.RIGHT,led.getDirection());
128128

129129
led =newEV3Led(EV3Led.Direction.RIGHT);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp