Skip to content
Browse files

Add stuff for Cape Contest submission.

  • Loading branch information...
1 parent 272f41f commit 4f8af59e4d84ffb15b8ee83655a41887306601f3 @mranostay committed Nov 17, 2012
View
BIN beaglebone_geiger_cape_rev8-pcb.pdf
Binary file not shown.
View
BIN beaglebone_geiger_cape_rev8-schematic.pdf
Binary file not shown.
View
218 cosm-geigerbone.py
@@ -0,0 +1,218 @@
+#!/usr/bin/env python
+"""
+Copyright 2012, Matt Ranostay. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are
+permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice, this list of
+ conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ of conditions and the following disclaimer in the documentation and/or other materials
+ provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY MATT RANOSTAY ``AS IS'' AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATT RANOSTAY OR
+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+The views and conclusions contained in the software and documentation are those of the
+authors and should not be interpreted as representing official policies, either expressed
+or implied, of Matt Ranostay.
+"""
+
+import atexit
+import os
+import select
+import sys
+
+from datetime import datetime
+from time import sleep, strftime
+
+try:
+ import eeml
+except ImportError, e:
+ print >>sys.stderr, "Cannot load Cosm (%s) module." % str(e)
+ print >>sys.stderr, "Aborting."
+ sys.exit(1)
+
+try:
+ from bbio import *
+ from bbio.bbio import _setReg, _pinMux
+except ImportError, e:
+ print >>sys.stderr, "No pybbio module found. Aborting."
+ sys.exit(1)
+
+### Linux Defines ###
+SYSFS_GPIO_DIR = "/sys/class/gpio"
+
+### BeagleBone Defines ###
+GPIO_MAX = 66
+
+### Parsing Functions ###
+
+def get_env_value(name):
+ if os.environ.has_key(name):
+ return os.environ[name]
+ return ""
+
+def error_out(string):
+ print >>sys.stderr, string
+ print >>sys.stderr, "Aborting."
+ sys.exit(1)
+
+def return_integer(value, range_count = 0, name = ""):
+ if value.isdigit():
+ value = int(value)
+ if range_count:
+ if value in range(range_count):
+ return value
+ elif value:
+ return value
+
+ error_out("Invalid value passed for '%s'" % name)
+
+### GPIO Control Functions ###
+
+def toggle_gpio(gpio, state = 1):
+ try:
+ with open(SYSFS_GPIO_DIR + "/%sexport" % ("un" if not state else ""), "w") as f:
+ f.write(str(gpio))
+ except IOError, e:
+ """ Probably already exported """
+ pass
+
+def set_gpio_direction(gpio, direction = "in"):
+ with open(SYSFS_GPIO_DIR + "/gpio%s/direction" % gpio, "w") as f:
+ f.write(str(direction))
+
+def set_interrupt_point(gpio, detect = "rising"):
+ with open(SYSFS_GPIO_DIR + "/gpio%s/edge" % gpio, "w") as f:
+ f.write(str(detect))
+
+def enable_gpio(gpio):
+ toggle_gpio(gpio, state = 1)
+ set_gpio_direction(gpio, direction = "in")
+ set_interrupt_point(gpio, detect = "rising")
+
+### No feedback loop. So know what you are doing... ###
+SYSFS_PWM = "/sys/class/pwm/ehrpwm.1:0/"
+
+def setup_pwm():
+ _setReg(CM_PER_EPWMSS1_CLKCTRL, 0x2)
+ _pinMux("gpmc_a2", 6)
+
+ with open(SYSFS_PWM + "duty_percent", "w") as f:
+ f.write("0")
+
+ try:
+ with open(SYSFS_PWM + "run", "w") as f:
+ f.write("0")
+ except IOError, e:
+ pass
+
+ ### 20 Khz ###
+ with open(SYSFS_PWM + "period_freq", "w") as f:
+ f.write("20000")
+
+ with open(SYSFS_PWM + "duty_percent", "w") as f:
+ f.write("40")
+
+ with open(SYSFS_PWM + "run", "w") as f:
+ f.write("1")
+
+def shutdown_pwm():
+ with open(SYSFS_PWM + "run", "w") as f:
+ f.write("0")
+
+### Disable GPIO ###
+disable_gpio = lambda gpio: toggle_gpio(gpio, state = 0)
+
+def get_value(data, seconds = 60):
+ now = datetime.now()
+ count = 0
+ for i in data:
+ if (now - i).seconds >= seconds:
+ data.remove(i)
+ else:
+ count +=1
+
+ return (count, data)
+
+def start_reporting(pac, gpio):
+ f = open(SYSFS_GPIO_DIR + "/gpio%s/value" % gpio, "r")
+ fileno = f.fileno()
+
+ poller = select.poll()
+ poller.register(fileno, select.POLLPRI)
+
+ run_time = datetime.now()
+ data = []
+
+ ### Initial update interval ###
+ UPDATE_INTERVAL = 60
+
+ while True:
+ events = poller.poll(500)
+
+ for fd, flags in events:
+ if ((flags & select.POLLPRI) and not f.read()):
+ data.append(datetime.now())
+
+ now = datetime.now()
+ if (now - run_time).seconds >= UPDATE_INTERVAL:
+ ### Update every 5 seconds ###
+ UPDATE_INTERVAL = 5
+ timestamp = strftime("%Y-%m-%d %H:%M:%S")
+ try:
+ value, data = get_value(data, seconds = 60)
+ pac.update([eeml.Data("geiger-cpm", value)])
+ pac.put()
+ except Exception, e:
+ """ *sigh* all exceptions are simple Exception()
+ so we can only parse the string message passed.
+ """
+ if str(e) == 'Bad Gateway':
+ # Try Uploading Again #
+ sleep(1)
+ continue
+ print e
+ #print "CPM (last 60 seconds):", value, "Time:", timestamp
+ run_time = datetime.now()
+ count = 0
+
+def usage():
+ print "%s feed_number gpio_number" % sys.argv[0]
+ sys.exit(0)
+
+if __name__ == "__main__":
+ API_KEY = get_env_value("COSM_KEY")
+ argv = sys.argv
+
+ if len(argv) == 3:
+ feed = return_integer(argv[1], name = "feed")
+ gpio = return_integer(argv[2], GPIO_MAX, name = "gpio")
+ else:
+ usage()
+ API_URL = "/v2/feeds/%s.xml" % feed
+
+ if not API_KEY:
+ print >>sys.stderr, "No COSM_KEY environment variable passed"
+ print >>sys.stderr, "Aborting."
+ sys.exit(1)
+
+ if os.fork() == 0:
+ enable_gpio(gpio)
+ setup_pwm()
+
+ atexit.register(disable_gpio, gpio = gpio)
+ atexit.register(shutdown_pwm)
+
+ pac = eeml.Cosm(API_URL, API_KEY, use_https = False)
+ start_reporting(pac, gpio)

0 comments on commit 4f8af59

Please sign in to comment.
Something went wrong with that request. Please try again.