IO: Add a byte-variant of I2C.write and SPI.write

This commit is contained in:
gohai
2016-04-03 16:08:33 +02:00
parent d0939a6552
commit f74c4e7a28
2 changed files with 29 additions and 6 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}