Skip to content

Commit 16f1a76

Browse files
authored
Merge pull request #462 from Pi4J/spi_chipselect_max
Further improved SpiSelect
2 parents 91ca86b + f3cdf61 commit 16f1a76

File tree

3 files changed

+65
-17
lines changed

3 files changed

+65
-17
lines changed

pi4j-core/src/main/java/com/pi4j/io/spi/SpiChipSelect.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
* #L%
2626
*/
2727

28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
2831
/**
2932
* <p>SpiChipSelect class.</p>
3033
*
@@ -35,6 +38,7 @@
3538
public enum SpiChipSelect {
3639
CS_0(0), CS_1(1), CS_2(2), CS_3(3), CS_4(4), CS_5(5), CS_6(6), CS_7(7), CS_8(8), CS_9(9), CS_10(10);
3740

41+
private static final Logger log = LoggerFactory.getLogger(SpiChipSelect.class);
3842
private final int address;
3943

4044
private SpiChipSelect(int address) {
@@ -57,7 +61,7 @@ public int getChipSelect() {
5761
* @return a {@link SpiChipSelect} object.
5862
*/
5963
public static SpiChipSelect getByNumber(short address){
60-
return getByNumber((int)address);
64+
return getByNumber((int) address);
6165
}
6266

6367
/**
@@ -67,8 +71,8 @@ public static SpiChipSelect getByNumber(short address){
6771
* @return a {@link SpiChipSelect} object.
6872
*/
6973
public static SpiChipSelect getByNumber(int address){
70-
for(var item : SpiChipSelect.values()){
71-
if(item.getChipSelect() == address){
74+
for (var item : SpiChipSelect.values()){
75+
if (item.getChipSelect() == address){
7276
return item;
7377
}
7478
}
@@ -82,17 +86,17 @@ public static SpiChipSelect getByNumber(int address){
8286
* @return a {@link SpiChipSelect} object.
8387
*/
8488
public static SpiChipSelect parse(String bus) {
85-
if(bus.equalsIgnoreCase("0")) return SpiChipSelect.CS_0;
86-
if(bus.equalsIgnoreCase("1")) return SpiChipSelect.CS_1;
87-
if(bus.equalsIgnoreCase("2")) return SpiChipSelect.CS_2;
88-
if(bus.equalsIgnoreCase("3")) return SpiChipSelect.CS_3;
89-
if(bus.equalsIgnoreCase("4")) return SpiChipSelect.CS_4;
90-
if(bus.equalsIgnoreCase("5")) return SpiChipSelect.CS_5;
91-
if(bus.equalsIgnoreCase("6")) return SpiChipSelect.CS_6;
92-
if(bus.equalsIgnoreCase("7")) return SpiChipSelect.CS_7;
93-
if(bus.equalsIgnoreCase("8")) return SpiChipSelect.CS_8;
94-
if(bus.equalsIgnoreCase("9")) return SpiChipSelect.CS_9;
95-
if(bus.equalsIgnoreCase("10")) return SpiChipSelect.CS_10;
89+
for (SpiChipSelect item : SpiChipSelect.values()) {
90+
try {
91+
if (item.getChipSelect() == Integer.parseInt(bus)) {
92+
return item;
93+
}
94+
} catch (NumberFormatException e) {
95+
log.warn("Unable to parse chip select bus as number: {}. Returning default {}.", bus, Spi.DEFAULT_CHIP_SELECT);
96+
return Spi.DEFAULT_CHIP_SELECT;
97+
}
98+
}
99+
log.warn("Didn't find matching chip select bus for: {}. Returning default {}.", bus, Spi.DEFAULT_CHIP_SELECT);
96100
return Spi.DEFAULT_CHIP_SELECT;
97101
}
98102
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.pi4j.io.spi;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
class SpiChipSelectTest {
8+
9+
@Test
10+
void shouldReturnDefaultForString() {
11+
assertEquals(Spi.DEFAULT_CHIP_SELECT, SpiChipSelect.parse(""));
12+
assertEquals(Spi.DEFAULT_CHIP_SELECT, SpiChipSelect.parse("A"));
13+
assertEquals(Spi.DEFAULT_CHIP_SELECT, SpiChipSelect.parse("*"));
14+
assertEquals(Spi.DEFAULT_CHIP_SELECT, SpiChipSelect.parse("none"));
15+
}
16+
17+
@Test
18+
void shouldReturnDefaultForUndefinedNumber() {
19+
assertEquals(Spi.DEFAULT_CHIP_SELECT, SpiChipSelect.parse("11"));
20+
assertEquals(Spi.DEFAULT_CHIP_SELECT, SpiChipSelect.parse("99"));
21+
}
22+
23+
@Test
24+
void shouldReturnCorrectSpiChipSelectFromString() {
25+
assertEquals(SpiChipSelect.CS_0, SpiChipSelect.parse("0"));
26+
assertEquals(SpiChipSelect.CS_1, SpiChipSelect.parse("1"));
27+
assertEquals(SpiChipSelect.CS_5, SpiChipSelect.parse("5"));
28+
assertEquals(SpiChipSelect.CS_10, SpiChipSelect.parse("10"));
29+
}
30+
31+
@Test
32+
void shouldReturnCorrectSpiChipSelectFromShort() {
33+
assertEquals(SpiChipSelect.CS_0, SpiChipSelect.getByNumber((short) 0));
34+
assertEquals(SpiChipSelect.CS_1, SpiChipSelect.getByNumber((short) 1));
35+
assertEquals(SpiChipSelect.CS_5, SpiChipSelect.getByNumber((short) 5));
36+
assertEquals(SpiChipSelect.CS_10, SpiChipSelect.getByNumber((short) 10));
37+
}
38+
39+
@Test
40+
void shouldReturnCorrectSpiChipSelectFromInt() {
41+
assertEquals(SpiChipSelect.CS_0, SpiChipSelect.getByNumber(0));
42+
assertEquals(SpiChipSelect.CS_1, SpiChipSelect.getByNumber(1));
43+
assertEquals(SpiChipSelect.CS_5, SpiChipSelect.getByNumber(5));
44+
assertEquals(SpiChipSelect.CS_10, SpiChipSelect.getByNumber(10));
45+
}
46+
}

plugins/pi4j-plugin-linuxfs/src/main/java/com/pi4j/plugin/linuxfs/provider/spi/LinuxFsSpi.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public class LinuxFsSpi extends SpiBase implements Spi {
6868
private final static byte SIZE_OF_INT = 4;
6969

7070
private static int SPI_IOC_MESSAGE(int n) {
71-
7271
// Even though we will pass the structure to ioctl as a pointer, the command needs to know
7372
// the actual size of the structure (i.e. sizeof). Therefore, we use the ByValue interface
7473
// when getting the struct size.
@@ -155,6 +154,7 @@ public void open() {
155154
// character special device, major number 153 with a dynamically chosen minor device number.
156155
// This is the node that userspace programs will open, created by “udev” or “mdev”.
157156
String spiDev = SPI_DEVICE_BASE + config().bus().getBus() + "." + config().address();
157+
LOG.info("Opening SPI bus {}, address {}", config().bus().getBus(), config().address());
158158
fd = libc.open(spiDev, LinuxLibC.O_RDWR);
159159
if (fd < 0) {
160160
throw new RuntimeException("Failed to open SPI device " + spiDev);
@@ -234,8 +234,6 @@ public void open() {
234234
libc.close(fd);
235235
throw new RuntimeException("Could not write the SPI SHIFT write.");
236236
}
237-
238-
239237
}
240238

241239
@Override

0 commit comments

Comments
 (0)