GPIO in Gambas - Need information.

Ask about the individual Gambas components here.
Post Reply
User avatar
Askjerry
Posts: 33
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

GPIO in Gambas - Need information.

Post by Askjerry » Saturday 13th February 2021 5:32am

I want to control GPIO pins in Gambas, but it appears that the way to do this (wiringpi) has been deprecated. (Pulled off the internet.)
RE: http://wiringpi.com/news/

So... I have the latest version of Gambas 3.12 or 3.15 at this writing... but I know of no way to access the GPIO pins.

Can anyone help??

If we get that working... I'm also interested in I2C and serial.

Thanks,
Jerry

User avatar
stevedee
Posts: 459
Joined: Monday 20th March 2017 6:06pm

Re: GPIO in Gambas - Need information.

Post by stevedee » Saturday 13th February 2021 9:23am

Askjerry wrote:
Saturday 13th February 2021 5:32am
I want to control GPIO pins in Gambas, but it appears that the way to do this (wiringpi) has been deprecated...
Yes, its a great shame that Gordon has received so much abuse that he has taken this decision: https://captainbodgit.blogspot.com/2019 ... mouth.html

You can try pigpio. Here are my usage notes: https://captainbodgit.blogspot.com/2016 ... brary.html

...and this is the pigpio website: http://abyz.me.uk/rpi/pigpio/index.html

User avatar
Askjerry
Posts: 33
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Re: GPIO in Gambas - Need information.

Post by Askjerry » Saturday 13th February 2021 10:02am

Thank you!

I followed the install instructions and it looks like it is loaded correctly...

Testing pigpio C I/F
pigpio version 78.
Hardware revision 10494163.
Mode/PUD/read/write tests.
TEST 1.1 PASS (set mode, get mode: 0)
TEST 1.2 PASS (set pull up down, read: 1)
TEST 1.3 PASS (set pull up down, read: 0)
TEST 1.4 PASS (write, get mode: 1)
TEST 1.5 PASS (read: 0)
TEST 1.6 PASS (write, read: 1)
PWM dutycycle/range/frequency tests.
TEST 2.1 PASS (set PWM range, set/get PWM frequency: 10)
TEST 2.2 PASS (get PWM dutycycle: 0)
TEST 2.3 PASS (set PWM dutycycle, callback: 0)
TEST 2.4 PASS (get PWM dutycycle: 128)
TEST 2.5 PASS (set PWM dutycycle, callback: 40)
TEST 2.6 PASS (set/get PWM frequency: 100)
TEST 2.7 PASS (callback: 400)
TEST 2.8 PASS (set/get PWM frequency: 1000)
TEST 2.9 PASS (callback: 4000)
TEST 2.10 PASS (get PWM range: 255)
TEST 2.11 PASS (get PWM real range: 200)
TEST 2.12 PASS (set/get PWM range: 2000)
TEST 2.13 PASS (get PWM real range: 200)
PWM/Servo pulse accuracy tests.
TEST 3.1 PASS (get servo pulsewidth: 500)
TEST 3.2 PASS (set servo pulsewidth: 40000)
TEST 3.3 PASS (get servo pulsewidth: 1500)
TEST 3.4 PASS (set servo pulsewidth: 13333)
TEST 3.5 PASS (get servo pulsewidth: 2500)
TEST 3.6 PASS (set servo pulsewidth: 8000)
TEST 3.7 PASS (set/get PWM frequency: 1000)
TEST 3.8 PASS (set PWM range: 200)
TEST 3.9 PASS (get PWM dutycycle: 20)
TEST 3.10 PASS (set PWM dutycycle: 200)
TEST 3.11 PASS (get PWM dutycycle: 40)
TEST 3.12 PASS (set PWM dutycycle: 400)
TEST 3.13 PASS (get PWM dutycycle: 60)
TEST 3.14 PASS (set PWM dutycycle: 600)
TEST 3.15 PASS (get PWM dutycycle: 80)
TEST 3.16 PASS (set PWM dutycycle: 800)
Pipe notification tests.
TEST 4.1 PASS (notify open/begin: 0)
TEST 4.2 PASS (notify pause: 0)
TEST 4.3 PASS (notify close: 0)
TEST 4.4 PASS (sequence numbers ok: 1)
TEST 4.5 PASS (gpio toggled ok: 1)
TEST 4.6 PASS (number of notifications: 80)
Waveforms & serial read/write tests.
TEST 5.1 PASS (callback, set mode, wave clear: 0)
TEST 5.2 PASS (pulse, wave add generic: 4)
TEST 5.3 PASS (wave tx repeat: 9)
TEST 5.4 PASS (callback: 50)
TEST 5.5 PASS (wave tx stop: 0)
TEST 5.6 PASS (serial read open: 0)
TEST 5.7 PASS (wave clear, wave add serial: 3405)
TEST 5.8 PASS (wave tx start: 6811)
TEST 5.9 PASS (NOT APPLICABLE: 0)
TEST 5.10 PASS (NOT APPLICABLE: 0)
TEST 5.11 PASS (wave tx busy, serial read: 0)
TEST 5.12 PASS (serial read close: 0)
TEST 5.13 PASS (wave get micros: 6158148)
TEST 5.14 PASS (wave get high micros: 6158148)
TEST 5.15 PASS (wave get max micros: 1800000000)
TEST 5.16 PASS (wave get pulses: 3405)
TEST 5.17 PASS (wave get high pulses: 3405)
TEST 5.18 PASS (wave get max pulses: 12000)
TEST 5.19 PASS (wave get cbs: 6810)
TEST 5.20 PASS (wave get high cbs: 6810)
TEST 5.21 PASS (wave get max cbs: 25016)
TEST 5.22 PASS (wave create pad, count==1, wid==: 0)
TEST 5.23 PASS (wave create pad, count==2, wid==: 1)
TEST 5.24 PASS (delete wid==0 success: 0)
TEST 5.25 PASS (No more CBs using wave create: -67)
TEST 5.26 PASS (wave create pad, count==3, wid==: 0)
TEST 5.27 PASS (wave chain [1,0]: 0)
TEST 5.28 PASS (callback count==: 5)
Trigger tests
TEST 6.1 PASS (gpio trigger count: 5)
TEST 6.2 PASS (gpio trigger pulse length: 150)
Watchdog tests.
TEST 7.1 PASS (set watchdog on count: 39)
TEST 7.2 PASS (set watchdog off count: 0)
Bank read/write tests.
TEST 8.1 PASS (read bank 1: 0)
TEST 8.2 PASS (read bank 1: 33554432)
TEST 8.3 PASS (clear bank 1: 0)
TEST 8.4 PASS (set bank 1: 1)
TEST 8.5 PASS (read bank 2: 0)
TEST 8.6 PASS (clear bank 2: 0)
TEST 8.7 PASS (NOT APPLICABLE: 0)
TEST 8.8 PASS (set bank 2: 0)
TEST 8.9 PASS (NOT APPLICABLE: 0)
Script store/run/status/stop/delete tests.
TEST 9.1 PASS (store/run script: 100)
TEST 9.2 PASS (run script/script status: 201)
TEST 9.3 PASS (run/stop script/script status: 110)
TEST 9.4 PASS (delete script: 0)

Next I will attempt to use it from within Gambas... if I can blink an LED... I should be able to figure the rest out quickly. (Been programming BASIC since 1977.)

User avatar
Askjerry
Posts: 33
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Re: GPIO in Gambas - Need information.

Post by Askjerry » Saturday 13th February 2021 10:34am

Not sure what to do now.

On your page: https://captainbodgit.blogspot.com/2016 ... brary.html

It says...
Now try to run the project in the Gambas IDE. It will probably report:-
"Failed: must be run as root"
...because you are not running as root. So close Gambas and restart by opening a terminal and typing:-

gksu gambas3

Hopefully, the program will now run properly.
Nope... I get this...
pi@cnc:~ $ gksu gambas3
bash: gksu: command not found
pi@cnc:~ $
So now what???
Jerry

User avatar
cogier
Site Admin
Posts: 764
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: GPIO in Gambas - Need information.

Post by cogier » Saturday 13th February 2021 11:18am

Try sudo instead of gksu.

User avatar
stevedee
Posts: 459
Joined: Monday 20th March 2017 6:06pm

Re: GPIO in Gambas - Need information.

Post by stevedee » Saturday 13th February 2021 11:24am

cogier wrote:
Saturday 13th February 2021 11:18am
Try sudo instead of gksu.
Yep, those Linux boys took away my favourite command a couple of years ago!

User avatar
BruceSteers
Posts: 657
Joined: Thursday 23rd July 2020 5:20pm

Re: GPIO in Gambas - Need information.

Post by BruceSteers » Saturday 13th February 2021 12:43pm

stevedee wrote:
Saturday 13th February 2021 11:24am
cogier wrote:
Saturday 13th February 2021 11:18am
Try sudo instead of gksu.
Yep, those Linux boys took away my favourite command a couple of years ago!
I made a similar simple tool in gambas because gksu vanished and i use it a lot :)
https://gitlab.com/bsteers4/groot
it creates a hidden app with a simple terminalview that runs the command as root.

the official way to do it now is through policy-kit with pkexec but you have to add gbr3 to pkexec list of allowed apps.
I wrote a program to help with that too...
https://gitlab.com/bsteers4/pkAppMan

shame, I used to use gksu a lot.
I thought everybody knew it had gone lol
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
Askjerry
Posts: 33
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Re: GPIO in Gambas - Need information.

Post by Askjerry » Saturday 13th February 2021 5:53pm

Ok... let me put it this way... I'm like a 10-year old that took dad's car keys... I know what the gas pedal is, and the steering wheel... but insofar as the minutia of properly operating the car... nope. I'm gonna hit a few mailboxes.

So... this is what happened...
pi@cnc:~ $ sudo gambas3
libEGL warning: DRI2: failed to authenticate
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
Gambas started up... but did so in the root directory... however I was able to navigate to the working directory "GAMBAS_PROGRAMS" that I had created earlier. I opened the "GPIO_Test" program, and it ran with zero errors. I think I'm in... now I will connect an LED and see what happens.

I'm thinking there is some way to add to the "sudo gambas3" command so that I'm telling it, "Open this program and point to the "home\pi\GAMBAS_PROGRAMS" directory... but I'll deal with that aspect later. Then I will try to figure out how to make a desktop icon that I can click on because I'm too lazy to open a terminal every time.

STATUS: Success!

Thank you so much for the extra help! :D
Last edited by Askjerry on Saturday 13th February 2021 6:50pm, edited 1 time in total.

User avatar
stevedee
Posts: 459
Joined: Monday 20th March 2017 6:06pm

Re: GPIO in Gambas - Need information.

Post by stevedee » Saturday 13th February 2021 6:18pm

Well done Jerry.

Its always a bit tricky working with programs (or libraries within programs) that need root permissions. Although I have 4 or 5 RaspberryPi based computers still in regular use using the gpio, its been a few years now since I put one of these together, so I'm very rusty.

In the long term you don't want to be running your final program as root, and there are a number of ways to avoids this.

Don't forget to work out the value of your LED series resistor based upon a suitable LED current (normally 10-20mA) and your supply voltage (probably either 3.3V or 5V).

Although there is nothing wrong with pigpio, I notice that this webpage (https://www.raspberrypi.org/documentation/usage/gpio/) mentions a couple of C type libraries which may be worth a second look.

User avatar
Askjerry
Posts: 33
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Re: GPIO in Gambas - Need information.

Post by Askjerry » Saturday 13th February 2021 7:03pm

Thank you again! I will look into the web pages you suggest in just a bit.

As far as the resistors and such go... I've got a solid handle on that... been doing electronics since 1972. (Thanks dad.) and I'm a CET. (Certified Electronics Tech) with over 30 years experience. Check out my Youtube channel: http://youtube.com/askjerry for more info and to see the CNC stuff I'm doing.

This is the first step in making a wind tunnel for teaching students as well as building a new PUMA Robot controller from scratch.

Now that I can turn a GPIO on and off... I can make a test program to control a stepper motor using a standard controller. The outputs and for that matter inputs will be connected via a 4N35 optoisolator. Overkill... yeah... but I buy them in tubes of 50... so not like I'm gonna run out. (Internal LED 1.2v @ 8 to 10mA. Output is up to 70v at 50mA, or 100mA up to 1 second.)

Sorry... ranting.

Here is my kluge of a test program... it works... I'll do better on the finished design.

Code: Select all

' Gambas class file

' Load the pigpio library
Library "/usr/local/lib/libpigpio"

' Set up the calls
Public Extern gpioInitialise() As Integer   'call at start of program to initialise 
Public Extern gpioTerminate() As Integer    'call at end of program to release resources and terminate threads
Public Extern gpioVersion() As Integer
Public Extern gpioHardwareRevision() As Integer
Public Extern gpioSetMode(iPin As Integer, iMode As Integer) As Integer
Public Extern gpioRead(iPin As Integer) As Integer
Public Extern gpioWrite(iPin As Integer, iLevel As Integer) As Integer

' Define constants
Const PI_INPUT As Integer = 0
Const PI_OUTPUT As Integer = 1


Public Sub Form_Open()
Dim intReply As Integer

  intReply = gpioInitialise()
  If intReply < 0 Then
    Label1.Text = "Failed: must be run as root"
    BTN_LED_16.Enabled = False
  Else
    Label1.Text = "Initialised: pigpio version no: " & intReply
    Me.Text = "Pi hardware rev: " & Hex$(gpioHardwareRevision())
    Me.Text &= ",  pigpio vers: " & gpioVersion()
    Me.Tag = 0
  Endif
 
End

Public Sub Form_Close()
 
  gpioTerminate()
 
End

Public Sub BTN_LED_16_Click()
  Dim LED_Pin As Integer 
  LED_Pin = 16
  Dim LED_STATUS As Integer
  gpioSetMode(LED_Pin, PI_OUTPUT)
  LED_STATUS = gpioRead(LED_Pin)
  If LED_STATUS = 0 Then
    LED_STAT_DSP.Text = "LED 16 ON"
    gpioWrite(LED_Pin, 1)
  Else
    LED_STAT_DSP.Text = "LED 16 OFF"
    gpioWrite(LED_Pin, 0)  
  Endif
End
I'll play with some steppers... then see if I can figure out the I2C for pulling data from an A/D chip, and then serial to talk to the ESP8266 and/or Arduino and/or Atmel Mega128 programmed in BASCOM. https://www.mcselec.com/?option=com_con ... &Itemid=41

Ok... off I go to get dangerous.
Jerry 8-)

EDIT:
I just did an input pin, worked great... but I used an external 1K resistor as the pull-up resistor.
Then I found this... and thought I'd drop it here for future me when I get confused...
gpioSetPullUpDown(17, PI_PUD_UP); // Sets a pull-up.
gpioSetPullUpDown(18, PI_PUD_DOWN); // Sets a pull-down.
gpioSetPullUpDown(23, PI_PUD_OFF); // Clear any pull-ups/downs.

Post Reply