1#!/usr/bin/env python3

2# Copyright 2019 Canonical Ltd.

3# All rights reserved.

4#

5# Written by:

6# Jonathan Cave <jonathan.cave@canonical.com>

7

8 import RPi.GPIO as GPIO

9

10 import os

11 import sys

12 import time

13

14

15 def loopback_test(out_lane, in_lane):

16 print("{} -> {}".format(out_lane, in_lane), flush=True)

17 out_lane = int(out_lane)

18 in_lane = int(in_lane)

19 GPIO.setup(out_lane, GPIO.OUT, initial=GPIO.LOW)

20 GPIO.setup(in_lane, GPIO.IN)

21 for i in range(6):

22 GPIO.output(out_lane, i % 2)

23 time.sleep(0.5)

24 if GPIO.input(in_lane) != (i % 2):

25 raise SystemExit(

26 "Failed loopback test out: {} in: {}".format(out_lane, in_lane)

27 )

28 time.sleep(0.5)

29

30

31 def gpio_pairs(model_name):

32 gpio_data = os.path.expandvars(

33 "$PLAINBOX_PROVIDER_DATA/gpio-loopback.{}.in".format(model_name)

34 )

35 if not os.path.exists(gpio_data):

36 raise SystemExit(

37 "ERROR: no gpio information found at: {}".format(gpio_data)

38 )

39 with open(gpio_data, "r") as f:

40 for line in f:

41 if line.startswith("#"):

42 continue

43 yield line.strip().split(",")

44

45

46 def main():

47 if len(sys.argv) < 2:

48 raise SystemExit("Usage: gpio_loopback.py MODEL_NAME")

49 model_name = sys.argv[1]

50

51 print("Using RPi.GPIO module {}".format(GPIO.VERSION))

52 GPIO.setmode(GPIO.BCM)

53 GPIO.setwarnings(False)

54

55 for pair in gpio_pairs(model_name):

56 loopback_test(*pair)

57

58 GPIO.cleanup()

59

60

61 if __name__ == "__main__":

62 main()