3 # Author: Costin Constantin <costin.c.constantin@intel.com>
4 # Copyright (c) 2015 Intel Corporation.
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:
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
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.
25 from __future__ import print_function
30 from time import sleep
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):
38 self.pin = m.Gpio(MRAA_GPIO)
39 self.gpio_path = "/sys/class/gpio/gpio" + str(self.pin.getPin(True))
42 # dereference pin to force cleanup
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")
49 def test_returning_pin_as_on_sys(self):
50 self.assertTrue(os.path.exists(self.gpio_path))
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]
57 self.assertMultiLineEqual(dir_file_content, "out")
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]
64 self.assertMultiLineEqual(dir_file_content, "in")
66 def test_GPIO_as_output_write_HIGH_level(self):
67 self.pin.dir(m.DIR_OUT)
69 val_file = open(self.gpio_path + "/value")
70 sysfs_pin_value = val_file.readline()[:-1]
72 self.assertEqual(int(sysfs_pin_value),1, "Value doesn't match the HIGH state")
74 def test_GPIO_as_output_write_LOW_level(self):
75 self.pin.dir(m.DIR_OUT)
77 val_file = open(self.gpio_path + "/value")
78 sysfs_pin_value = val_file.readline()[:-1]
80 self.assertEqual(int(sysfs_pin_value), 0, "Value doesn't match the LOW state")
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")
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")
92 if __name__ == '__main__':