From 540e096c20ca25fa0819741b7018c6003669404e Mon Sep 17 00:00:00 2001 From: MMMMMNG <113881019+MMMMMNG@users.noreply.github.com> Date: Wed, 30 Nov 2022 16:26:57 +0100 Subject: [PATCH] refactors runMCPExample() into three methods to test using it in parallel with the LEDStrip. --- src/main/java/com/example/Main.java | 44 ++++++++++++++++++----------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/example/Main.java b/src/main/java/com/example/Main.java index c462a71..c6333fc 100644 --- a/src/main/java/com/example/Main.java +++ b/src/main/java/com/example/Main.java @@ -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") @@ -75,7 +75,9 @@ 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 getPinsMCP(MCP23S17 IC)throws Exception{ var ICPinsIter = IC.getPinViewIterator(); var ICPins = new ArrayList(16); for(var pin = ICPinsIter.next();ICPinsIter.hasNext();pin = ICPinsIter.next()){ @@ -83,24 +85,29 @@ private void runMCPExample(Context pi4j) throws Exception{ 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 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 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 /** @@ -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 @@ -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); } /*