From c6d7c78772d7aa6e2c89e88ed7c90c615fc92525 Mon Sep 17 00:00:00 2001 From: Luca Marchiorello Date: Wed, 19 Jun 2019 09:33:55 +0000 Subject: [PATCH] added support for Debian 9 official BB images --- backend/config.py | 117 +++++++++++++++++------------- backend/flash.py | 132 +++++++++++++++++++--------------- scripts/driveboardapp.service | 11 +++ 3 files changed, 155 insertions(+), 105 deletions(-) create mode 100644 scripts/driveboardapp.service diff --git a/backend/config.py b/backend/config.py index 1f9cf13..ecc48c6 100644 --- a/backend/config.py +++ b/backend/config.py @@ -128,6 +128,13 @@ import RPi.GPIO conf['hardware'] = 'raspberrypi' except ImportError: + # try to import Adafruit GPIO library for Debian 9 based official BB image + try: + global GPIO + import Adafruit_BBIO.GPIO as GPIO + except ImportError: + # Adafruit GPIO library unavailable. Do nothing and continue. + pass # os.uname() on BBB: # ('Linux', 'lasersaur', '3.8.13-bone20', # '#1 SMP Wed May 29 06:14:59 UTC 2013', 'armv7l') @@ -180,24 +187,28 @@ for pin46 in pin46list: os.system("echo gpio > %s" % (pin46)) - try: - fw = file("/sys/class/gpio/export", "w") - fw.write("%d" % (71)) + if ('GPIO' in globals()): + GPIO.setup("P8_46", GPIO.OUT) + GPIO.output("P8_46", GPIO.HIGH) + else: + try: + fw = file("/sys/class/gpio/export", "w") + fw.write("%d" % (71)) + fw.close() + except IOError: + # probably already exported + pass + # set the gpio pin to output + # echo out > /sys/class/gpio/gpio71/direction + fw = file("/sys/class/gpio/gpio71/direction", "w") + fw.write("out") + fw.close() + # set the gpio pin high + # echo 1 > /sys/class/gpio/gpio71/value + fw = file("/sys/class/gpio/gpio71/value", "w") + fw.write("1") + fw.flush() fw.close() - except IOError: - # probably already exported - pass - # set the gpio pin to output - # echo out > /sys/class/gpio/gpio71/direction - fw = file("/sys/class/gpio/gpio71/direction", "w") - fw.write("out") - fw.close() - # set the gpio pin high - # echo 1 > /sys/class/gpio/gpio71/value - fw = file("/sys/class/gpio/gpio71/value", "w") - fw.write("1") - fw.flush() - fw.close() ### Set up atmega328 reset control - BeagleBone Black # The reset pin is connected to GPIO2_9 (2*32+9 = 73). @@ -209,24 +220,28 @@ for pin44 in pin44list: os.system("echo gpio > %s" % (pin44)) - try: - fw = file("/sys/class/gpio/export", "w") - fw.write("%d" % (73)) + if ('GPIO' in globals()): + GPIO.setup("P8_44", GPIO.OUT) + GPIO.output("P8_44", GPIO.HIGH) + else: + try: + fw = file("/sys/class/gpio/export", "w") + fw.write("%d" % (73)) + fw.close() + except IOError: + # probably already exported + pass + # set the gpio pin to output + # echo out > /sys/class/gpio/gpio73/direction + fw = file("/sys/class/gpio/gpio73/direction", "w") + fw.write("out") + fw.close() + # set the gpio pin high + # echo 1 > /sys/class/gpio/gpio73/value + fw = file("/sys/class/gpio/gpio73/value", "w") + fw.write("1") + fw.flush() fw.close() - except IOError: - # probably already exported - pass - # set the gpio pin to output - # echo out > /sys/class/gpio/gpio73/direction - fw = file("/sys/class/gpio/gpio73/direction", "w") - fw.write("out") - fw.close() - # set the gpio pin high - # echo 1 > /sys/class/gpio/gpio73/value - fw = file("/sys/class/gpio/gpio73/value", "w") - fw.write("1") - fw.flush() - fw.close() ### read stepper driver configure pin GPIO2_12 (2*32+12 = 76). # Low means Geckos, high means SMC11s @@ -236,22 +251,26 @@ for pin39 in pin39list: os.system("echo gpio > %s" % (pin39)) - try: - fw = file("/sys/class/gpio/export", "w") - fw.write("%d" % (76)) + if ('GPIO' in globals()): + GPIO.setup("P8_39", GPIO.IN) + ret = GPIO.input("P8_39") + else: + try: + fw = file("/sys/class/gpio/export", "w") + fw.write("%d" % (76)) + fw.close() + except IOError: + # probably already exported + pass + # set the gpio pin to input + fw = file("/sys/class/gpio/gpio76/direction", "w") + fw.write("in") + fw.close() + # set the gpio pin high + fw = file("/sys/class/gpio/gpio76/value", "r") + ret = fw.read() fw.close() - except IOError: - # probably already exported - pass - # set the gpio pin to input - fw = file("/sys/class/gpio/gpio76/direction", "w") - fw.write("in") - fw.close() - # set the gpio pin high - fw = file("/sys/class/gpio/gpio76/value", "r") - ret = fw.read() - fw.close() - # print "Stepper driver configure pin is: " + str(ret) + # print "Stepper driver configure pin is: " + str(ret) elif conf['hardware'] == 'raspberrypi': if not conf['firmware']: diff --git a/backend/flash.py b/backend/flash.py index 5d6d343..2c82ba0 100644 --- a/backend/flash.py +++ b/backend/flash.py @@ -86,39 +86,49 @@ def flash_upload(serial_port=conf['serial_port'], resources_dir=conf['rootdir'], # Setting it to low triggers a reset. # echo 71 > /sys/class/gpio/export try: - fw = file("/sys/class/gpio/export", "w") - fw.write("%d" % (71)) + import Adafruit_BBIO.GPIO as GPIO + GPIO.setup("P8_46", GPIO.OUT) + GPIO.output("P8_46", GPIO.LOW) + GPIO.setup("P8_44", GPIO.OUT) + GPIO.output("P8_44", GPIO.LOW) + time.sleep(0.5) + GPIO.output("P8_46", GPIO.HIGH) + GPIO.output("P8_44", GPIO.HIGH) + except ImportError: + try: + fw = file("/sys/class/gpio/export", "w") + fw.write("%d" % (71)) + fw.close() + fwb = file("/sys/class/gpio/export", "w") + fwb.write("%d" % (73)) + fwb.close() + except IOError: + # probably already exported + pass + # set the gpio pin to output + # echo out > /sys/class/gpio/gpio71/direction + fw = file("/sys/class/gpio/gpio71/direction", "w") + fw.write("out") fw.close() - fwb = file("/sys/class/gpio/export", "w") - fwb.write("%d" % (73)) + fwb = file("/sys/class/gpio/gpio73/direction", "w") + fwb.write("out") fwb.close() - except IOError: - # probably already exported - pass - # set the gpio pin to output - # echo out > /sys/class/gpio/gpio71/direction - fw = file("/sys/class/gpio/gpio71/direction", "w") - fw.write("out") - fw.close() - fwb = file("/sys/class/gpio/gpio73/direction", "w") - fwb.write("out") - fwb.close() - # set the gpio pin low -> high - # echo 1 > /sys/class/gpio/gpio71/value - fw = file("/sys/class/gpio/gpio71/value", "w") - fw.write("0") - fw.flush() - fwb = file("/sys/class/gpio/gpio73/value", "w") - fwb.write("0") - fwb.flush() - time.sleep(0.5) - fw.write("1") - fw.flush() - fw.close() - fwb.write("1") - fwb.flush() - fwb.close() - time.sleep(0.1) + # set the gpio pin low -> high + # echo 1 > /sys/class/gpio/gpio71/value + fw = file("/sys/class/gpio/gpio71/value", "w") + fw.write("0") + fw.flush() + fwb = file("/sys/class/gpio/gpio73/value", "w") + fwb.write("0") + fwb.flush() + time.sleep(0.5) + fw.write("1") + fw.flush() + fw.close() + fwb.write("1") + fwb.flush() + fwb.close() + time.sleep(0.1) elif conf['hardware'] == 'raspberrypi': print "Flashing from Raspberry Pi ..." import thread @@ -148,33 +158,43 @@ def reset_atmega(): print "Resetting Atmega ..." if conf['hardware'] == 'beaglebone': try: - fw = file("/sys/class/gpio/export", "w") - fw.write("%d" % (71)) + import Adafruit_BBIO.GPIO as GPIO + GPIO.setup("P8_46", GPIO.OUT) + GPIO.output("P8_46", GPIO.LOW) + GPIO.setup("P8_44", GPIO.OUT) + GPIO.output("P8_44", GPIO.LOW) + time.sleep(0.2) + GPIO.output("P8_46", GPIO.HIGH) + GPIO.output("P8_44", GPIO.HIGH) + except ImportError: + try: + fw = file("/sys/class/gpio/export", "w") + fw.write("%d" % (71)) + fw.close() + fwb = file("/sys/class/gpio/export", "w") + fwb.write("%d" % (73)) + fwb.close() + except IOError: + pass + fw = file("/sys/class/gpio/gpio71/direction", "w") + fw.write("out") + fw.close() + fwb = file("/sys/class/gpio/gpio73/direction", "w") + fwb.write("out") + fwb.close() + fw = file("/sys/class/gpio/gpio71/value", "w") + fw.write("0") + fw.flush() + fwb = file("/sys/class/gpio/gpio73/value", "w") + fwb.write("0") + fwb.flush() + time.sleep(0.2) + fw.write("1") + fw.flush() fw.close() - fwb = file("/sys/class/gpio/export", "w") - fwb.write("%d" % (73)) + fwb.write("1") + fwb.flush() fwb.close() - except IOError: - pass - fw = file("/sys/class/gpio/gpio71/direction", "w") - fw.write("out") - fw.close() - fwb = file("/sys/class/gpio/gpio73/direction", "w") - fwb.write("out") - fwb.close() - fw = file("/sys/class/gpio/gpio71/value", "w") - fw.write("0") - fw.flush() - fwb = file("/sys/class/gpio/gpio73/value", "w") - fwb.write("0") - fwb.flush() - time.sleep(0.2) - fw.write("1") - fw.flush() - fw.close() - fwb.write("1") - fwb.flush() - fwb.close() elif conf['hardware'] == 'raspberrypi': import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # use chip pin number diff --git a/scripts/driveboardapp.service b/scripts/driveboardapp.service new file mode 100644 index 0000000..0374f7b --- /dev/null +++ b/scripts/driveboardapp.service @@ -0,0 +1,11 @@ +[Unit] +Description=Lasersaur driveboardapp +After=network.target + +[Service] +Type=simple +Restart=always +ExecStart=/usr/bin/python /root/driveboardapp/backend/app.py + +[Install] +WantedBy=multi-user.target