mechanizmos58

raspberry_gpio_emulator.py

May 17th, 2021
708
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. # GPIO_EMULATOR.py
  3. # 18_05_21
  4.  
  5. import sys
  6.  
  7. modes = {10: 'BOARD', 11: 'BCM'}
  8. inout = {0: 'OUT', 1: 'IN'}
  9. interror = 'type should be int'
  10. boolerror = 'type should be bool'
  11. UNKNOWN = -1
  12.  
  13.  
  14. class GPIO:
  15.  
  16.     """A simple, stand-alone, allow all IO Raspberry Pi GPIO
  17.    simulator/emulator that can be added to as needed."""
  18.  
  19.  
  20.     # values gathered by print(GPIO.[desciptor]) on a RPi
  21.  
  22.     # IO addressing
  23.     BOARD    = 10
  24.     BCM      = 11
  25.  
  26.     # resistor Pull Up Down
  27.     PUD_OFF  = 20
  28.     PUD_DOWN = 21
  29.     PUD_UP   = 22
  30.  
  31.     # edge detection
  32.     RISING   = 31
  33.     FALLING  = 32
  34.     BOTH     = 33
  35.  
  36.     # comms
  37.     SERIAL   = 40
  38.     SPI      = 41
  39.     I2C      = 42
  40.     PWM      = "<class 'RPi.GPIO.PWM'>" # should be 43 ?
  41.  
  42.     # other
  43.     LOW      = 0
  44.     HIGH     = 1
  45.     OUT      = 0
  46.     IN       = 1
  47.     VERSION = '0.7.0'
  48.     RPI_REVISION = '?'
  49.  
  50.     UNKNOWN = UNKNOWN
  51.  
  52.     mode = UNKNOWN
  53.  
  54.     NUMBER_OF_GPIO = 54 # ref. Broadcom BCM2835 ARM processor datasheet p. 89
  55.  
  56.     io = dict(enumerate([UNKNOWN] * NUMBER_OF_GPIO))
  57.  
  58.  
  59.  
  60.     def setmode(mode):
  61.  
  62.         if type(mode) is not int:
  63.             raise TypeError(interror)
  64.  
  65.         if mode not in [GPIO.BOARD, GPIO.BCM]:
  66.             raise ValueError('A mode other than GPIO.BOARD or GPIO.BCM was requested\n')
  67.  
  68.         if GPIO.mode != modes[mode] and GPIO.mode != UNKNOWN:
  69.             raise ValueError('A different mode has already been set\n')
  70.  
  71.         GPIO.mode = modes[mode]
  72.         print('mode set to', modes[mode], '\n')
  73.  
  74.  
  75.  
  76.     def setwarnings(flag):
  77.  
  78.         if type(flag) is not bool:
  79.             raise TypeError(boolerror)
  80.         print('warnings set to', flag, '\n')
  81.  
  82.  
  83.  
  84.     def setup(gpio, state, initial=UNKNOWN, pull_up_down=UNKNOWN):
  85.  
  86.         if GPIO.mode == UNKNOWN:
  87.             print('RuntimeError: Pin numbering mode not set')
  88.             print('Use GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)\n')
  89.             sys.exit()
  90.  
  91.         #if not all((type(i) is int) for i in [gpio, state, initial, pull_up_down]):
  92.             #raise TypeError(interror)
  93.  
  94.         if type(gpio) is not int:
  95.             raise TypeError('gpio should be type int')
  96.         if type(state) is not int:
  97.             raise TypeError('state should be type int')
  98.         if type(initial) is not int:
  99.             raise TypeError('initial should be type int')
  100.         if type(pull_up_down) is not int:
  101.             raise TypeError('pull_up_down should be type int')
  102.  
  103.         if state == GPIO.IN and initial != UNKNOWN:
  104.             raise RuntimeError('initial value not valid for inputs')
  105.  
  106.         if not 0 <= gpio < GPIO.NUMBER_OF_GPIO:
  107.             raise ValueError('The gpio value is invalid')
  108.  
  109.         if state not in [GPIO.IN, GPIO.OUT]:
  110.             raise ValueError('state must be GPIO.IN or GPIO.OUT')
  111.  
  112.         if pull_up_down not in [GPIO.PUD_OFF, GPIO.PUD_DOWN, GPIO.PUD_UP, UNKNOWN]:
  113.             raise ValueError('pull_up_down is invalid')
  114.  
  115.         if gpio > 27:
  116.             print('WARNING: There are only GPIO 0 to 27 available as pinouts\n')
  117.  
  118.         print('gpio', gpio, 'set to', inout[state])
  119.         GPIO.io[gpio] = state
  120.  
  121.  
  122.  
  123.     def output(gpio, level):
  124.  
  125.         if type(gpio) is not int:
  126.             raise TypeError('gpio should type int')
  127.  
  128.         if type(level) is not int:
  129.             raise TypeError('level should type int')
  130.  
  131.         if GPIO.io[gpio] != GPIO.OUT:
  132.             raise RuntimeError('The GPIO channel has not been set up as an GPIO.OUT')
  133.  
  134.  
  135.  
  136.     def input(gpio):
  137.  
  138.         if type(gpio) is not int:
  139.             raise TypeError(interror)
  140.  
  141.         if GPIO.io[gpio] != GPIO.IN:
  142.             raise RuntimeError('You must setup the GPIO channel as an GPIO.IN first')
  143.  
  144.         return None
  145.  
  146.  
  147.  
  148.     def cleanup():
  149.         for i in GPIO.io.keys():
  150.             GPIO.io[i] = UNKNOWN
  151.         print('cleanup()')
  152.  
  153.  
  154. if __name__ == '__main__':
  155.  
  156.     try:
  157.         import RPi.GPIO as GPIO
  158.     except:
  159.         print("RPi.GPIO not available - switching to GPIO_EMULATOR")
  160.         from GPIO_EMULATOR import GPIO
  161.  
  162.     print()
  163.     print(GPIO.__doc__)
  164.     print()
  165.     GPIO.setwarnings(False)
  166.     GPIO.setmode(GPIO.BCM)
  167.     # ~ GPIO.setmode(GPIO.BOARD)
  168.  
  169.  
  170.     pin = 0
  171.     GPIO.setup(pin, GPIO.OUT)
  172.     GPIO.output(pin, 1)
  173.     print()
  174.     pin = 0
  175.     GPIO.setup(pin, GPIO.OUT)
  176.     GPIO.output(pin, 1)
  177.  
  178.     print()
  179.     GPIO.cleanup()
  180.  
RAW Paste Data