Pi4J / pi4j-v2

Pi4J Version 2.0
Apache License 2.0
266 stars 54 forks source link

[HACKTOBERFEST] MockSpi Class revision #301

Closed MMMMMNG closed 9 months ago

MMMMMNG commented 9 months ago

This PR is supposed to fix Issues #298, #299 and last but certainly not least Issue #300!

It is also supposed to be part of Hacktoberfest. According to the FAQ, if you add the hacktoberfest-accepted label to my PR, it will count - that is, should you choose to accept it ofc. ;) grafik You could also open the entire repo to Hacktoberfest, could be worthwhile. Here's the relevant info.


I was made aware of these shortcomings of the MockSpi class by my working extensively with Roberts class for interfacing with the MCP23S17 Integrated Circuit.

Here's an example of how I use this revised version of MockSpi:

var turnEverySecondPinOn = MCPData.builder().read().response(1, 0, 1, 0, 1, 0, 1, 0).next().build();
//-----------------------------------------------------------------------------------------------------//
//   I assemble the data bytes (MCPData is just a convenience class to create MCP23S17-SPI-packets)    //
//-----------------------------------------------------------------------------------------------------//
        //before we pretend every second pin is on verify that all pins are off
        for (var pin : pins) {
            assertFalse(pin.get());
        }

        //when
        mockSpi.write(turnEverySecondPinOn);
//-----------------------------------------------------------------------------------------------------//
//   I write the bytes to the internal buffer of the MockSpi class, where they will later be           //
//   be consumed by the transfer() calls from the MCP23S17 class                                       //
//-----------------------------------------------------------------------------------------------------//
        try {                     //-----------------------------------------------//
            cut.readGPIOA();      // these functions trigger  the transfer() calls //
            cut.readGPIOB();      //-----------------------------------------------//
        } catch (Exception e) {
            //don't care
        }
        //then
        for (int i = 0; i < pins.size(); i++) {
            assertEquals(i % 2 == 1, pins.get(i).get());
//-----------------------------------------------------------------------------------------------------//
//   Now the actual test to check if the state was computed correctly according                        //
//   to the mock data I fed                                                                            //
//-----------------------------------------------------------------------------------------------------//
        }

There are more working examples in my fork of Roberts project.

Feel free to give feedback and I will try to adapt the code as ASAP as possible. Also, if you accept the PR, please don't forget to label it hacktoberfest-accepted 🙏.

Cheers! -MNG

FDelporte commented 9 months ago

Hi MNG, thanks for your contribution! Our first Hacktoberfest ever. I love your humor regarding #300 and indeed a project without issues is a dead project so thanks for reporting and proposing a fix. Looks good at first quick sight, will dive in a bit deeper later and come back to this.