Forums - Open Redstone Engineers
Un-pythonic program for the Beaglebone Black - Printable Version

+- Forums - Open Redstone Engineers (https://forum.openredstone.org)
+-- Forum: Off-Topic (https://forum.openredstone.org/forum-4.html)
+--- Forum: Programming (https://forum.openredstone.org/forum-8.html)
+--- Thread: Un-pythonic program for the Beaglebone Black (/thread-1806.html)



Un-pythonic program for the Beaglebone Black - Malcolmforde - 01-01-2014

I got my beaglebone black a few days ago, and I finally wrote a program for it. It is very ugly and I have no idea why it works, but it does.

When you start it, it asks you about how much LEDs you have connected (which I should rewrite, because its the GPIO pin that the program ends on) and the GPIO pin that the first LED is on. Then it turns on the LEDs between those two pins in order, then turns them off in order, etc. until you press a button to stop it.

Here is the code:
Code:
import Adafruit_BBIO.GPIO as GPIO
import time

leds = int(raw_input("LEDs (starting LED + this): "))
ledStart = int(raw_input("Starting LED: "))
if ledStart < 10: #Don't you dare choose a value less than 10
    print "Error! Starting position less than 10."
    exit()
curLed = ledStart
curState = GPIO.HIGH
step = 0

GPIO.setup("P8_9", GPIO.IN) #Setup the button
GPIO.add_event_detect("P8_9", GPIO.FALLING)

def swapstate(c): #Swap states
    if c == GPIO.HIGH:
        c = GPIO.LOW
    elif c == GPIO.LOW:
        c = GPIO.HIGH
    else:
        c = "This is a horrible way to pass an error"
    return c

def reset(): #infitely ugly reset code
    global step
    global leds
    global curState
    global curLed
    global ledStart
    if step == leds:
        step = 0
        curLed = ledStart
        curState = swapstate(curState)
    else:
        curLed = ledStart + step

while step < leds: #Setup all LEDS
    curLed = ledStart + step
    GPIO.setup("P8_" + str(curLed), GPIO.OUT)
    step += 1

curLed = ledStart #Prepare for next set of code
step = 0

while True: #Da thing that actually controlls the LEDS
    if GPIO.event_detected("P8_9"): #Shutdown mechanism
         exit()

    GPIO.output("P8_" + str(curLed), curState) #Control LEDS
    step += 1
    curLed = ledStart + step

    reset()
    time.sleep(0.5)

As you can see, the code sucks. I really hate that list of globals I have and really want to fix that. I already have noticed I have some unnecessary lines and operations, and I think there are some more.

Also, don't know what the beaglebone black is? LOOK IT UP.

[Image: B9vLiVI.jpg]


RE: Un-pythonic program for the Beaglebone Black - Chibill - 01-01-2014

Would this work with the raspberry pi considering that the beaglebone is a Raspberry pi with a second CPU and the GPIO pins in the adunios configeration.


RE: Un-pythonic program for the Beaglebone Black - Malcolmforde - 01-01-2014

(01-01-2014, 03:31 AM)Chibill Wrote: Would this work with the raspberry pi considering that the beaglebone is a Raspberry pi with a second CPU and the GPIO pins in the adunios configeration.
It should, but you would need to swap out the GPIO libraries, and change some code to use the raspis config (I doubt it has P8 and P9 rails, etc).

I just remembered that I have an image on my clipboard of it in action *adds*

Also, the Beaglebone Black is NOT a raspi (its not even made by the Raspi devs) and the pins are not in the Arduinos configuration. I have no idea where you got this information.


RE: Un-pythonic program for the Beaglebone Black - Chibill - 01-01-2014

I was reading it from back when it was new new on Element14 where I got my Raspi