tests: make tests work on python3
[contrib/mraa.git] / tests / gpio_checks.py
1 #!/usr/bin/env python
2
3 # Author: Costin Constantin <costin.c.constantin@intel.com>
4 # Copyright (c) 2015 Intel Corporation.
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 from __future__ import print_function
26
27 import mraa as m
28 import unittest as u
29 import os, re, sys
30 from time import sleep
31
32 MRAA_GPIO = 3
33
34 @u.skipIf(m.pinModeTest(MRAA_GPIO, m.PIN_GPIO) != True, str(MRAA_GPIO) + "is not a valid Gpio on this platform")
35 class GpioChecks(u.TestCase):
36
37   def setUp(self):
38     self.pin = m.Gpio(MRAA_GPIO)
39     self.gpio_path = "/sys/class/gpio/gpio" + str(self.pin.getPin(True))
40
41   def tearDown(self):
42     # dereference pin to force cleanup
43     self.pin = ""
44
45   def test_returning_pin_no(self):
46     self.pin_no = self.pin.getPin() # i should have the pin no. here as set when initing Gpio class
47     self.assertEqual(self.pin_no, MRAA_GPIO, "Something wrong happened ... set pin doesn't correspond to retrieved one")
48
49   def test_returning_pin_as_on_sys(self):
50     self.assertTrue(os.path.exists(self.gpio_path))
51
52   def test_set_GPIO_as_output(self):
53     self.pin.dir(m.DIR_OUT)
54     dir_file = open(self.gpio_path + "/direction")
55     dir_file_content = dir_file.readline()[:-1]
56     dir_file.close()
57     self.assertMultiLineEqual(dir_file_content, "out")
58
59   def test_set_GPIO_as_input(self):
60     self.pin.dir(m.DIR_IN)
61     dir_file = open(self.gpio_path + "/direction")
62     dir_file_content = dir_file.readline()[:-1]
63     dir_file.close()
64     self.assertMultiLineEqual(dir_file_content, "in")
65
66   def test_GPIO_as_output_write_HIGH_level(self):
67     self.pin.dir(m.DIR_OUT)
68     self.pin.write(1)
69     val_file = open(self.gpio_path + "/value")
70     sysfs_pin_value = val_file.readline()[:-1]
71     val_file.close()
72     self.assertEqual(int(sysfs_pin_value),1, "Value doesn't match the HIGH state")
73
74   def test_GPIO_as_output_write_LOW_level(self):
75     self.pin.dir(m.DIR_OUT)
76     self.pin.write(0)
77     val_file = open(self.gpio_path + "/value")
78     sysfs_pin_value = val_file.readline()[:-1]
79     val_file.close()
80     self.assertEqual(int(sysfs_pin_value), 0, "Value doesn't match the LOW state")
81
82   def test_GPIO_as_input_and_write_HIGH_on_it(self):
83     self.pin.dir(m.DIR_IN)
84     res = self.pin.write(1)
85     self.assertNotEqual(res, m.SUCCESS, "The command should fail")
86
87   def test_GPIO_as_input_and_write_LOW_on_it(self):
88     self.pin.dir(m.DIR_IN)
89     res = self.pin.write(0)
90     self.assertGreater(res, 0, "The command should have returned value greater than 0")
91
92 if __name__ == '__main__':
93   u.main()