Program example GPIO Pi 5

Post your Gambas programming questions here.
Post Reply
Zeke99
Newbie
Posts: 17
Joined: Tue Jan 12, 2021 7:44 pm

Program example GPIO Pi 5

Post by Zeke99 »

Hello all!
Since the architecture of Raspberry Pi 5 is different than the Pi 4 Pigpio doesnt work anymore. Do anyone have a programming example (in Gambas) for declaring and using the GPIO pins on a Pi 5?

In python you can use GPIOD like this:

Code: Select all

import gpiod
import time
LED_PIN = 17
chip = gpiod.Chip('gpiochip4')
led_line = chip.get_line(LED_PIN)
led_line.request(consumer="LED", type=gpiod.LINE_REQ_DIR_OUT)
try:
   while True:
       led_line.set_value(1)
       time.sleep(1)
       led_line.set_value(0)
       time.sleep(1)
finally:
   led_line.release()
Can you use gpiod in a similar way and how do you declare it?
User avatar
cogier
Site Admin
Posts: 1197
Joined: Wed Sep 21, 2016 2:22 pm
Location: Guernsey, Channel Islands

Re: Program example GPIO Pi 5

Post by cogier »

I don't have a Pi 5, so I can't offer any code, but have a look at the command line tools you can use here. You should be able to Shell the necessary command in Gambas to change or read the pin(s) settings.
Zeke99
Newbie
Posts: 17
Joined: Tue Jan 12, 2021 7:44 pm

Re: Program example GPIO Pi 5

Post by Zeke99 »

Thanks Cogier
Pinctrl worked. If someone is interested in a program example I attach one below.

Code: Select all

Public Sub Form_Open()
Dim f As Integer
Dim Pininit As String
  For f = 4 To 27
      Pininit = "pinctrl set " & Str(f) & " ip pd"
      Shell Pininit
  Next
End

Function GetIO(PinNo As Integer) As Boolean
Dim shellResult As String
Dim placeHolder As Integer
Dim pinResult As String
Dim returnIOvalue As Boolean
Dim shellString As String

  shellString = "pinctrl get " & Str(PinNo)
  Shell shellString To shellResult
  
  placeholder = InStr(shellResult, "|")
  pinResult = Mid$(shellResult, placeholder + 2, 2)
  If pinResult = "hi" Then 
    returnIOvalue = True
  Else 
    returnIOvalue = False
  Endif   
  
  Return returnIOvalue
End

Public Sub Timer1_Timer()
Dim Redin As Boolean
Dim GreenIn As Boolean
Dim f As Integer
Dim RedGreen As New Boolean[13, 3]
 
 '---- M140 ----
 Redin = GetIO(4)
 GreenIn = GetIO(5)
 RedGreen[1, 1] = Redin
 RedGreen[1, 2] = GreenIn
 Label1.Background = Color.Gray
 If RedGreen[1, 1] = True Then 
    Label1.Background = Color.Red
 Else 
    If RedGreen[1, 2] = True Then
      Label1.Background = Color.Green
    Endif 
 Endif
 
 '---- S700 ----
 Redin = GetIO(6)
 GreenIn = GetIO(7)
 RedGreen[2, 1] = Redin
 RedGreen[2, 2] = GreenIn
 Label2.Background = Color.Gray
 If RedGreen[2, 1] = True Then 
    Label2.Background = Color.Red
 Else 
    If RedGreen[2, 2] = True Then
      Label2.Background = Color.Green
    Endif 
 Endif
  
User avatar
cogier
Site Admin
Posts: 1197
Joined: Wed Sep 21, 2016 2:22 pm
Location: Guernsey, Channel Islands

Re: Program example GPIO Pi 5

Post by cogier »

Glad it worked. Tip use the gb button to add code to your post, it looks so much better

Image

Public Sub Form_Open()
Dim f As Integer
Dim Pininit As String
  For f = 4 To 27
      Pininit = "pinctrl set " & Str(f) & " ip pd"
      Shell Pininit
  Next
End

Function GetIO(PinNo As Integer) As Boolean
Dim shellResult As String
Dim placeHolder As Integer
Dim pinResult As String
Dim returnIOvalue As Boolean
Dim shellString As String

  shellString = "pinctrl get " & Str(PinNo)
  Shell shellString To shellResult
  
  placeholder = InStr(shellResult, "|")
  pinResult = Mid$(shellResult, placeholder + 2, 2)
  If pinResult = "hi" Then 
    returnIOvalue = True
  Else 
    returnIOvalue = False
  Endif   
  
  Return returnIOvalue
End

Public Sub Timer1_Timer()
Dim Redin As Boolean
Dim GreenIn As Boolean
Dim f As Integer
Dim RedGreen As New Boolean[13, 3]
 
 '---- M140 ----
 Redin = GetIO(4)
 GreenIn = GetIO(5)
 RedGreen[1, 1] = Redin
 RedGreen[1, 2] = GreenIn
 Label1.Background = Color.Gray
 If RedGreen[1, 1] = True Then 
    Label1.Background = Color.Red
 Else 
    If RedGreen[1, 2] = True Then
      Label1.Background = Color.Green
    Endif 
 Endif
 
 '---- S700 ----
 Redin = GetIO(6)
 GreenIn = GetIO(7)
 RedGreen[2, 1] = Redin
 RedGreen[2, 2] = GreenIn
 Label2.Background = Color.Gray
 If RedGreen[2, 1] = True Then 
    Label2.Background = Color.Red
 Else 
    If RedGreen[2, 2] = True Then
      Label2.Background = Color.Green
    Endif 
 Endif
  
Post Reply