12-28-2017, 03:27 AM
Hi
First of all thank you for your work. Very helpful!
I'd like to share a modified version of the function to calculate the pin number, with a bit of validation to be sure that the string is ok (I hope it is ok). The function returns -1 if there was any problem with the string
First of all thank you for your work. Very helpful!
I'd like to share a modified version of the function to calculate the pin number, with a bit of validation to be sure that the string is ok (I hope it is ok). The function returns -1 if there was any problem with the string
Code:
def GPIO_name2gpio(value):
value = value.upper()
if (value[0:4] != 'GPIO') or (value[5] != '_'):
return -1
try:
bank_num = int(value[4:5], 10)
pad_nam = GPIO_pads[value[6:7]]
pad_num = int(value[7:], 10)
if (bank_num < 0) or (bank_num > 3) or (pad_num < 0) or (pad_num > 7):
return -1;
return (bank_num * 32) + (pad_nam * 8) + pad_num
except (ValueError,KeyError):
return -1