#!/usr/bin/env python

import sys
import re
from neopixel import *

# LED strip configuration:
LED_COUNT      = 300     # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest


if __name__ == "__main__":
  opts = []
  if len(sys.argv) > 1:
    for i in range(1,len(sys.argv)):
      if sys.argv[i] == '-h' or sys.argv[i] == '--help':
        opts.append('h')
    if 'h' in opts:
      print "Usage:"
      print "<led_index | \"all\">"
      print "\tLight given LED or all LEDs to blue (0,0,255)."
      print "<led_index | \"all\"> 0"
      print "\tSet given LED or all LEDs to off (0,0,0)."
      print "<led_index | \"all\"> <r> <g> <b>"
      print "\tSet given LED or all LEDs to the given color (r,g,b)."
      print "\"q\""
      print "\tQuit."
      exit()
  strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
  strip.begin()
  while True:
    sys.stdout.write(">")
    sys.stdout.flush
    line = sys.stdin.readline().rstrip()
    tokens = line.split(" ")
    if tokens[0] == "q":
      break
    try:
      color = Color(0,0,255)
      if len(tokens) == 2 and int(tokens[1]) == 0:
        color = Color(0,0,0)
      if len(tokens) == 4:
        try:
          color = Color(int(tokens[1]),int(tokens[2]),int(tokens[3]))
        except ValueError:
          pass
      if tokens[0] == "all":
        for i in range(strip.numPixels()):
          strip.setPixelColor(i,color)
      else:
        p = re.compile('\d+-\d+')
        m = p.match(tokens[0])
        if m:
          indices = m.group().split('-')
          for i in range(int(indices[0]),int(indices[1])+1):
            strip.setPixelColor(i,color)
        else:
          strip.setPixelColor(int(tokens[0]),color)
      strip.show()
    except ValueError:
      pass

#  for i in range(0, strip.numPixels()):
#    strip.setPixelColor(i,Color(0,0,0))
#  strip.show()

  print "Done!"


#!/usr/bin/env python

import sys
import re
from neopixel import *

# LED strip configuration:
LED_COUNT      = 300     # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest


if __name__ == "__main__":
  opts = []
  if len(sys.argv) > 1:
    for i in range(1,len(sys.argv)):
      if sys.argv[i] == '-h' or sys.argv[i] == '--help':
        opts.append('h')
    if 'h' in opts:
      print "Usage:"
      print "<led_index | \"all\">"
      print "\tLight given LED or all LEDs to blue (0,0,255)."
      print "<led_index | \"all\"> 0"
      print "\tSet given LED or all LEDs to off (0,0,0)."
      print "<led_index | \"all\"> <r> <g> <b>"
      print "\tSet given LED or all LEDs to the given color (r,g,b)."
      print "\"q\""
      print "\tQuit."
      exit()
  strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
  strip.begin()
  while True:
    sys.stdout.write(">")
    sys.stdout.flush
    line = sys.stdin.readline().rstrip()
    tokens = line.split(" ")
    if tokens[0] == "q":
      break
    try:
      color = Color(0,0,255)
      if len(tokens) == 2 and int(tokens[1]) == 0:
        color = Color(0,0,0)
      if len(tokens) == 4:
        try:
          color = Color(int(tokens[1]),int(tokens[2]),int(tokens[3]))
        except ValueError:
          pass
      if tokens[0] == "all":
        for i in range(strip.numPixels()):
          strip.setPixelColor(i,color)
      else:
        p = re.compile('\d+-\d+')
        m = p.match(tokens[0])
        if m:
          indices = m.group().split('-')
          for i in range(int(indices[0]),int(indices[1])+1):
            strip.setPixelColor(i,color)
        else:
          strip.setPixelColor(int(tokens[0]),color)
      strip.show()
    except ValueError:
      pass

#  for i in range(0, strip.numPixels()):
#    strip.setPixelColor(i,Color(0,0,0))
#  strip.show()

  print "Done!"


#!/usr/bin/env python # LED strip configuration: # Number of LED pixels. # GPIO pin connected to the pixels (must support PWM!). # LED signal frequency in hertz (usually 800khz) # DMA channel to use for generating signal (try 5) # True to invert the signal (when using NPN transistor level shift) # Set to 0 for darkest and 255 for brightest #  for i in range(0, strip.numPixels()): #    strip.setPixelColor(i,Color(0,0,0)) #  strip.show()