nedoskiv Posted December 3, 2016 Share Posted December 3, 2016 Hello I'm using OPI PC+ and tried to figure out some free gpio to play with it. Taking a look at forums/etc there are formula to calculate current GPIO number according to this https://linux-sunxi.org/GPIO and some posts here, there is a formula: (position of letter in alphabet - 1) * 32 + pin number so can someone explain me how pin 38 (PG6) becomes GPIO 198? calculation back from the formula point letter F Link to comment Share on other sites More sharing options...
zador.blood.stained Posted December 3, 2016 Share Posted December 3, 2016 (edited) so can someone explain me how pin 38 (PG6) becomes GPIO 198? It should become pin 166, not 198 (6-1) * 32 + 6 = 166 Lost in own fingers, (7-1) * 32 + 6 = 198 Edited December 3, 2016 by zador.blood.stained Derp Link to comment Share on other sites More sharing options...
nedoskiv Posted December 3, 2016 Author Share Posted December 3, 2016 figured it out thanks to you, by pin number they do not means pin number on the connector. so PG6 becomes : G (7) - 1 = 6 6*32 = 192 and then we add number from the name (PG6) 192 + 6 = 198 Link to comment Share on other sites More sharing options...
martinayotte Posted December 3, 2016 Share Posted December 3, 2016 When you are doing frequently such calculations and getting bored, you simply use python script to help you out ... #!/usr/bin/python3 import sys import string def convert(value): value = value.upper() alp = value[1] idx = string.uppercase.index(alp) num = int(value[2:], 10) res = idx * 32 + num return res if __name__ == "__main__": args = sys.argv[1:] if not args: print("Usage: %s <pin>" % sys.argv[0]) sys.exit(1) print("%d" % convert(args[0])) Link to comment Share on other sites More sharing options...
gnasch Posted December 3, 2016 Share Posted December 3, 2016 sry if this is a stupid question: in which format should <pin> be given to your script? I only get these: cg@poisson:/usr/local/bin$ GpioCalc.py pg6 Traceback (most recent call last): File "/usr/local/bin/GpioCalc.py", line 22, in <module> print("%d" % convert(args[0])) File "/usr/local/bin/GpioCalc.py", line 11, in convert idx = string.uppercase.index(alp) AttributeError: 'module' object has no attribute 'uppercase' cg@poisson:/usr/local/bin$ GpioCalc.py g6 Traceback (most recent call last): File "/usr/local/bin/GpioCalc.py", line 22, in <module> print("%d" % convert(args[0])) File "/usr/local/bin/GpioCalc.py", line 11, in convert idx = string.uppercase.index(alp) AttributeError: 'module' object has no attribute 'uppercase' cg@poisson:/usr/local/bin$ GpioCalc.py g 6 Traceback (most recent call last): File "/usr/local/bin/GpioCalc.py", line 22, in <module> print("%d" % convert(args[0])) File "/usr/local/bin/GpioCalc.py", line 10, in convert alp = value[1] IndexError: string index out of range cg@poisson:/usr/local/bin$ GpioCalc.py 38 Traceback (most recent call last): File "/usr/local/bin/GpioCalc.py", line 22, in <module> print("%d" % convert(args[0])) File "/usr/local/bin/GpioCalc.py", line 11, in convert idx = string.uppercase.index(alp) AttributeError: 'module' object has no attribute 'uppercase' cg@poisson:/usr/local/bin$ Link to comment Share on other sites More sharing options...
martinayotte Posted December 3, 2016 Share Posted December 3, 2016 Sorry, I wasn't the original author of that script. Although it headed with #!/usr/bin/python3, your issues seems that it is not python3 compatible, try it with python2.7 ... (Or if you really python3, change the "uppercase" which is now "ascii_uppercase") EDIT : it is better to change the "uppercase" to "ascii_uppercase", since it works on both python2.7 and python3 Link to comment Share on other sites More sharing options...
gnasch Posted December 4, 2016 Share Posted December 4, 2016 Yes, this works, and it expexts input like pg6. Thank you Link to comment Share on other sites More sharing options...
Recommended Posts