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.
28 from time import sleep
32 @u.skipIf(m.pinModeTest(MRAA_GPIO, m.PIN_GPIO) != True, str(MRAA_GPIO) + "is not a valid Gpio on this platform")
33 class GpioChecks(u.TestCase):
36 self.pin = m.Gpio(MRAA_GPIO)
37 self.gpio_path = "/sys/class/gpio/gpio" + str(self.pin.getPin(True))
40 # dereference pin to force cleanup
43 def test_returning_pin_no(self):
44 self.pin_no = self.pin.getPin() # i should have the pin no. here as set when initing Gpio class
45 self.assertEqual(self.pin_no, MRAA_GPIO, "Something wrong happened ... set pin doesn't correspond to retrieved one")
47 def test_returning_pin_as_on_sys(self):
48 self.assertTrue(os.path.exists(self.gpio_path))
50 def test_set_GPIO_as_output(self):
51 self.pin.dir(m.DIR_OUT)
52 dir_file = open(self.gpio_path + "/direction")
53 dir_file_content = dir_file.readline()[:-1]
55 self.assertMultiLineEqual(dir_file_content, "out")
57 def test_set_GPIO_as_input(self):
58 self.pin.dir(m.DIR_IN)
59 dir_file = open(self.gpio_path + "/direction")
60 dir_file_content = dir_file.readline()[:-1]
62 self.assertMultiLineEqual(dir_file_content, "in")
64 def test_GPIO_as_output_write_HIGH_level(self):
65 self.pin.dir(m.DIR_OUT)
67 val_file = open(self.gpio_path + "/value")
68 sysfs_pin_value = val_file.readline()[:-1]
70 self.assertEqual(int(sysfs_pin_value),1, "Value doesn't match the HIGH state")
72 def test_GPIO_as_output_write_LOW_level(self):
73 self.pin.dir(m.DIR_OUT)
75 val_file = open(self.gpio_path + "/value")
76 sysfs_pin_value = val_file.readline()[:-1]
78 self.assertEqual(int(sysfs_pin_value), 0, "Value doesn't match the LOW state")
80 def test_GPIO_as_input_and_write_HIGH_on_it(self):
81 self.pin.dir(m.DIR_IN)
82 res = self.pin.write(1)
83 self.assertNotEqual(res, m.SUCCESS, "The command should fail")
85 def test_GPIO_as_input_and_write_LOW_on_it(self):
86 self.pin.dir(m.DIR_IN)
87 res = self.pin.write(0)
88 self.assertGreater(res, 0, "The command should have returned value greater than 0")
90 if __name__ == '__main__':