ffrlr
Posts: 11
Joined: Mon Mar 18, 2013 6:22 pm

Gambas & WiringPi

Tue Mar 19, 2013 4:08 pm

Hi everyone

I am very new to linux, but following threads on here have managed to get my pi up and running Gambas 3.21.
I am quite familiar with VB6, and was hoping to use this knowledge to play around with Gambas on the pi, which seems to be going ok so far.
What I would like to do next is controlling I2C devices from Gambas, has anyone had any success with using Gordon's WiringPi library with Gambas?
If so would you be able to give me some idea of how to start (test program or example) please.
I have no C knowledge at all, and have always used Windows and the VB6 enviroment for programming and developing code, although I have hardware design experience and microprocessor programming in assembler (8051 / PIC etc).

I have trawled the www but can't seem to find anything relevant.

Regards
Tony.

kru64
Posts: 23
Joined: Tue Aug 28, 2012 8:57 am

Re: Gambas & WiringPi

Tue Mar 19, 2013 7:04 pm

Hi Tony!
I'm hardware engineer too. And as you in my work I use VB6. And of course I use Raspberry Pi as another convinient device. In particular I use RaspberryPi to read or to write any register on smth chip by I2C. It can be done by any language. But I preffer namely Gambas, because it's practically VB6.
First find at this forum and download gpiotest-0.0.1.tar It's main test programm
If you look at I2c protocol - it's very simple. So write about (I'm lazy to check all bits sequence).

' Gambas class file

'external function definitions for wiringPi Library
'
Library "/usr/local/lib/libwiringPi"
Public Extern wiringPiSetupGpio() As Integer
Public Extern digitalReadGpio(pin As Integer) As Integer
Public Extern digitalWriteGpio(pin As Integer, value As Integer)
Public Extern pinModeGpio(pin As Integer, pud As Integer)
Public Extern pullUpDnControlGpio(pin As Integer, mode As Integer)
Public Extern pwmWriteGpio(pin As Integer, value As Integer)
Public Extern lcdInit(rows As Integer, cols As Integer, bits As Integer, rs As Integer, strb As Integer, d0 As Integer, d1 As Integer, d2 As Integer, d3 As Integer, d4 As Integer, d5 As Integer, d6 As Integer, d7 As Integer) As Integer
Public Extern lcdHome(fd As Integer)
Public Extern lcdClear(fd As Integer)
Public Extern lcdPosition(fd As Integer, x As Integer, y As Integer)
Public Extern lcdPutchar(fd As Integer, data As Byte)
Public Extern lcdPuts(fd As Integer, s As String)

'useful constants for wiringPi Library
'
Public Const PIN_OUTPUT As Integer = 1
Public Const PIN_INPUT As Integer = 0
Public Const PIN_PWM As Integer = 2
Public Const PUD_OFF As Integer = 0
Public Const PUD_DOWN As Integer = 1
Public Const PUD_UP As Integer = 2

Public SDA As Integer
Public SCL As Integer
Public myBit[8] As Integer
Public addr7002[50] As String

Public Sub Form_Open()
Dim x As Integer

If (wiringPiSetupGpio() = -1)
Message("Failed to setup wiringPi. This program must be run as root")
Quit
Endif

' Allocate BCM2835 GPIO pins to each of 10 LEDs. Valid pin numbers are 0 1 4 7 8 9 10 11 14 15 17 18 21 22 23 24 25
' note: pins 14 and 15 are not used in this example as they are used by the serial port console and are not be available if this is in use by linux
' connect wires to these pins

SDA = 7
SCL = 8

End

Public Sub w1()

digitalWriteGpio(SDA, 1)
digitalWriteGpio(SCL, 1)
digitalWriteGpio(SCL, 0)

End

Public Sub w0()

digitalWriteGpio(SDA, 0)
digitalWriteGpio(SCL, 1)
digitalWriteGpio(SCL, 0)

End


Public Sub btnWriteExample_Click()

digitalWriteGpio(SDA, 1)
digitalWriteGpio(SCL, 1)

pinModeGpio(SDA, PIN_OUTPUT)
pinModeGpio(SCL, PIN_OUTPUT)

'start
digitalWriteGpio(SDA, 0)
digitalWriteGpio(SCL, 0)

'write example address 0x5D
w1()
w0()
w1()

w1()
w1()
w0()
w1()

'write
w0()

'check acknowledgment
pinModeGpio(SDA, PIN_INPUT)
sw = digitalReadGpio(SDA)
If sw = 1 Then
lblMessage.Caption = "NO ACK!"
Else
lblMessage.Caption = "OK"
Endif

'write example value 0xBD
w1()
w0()
w1()
w1()

w1()
w1()
w0()
w1()

'check acknowledgment
pinModeGpio(SDA, PIN_INPUT)
sw = digitalReadGpio(SDA)
If sw = 1 Then
lblMessage.Caption = "NO ACK!"
Else
lblMessage.Caption = "OK"
Endif

'stop
digitalWriteGpio(SCL, 1)
digitalWriteGpio(SDA, 1)

End

I show you way. Next in such manner write your own function to write or read the whole chip or all your development board.

Regards
Serge

kru64
Posts: 23
Joined: Tue Aug 28, 2012 8:57 am

Re: Gambas & WiringPi

Tue Mar 19, 2013 7:10 pm

I forgot to recall, that program must be run as root, so don't run programm in Gambas, but compile and run in command line "sudo ./name_of_programm.gambas"

ffrlr
Posts: 11
Joined: Mon Mar 18, 2013 6:22 pm

Re: Gambas & WiringPi

Wed Mar 20, 2013 3:35 pm

Hi Serge

First, thank you for the reply, however, I am not back home until Friday so I am without the Pi until then.
When I get back home I will try and follow your information and hopefully get my led's flashing. Hardware wise, I have connected my Pi to a MCP23017 IO expander, it's GPA0-7 and GPB0-7 I have connected to led's, the I2C clk and I2C data connect to pins 3 and 5 on the PI header.
I have managed to get the led's to light using Python, but found it an extremely difficult language to learn, much like when I tried to learn C! guess I am just to old and set in my ways to learn new stuff.

Thanks again Serge.

Regards
Tony

ffrlr
Posts: 11
Joined: Mon Mar 18, 2013 6:22 pm

Re: Gambas & WiringPi

Sat Mar 23, 2013 3:38 pm

Hi Serge

I searched for gpiotest-0.0.1.tar but couldn't find anything!, no matter, your example helped me undertsand how it all goes together but....

I have installed wiringPi, and tested its functionality with a scope connected to GPIO0, typing GPIO write 8 0 and GPIO write 8 1 from a terminal window sends the GPIO pin high and low, can be seen on the scope.
From Gambas, I cannot see the GPIO pin change state, i've copied your program, created two buttons 'Write a 1' and 'Write a 0'

The code is below

public button write1_click()
pinModeGpio(8,PIN_OUTPUT)
pinModeGpio(8,1)
End sub

public button write0_click()
pinModeGpio(8,PIN_OUTPUT)
pinModeGpio(8,0)
End sub

Clicking them, the SDA0 (GPIO0) pin doesn't change at all.
I've compiled (make executable) and run in a terminal window sudo ./IOtest.gambas
but it works from the terminal window using the gpio utility. I was confused initially by the wiringPi 'pin' and the IO connector 'pin' and the BCM2835 'pin', I think that '8' is GPIO0 which is pin '3' on the IO connector.

Can you suggest anything I can check?

Regards
Tony

kru64
Posts: 23
Joined: Tue Aug 28, 2012 8:57 am

Re: Gambas & WiringPi

Sat Mar 23, 2013 10:13 pm

Hi Tony
Here files that You need
http://www.filedropper.com/gpiotest
Place libwiringPi.so into /usr/local/lib
Untar gpiotest-0.0.1.tar in any place and go on.
I think you'll be satisfy :P
Best regards
Serge

ffrlr
Posts: 11
Joined: Mon Mar 18, 2013 6:22 pm

Re: Gambas & WiringPi

Sun Mar 24, 2013 10:27 am

Hi Serge

Thank you for the files, I have done exactly as you say, but still cannot control the GPIO from Gambas.
I've put 2 LED's through 1k resistors from the IO connector ground pin to pin 3 (wiringPi 8) and pin 5 (wiringPi 9).
From a terminal windows, I can type gpio mode 8 out, gpio mode 9 out, and by typing gpio write 8 0 and gpio write 9 0 both leds turn off, gpio write 9 1 and gpio write 8 1 turn on the leds.
Running the GPIO test program (compiled in gambas) and run in a terminal with sudo ./gpiotest.gambas, the program runs, I see the form with buttons on, but clicking all leds off and all leds on doesn't change the led's, there is also nothing coming out on the PWM io pin (checked with the scope).
I have copied libwiringPi.so to /usr/local/lib.

I am at my wits end with it to be honest, I am currently downloading Raspbian wheezy and gambas 3.2.1 again, I am going to make another SD card from scratch and try again in case there is something corrupt somewhere.

Regards
Tony

ffrlr
Posts: 11
Joined: Mon Mar 18, 2013 6:22 pm

Re: Gambas & WiringPi

Sun Mar 24, 2013 6:19 pm

Hi Serge

Just a bit more information on this, I have downloaded a new sd card image of Rasbian Wheezy and created a new SD card, booted it, then, installed Gambas using 'sudo apt-get install gambas' as described elsewhere the forum, finally,i've added the gambas 3.2.1 patch found on this forum, Gambas runs fine.
I have then installed the wiringPi library as described on Gordons page, running GPio commands from the terminal windows turns off / on the LED's attached to Pin 3 and 5 of the IO connector, so I am happy the wiringPi is installed correctly.
Running the compiled gpiotest program as root (sudo ./gpiotest.gambas) in a terminal windows does not control the leds at all.
Is it permissions?, have I not done something somwhere?, I am lost!!

Regards
Tony

kru64
Posts: 23
Joined: Tue Aug 28, 2012 8:57 am

Re: Gambas & WiringPi

Sun Mar 24, 2013 7:37 pm

Hi Tony.
On Gordon page
https://projects.drogon.net/raspberry-p ... d-install/
written
" Unfortunately Gambas3′s external function definition won’t work with a .a library. It needs a shared object .so library instead..."
and so I use libwiringPi.so
and all works fine.
I suggest to you not install gambas by yourself, but install .img file where gambas already integrated.
Look here
http://ediy.com.my/index.php/blog/item/ ... spberry-pi
and then copy libwiringPi.so into /usr/local/lib
Regards
Serge

ffrlr
Posts: 11
Joined: Mon Mar 18, 2013 6:22 pm

Re: Gambas & WiringPi

Mon Mar 25, 2013 5:56 pm

Hi again Serge

I have done EXACTLY what you described above, downloaded the ready made image with Gambas installed, written it to an SDcard with win32diskimager, booted it, copied libwiringPi.so from gpiotest into /usr/local/lib.
I then ran Gambas and opened the gpiotest program and clicked 'make' to build the executable gpiotest.gambas.
then in a terminal window, I type sudo ./gpiotest.gambas, the program runs and if I cliock the 'all leds off' or 'all leds on' boxes, the three leds stay on, never turn off, there is no PWM coming out on the scope either.
This time, I haven't installed Gordons wiringPi as he describes on his web page, merely copied the libwiringPi.so from gpiotest to /usr/local/lib so I can't type gpio from the terminal window to test that I have control of the io port leds, but they work in both the previous sd card images I tried.
My Pi is the very latest rev 2 pcb, not sure if that makes any difference at all?

Regards
Tony.

kru64
Posts: 23
Joined: Tue Aug 28, 2012 8:57 am

Re: Gambas & WiringPi

Mon Mar 25, 2013 7:37 pm

Hi Tony.
I just repeated the same steps (wrote image on SD card etc...) All works fine. The levels switch from 0v to 3.3v. I bought my Pi in october 2012, it's not latest revision.
Sorry, but I have no more ideas :(

Regards
Serge

kru64
Posts: 23
Joined: Tue Aug 28, 2012 8:57 am

Re: Gambas & WiringPi

Wed Mar 27, 2013 9:12 pm

Hi Tony.
It's again me. We forgot about old magic command "Shell".
I recalled that you can make set any pin in terminal window.
So write in Gambas for example
Shell "gpio mode 6 out"
Shell "gpio write 6 1"

The same command "shell" you may use to access to I2c device. First you must enable i2c bus.There are many articles in internet how to do this. For example
http://www.abelectronics.co.uk/i2c-rasp ... /info.aspx
Check I2c bus by typing
i2cdetect -y 0
to see if there is anything connected
In the table you can see various devices connected to i2c and there addresses.
To read you type in command line
i2cget -f -y 0 0x30 0x09
(where, for example, 0x30 - chip address, 0x09 - register address)
To write type
i2cset -f -y 1 0x30 0x09 0x91
(where, for example, 0x30 - chip address, 0x09 - register address, 0x91 -value)
And, of course, write the same code in Gambas, for example
Shell "i2cset -f -y 1 0x30 0x09 0x91"
So, connect your device to i2c pins and go on :P
Regards
Serge

tjc
Posts: 1
Joined: Fri Mar 29, 2013 11:57 am

Re: Gambas & WiringPi

Mon Apr 01, 2013 1:44 pm

Hi Serge,
Thanks for the information about the Gambas shell command. If you only need the i2c bus you do not need wiringPi or use sudo to run Gambas as root.

You can use a Gambas string variable with the shell command to write to the i2c bus

mystr1 = "i2cset -y 0 0x70 0x00 0x06"
Shell mystr1

and to read from the bus and place the data in mystr1

Shell "(i2cget -y 0 0x4c 0xaa w)" To mystr1

which will read 2 bytes as a word.

to read 1 byte omit the w.

You can also use wait to allow time to receive i2c bus data.

Shell "(i2cget -y 0 0x4c 0xaa )" wait To mystr1

I have an older raspberry Pi so the 0 after the -y will need to be changed to 1 for a newer Pi.

Trevor

IRQ
Posts: 35
Joined: Thu Aug 09, 2012 4:56 pm

Re: Gambas & WiringPi

Tue Apr 02, 2013 4:38 pm

Using i2cset and i2cget along with Shell in Gambas 3 works but using wiringPi's I2C library makes the code easier to work with IMHO. Below are Gambas 3 declarations for Gordons I2C stuff...

Code: Select all

' This initialises the I2C system with your given device identifier
Public Extern wiringPiI2CSetup(devId As Integer) As Integer In "libwiringPi:1"
' Simple device read. Some devices present data when you read them without having to do any register transactions.
Public Extern wiringPiI2CRead(FileHandle As Integer) As Integer In "libwiringPi:1"
' Simple device write. Some devices accept data this way without needing to access any internal registers.
Public Extern wiringPiI2CWrite(FileHandle As Integer, Data As Integer) As Integer In "libwiringPi:1"
' Read an 8-bit value from the device register indicated.
Public Extern wiringPiI2CReadReg8(FileHandle As Integer, Register As Integer) As Integer In "libwiringPi:1"
' Read an 16-bit value from the device register indicated.
Public Extern wiringPiI2CReadReg16(FileHandle As Integer, Register As Integer) As Integer In "libwiringPi:1"
' Write an 8-bit data value into the device register indicated.
Public Extern wiringPiI2CWriteReg8(FileHandle As Integer, Register As Integer, Data As Integer) As Integer In "libwiringPi:1"
' Write an 16-bit data value into the device register indicated.
Public Extern wiringPiI2CWriteReg16(FileHandle As Integer, Register As Integer, Data As Integer) As Integer In "libwiringPi:1"
More information here...

https://projects.drogon.net/raspberry-p ... c-library/

ffrlr
Posts: 11
Joined: Mon Mar 18, 2013 6:22 pm

Re: Gambas & WiringPi

Wed Apr 03, 2013 8:18 am

Sorry for not coming back to the thread, i've been working away and not had any internet access.
I have tried the wiringPi and Gambas so many times I've lost count, each time no matter which way I go about it I get the same result, no control of the GPIO pins, but I can control them using the commands in a terminal window.
I even tied it the way Serge suggested worked for him, but it still doesn't work for me.

Would anyone be able to try it using the same Pi as mine, it is a 512mb model B revision 2 pcb, I can only imagine that there is somehow a difference that stops the library communicating with the hardware.
I have been trying to get a hold of another Pi, the older version but not yet managed to get one.

Regards

Tony

IRQ
Posts: 35
Joined: Thu Aug 09, 2012 4:56 pm

Re: Gambas & WiringPi

Wed Apr 03, 2013 12:44 pm

I doubt it's a "Rev 1 vs Rev 2" issue, looking at wiringPi's documentation Gordon have tested it on both versions and it should work on both. My suggestion is that you test writing a tiny CLI program that just toggles one of the pins and then log in as root via SSH and run it there. If it doesn't work first time try to use another GPIO pin just in case something happend to that specific pin.

ffrlr
Posts: 11
Joined: Mon Mar 18, 2013 6:22 pm

Re: Gambas & WiringPi

Thu Apr 04, 2013 9:47 am

Hi IRQ

I cannot for the life of me see what else it could be, I can from a terminal windows type gpio mode 8 out, gpio mode 9 out, and by typing gpio write 8 0 and gpio write 9 0 which both leds turn off, gpio write 9 1 and gpio write 8 1 which turn on the leds, so the IO pins are fine.
My only other thought is that there is something in the permissions somewhere

Regards
Tony

IRQ
Posts: 35
Joined: Thu Aug 09, 2012 4:56 pm

Re: Gambas & WiringPi

Thu Apr 04, 2013 11:25 am

I just ordered a Rev 2 and will receive it next week then I will try here and make a simple testprogram in Gambas 3 for you.

ffrlr
Posts: 11
Joined: Mon Mar 18, 2013 6:22 pm

Re: Gambas & WiringPi

Tue Apr 09, 2013 10:20 am

Thank you IRQ, I would love to see if it works for you, if it does, I don't know what to do next!

Please let me know how it goes on the new board.

Regards
Tony

IRQ
Posts: 35
Joined: Thu Aug 09, 2012 4:56 pm

Re: Gambas & WiringPi

Tue Apr 09, 2013 7:58 pm

Hope it will show up this week and will test as soon as it gets here, stay tuned...

IRQ
Posts: 35
Joined: Thu Aug 09, 2012 4:56 pm

Re: Gambas & WiringPi

Sat Apr 13, 2013 10:30 am

Got my Rev 2 board yesterday.

Just tested and using the code below I can toggle GPIO0 just like on a Rev 1 board. Use a multimeter and check GPIO0 [GPIO17] when running program as root in console.

Code: Select all

' Tested using wiringPi version 130207
Public Extern delay(millis As Integer) In "libwiringPi:1"
Public Extern wiringPiSetupGpio() As Integer In "libwiringPi:1"
Public Extern pinModeGpio(pin As Integer, mode As Integer) In "libwiringPi:1"
Public Extern digitalWriteGpio(pin As Integer, state As Integer) In "libwiringPi:1"
Public Extern digitalReadGpio(pin As Integer) As Integer In "libwiringPi:1"

Public Sub Main()
  ' Init library
  If wiringPiSetupGpio() = 0 Then
    Print "wiringPi Init OK..."
  Else
    Print "wiringPi Init Failed!"
    Quit
  Endif
  
  ' Test  
  delay(500)
  Print "Delay function test...."
  
  ' Set GPIO0 [GPIO17] as output
  pinModeGpio(17, 1)
  
  ' Loop forever
  Do
    Wait 3
    digitalWriteGpio(17, 1)
    Print "GPIO0 [GPIO17] is HIGH"
    Wait 3
    digitalWriteGpio(17, 0)
    Print "GPIO0 [GPIO17] is LOW"
  Loop  
End
However that being said I did bump into a wierd issue when testing the same code using wiringPi's Arduino numbering scheme. For unknown reason I get a "Segmentation fault" when trying to set pins direction using pinMode() in Gambas 3.3.4, it works fine using pinModeGpio()??? I will have check and see if I'm missing something obvious...

Hope this helps...

carlsnilsson
Posts: 6
Joined: Sat Nov 16, 2013 11:50 am

Re: Gambas & WiringPi

Sat Nov 16, 2013 12:11 pm

ffrlr wrote:Hi everyone

I am very new to linux, but following threads on here have managed to get my pi up and running Gambas 3.21.
I am quite familiar with VB6, and was hoping to use this knowledge to play around with Gambas on the pi, which seems to be going ok so far.
What I would like to do next is controlling I2C devices from Gambas, has anyone had any success with using Gordon's WiringPi library with Gambas?
If so would you be able to give me some idea of how to start (test program or example) please.
I have no C knowledge at all, and have always used Windows and the VB6 enviroment for programming and developing code, although I have hardware design experience and microprocessor programming in assembler (8051 / PIC etc).

I have trawled the www but can't seem to find anything relevant.

Regards
Tony.
G'day Tony: I was very pleased to read your post from March. I could have written and signed it myself. I have built and programmed a attitude display and Flight Management System for Light Aircraft. It's a prototype that I have been developing for my own use for nearly a decade. It runs on an ex-ebay Advantech SBC 9576 which has a pentium 3 running Win 2K. I have done all the programming in VB6. Tried VB.net and found it too verbose and MS had dropped the MSComm control, which I lived and died by. Lots of RS232 inputs from aircrfat sensors like altimeter, indicated airspeed, GPS etc etc. Also have an independent display unit which is a HP PDA (Ipaq h2110). All very retro stuff, like my aircraft and me! I'm an oldie. Looking ahead, I want to add a pre-processing unit for some of the serial data, some of which comes in nice and slow and other stuff, like the GPS (at about 10 kbps) and Inertial Management Unit (about 20 kbps). That has to be truly embedded and I want to use a RAsberry Pi. I need five serial lines and hence I have to use a couple of ICs like the NXP SC16IS750 to put the serial Rx, Tx's onto I2C or SPI. So I want to use gambas on Deb Wheezy to program the I2C input for further processing.
So, how are you getting along - have you got your I2C or SPI going yet under gambas 3?
Happy to collaborate in the future, but right now I haven't yet got my Pi. I have installed an older gambas on SUSE 10.0 and had a look at it, though. Just to check I could see some serial ports!
Regards Carl

Return to “Other languages”

Who is online

Users browsing this forum: No registered users and 2 guests