Re: Raspberry pi 4 GPIO ports control

Discussions related to network, internet and socket programming; also to serial, parallel, Bluetooth, USB etc.
Hated Moron

Re: Raspberry pi 4 GPIO ports control

Post by Hated Moron »

On 25/09/2023 15:54, Katsaras Christos wrote (cross-posted from the Discussion Group):
I'm a really novice in this and I'd like to ask how I could get control of GPIO ports, by using BBC Basic commands.
Check out the supplied gpiolib.bbc library and the gpiodemo.bbc example program. Hopefully they should be fairly self-explanatory; the library includes some comments describing its use, the demo program is listed below:

Code: Select all

      REM Initialise GPIO:
      INSTALL @lib$ + "gpiolib"
      GPIO% = FN_gpio_setup

      REM Pin numbers to activate, in sequence:
      DATA 17, 23, 25, 12, 16, 26

      REM Set to input first:
      RESTORE
      FOR I% = 1 TO 6
        READ pin%
        PROC_gpio_inp(GPIO%, pin%)
      NEXT

      REM Set pins to output:
      RESTORE
      FOR I% = 1 TO 6
        READ pin%
        PROC_gpio_out(GPIO%, pin%)
      NEXT

      REM Cycle LEDs in sequence:
      REPEAT
        RESTORE
        FOR I% = 1 TO 6
          READ pin%
          PROC_gpio_set(GPIO%, 1 << pin%)
          WAIT 20
          PROC_gpio_clr(GPIO%, 1 << pin%)
        NEXT
      UNTIL FALSE
      END
Hated Moron

Re: Raspberry pi 4 GPIO ports control

Post by Hated Moron »

Hated Moron wrote: Mon 25 Sep 2023, 17:44 Check out the supplied gpiolib.bbc library and the gpiodemo.bbc example program.
Did this help?
Ric
Posts: 200
Joined: Tue 17 Apr 2018, 21:03

Re: Raspberry pi 4 GPIO ports control

Post by Ric »

I have taken the plunge in to the world of Raspberry Pi, and after much frustration yesterday trying to set it up and get started, I now have flashing LEDs. Thank you for the nudge in the right direction

Kind regards Ric
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Ric
Posts: 200
Joined: Tue 17 Apr 2018, 21:03

Re: Raspberry pi 4 GPIO ports control

Post by Ric »

I now have constructed the basis for an app to turn the lights on/off in my house. This works fine as long as the socket connection isnt lost. I have tried reinstating the socket but it fails(i think at the client end). Is there an easy way to reistablish the lost connection

Kind Regards Ric
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Hated Moron

Re: Raspberry pi 4 GPIO ports control

Post by Hated Moron »

Ric wrote: Wed 08 Nov 2023, 20:40 Is there an easy way to reestablish the lost connection
I can't say that I've ever experienced a similar difficulty. My little inverter app, in the event of the connection being lost, simply reconnects and carries on. Of course for that to work the server must be in a state where it can receive new connections.

Do you know why the connection is being lost in the first place? Perhaps if you can discover that it may indicate why the subsequent attempt to reconnect is also failing.
Ric
Posts: 200
Joined: Tue 17 Apr 2018, 21:03

Re: Raspberry pi 4 GPIO ports control

Post by Ric »

My apologies Richard, I had the reset of the sockets accidently incorporated in to ON ERROR as well as by result% = 0, whoops!!

Slightly off topic, how do I load files to my phone to use with bbcsdl and is it normal for all the library files to be greyed out on my phone.

Kind Regards Ric
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Hated Moron

Re: Raspberry pi 4 GPIO ports control

Post by Hated Moron »

Ric wrote: Fri 10 Nov 2023, 19:03 Slightly off topic, how do I load files to my phone to use with bbcsdl
Android or iOS? The procedure is somewhat different.
and is it normal for all the library files to be greyed out on my phone.
Yes, I did that because of how easy it would otherwise be to accidentally run a library, which of course is guaranteed to fail.
Ric
Posts: 200
Joined: Tue 17 Apr 2018, 21:03

Re: Raspberry pi 4 GPIO ports control

Post by Ric »

Thanks Richard, my phone is a pixel6.
Also is there a way of creating a time out on the socket listen/connection routine, because I would like to have many connections and if the server is waiting for a connection to one of them indefinitely the others obviously can't communicate.

Kind regards Ric
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Hated Moron

Re: Raspberry pi 4 GPIO ports control

Post by Hated Moron »

Ric wrote: Fri 10 Nov 2023, 22:44 my phone is a pixel6.
Being Android that makes it particularly easy: connect the phone to your (presumably Windows) PC using a standard USB lead; the phone will likely prompt you to enable File Transfer, so do that.

Once connected, simply treat the phone like any other external Windows drive (e.g. a USB memory stick). You can directly edit BBC BASIC programs on the phone by navigating to the relevant directory, which is likely to be Android/data/com.rtrussell.bbcbasic/files/ (there is some variability between versions of Android in this regard). This is the directory which corresponds to @usr$.
I would like to have many connections
Simply use FN_check_connectionM() (M for multiple) rather than FN_check_connection().

Since BBC BASIC isn't multi-threaded you can't expect to write a server which will handle hundreds or thousands of concurrent connections, as you can in some languages. You have no choice but to use polling in a single 'thread', which will place a practical limit on the number of simultaneous clients.

How often you poll will have an impact on power consumption, which may mean it is unwise to run the server on a battery-powered device.

I should add, if it isn't obvious, do be very careful with your choice of port number. Ports are a highly limited resource (65536 in total) which have to serve the requirements of the entire world!! Whatever you do, don't use the same port number as any of the existing BBC BASIC demos (client.bbc, server.bbc and lanchat.bbc). In choosing a port, definitely do a Google search to discover who else is using the same one in case the duplication could cause problems.

I tend to assume that everybody writing socket-based programs will be aware of this, but that might not always be the case.
Ric
Posts: 200
Joined: Tue 17 Apr 2018, 21:03

Re: Raspberry pi 4 GPIO ports control

Post by Ric »

Thanks again Richard, I now have the program loaded on my phone, but it crashes! Or more to the point, cant OPENUP the backup file.

Code: Select all

  
  430 TIME = 0
  440 REPEAT
  450   C% = OPENUP("testFile")
  460 UNTIL C% <> 0 OR TIME > 3000
  470 IF C% = 0 THEN PROCcleanUp : QUIT
This is the code, but i can see nothing wrong and it works on my laptop fine. I am assuming I have to do something different on a phone/tablet?

Back on sockets, I assume it would be better to poll as i only require 10 connections? And i know nothing about sockets or how they work.

Kind Regards Ric
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023