###############################################################################################################################################
#ITBP_gpioBBB.py v01 - RPI.GPIO helper for ITBP modular modem - port for BBB[debian] 
#COPYRIGHT (c) 2017 Dragos Iosub / R&D Software Solutions srl
#
#You are legaly entitled to use this SOFTWARE ONLY IN CONJUNCTION WITH ITBP MODULAR MODEMS DEVICES USAGE [c-uGSM, h-nanoGSM and d-u3G shileds]. Modifications, derivates and redistribution 
#of this software must include unmodified this COPYRIGHT NOTICE. You can redistribute this SOFTWARE and/or modify it under the terms 
#of this COPYRIGHT NOTICE. Any other usage may be permited only after written notice of Dragos Iosub / R&D Software Solutions srl.
#
#This SOFTWARE is distributed is provide "AS IS" in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
#warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
#Dragos Iosub, Bucharest 2017.
#http://itbrainpower.net
###############################################################################################################################################
import subprocess
ports   = ['P9_14','P9_16','P9_18']
mapping ={'P9_14':'gpio50','P9_16':'gpio51','P9_18':'gpio4'}

#some definitions, do not change
OUT     ='out'
IN      ='in'
HIGH    = 1
LOW     = 0
BOARD   = 1 #just 4 compatibility with RPi.GPIO
dirmap = {'out':1,'in':0}

def output(port, value):
    p = subprocess.Popen('echo '+str(value)+' > /sys/class/gpio/'+mapping[port]+'/value', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate()
    #print str(out)
    #print str(err)

def setup(port, direction, initial=-100):
    p = subprocess.Popen('/usr/local/bin/config-pin '+str(port)+' '+str(direction), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate()
    #print out
    #print err
    if (initial > -100):
        output(port, initial) #write initial state

def input(port):
    p = subprocess.Popen(['cat','/sys/class/gpio/'+mapping[port]+'/value'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate()
    #print out
    #print err
    try:
        return int(out)
    except:
        return -100
    #print str(out)
    #print str(err)

def cleanup():
    global ports
    for port in ports:
        setup(port, 'in', -101)# force input float[no pull up or pull down]


#just 4 compatibility with RPi.GPIO
def setmode(whatever):
    return

#just 4 compatibility with RPi.GPIO
def setwarnings(whatever):
    return

