Skip to main content
added 61 characters in body
Source Link
Graipher
  • 41.1k
  • 7
  • 69
  • 133

You should start by organising your code better. Try to avoid global variables and group together code in functions.

In addition, you should learn about list comprehensions. They make initializing your signals way easier.

import RPi.GPIO as GPIO

def twos_comp(i):
    ...

def gpio_setup(inputs, outputs):
    GPIO.setmode(GPIO.BCM)
    for pin in inputs:
        GPIO.setup(pin, GPIO.IN)
    for pin in outputs:
        GPIO.setup(pin, GPIO.OUT)
    
def main(input_pin, output_pins):    
    signals = [1 if i % 2 else -1 for i in range(26)]
    bit_signals = [twos_comp(i) for i in signals]
    
    for i in bit_signals:
        GPIO.wait_for_edge(5input_pin, GPIO.RISING)
        for counterpin, k in enumeratezip(output_pins, str(i), 21):
            GPIO.output(counterpin, k) #k will be 0 or 1 which is accepted input

if __name__ == "__main__":
    gpio_setup(in, out = [5], range(21, 29)
    gpio_setup(in, out)
    main(in[0], out)
    GPIO.cleanup()

And finally, there are better ways to do twos complement. A nice one is for example here.

You should start by organising your code better. Try to avoid global variables and group together code in functions.

In addition, you should learn about list comprehensions. They make initializing your signals way easier.

import RPi.GPIO as GPIO

def twos_comp(i):
    ...

def gpio_setup(inputs, outputs):
    GPIO.setmode(GPIO.BCM)
    for pin in inputs:
        GPIO.setup(pin, GPIO.IN)
    for pin in outputs:
        GPIO.setup(pin, GPIO.OUT)
    
def main():    
    signals = [1 if i % 2 else -1 for i in range(26)]
    bit_signals = [twos_comp(i) for i in signals]
    
    for i in bit_signals:
        GPIO.wait_for_edge(5, GPIO.RISING)
        for counter, k in enumerate(str(i), 21):
            GPIO.output(counter, k) #k will be 0 or 1 which is accepted input

if __name__ == "__main__":
    gpio_setup([5], range(21, 29))
    main()
    GPIO.cleanup()

And finally, there are better ways to do twos complement. A nice one is for example here.

You should start by organising your code better. Try to avoid global variables and group together code in functions.

In addition, you should learn about list comprehensions. They make initializing your signals way easier.

import RPi.GPIO as GPIO

def twos_comp(i):
    ...

def gpio_setup(inputs, outputs):
    GPIO.setmode(GPIO.BCM)
    for pin in inputs:
        GPIO.setup(pin, GPIO.IN)
    for pin in outputs:
        GPIO.setup(pin, GPIO.OUT)
    
def main(input_pin, output_pins):    
    signals = [1 if i % 2 else -1 for i in range(26)]
    bit_signals = [twos_comp(i) for i in signals]
    
    for i in bit_signals:
        GPIO.wait_for_edge(input_pin, GPIO.RISING)
        for pin, k in zip(output_pins, str(i)):
            GPIO.output(pin, k) #k will be 0 or 1 which is accepted input

if __name__ == "__main__":
    in, out = [5], range(21, 29)
    gpio_setup(in, out)
    main(in[0], out)
    GPIO.cleanup()

And finally, there are better ways to do twos complement. A nice one is for example here.

Source Link
Graipher
  • 41.1k
  • 7
  • 69
  • 133

You should start by organising your code better. Try to avoid global variables and group together code in functions.

In addition, you should learn about list comprehensions. They make initializing your signals way easier.

import RPi.GPIO as GPIO

def twos_comp(i):
    ...

def gpio_setup(inputs, outputs):
    GPIO.setmode(GPIO.BCM)
    for pin in inputs:
        GPIO.setup(pin, GPIO.IN)
    for pin in outputs:
        GPIO.setup(pin, GPIO.OUT)
    
def main():    
    signals = [1 if i % 2 else -1 for i in range(26)]
    bit_signals = [twos_comp(i) for i in signals]
    
    for i in bit_signals:
        GPIO.wait_for_edge(5, GPIO.RISING)
        for counter, k in enumerate(str(i), 21):
            GPIO.output(counter, k) #k will be 0 or 1 which is accepted input

if __name__ == "__main__":
    gpio_setup([5], range(21, 29))
    main()
    GPIO.cleanup()

And finally, there are better ways to do twos complement. A nice one is for example here.