Home | History | Annotate | Download | only in python
      1 #!/usr/bin/python
      2 # Author: Jon Trulson <jtrulson (at] ics.com>
      3 # Copyright (c) 2015 Intel Corporation.
      4 #
      5 # Permission is hereby granted, free of charge, to any person obtaining
      6 # a copy of this software and associated documentation files (the
      7 # "Software"), to deal in the Software without restriction, including
      8 # without limitation the rights to use, copy, modify, merge, publish,
      9 # distribute, sublicense, and/or sell copies of the Software, and to
     10 # permit persons to whom the Software is furnished to do so, subject to
     11 # the following conditions:
     12 #
     13 # The above copyright notice and this permission notice shall be
     14 # included in all copies or substantial portions of the Software.
     15 #
     16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     23 
     24 import time, sys, signal, atexit
     25 import pyupm_rgbringcoder as upmRGBRingCoder
     26 
     27 # There are a lot of pins to hook up.  These pins are valid for the
     28 # Edison board, but may need to be adjusted for other platforms.
     29 
     30 # In order:
     31 # enable      - 4
     32 # latch       - 10
     33 # clear       - 11
     34 # clock       - 2
     35 # data        - 9
     36 # switch      - 7
     37 
     38 # red pwm     - 3
     39 # green pwm   - 5
     40 # blue pwm    - 6
     41 
     42 # encA        - 12
     43 # encB        - 13
     44 ringCoder = upmRGBRingCoder.RGBRingCoder(4, 10, 11, 2, 9, 7, 12, 13, 3, 
     45                                          5, 6)
     46 
     47 ## Exit handlers ##
     48 # This stops python from printing a stacktrace when you hit control-C
     49 def SIGINTHandler(signum, frame):
     50 	raise SystemExit
     51 
     52 # This function lets you run code on exit,
     53 # including functions from ringCoder
     54 def exitHandler():
     55 	print "Exiting"
     56 	sys.exit(0)
     57 
     58 # Register exit handlers
     59 atexit.register(exitHandler)
     60 signal.signal(signal.SIGINT, SIGINTHandler)
     61 
     62 
     63 spin = 0x0001;
     64 oldState = False;
     65 oldPos = 0;
     66 
     67 # Lets go green
     68 ringCoder.setRGBLED(0.99, 0.01, 0.99);
     69 
     70 while(1):
     71         # you spin me round...
     72         if ((spin & 0xffff) == 0):
     73                 spin = 0x0001
     74     
     75         ringCoder.setRingLEDS(spin)
     76         spin <<= 1
     77 
     78         # check button state
     79         bstate = ringCoder.getButtonState()
     80         if (bstate != oldState):
     81                 print "Button state changed from", oldState, " to ", bstate
     82                 oldState = bstate
     83     
     84         # check encoder position
     85         epos = ringCoder.getEncoderPosition()
     86         if (epos != oldPos):
     87                 print "Encoder position changed from", oldPos, "to", epos
     88                 oldPos = epos
     89 
     90         time.sleep(0.1)
     91