Skip to content

Commit

Permalink
refactors runMCPExample() into three methods to test using it in para…
Browse files Browse the repository at this point in the history
…llel with the LEDStrip.
  • Loading branch information
MMMMMNG committed Nov 30, 2022
1 parent 0a94857 commit 540e096
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions src/main/java/com/example/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void main(String[] args) throws Exception {
}
}
}
private void runMCPExample(Context pi4j) throws Exception{
private MCP23S17 setupMCP(Context pi4j) throws Exception{
var ChipSelectConfig = DigitalOutput.newConfigBuilder(pi4j)
.id("CS"+(buttonIds++))
.name("chip select")
Expand All @@ -75,32 +75,39 @@ private void runMCPExample(Context pi4j) throws Exception{

var ChipSelect = pi4j.create(ChipSelectConfig);

MCP23S17 IC = MCP23S17.newWithoutInterrupts(pi4j,0,ChipSelect);
return MCP23S17.newWithoutInterrupts(pi4j,SpiBus.BUS_1,ChipSelect);
}
private ArrayList<MCP23S17.PinView> getPinsMCP(MCP23S17 IC)throws Exception{
var ICPinsIter = IC.getPinViewIterator();
var ICPins = new ArrayList<MCP23S17.PinView>(16);
for(var pin = ICPinsIter.next();ICPinsIter.hasNext();pin = ICPinsIter.next()){
pin.setAsOutput();
ICPins.add(pin);
console.println("pinview direction set");
}
for(var pin : ICPins){
pin.set(true);
console.println("huh");
}
IC.writeIODIRA();
IC.writeIODIRB();
IC.writeOLATA();
IC.writeOLATB();
for(int i = 0; i < 10;++i){
console.println("on"+i);
ICPins.get(5).set(true);
IC.writeOLATA();
delay(1000);
return ICPins;
}
private void MCPon(MCP23S17 IC, ArrayList<MCP23S17.PinView> ICPins) throws Exception{

//for(int i = 0; i < 10;++i){
var i = 5;
console.println("off"+i);
ICPins.get(5).set(false);
ICPins.get(i).set(false);
IC.writeOLATA();
delay(1000);
}
// }
}
private void MCPoff(MCP23S17 IC, ArrayList<MCP23S17.PinView> ICPins) throws Exception{

// for(int i = 0; i < 10;++i){
var i = 5;
console.println("on"+i);
ICPins.get(i).set(true);
IC.writeOLATA();
// }
}
//TODO: extract main logic from boilerplate
/**
Expand All @@ -125,6 +132,11 @@ private void run(Context pi4j) throws Exception {
button2.addListener(createListener(()->{console.println("two");}));
button3.addListener(createListener(()->{console.println("three");}));

//setup MCP
MCP23S17 IntCirc = setupMCP(pi4j);
var ICPins = getPinsMCP(IntCirc);


/*var ledStrib = new Ws281xLedStrip(
12, // leds
10, // Using pin 10 to do SPI, which should allow non-sudo access
Expand All @@ -149,11 +161,11 @@ private void run(Context pi4j) throws Exception {
//waitForKey("Set led strip to ORANGE");
ledStrip.setStripColor(LEDStrip.PixelColor.YELLOW);
ledStrip.render();

MCPon(IntCirc, ICPins);
delay(1000);
ledStrip.setStripColor(LEDStrip.PixelColor.GREEN);
ledStrip.render();

MCPoff(IntCirc, ICPins);
delay(1000);
}
/*
Expand Down

0 comments on commit 540e096

Please sign in to comment.