Commits

Craig Swank committed 9ab1398 Merge

Merge branch 'master' of bitbucket.org:cswank/gadgets

  • Participants
  • Parent commits b53824d, 6b647dd

Comments (0)

Files changed (5)

File examples/brewery.py

     }
 }
 
-factory = GadgetsFactory(ensure_off=True)
+factory = GadgetsFactory(ensure_off=True, startup_script='/opt/gadgets/extras/load-device-tree.sh')
 gadgets = factory(args)
 gadgets.start()

File extras/load-device-tree.sh

+#!/bin/bash
+echo BB-W1:00A0 > /sys/devices/bone_capemgr.9/slots
+echo am33xx_pwm > /sys/devices/bone_capemgr.9/slots
+echo bone_pwm_P8_13 > /sys/devices/bone_capemgr.9/slots
+echo bone_pwm_P8_19 > /sys/devices/bone_capemgr.9/slots
+

File gadgets/__init__.py

+import os
 import time
 import platform
 
 
 class GadgetsFactory(object):
 
-    def __init__(self, addresses=None, ensure_off=False):
+    def __init__(self, addresses=None, ensure_off=False, startup_script=None):
         self._ensure_off = ensure_off
+        self._startup_script = startup_script
         if addresses is None:
             addresses = Addresses()
         self._addresses = addresses
                 factory = self._factories[device_arguments['type']]
                 gadget = factory(location, name, device_arguments, self._addresses)
                 output.append(gadget)
-        return Gadgets(output, self._addresses, ensure_off=self._ensure_off)
+        return Gadgets(output, self._addresses, ensure_off=self._ensure_off, startup_script=self._startup_script)
 
 class Gadgets(object):
     """
     
     """
 
-    def __init__(self, gadgets, addresses=None, ensure_off=False, name=None):
+    def __init__(self, gadgets, addresses=None, ensure_off=False, name=None, startup_script=None):
         if name is None:
             name = platform.node()
         self._name = name
+        self._startup_script = startup_script
         self._gadgets = gadgets
         self._ensure_off = ensure_off
         if addresses is not None:
         self._gadgets.append(gadget)
 
     def start(self):
+        if self._startup_script is not None:
+            os.system(self._startup_script)
         if self._is_master:
             broker = Broker(self._addresses)
             broker.start()

File gadgets/io/pwm.py

 import os
+import glob
+
 from gadgets.io.io import IO
 
 class PWMFactory(object):
     
     _command_template = 'echo {0} > {1}/{2}'
     _command_path_template = '/sys/devices/{0}'
+    _one_second = 1000000000
 
     def __init__(self, pin, frequency=1.0):
         """
         mux_path:  defaults to /sys/kernel/debug/omap_mux
         """
         self._current_value = None
-        self._command = self._command_path_template.format(pin['directory'])
+        directory = self._find_directory(pin['directory'])
+        self._command = self._command_path_template.format(directory)
         self._write_value('run', '0')
-        frequency = int(1000000000 * frequency)
-        self._write_value('period', frequency)
+        period = int(self._one_second * frequency)
+        self._write_value('period', period)
         self._status = False
 
     @property
     def status(self):
         return self._status
+        
+    def _find_directory(self, directory):
+        """
+        The directory names change when using pwm under
+        the device tree overlay, so this finds the directory.
+        """
+        filenames = glob.glob(self._command_path_template.format(directory))
+        if len(filenames != 1:
+            raise GadgetsError("the pwm glob didn't find a pwm directory: {0}".format(directory)
+        return filenames[0]
 
     def on(self):
         """
         if self._current_value != value:
             self._write_value('run', '1')
             self._current_value = value
-            value = 1000000000 * (value / 100)
+            value = self._one_second * (value / 100)
             self._write_value('duty', value)
 
     def _write_value(self, path, value):

File gadgets/pins/beaglebone_black.py

     'pwm': {
         8: {
             13:{
-                'directory': 'ocp.3/pwm_test_P8_13.12',
+                'directory': 'ocp.*/pwm_test_P8_13.*',
                 },
             19: {
-                'directory': 'ocp.3/pwm_test_P8_19.13',
+                'directory': 'ocp.*/pwm_test_P8_19.*',
                 },
             },
         9: {
             14: {
-                'directory': 'ehrpwm.1:0', #ehrpwm1A_mux1
+                'directory': 'ocp.*/pwm_test_P9_14.*',
             },
             16:{
-                'directory': 'ehrpwm.1:1',
+                'directory': 'ocp.*/pwm_test_P9_16.*',
             },
             21:{
-                'directory': 'ehrpwm.0:1',
+                'directory': 'ocp.*/pwm_test_P9_21.*',
             },
             22:{
-                'directory': 'ehrpwm.0:0',
+                'directory': 'ocp.*/pwm_test_P9_22.*',
             },
         },
     },