I/O: Make I2C & SPI accept ints for write()

This makes everyone's sketches a bit lighter, since it is not longer necessary to explicitly cast to byte or call byte(). Instead, we throw an exception if the value does not fit into the byte.
This commit is contained in:
gohai
2015-10-12 16:56:01 +02:00
parent bc02e4b7b6
commit edf411e566
2 changed files with 14 additions and 6 deletions
+8 -4
View File
@@ -221,19 +221,23 @@ public class I2C {
/**
* Adds bytes to be written to the device
* Adds a byte to be written to the device
*
* You must call beginTransmission() before calling this function.
* The actual writing takes part when read() or endTransmission() is being
* called.
* @param out single byte to be written
* @param out single byte to be written (0-255)
* @see beginTransmission
* @see read
* @see endTransmission
*/
public void write(byte out) {
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.");
throw new RuntimeException("Argument does not fit into a single byte");
}
byte[] tmp = new byte[1];
tmp[0] = out;
tmp[0] = (byte)out;
write(tmp);
}
}
+6 -2
View File
@@ -171,9 +171,13 @@ public class SPI {
* @param out single byte to send
* @return bytes read in (array is the same length as out)
*/
public byte[] transfer(byte 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.");
throw new RuntimeException("Argument does not fit into a single byte");
}
byte[] tmp = new byte[1];
tmp[0] = out;
tmp[0] = (byte)out;
return transfer(tmp);
}
}