I have a setupCar method which initializes the inputs (individual motors)I use on my RPD (Raspberry Pi Driven) Car. When I look at the code I notice this is not ideal. Too much hardcoded dependencies and this is not really something I want. Imagine I would like to swtich pins because a pin broke, … I would have to modify the method and do a new build. If I was using Spring I would only have to change the bean xml file and up we go. I prefer to have some flexibility. As mentioned, Spring might bring the solution but I need to have a look at that and try to find out if I can configure the PI4J depencies in a Spring bean xml file. The source code as it is now, smells dirty.
See source of the method setupCar as it is now here under :
[code language=”java”]
public void setupCar() {
input1 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02,
"INPUT1",
PinState.LOW);
input2 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_03,
"INPUT2",
PinState.LOW);
input3 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_05,
"INPUT3",
PinState.LOW);
input4 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_06,
"INPUT4",
PinState.LOW);
leftWheel = new Wheel(0, input1, input2);
rightWheel = new Wheel(4, input3, input4);
wheelAxle = new WheelAxle(leftWheel, rightWheel);
car = new TwoWheelDriveRaspyCar(wheelAxle);
}
[/code]