BASIC

Author Topic: RPi and GPIO  (Read 3610 times)

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1539
    • View Profile
RPi and GPIO
« on: 2013-May-23 »
Although it would be nice to import direct, the simplest way I could access the GPIO on RaspberryPi was to use the GPIO Utility from wiringPi via SHELLCMD. This can mode set and write to the pins, for at least LED test, but doesn't seem to return the pin read value from the command line (0 or 1) for a button/joystick. Values can be saved to a data file for slower inputs. Anyhow, here's a mini-lib to start off for now.

gpio.gbas
Code: GLBasic [Select]
// --------------------------------- //
// Project: gpio - glb led/button sample
// Start: Thursday, May 23, 2013
// IDE Version: 11.414

GLOBAL PIN_LED = 4 // GPIO 23
GLOBAL PIN_BUTTON = 0 // GPIO 17

GLOBAL led_state = 0
GLOBAL button_state = 0
GLOBAL app_timer = 0


setup()
loop()

FUNCTION setup:
        gpio.init()
        gpio.pinMode(PIN_BUTTON, P_UP)
        gpio.pinMode(PIN_LED, P_OUT)
ENDFUNCTION

FUNCTION loop_led:
        IF led_state = 0
                led_state = 1
                gpio.digitalWrite(PIN_LED, HIGH);
        ELSE
                led_state = 0
                gpio.digitalWrite(PIN_LED, LOW);
        ENDIF
ENDFUNCTION

FUNCTION loop_draw:
        LOCAL x = (50 * led_state)
        DRAWRECT x, 0, 50, 50, 0xffffff
        x = (50 * button_state)
        DRAWRECT x, 50, 50, 50, 0xffffff
ENDFUNCTION

FUNCTION loop:
        LOCAL ti = 0
        WHILE ti < 10 AND TRUE
                CLEARSCREEN 0x00ff00
                INC app_timer
                IF app_timer = 10
                        app_timer = 0
                        loop_led()
                        INC ti
                        button_state = gpio.digitalRead(PIN_BUTTON) // :(
                ENDIF
                loop_draw()
                PRINT ti, 0, 110
                PRINT app_timer, 50, 110
                SHOWSCREEN
        WEND
ENDFUNCTION
 

lib_gpio.gbas
Code: GLBasic [Select]
// --------------------------------- //
// Project: matchy_gpio_lib
// RaspberryPi wiringPi GPIO Utility: http://wiringpi.com/the-gpio-utility/
// Start: Thursday, May 23, 2013
// IDE Version: 11.414

GLOBAL P_IN =           0
GLOBAL P_OUT =          1
GLOBAL P_UP =           2
GLOBAL P_DOWN =         3
GLOBAL P_PWM =          4
GLOBAL P_TRI =          5
GLOBAL P_CLOCK =        6

GLOBAL LOW =            0
GLOBAL HIGH =           1

GLOBAL mode_name$[]

GLOBAL gpio AS _gpio

TYPE _gpio
        FUNCTION digitalRead#: pin_num
                RETURN gpio_cmd("gpio read " + pin_num) // :(
        ENDFUNCTION

        FUNCTION digitalWrite: pin_num, pin_val
                gpio_cmd("gpio write " + pin_num + " " + pin_val)
        ENDFUNCTION

        FUNCTION pinMode: pin_num, pin_mode
                gpio_cmd("gpio mode " + pin_num + " " + mode_name$[pin_mode])
        ENDFUNCTION

        FUNCTION gpio_cmd#: cmd$
                LOCAL rv#, ok
        //      ?IF WIN32
        //              DEBUG cmd$ + "\n"
        //      ?ELSE
                        ok = SHELLCMD(cmd$, TRUE, TRUE, rv#)
        //      ?ENDIF
        //      RETURN INTEGER(rv)
                RETURN rv# // :(
        ENDFUNCTION

        FUNCTION init:
                DIM mode_name$[7]

                mode_name$[P_IN]                = "in"
                mode_name$[P_OUT]               = "out"
                mode_name$[P_UP]                = "in"
                mode_name$[P_DOWN]              = "down"
                mode_name$[P_PWM]               = "pwm"
                mode_name$[P_TRI]               = "in"
                mode_name$[P_CLOCK]             = "clock"
        ENDFUNCTION
ENDTYPE
 
8)
« Last Edit: 2013-May-23 by Schranz0r »

Offline Hark0

  • Prof. Inline
  • *****
  • Posts: 1020
  • Geek Developer
    • View Profile
    • LitioPixel - Desarrollo de videojuegos con GLBasic | Videogame development with GLBasic
Re: RPi and GPIO
« Reply #1 on: 2013-May-23 »
hmmm

Very interesting... and... this piece of software are good for Arduino too?

 :P
http://litiopixel.blogspot.com
litiopixel.blogspot.com - Desarrollo videojuegos Indie · Pixel-Art · Retroinformática · Electrónica Development Indie Videogames · Pixel-Art · Retrocomputing · Electronic

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1539
    • View Profile
Re: RPi and GPIO
« Reply #2 on: 2013-May-23 »
Hark0, No, you can't compile for Arduino or other microcontrollers unlike the RPi with an ARM microprocessor and OS with display output. Is there something specific you need on Arduino?  :glare:

Offline Marmor

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 908
  • 96A285CC
    • View Profile
    • my youtube channel
Re: RPi and GPIO
« Reply #3 on: 2013-May-23 »
 :good: matchy !! i need this inline now  :nana:

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1539
    • View Profile
Re: RPi and GPIO
« Reply #4 on: 2013-May-23 »
Marmor, why is that?  :-[

Simply right now, I just wonder how to retrieve the value from the shell command so that I can wire up a button.

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3336
  • Integrated Brain
    • View Profile
Re: RPi and GPIO
« Reply #5 on: 2013-May-23 »
And something like this will be posible whit COM Commands?¿... I focus more in Arduino, because the CGG don't runs whit AVR

Whit Serial Library (Com ports), and TX-RX of the IC you can change the behaviour of the chip..

Sending in example a "a" in the serial, change whit a if from input to ouput and change the impedanze of the Pin, I thikn have to works, the only problem you have to stop the main function in the chip, and call the setting out of this bucle.

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1539
    • View Profile
Re: RPi and GPIO
« Reply #6 on: 2013-May-24 »
mentalthink, Arduino serial requires comms from a device already running GLB so it makes no sense to compare to RPi that can do it internally and alone.

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1539
    • View Profile
Re: RPi and GPIO
« Reply #7 on: 2013-May-31 »
The inline function has an error: invalid type argument of `unary *':whistle: Also any ideas how to import mmap as that is declared like:  :-[
Code: GLBasic [Select]
extern void *mmap (void *__addr, size_t __len, int __prot, int __flags, int __fd, __off_t __offset) __THROW;
 

Code: GLBasic [Select]
GLOBAL gpio% // global pointer to the registers (from mmap)

FUNCTION INP_GPIO:g% // by KH
INLINE
   return *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3));
ENDINLINE
ENDFUNCTION
 

(:rtfm:Any INLINE/ENDLINE colour scheme for the thread code post)?  :zzz:
« Last Edit: 2013-May-31 by matchy »