Pi 4 GPIO

Support for connecting hardware to the Raspberry Pi GPIO

Moderators: russ, Ralph

Message
Author
User avatar
HughB
Posts: 64
Joined: Sun Feb 23, 2020 9:01 pm

Pi 4 GPIO

#1 Post by HughB »

Hi Guys.
ive decided to play around with the Pi GPIO to use a button (rotary encode button push). However im not getting very far at all.

The code below works well in the AM editor and i can simulate the pushhbutton in the editor but once ive sent it to the pi i just cannot get it to work. I followed the tutorials here https://siminnovations.com/wiki/index.p ... n_tutorial and still no joy.

Code: Select all

function callback_functionBatt(status)
end

-- Callback function which is called when the button is pressed
function button_pressed()
xpl_command("sim/electrical/battery_1_toggle")

end
-- Create a button with the name 'Battery Toggle'
hw_button_add("Battery Toggle", button_pressed)

xpl_dataref_subscribe("sim/cockpit2/electrical/battery_on","INT[8]", callback_functionBatt)
image.png
image.png
image.png (8.77 KiB) Viewed 5731 times
The Pi IO is set correctly for the button push (i Know the button works as its taken from being used on an arduino which was used both as a diy knobster and a standard arduinio air manager device)
Am i missing something fundamental? Is there something you have to do on teh Pi to enable GPIO use that ive missed?
Cheers Hugh

User avatar
Ralph
Posts: 7867
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Pi 4 GPIO

#2 Post by Ralph »

The fist step would be to just print something with that button, so you know if the button is working.
You are aware that these are pin numbers and not GPIO numbers?

User avatar
HughB
Posts: 64
Joined: Sun Feb 23, 2020 9:01 pm

Re: Pi 4 GPIO

#3 Post by HughB »

I thought about that but not sure where the pi prints to. I had some print statements in the code to do that and runing it in AM worked fine but once i assigned the button to the pin on the pi and run it in AM i get "INFO - logic.lua:11: Failed to bind hardware id's to connected hardware" (line 11 is where the hw_button_add is) in the console. Im assuming thats because its trying to find an Arduino. I had this working fine on a nano and wanted to move it to the pi to keep things clean and simple :D
Should i expect to see prints in the console from an instrument running on the pi?

User avatar
Ralph
Posts: 7867
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Pi 4 GPIO

#4 Post by Ralph »

Yes just run the instrument from the Create/Edit tab on the Raspberry Pi.

User avatar
HughB
Posts: 64
Joined: Sun Feb 23, 2020 9:01 pm

Re: Pi 4 GPIO

#5 Post by HughB »

totally forgot you could do that.
It seems that console print messages dont work so i ended up putting some text on the instrument and changing it on the button press just to see if it was detecting it.
Worked well but its a little frustrating thatthe print statement doesnt work. @Corjan should this work as I think it should?

User avatar
Keith Baxter
Posts: 4671
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Pi 4 GPIO

#6 Post by Keith Baxter »

Hi,

Does the print statement print in console when the <console> <hardware> button is activated?

Also when the sim button is activated?


Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Ralph
Posts: 7867
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Pi 4 GPIO

#7 Post by Ralph »

As far as I know print still works. I'll give it a try on Monday.

User avatar
HughB
Posts: 64
Joined: Sun Feb 23, 2020 9:01 pm

Re: Pi 4 GPIO

#8 Post by HughB »

Keith Baxter wrote: Sat Nov 07, 2020 6:19 pm Hi,

Does the print statement print in console when the <console> <hardware> button is activated?

Also when the sim button is activated?


Keith
Yes it does. When you run the instrument with the pi as the target in the create / edit tab it doesn’t.
The button works as evidenced by changing some text on screen on the button press.

User avatar
Keith Baxter
Posts: 4671
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Pi 4 GPIO

#9 Post by Keith Baxter »

HughB wrote: Sun Nov 08, 2020 11:20 am

Yes it does. When you run the instrument with the pi as the target in the create / edit tab it doesn’t.
The button works as evidenced by changing some text on screen on the button press.
Hi

This might seem a silly question so ignore if you did so.
Did you remove and then replace the instrument in the Pi after you added the print statements?

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
HughB
Posts: 64
Joined: Sun Feb 23, 2020 9:01 pm

Re: Pi 4 GPIO

#10 Post by HughB »

No such thing as a silly question. I removed everythign from the pi for this test.
I knocked up a bit of code as a test.

Code: Select all


-- Load and display text and images
-- The myfont.ttf should be placed in the instrument resources folder
mytext1 = txt_add("off", "font:roboto_regular.ttf; size:44; color: black; halign:left;", 400, 400, 200, 200)


-- bool to store current power state of the aircraft
onstatus = 0

-- callback to monitor what the sim is set to and adjust the onstatus so the batterystate is correct at load time
function callback_functionBatt(status)
print("BattA")
print(status[1])
   if status[1] == 0 then
        txt_set(mytext1,"off")
        onstatus = 0
        else 
        txt_set(mytext1, "on")
        onstatus =1
        end
end

-- Callback function which is called when the button is pressed
function button_pressed()

print("button Pressed")
        if onstatus == 1 then
        xpl_command("sim/electrical/battery_1_off")
        else 
        xpl_command("sim/electrical/battery_1_on")
        end
end

-- Create a button with the name 'Battery Toggle'
hw_button_add("Battery Toggle", button_pressed)

xpl_dataref_subscribe("sim/cockpit2/electrical/battery_on","INT[8]", callback_functionBatt)

When you run this on with the PC as a target you get the console messages and also the hardware tab in the bottom right panel allows you to trigger the button press.
image.png
When you run it with the pi as a target you get no console messages and the hardware tab doesnt populate.
image.png
I understand that the hardware tab may be a bit of a stretch to populate as its coming from an airplayer running on a pi but not having console print statements is a bit of a pain but not insurmountable as a little bit of text on screen does the same job.
If I am expecting too much then no worries (and appologies if i have wasted peoples time) maybe something for a future version??

Post Reply