-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serial monitor send/receive encoding options
- Add "send as <encoding>" dropdown menu. - Add "receive as <encoding>" dropdown menu. Sending and receiving can be done in any encoding specified in StandardCharsets with the additional option to send comma-separated bytes and receive newline-separated bytes directly. This fixes #4452 and offers an easy implementation for issue #4632.
- Loading branch information
1 parent
74f93fe
commit d73de65
Showing
4 changed files
with
259 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package processing.app; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* Represents the encoding option for decoding or encoding bytes that are | ||
* read from or written to a stream. | ||
*/ | ||
public enum EncodingOption { | ||
|
||
/** | ||
* The system default character set as returned by | ||
* {@link Charset#defaultCharset()}. | ||
*/ | ||
SYSTEM_DEFAULT(Charset.defaultCharset()), | ||
|
||
/** | ||
* Comma separated unsigned byte representation. | ||
*/ | ||
BYTES(null), | ||
|
||
UTF_8(StandardCharsets.UTF_8), | ||
UTF_16(StandardCharsets.UTF_16), | ||
UTF_16BE(StandardCharsets.UTF_16BE), | ||
UTF_16LE(StandardCharsets.UTF_16LE), | ||
ISO_8859_1(StandardCharsets.ISO_8859_1), | ||
US_ASCII(StandardCharsets.US_ASCII); | ||
|
||
private final Charset charset; | ||
|
||
private EncodingOption(Charset charset) { | ||
this.charset = charset; | ||
} | ||
|
||
public Charset getCharset() { | ||
return this.charset; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
switch (this) { | ||
case SYSTEM_DEFAULT: | ||
case BYTES: | ||
return this.name().replace('_', ' ').toLowerCase(); | ||
default: | ||
return this.charset.name(); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the {@link EncodingOption} with the given name. | ||
* The name match is case-insensitive and | ||
* whitespaces/dashes are interpreted as '_'. | ||
* @param name - The name of the {@link EncodingOption}. | ||
* @return The matching {@link EncodingOption} | ||
* or null when no such option exists. | ||
*/ | ||
public static EncodingOption forName(String name) { | ||
if (name == null) { | ||
return null; | ||
} | ||
try { | ||
return EncodingOption.valueOf( | ||
name.replace(' ', '_').replace('-', '_').toUpperCase()); | ||
} catch (IllegalArgumentException e) { | ||
return null; | ||
} | ||
// name = name.replace(' ', '_').replace('-', '_'); | ||
// for (EncodingOption option : EncodingOption.values()) { | ||
// if (option.name().equalsIgnoreCase(name)) { | ||
// return option; | ||
// } | ||
// } | ||
// return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.