#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This program is copyright (c) 2016, P. Lutus and is released
# under the GPL (http://www.gnu.org/licenses/gpl-3.0.en.html).

import RPi.GPIO as G

G.setmode(G.BCM)

G.setwarnings(False)

# map binary digits to I/O pins

gpio_pins = tuple(range(2,27))

# set I/O pins to output mode

G.setup(gpio_pins,G.OUT)

# accept user inputs until 'q'

while True:
  s =  raw_input("Enter a number in the range [0,255] or 'q' for quit:")
  try:
    n = int(s)
  except:
    if(s.lower() == 'q'):
      break
  print("You entered %3d (base ten) or %8s (base two)." % (n,bin(n)[2:]))
  # set LEDs to binary code
  for i in range(len(gpio_pins)):
    s = (2**i & n) != 0
    G.output(gpio_pins[i],(0,1)[s])

print("So long for now.") 
G.cleanup()