Skip to content

Commit

Permalink
format with IDEA and .editorconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
MMMMMNG committed Oct 10, 2023
1 parent 34021a7 commit 80e0b34
Showing 1 changed file with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,44 +57,48 @@ public class MockSpi extends SpiBase implements Spi {
* <p>Constructor for MockSpi.</p>
*
* @param provider a {@link com.pi4j.io.spi.SpiProvider} object.
* @param config a {@link com.pi4j.io.spi.SpiConfig} object.
* @param config a {@link com.pi4j.io.spi.SpiConfig} object.
*/
public MockSpi(SpiProvider provider, SpiConfig config) {
super(provider, config);
logPreamble = "["+Mock.SPI_PROVIDER_NAME+"::"+this.id+"] ::";
logPreamble = "[" + Mock.SPI_PROVIDER_NAME + "::" + this.id + "] ::";
logger.info("{} OPEN(CHANNEL={}; BAUD={})", logPreamble, config.address(), config.baud());
}

/**
* <p>Lets the tester read all the data in this mocks raw buffer.</p>
*
* <p>
* It returns all the data that has been accumulated by write() or transfer() calls
* and not yet been consumed by read() or transfer() calls.
*
* @return the bytes in the buffer
*/
public byte[] readEntireMockBuffer() {
var bytes = new byte[raw.size()];
for(int i = 0;!raw.isEmpty();++i){
for (int i = 0; !raw.isEmpty(); ++i) {
bytes[i] = raw.pop();
}
logger.info("{} READALL (0x{})", logPreamble, StringUtil.toHexString(bytes));
return bytes;
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public void close() {
logger.info("{} CLOSE(CHANNEL={}; BAUD={})", logPreamble, config.address(), config.baud());
super.close();
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public int transfer(byte[] write, int writeOffset, byte[] read, int readOffset, int numberOfBytes) {
byte[] prepared = new byte[numberOfBytes];
// read the (potentially) prepared mock data
readNoLogging(prepared,0,numberOfBytes);
readNoLogging(prepared, 0, numberOfBytes);
//write the provided data for later verification
writeNoLogging(write, writeOffset, numberOfBytes);

Expand All @@ -109,15 +113,19 @@ public int transfer(byte[] write, int writeOffset, byte[] read, int readOffset,
return 0;
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public int write(byte b) {
raw.add(b);
logger.info("{} WRITE(0x{})", logPreamble, StringUtil.toHexString(b));
return 0;
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public int write(byte[] data, int offset, int length) {
writeNoLogging(data, offset, length);
Expand All @@ -127,32 +135,38 @@ public int write(byte[] data, int offset, int length) {

private void writeNoLogging(byte[] data, int offset, int length) {
Objects.checkFromIndexSize(offset, length, data.length);
for(int p = offset; p-offset < length; p++){
for (int p = offset; p - offset < length; p++) {
raw.add(data[p]); // add to internal buffer
}
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public int write(Charset charset, CharSequence data) {
byte[] buffer = data.toString().getBytes(charset);
for(int p = 0; p < buffer.length; p++){
for (int p = 0; p < buffer.length; p++) {
raw.add(buffer[p]); // add to internal buffer
}
logger.info("{} WRITE(\"{}\")", logPreamble, data);
return data.length();
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public int read() {
if(raw.isEmpty()) return -1;
if (raw.isEmpty()) return -1;
byte b = raw.pop();
logger.info("{} READ (0x{})", logPreamble, StringUtil.toHexString(b));
return b;
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public int read(byte[] buffer, int offset, int length) {
Integer counter = readNoLogging(buffer, offset, length);
Expand All @@ -166,11 +180,11 @@ public int read(byte[] buffer, int offset, int length) {
private Integer readNoLogging(byte[] buffer, int offset, int length) {
Objects.checkFromIndexSize(offset, length, buffer.length);

if(raw.isEmpty()) return null;
if (raw.isEmpty()) return null;
int counter = 0;
for(int p = 0; p < length; p++) {
if(p+ offset > buffer.length) break;
if(raw.isEmpty()) break;
for (int p = 0; p < length; p++) {
if (p + offset > buffer.length) break;
if (raw.isEmpty()) break;
buffer[offset + p] = raw.pop();
counter++;
}
Expand Down

0 comments on commit 80e0b34

Please sign in to comment.