import RPi.GPIO as GPIO
import time

boardLeftControl  =   1 ## 0x01
KeyboardLeftShift    =   2 ## 0x02
KeyboardLeftAlt      =   4 ## 0x04
KeyboardLeftMeta     =   8 ## 0x08
KeyboardRightControl =  16 ## 0x10
KeyboardRightShift   =  32 ## 0x20
KeyboardRightAlt     =  64 ## 0x40
KeyboardRightMeta    = 128 ## 0x80

OldKeys = [0,0,0,0,0,0]
EndKeys = [0,0,0,0,0,0]

def cmp(a, b):
    return (a > b) - (a < b)

def report_keyboard(c,k):
    with open('/dev/hidg0', 'rb+') as fk:
        fk.write((c).to_bytes(1, byteorder='big')+    \
                 (0).to_bytes(1, byteorder='big')+    \
                 (k[0]).to_bytes(1, byteorder='big')+ \
                 (k[1]).to_bytes(1, byteorder='big')+ \
                 (k[2]).to_bytes(1, byteorder='big')+ \
                 (k[3]).to_bytes(1, byteorder='big')+ \
                 (k[4]).to_bytes(1, byteorder='big')+ \
                 (k[5]).to_bytes(1, byteorder='big'))

GPIO.setmode(GPIO.BCM)

GPIO.setup(10, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)

GPIO.setup(20, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)

while True:

   NewKeys = [0,0,0,0,0,0]
   Keys = 0

   GPIO.output(13, 1)
   GPIO.output(11, 1)
   GPIO.output(12, 1)
   GPIO.output(10, 0)

   if(GPIO.input(20) == 0):
      NewKeys[Keys] = 19
      Keys+=1
   if(GPIO.input(21) == 0):
      NewKeys[Keys] = 18
      Keys+=1
   if(GPIO.input(22) == 0):
      NewKeys[Keys] = 17
      Keys+=1
   if(GPIO.input(23) == 0):
      NewKeys[Keys] = 16
      Keys+=1

   GPIO.output(10, 1)
   GPIO.output(13, 1)
   GPIO.output(12, 1)
   GPIO.output(11, 0)

   if(GPIO.input(20) == 0):
      NewKeys[Keys] = 15
      Keys+=1
   if(GPIO.input(21) == 0):
      NewKeys[Keys] = 14
      Keys+=1
   if(GPIO.input(22) == 0):
      NewKeys[Keys] = 13
      Keys+=1
   if(GPIO.input(23) == 0):
      NewKeys[Keys] = 12
      Keys+=1

   GPIO.output(10, 1)
   GPIO.output(11, 1)
   GPIO.output(13, 1)
   GPIO.output(12, 0)

   if(GPIO.input(20) == 0):
      NewKeys[Keys] = 11
      Keys+=1
   if(GPIO.input(21) == 0):
      NewKeys[Keys] = 10
      Keys+=1
   if(GPIO.input(22) == 0):
      NewKeys[Keys] = 9
      Keys+=1
   if(GPIO.input(23) == 0):
      NewKeys[Keys] = 8
      Keys+=1

   GPIO.output(10, 1)
   GPIO.output(11, 1)
   GPIO.output(12, 1)
   GPIO.output(13, 0)

   if(GPIO.input(20) == 0):
      NewKeys[Keys] = 7
      Keys+=1
   if(GPIO.input(21) == 0):
      NewKeys[Keys] = 6
      Keys+=1
   if(GPIO.input(22) == 0):
      NewKeys[Keys] = 5
      Keys+=1
   if(GPIO.input(23) == 0):
      NewKeys[Keys] = 4
      Keys+=1

   if cmp(OldKeys,NewKeys) != 0:
      report_keyboard(0,NewKeys)
      OldKeys = NewKeys

   time.sleep (0.1)

GPIO.cleanup()
