From f74c4e7a28e91b832297a5ac7fe6536df9b4a7cf Mon Sep 17 00:00:00 2001 From: gohai Date: Sun, 3 Apr 2016 16:08:33 +0200 Subject: [PATCH] IO: Add a byte-variant of I2C.write and SPI.write --- java/libraries/io/src/processing/io/I2C.java | 18 +++++++++++++++--- java/libraries/io/src/processing/io/SPI.java | 17 ++++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/java/libraries/io/src/processing/io/I2C.java b/java/libraries/io/src/processing/io/I2C.java index 96b62767b..5b225a529 100644 --- a/java/libraries/io/src/processing/io/I2C.java +++ b/java/libraries/io/src/processing/io/I2C.java @@ -211,18 +211,30 @@ public class I2C { /** * Adds a byte to be written to the attached device - * @param out single byte to be written (0-255) + * @param out single byte to be written, e.g. numeric literal (0 to 255, or -128 to 127) * @see beginTransmission * @see read * @see endTransmission */ public void write(int out) { - if (out < 0 || 255 < out) { - System.err.println("The write function can only operate on a single byte at a time. Call it with a value from 0 to 255."); + if (out < -128 || 255 < out) { + System.err.println("The write function can only operate on a single byte at a time. Call it with a value from 0 to 255, or -128 to 127."); throw new RuntimeException("Argument does not fit into a single byte"); } byte[] tmp = new byte[1]; tmp[0] = (byte)out; write(tmp); } + + /** + * Adds a byte to be written to the attached device + * @param out single byte to be written + * @see beginTransmission + * @see read + * @see endTransmission + */ + public void write(byte out) { + // cast to (unsigned) int + write(out & 0xff); + } } diff --git a/java/libraries/io/src/processing/io/SPI.java b/java/libraries/io/src/processing/io/SPI.java index 83b9b7a4c..b6973a8f8 100644 --- a/java/libraries/io/src/processing/io/SPI.java +++ b/java/libraries/io/src/processing/io/SPI.java @@ -182,16 +182,27 @@ public class SPI { /** * Transfers data over the SPI bus - * @param out single byte to send + * @param out single byte to send, e.g. numeric literal (0 to 255, or -128 to 127) * @return bytes read in (array is the same length as out) */ public byte[] transfer(int out) { - if (out < 0 || 255 < out) { - System.err.println("The transfer function can only operate on a single byte at a time. Call it with a value from 0 to 255."); + if (out < -128 || 255 < out) { + System.err.println("The transfer function can only operate on a single byte at a time. Call it with a value from 0 to 255, or -128 to 127."); throw new RuntimeException("Argument does not fit into a single byte"); } byte[] tmp = new byte[1]; tmp[0] = (byte)out; return transfer(tmp); } + + + /** + * Transfers data over the SPI bus + * @param out single byte to send + * @return bytes read in (array is the same length as out) + */ + public byte[] transfer(byte out) { + // cast to (unsigned) int + return transfer(out & 0xff); + } }