No Description

rgbringcoder.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/python
  2. # Author: Jon Trulson <jtrulson@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. import time, sys, signal, atexit
  24. import pyupm_rgbringcoder as upmRGBRingCoder
  25. # There are a lot of pins to hook up. These pins are valid for the
  26. # Edison board, but may need to be adjusted for other platforms.
  27. # In order:
  28. # enable - 4
  29. # latch - 10
  30. # clear - 11
  31. # clock - 2
  32. # data - 9
  33. # switch - 7
  34. # red pwm - 3
  35. # green pwm - 5
  36. # blue pwm - 6
  37. # encA - 12
  38. # encB - 13
  39. ringCoder = upmRGBRingCoder.RGBRingCoder(4, 10, 11, 2, 9, 7, 12, 13, 3,
  40. 5, 6)
  41. ## Exit handlers ##
  42. # This stops python from printing a stacktrace when you hit control-C
  43. def SIGINTHandler(signum, frame):
  44. raise SystemExit
  45. # This function lets you run code on exit,
  46. # including functions from ringCoder
  47. def exitHandler():
  48. print "Exiting"
  49. sys.exit(0)
  50. # Register exit handlers
  51. atexit.register(exitHandler)
  52. signal.signal(signal.SIGINT, SIGINTHandler)
  53. spin = 0x0001;
  54. oldState = False;
  55. oldPos = 0;
  56. # Lets go green
  57. ringCoder.setRGBLED(0.99, 0.01, 0.99);
  58. while(1):
  59. # you spin me round...
  60. if ((spin & 0xffff) == 0):
  61. spin = 0x0001
  62. ringCoder.setRingLEDS(spin)
  63. spin <<= 1
  64. # check button state
  65. bstate = ringCoder.getButtonState()
  66. if (bstate != oldState):
  67. print "Button state changed from", oldState, " to ", bstate
  68. oldState = bstate
  69. # check encoder position
  70. epos = ringCoder.getEncoderPosition()
  71. if (epos != oldPos):
  72. print "Encoder position changed from", oldPos, "to", epos
  73. oldPos = epos
  74. time.sleep(0.1)