Menu

#128 input.vector( channel, nBits); added

New
nobody
None
Low
Enhancement
2016-02-28
2016-02-28
robert
No

I have modified the package by adding a new method, GPIO.input.vector( channel, nBits) where channel can be from 31-0 specifying the lowest GPIO pin to read, and nBits can be from 1 - 32 specifying the number of bits to be read. The result is returned as an integer. The routine does this by a single read of the GPIO register, shifts the specified bit to the LSB position of the return value, then masks off any unwanted bits.

For example, this method is useful for read 8 bits from an external A/D converter. But please note that higher numbered GPIO pins are more significant.

Three files needed to be modified: py_gpio.c gpio.c gpio.h
common.c was also changed by adding a coimment line.

Test Example:

        GPIO.setmode(GPIO.BCM)

        GPIO.setup( 27, GPIO.IN, GPIO.PUD_UP)
        GPIO.setup( 26, GPIO.IN, GPIO.PUD_DOWN)
        GPIO.setup( 25, GPIO.IN, GPIO.PUD_UP)
        GPIO.setup( 24, GPIO.IN, GPIO.PUD_DOWN)
        GPIO.setup( 23, GPIO.IN, GPIO.PUD_DOWN)
        GPIO.setup( 22, GPIO.IN, GPIO.PUD_UP)
        GPIO.setup( 21, GPIO.IN, GPIO.PUD_DOWN)
        GPIO.setup( 20, GPIO.IN, GPIO.PUD_UP)

        vec = []
        for i in range(27, 19, -1):
            v = GPIO.input( i )
            vec.append(v)
        print vec

        vec2 = GPIO.input_vector( 20, 6 )
        print "VECT2:  %d  0x%X" % (vec2, 0x7ffffffff & vec2)

        GPIO.cleanup()

result:

$ sudo python test2.py
[1, 0, 1, 0, 0, 1, 0, 1]
VECT2:  37  0x25
4 Attachments

Discussion


Log in to post a comment.