Browse Source

- fix a bug if gpio-number == 0
- up the version number
- improved the --help text
- exit if you attempt a direct I/O and you're not root

Pat Beirne 2 years ago
parent
commit
f92e63145a
1 changed files with 12 additions and 6 deletions
  1. 12 6
      opio

+ 12 - 6
opio

@@ -52,7 +52,7 @@ from mmap import mmap
 from struct import pack, unpack
 import os, sys, argparse, pathlib, re, logging
 
-VERSION = "2.1"
+VERSION = "2.2"
 
 ############ board specific
 
@@ -254,6 +254,7 @@ def mem_set(address, bitmask, value):
       mem[address:address+4] = pack("<L",data)
   except PermissionError:
     print("failed to open /dev/mem.....you must execute this script as root")
+    sys.exit(1)
 
 def mem_get(address, bitmask):
   try:
@@ -263,7 +264,7 @@ def mem_get(address, bitmask):
       return unpack("<L",mem[address:address+4])[0] & bitmask
   except PermissionError:
     print("failed to open /dev/mem.....you must execute this script as root")
-
+    sys.exit(1)
 
 ############################ pin access
 """ decide which kind of GPIO """
@@ -497,14 +498,21 @@ def do_leds():
         p[2],p[0],m,v))
     print("+------+-------+------+-----+")
 
+def check_gpio_valid():
+  if args.gpio == None or args.gpio<0 or args.gpio>127:
+    print("the {} command requires a gpio pin number".format(args.cmd))
+    sys.exit(1)
+    
 def do_read():
   logging.debug("do_read: %d",args.gpio)
+  check_gpio_valid()
   gpio = args.gpio
   g = GPIO_factory(gpio,args.direct)
   print(g.get())
 
 def do_write():
   logging.debug("do_write: %d %s",args.gpio, args.extra)
+  check_gpio_valid()
   gpio = args.gpio
   g = GPIO_factory(gpio, args.direct)
   if args.extra in ('1', 'on', 'ON'):
@@ -519,9 +527,7 @@ def do_write():
 def do_mode():
   logging.info("do_mode %d %s", args.gpio, args.extra)
   logging.info("the -d flag is {}".format(args.direct))
-  if not args.gpio or args.gpio<0 or args.gpio>127:
-    print("the {} command requires a gpio number".format(args.cmd))
-    sys.exit(1)
+  check_gpio_valid()
 
   if args.extra == None:
     m,v = GPIO_DIRECT(args.gpio).get_mode_value()
@@ -584,7 +590,7 @@ if __name__ == "__main__":
   parser = argparse.ArgumentParser(description = "Access GPIO on the OrangePi RDA boards")
   parser.add_argument('-v','--version', action='version', 
    version="opio "+VERSION+" -- python rewrite of gpio; by Pat Beirne")
-  parser.add_argument('cmd',help="one of: read, write, mode, readall, readallx")
+  parser.add_argument('cmd',help="one of: read/in, write/out, mode, readall/status, readallx/statusx, leds, exports")
   parser.add_argument('gpio',nargs='?',type=int,help="gpio number 0...126")
   parser.add_argument('extra',nargs='?')
   parser.add_argument('-d',"--direct",help="use low-level access",action="store_true")