Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

wiringPiSetup*: You must only call this once per program run. This is a fatal error. Please fix your code. #278

Closed
shaktiphartiyal opened this issue Oct 9, 2016 · 7 comments
Assignees
Labels

Comments

@shaktiphartiyal
Copy link

Whenver LCD is used along with any other GPIO instance the error wiringPiSetup: You must only call this once per program run. This is a fatal error. Please fix your code.* is thrown and the program exits.
Even the LCD Example Program doesn't run.

@BigDrakar
Copy link

I have solved this issue commenting line 51 of com.pi4j.component.lcd.impl.GpioLcdDisplay

@shaktiphartiyal
Copy link
Author

Yes this seems to work , after removing the line 51 I included the code of the file in my own package named Core and rather than using GpioLcdDisplay simply use Core.LCDLib and the code runs smoothy.
Thank you Tutankhamon for the fix.
Below is the new updated code that we need to use.

package Core;
import com.pi4j.component.lcd.LCD;
import com.pi4j.component.lcd.LCDBase;
import com.pi4j.io.gpio.Pin;
import com.pi4j.wiringpi.Lcd;

public class LCDLib extends LCDBase implements LCD
{
    protected int rows;
    protected int columns;
    private int lcdHandle;
    public LCDLib(int rows, int columns, Pin rsPin, Pin strobePin, Pin... dataPins) {
        this.rows = rows;
        this.columns = columns;
        int bits[] = { 0,0,0,0,0,0,0,0 };
        // set wiringPi interface for internal use
        // we will use the WiringPi pin number scheme with the wiringPi library
        //com.pi4j.wiringpi.Gpio.wiringPiSetup();--------THIS IS THE FIX (REASON FOR INCLUSION)

        // seed bit pin address array
        for(int index = 0; index < 8; index++) {
            if(index < dataPins.length)
                bits[index] = dataPins[index].getAddress();
        }

        // initialize LCD
        lcdHandle = Lcd.lcdInit(rows,
                                columns,
                                dataPins.length,
                                rsPin.getAddress(),
                                strobePin.getAddress(),
                                bits[0], bits[1], bits[2], bits[3], bits[4], bits[5], bits[6], bits[7]);

        // verify LCD initialization
        if (lcdHandle == -1)
            throw new RuntimeException("Invalid LCD handle returned from wiringPi: " + lcdHandle);
    }

    @Override
    public int getRowCount() {
        return rows;
    }

    @Override
    public int getColumnCount() {
        return columns;
    }

    @Override
    public void clear() {
        Lcd.lcdClear(lcdHandle);
    }

    @Override
    public void setCursorHome() {
        Lcd.lcdHome(lcdHandle);
    }

    @Override
    public void setCursorPosition(int row, int column) {
        validateCoordinates(row, column);
        Lcd.lcdPosition(lcdHandle, column, row);
    }

    @Override
    public void write(byte data) {
        Lcd.lcdPutchar(lcdHandle, data);
    }

    @Override
    public void write(String data) {
        Lcd.lcdPuts(lcdHandle, data);
    }
}

@savageautomate
Copy link
Member

Re-opening because this still needs to get fixed in the project.

@Swinkid
Copy link

Swinkid commented Oct 28, 2016

Any update on this?

@duygur
Copy link

duygur commented Nov 8, 2016

Any update?

@savageautomate
Copy link
Member

New 1.2-SNAPSHOT build published with this fix.

@vdegrandpre
Copy link

Hi savageautomate. Seems not fixed when it is wanted to set BROADCOM_PIN_NUMBERING AND uses LCD. I got the same message wiringPiSetup*: You must only call this once per program run. This is a fatal error. Please fix your code.

Snippet :

try { GpioFactory.setDefaultProvider( new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING) ); lcd = new GpioLcdDisplay( LCD_ROWS, LCD_COLS, RaspiBcmPin.GPIO_04, RaspiBcmPin.GPIO_13, RaspiBcmPin.GPIO_05, RaspiBcmPin.GPIO_06, RaspiBcmPin.GPIO_16, RaspiBcmPin.GPIO_12 ); PalLog.v(TAG, "GpioLcdDisplay created"); } catch(Exception e){ PalLog.v(TAG, "Failed to create GpioLcdDisplay"); lcd = null; }

What do you suggest ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants