setChannels.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/python
  2. import os
  3. import sys
  4. import array
  5. import string
  6. from ola.ClientWrapper import ClientWrapper
  7. wrapper = None
  8. dim = False
  9. dimTick = 0
  10. dimTime = 0
  11. numTicks = 0
  12. # Wrapper callback
  13. def DmxSent(state):
  14. # Stop on errors
  15. if not state.Succeeded():
  16. wrapper.Stop()
  17. # Only run one command if we're setting
  18. if not dim:
  19. wrapper.Stop()
  20. # Stop when we're done dimming
  21. if dim and dimTick >= numTicks:
  22. wrapper.Stop()
  23. # Pick a universe (0, or as specified in the environment)
  24. universe = 0
  25. if 'UNIVERSE' in os.environ:
  26. universe = int(os.environ['UNIVERSE'])
  27. # Pick a tick interval (50ms, or as specified in the environment)
  28. interval = 50
  29. if 'INTERVAL' in os.environ:
  30. universe = int(os.environ['INTERVAL'])
  31. # Choose direct-set or dimming
  32. dim = False
  33. if 'dim' in string.lower(sys.argv[0]):
  34. dim = True
  35. # If we are dimming, parse the command line into 3-part blocks
  36. # Otherwise just collect the intensities directly
  37. cmdData = [];
  38. if dim:
  39. dimTime = int(sys.argv[1])
  40. numTicks = dimTime / interval
  41. for i in range(2, len(sys.argv), 2):
  42. dimData = array.array('I')
  43. dimData.append(int(sys.argv[i]))
  44. dimData.append(int(sys.argv[i + 1]))
  45. cmdData.append(dimData)
  46. else:
  47. for i in range(1, len(sys.argv)):
  48. cmdData.append(int(sys.argv[i]))
  49. def SendDMXFrame():
  50. # Re-schedule ourselves in interval ms (do this first to keep the timing consistent)
  51. if dim:
  52. wrapper.AddEvent(interval, SendDMXFrame)
  53. # Eventually we need an array of intesnty bytes for each channel
  54. data = array.array('B')
  55. # Calculate intensities
  56. if dim:
  57. global dimTick
  58. dimTick += 1
  59. for i in cmdData:
  60. totalRange = i[1] - i[0]
  61. delta = float(totalRange) / float(numTicks) * float(dimTick)
  62. data.append(int(i[0] + delta))
  63. else:
  64. # Copy each intensity level from the command line
  65. for i in cmdData:
  66. data.append(i)
  67. # Send
  68. wrapper.Client().SendDmx(universe, data, DmxSent)
  69. # Send the DMX command
  70. wrapper = ClientWrapper()
  71. wrapper.AddEvent(interval, SendDMXFrame)
  72. wrapper.Run()