###############################################################################################################################################################
#ITBP_gpioBBB.py v022 - RPI.GPIO helper file for ITBP u-GSM modems and modular modems - [Debian 10] port 
#COPYRIGHT (c) 2017-2023 Dragos Iosub / R&D Software Solutions srl
#
#You are legaly entitled to use this SOFTWARE ONLY IN CONJUNCTION WITH ITBP u-GSM modems or with ITBP MODULAR MODEMS DEVICES USAGE
#[l-LTE, 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 2023.
#http://itbrainpower.net
###############################################################################################################################################################

###############################################################################################################################################################
#next, configs required for u-GSM modems (uFL) connected via u-GSM BBB interface [https://itbrainpower.net/images/u-GSM-top-03-pinout_1200px.jpg]
#or, u-GSM SMA version / ITBP MODULAR MODEMS conected via ITBPMM using wires [https://itbrainpower.net/a-gsm/BBB_u-GSM_Debian10_notes#wires]
###############################################################################################################################################################
# Edit the "uGSM2_19_hw_control.py" - "*********_hw_control.py" for other modems - file [mcedit uGSM2_19_hw_control.py] and replace the line 17":
# import RPi.GPIO as GPIO with:
# import ITBP_gpioBBB as GPIO
#
# Edit the "globalParVar.py" file [mcedit globalParVar.py] and set the "CONTROL interface for the ITBP modems" as bellow:
#   RESET = "P9_14"
#   POWER = "P9_16"
#   STATUS = "P9_12"
#
# Edit the "globalParVar.py" file [mcedit globalParVar.py] and replace the line
#   SERIALPORT = "/dev/ttyAMA0" with
#   SERIALPORT = "/dev/ttyO4"
#
# If previous line not found (for older version of python support files - mainly for ITBP MODULAR MODEMS, edit the "******GSM_Serial_Lib.py" file
# [mcedit ******GSM_Serial_Lib.py] and replace the line":
#   agsm = serial.Serial("/dev/ttyAMA0", serialSpeed, timeout=1) with:
#   agsm = serial.Serial("/dev/ttyO4", serialSpeed, timeout=1)
#
###############################################################################################################################################################

import subprocess
ports   = ['P9_12','P9_14','P9_16','P9_18']
mapping = {'P9_12':'gpio60','P9_14':'gpio50','P9_16':'gpio51','P9_18':'gpio4'}
gpioPath = '/sys/class/gpio/'
configPin = '/usr/bin/config-pin'

#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)+' > '+gpioPath+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(configPin+' '+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',gpioPath+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)# input float


#just 4 compatibility with RPi.GPIO
def setmode(whatever):
    return

#just 4 compatibility with RPi.GPIO    
def setwarnings(whatever):
    return
    
