| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
from __future__ import print_function |
| 22 |
if hasattr(__builtins__, 'raw_input'): |
| 23 |
input = raw_input |
| 24 |
|
| 25 |
"""RPi.GPIO unit tests |
| 26 |
|
| 27 |
This test suite assumes the following circuit is connected: |
| 28 |
GND_PIN = 6 |
| 29 |
LED_PIN = 12 (with resistor to 0v) |
| 30 |
SWITCH_PIN = 18 (with 0.1 uF capacitor around switch) to 0v |
| 31 |
LOOP_IN = 16 connected with 1K resistor to LOOP_OUT |
| 32 |
LOOP_OUT = 22 |
| 33 |
NC_PIN = 24 not connected to anything |
| 34 |
""" |
| 35 |
|
| 36 |
import os |
| 37 |
import subprocess |
| 38 |
import sys |
| 39 |
import warnings |
| 40 |
import time |
| 41 |
from threading import Timer |
| 42 |
import RPi.GPIO as GPIO |
| 43 |
if sys.version[:3] == '2.6': |
| 44 |
import unittest2 as unittest |
| 45 |
else: |
| 46 |
import unittest |
| 47 |
|
| 48 |
GND_PIN = 6 |
| 49 |
LED_PIN = 12 |
| 50 |
LED_PIN_BCM = 18 |
| 51 |
SWITCH_PIN = 18 |
| 52 |
SWITCH_PIN_BCM = 24 |
| 53 |
LOOP_IN = 16 |
| 54 |
LOOP_IN_BCM = 23 |
| 55 |
LOOP_OUT = 22 |
| 56 |
NC_PIN = 24 |
| 57 |
|
| 58 |
non_interactive = False |
| 59 |
for i,val in enumerate(sys.argv): |
| 60 |
if val == '--non_interactive': |
| 61 |
non_interactive = True |
| 62 |
sys.argv.pop(i) |
| 63 |
|
| 64 |
|
| 65 |
class TestAAASetup(unittest.TestCase): |
| 66 |
def runTest(self): |
| 67 |
|
| 68 |
with self.assertRaises(RuntimeError) as e: |
| 69 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 70 |
self.assertEqual(str(e.exception), 'Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)') |
| 71 |
|
| 72 |
|
| 73 |
GPIO.setmode(GPIO.BCM) |
| 74 |
with self.assertRaises(ValueError) as e: |
| 75 |
GPIO.setmode(GPIO.BOARD) |
| 76 |
GPIO.setup(LED_PIN_BCM, GPIO.IN) |
| 77 |
GPIO.cleanup() |
| 78 |
|
| 79 |
|
| 80 |
with self.assertRaises(ValueError): |
| 81 |
GPIO.setmode(666) |
| 82 |
|
| 83 |
|
| 84 |
self.assertEqual(GPIO.getmode(), None) |
| 85 |
GPIO.setmode(GPIO.BCM) |
| 86 |
self.assertEqual(GPIO.getmode(), GPIO.BCM) |
| 87 |
GPIO.setup(LED_PIN_BCM, GPIO.IN) |
| 88 |
GPIO.cleanup() |
| 89 |
GPIO.setmode(GPIO.BOARD) |
| 90 |
self.assertEqual(GPIO.getmode(), GPIO.BOARD) |
| 91 |
|
| 92 |
|
| 93 |
GPIO.setmode(GPIO.BOARD) |
| 94 |
with self.assertRaises(RuntimeError) as e: |
| 95 |
GPIO.output(LED_PIN, GPIO.HIGH) |
| 96 |
self.assertEqual(str(e.exception), 'The GPIO channel has not been set up as an OUTPUT') |
| 97 |
|
| 98 |
|
| 99 |
GPIO.setmode(GPIO.BOARD) |
| 100 |
with self.assertRaises(ValueError): |
| 101 |
GPIO.setup(LED_PIN, GPIO.IN, pull_up_down=GPIO.HIGH) |
| 102 |
|
| 103 |
|
| 104 |
GPIO.setmode(GPIO.BOARD) |
| 105 |
with self.assertRaises(ValueError) as e: |
| 106 |
GPIO.setup(GND_PIN, GPIO.OUT) |
| 107 |
self.assertEqual(str(e.exception), 'The channel sent is invalid on a Raspberry Pi') |
| 108 |
|
| 109 |
|
| 110 |
GPIO.setmode(GPIO.BOARD) |
| 111 |
with open('/sys/class/gpio/export','wb') as f: |
| 112 |
f.write(str(LED_PIN_BCM).encode()) |
| 113 |
time.sleep(0.2) |
| 114 |
with open('/sys/class/gpio/gpio%s/direction'%LED_PIN_BCM,'wb') as f: |
| 115 |
f.write(b'out') |
| 116 |
time.sleep(0.2) |
| 117 |
with warnings.catch_warnings(record=True) as w: |
| 118 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 119 |
self.assertEqual(w[0].category, RuntimeWarning) |
| 120 |
with open('/sys/class/gpio/unexport','wb') as f: |
| 121 |
f.write(str(LED_PIN_BCM).encode()) |
| 122 |
GPIO.cleanup() |
| 123 |
|
| 124 |
|
| 125 |
GPIO.setmode(GPIO.BOARD) |
| 126 |
GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.HIGH) |
| 127 |
self.assertEqual(GPIO.input(LED_PIN), GPIO.HIGH) |
| 128 |
GPIO.cleanup() |
| 129 |
|
| 130 |
|
| 131 |
GPIO.setmode(GPIO.BOARD) |
| 132 |
GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.LOW) |
| 133 |
self.assertEqual(GPIO.input(LED_PIN), GPIO.LOW) |
| 134 |
GPIO.cleanup() |
| 135 |
|
| 136 |
|
| 137 |
GPIO.setmode(GPIO.BOARD) |
| 138 |
for i in range(1000): |
| 139 |
GPIO.setup(NC_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) |
| 140 |
time.sleep(0.001) |
| 141 |
self.assertEqual(GPIO.input(NC_PIN), GPIO.LOW) |
| 142 |
GPIO.setup(NC_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) |
| 143 |
time.sleep(0.001) |
| 144 |
self.assertEqual(GPIO.input(NC_PIN), GPIO.HIGH) |
| 145 |
GPIO.cleanup() |
| 146 |
|
| 147 |
|
| 148 |
GPIO.setmode(GPIO.BOARD) |
| 149 |
GPIO.setup( [LED_PIN, LOOP_OUT], GPIO.OUT) |
| 150 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT) |
| 151 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.OUT) |
| 152 |
GPIO.cleanup() |
| 153 |
GPIO.setmode(GPIO.BOARD) |
| 154 |
with self.assertRaises(ValueError) as e: |
| 155 |
GPIO.setup( [LED_PIN, GND_PIN], GPIO.OUT) |
| 156 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT) |
| 157 |
self.assertEqual(str(e.exception), 'The channel sent is invalid on a Raspberry Pi') |
| 158 |
GPIO.cleanup() |
| 159 |
|
| 160 |
|
| 161 |
GPIO.setmode(GPIO.BOARD) |
| 162 |
GPIO.setup( (LED_PIN, LOOP_OUT), GPIO.OUT) |
| 163 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT) |
| 164 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.OUT) |
| 165 |
GPIO.cleanup() |
| 166 |
|
| 167 |
|
| 168 |
GPIO.setmode(GPIO.BOARD) |
| 169 |
if GPIO.RPI_INFO['P1_REVISION'] == 0: |
| 170 |
pass |
| 171 |
else: |
| 172 |
with warnings.catch_warnings(record=True) as w: |
| 173 |
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) |
| 174 |
self.assertEqual(w[0].category, RuntimeWarning) |
| 175 |
with warnings.catch_warnings(record=True) as w: |
| 176 |
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) |
| 177 |
self.assertEqual(w[0].category, RuntimeWarning) |
| 178 |
GPIO.cleanup() |
| 179 |
|
| 180 |
|
| 181 |
GPIO.setmode(GPIO.BOARD) |
| 182 |
with self.assertRaises(ValueError): |
| 183 |
GPIO.setup('d', GPIO.OUT) |
| 184 |
with self.assertRaises(ValueError): |
| 185 |
GPIO.setup(('d',LED_PIN), GPIO.OUT) |
| 186 |
|
| 187 |
|
| 188 |
GPIO.setmode(GPIO.BOARD) |
| 189 |
with self.assertRaises(ValueError): |
| 190 |
GPIO.setup(LOOP_OUT, GPIO.OUT, pull_up_down=GPIO.PUD_DOWN) |
| 191 |
|
| 192 |
|
| 193 |
GPIO.setmode(GPIO.BOARD) |
| 194 |
with self.assertRaises(ValueError): |
| 195 |
GPIO.setup(LOOP_IN, GPIO.IN, initial=GPIO.LOW) |
| 196 |
|
| 197 |
class TestInputOutput(unittest.TestCase): |
| 198 |
def setUp(self): |
| 199 |
GPIO.setmode(GPIO.BOARD) |
| 200 |
|
| 201 |
def test_outputread(self): |
| 202 |
"""Test that an output() can be input()""" |
| 203 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 204 |
GPIO.output(LED_PIN, GPIO.HIGH) |
| 205 |
self.assertEqual(GPIO.input(LED_PIN), GPIO.HIGH) |
| 206 |
GPIO.output(LED_PIN, GPIO.LOW) |
| 207 |
self.assertEqual(GPIO.input(LED_PIN), GPIO.LOW) |
| 208 |
|
| 209 |
def test_loopback(self): |
| 210 |
"""Test output loops back to another input""" |
| 211 |
GPIO.setup(LOOP_IN, GPIO.IN, pull_up_down=GPIO.PUD_OFF) |
| 212 |
GPIO.setup(LOOP_OUT, GPIO.OUT, initial=GPIO.LOW) |
| 213 |
self.assertEqual(GPIO.input(LOOP_IN), GPIO.LOW) |
| 214 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 215 |
self.assertEqual(GPIO.input(LOOP_IN), GPIO.HIGH) |
| 216 |
|
| 217 |
def test_output_on_input(self): |
| 218 |
"""Test output() can not be done on input""" |
| 219 |
GPIO.setup(SWITCH_PIN, GPIO.IN) |
| 220 |
with self.assertRaises(RuntimeError): |
| 221 |
GPIO.output(SWITCH_PIN, GPIO.LOW) |
| 222 |
|
| 223 |
def test_output_list(self): |
| 224 |
"""Test output() using lists""" |
| 225 |
GPIO.setup(LOOP_OUT, GPIO.OUT) |
| 226 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 227 |
|
| 228 |
GPIO.output( [LOOP_OUT, LED_PIN], GPIO.HIGH) |
| 229 |
self.assertEqual(GPIO.input(LOOP_OUT), GPIO.HIGH) |
| 230 |
self.assertEqual(GPIO.input(LED_PIN), GPIO.HIGH) |
| 231 |
|
| 232 |
GPIO.output( (LOOP_OUT, LED_PIN), GPIO.LOW) |
| 233 |
self.assertEqual(GPIO.input(LOOP_OUT), GPIO.LOW) |
| 234 |
self.assertEqual(GPIO.input(LED_PIN), GPIO.LOW) |
| 235 |
|
| 236 |
GPIO.output( [LOOP_OUT, LED_PIN], [GPIO.HIGH, GPIO.LOW] ) |
| 237 |
self.assertEqual(GPIO.input(LOOP_OUT), GPIO.HIGH) |
| 238 |
self.assertEqual(GPIO.input(LED_PIN), GPIO.LOW) |
| 239 |
|
| 240 |
GPIO.output( (LOOP_OUT, LED_PIN), (GPIO.LOW, GPIO.HIGH) ) |
| 241 |
self.assertEqual(GPIO.input(LOOP_OUT), GPIO.LOW) |
| 242 |
self.assertEqual(GPIO.input(LED_PIN), GPIO.HIGH) |
| 243 |
|
| 244 |
with self.assertRaises(RuntimeError): |
| 245 |
GPIO.output( [LOOP_OUT, LED_PIN], [0,0,0] ) |
| 246 |
|
| 247 |
with self.assertRaises(RuntimeError): |
| 248 |
GPIO.output( [LOOP_OUT, LED_PIN], (0,) ) |
| 249 |
|
| 250 |
with self.assertRaises(RuntimeError): |
| 251 |
GPIO.output(LOOP_OUT, (0,0)) |
| 252 |
|
| 253 |
with self.assertRaises(ValueError): |
| 254 |
GPIO.output( [LOOP_OUT, 'x'], (0,0) ) |
| 255 |
|
| 256 |
with self.assertRaises(ValueError): |
| 257 |
GPIO.output( [LOOP_OUT, LED_PIN], (0,'x') ) |
| 258 |
|
| 259 |
with self.assertRaises(ValueError): |
| 260 |
GPIO.output( [LOOP_OUT, GND_PIN], (0,0) ) |
| 261 |
|
| 262 |
with self.assertRaises(RuntimeError): |
| 263 |
GPIO.output( [LOOP_OUT, LOOP_IN], (0,0) ) |
| 264 |
|
| 265 |
def tearDown(self): |
| 266 |
GPIO.cleanup() |
| 267 |
|
| 268 |
class TestSoftPWMExists(unittest.TestCase): |
| 269 |
def runTest(self): |
| 270 |
GPIO.setmode(GPIO.BOARD) |
| 271 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 272 |
pwm = GPIO.PWM(LED_PIN, 50) |
| 273 |
with self.assertRaises(RuntimeError): |
| 274 |
pwm2 = GPIO.PWM(LED_PIN, 49) |
| 275 |
GPIO.cleanup() |
| 276 |
|
| 277 |
class TestSoftPWM(unittest.TestCase): |
| 278 |
@unittest.skipIf(non_interactive, 'Non interactive mode') |
| 279 |
def runTest(self): |
| 280 |
GPIO.setmode(GPIO.BOARD) |
| 281 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 282 |
pwm = GPIO.PWM(LED_PIN, 50) |
| 283 |
pwm.start(100) |
| 284 |
print("\nPWM tests") |
| 285 |
response = input('Is the LED on (y/n) ? ').upper() |
| 286 |
self.assertEqual(response,'Y') |
| 287 |
pwm.start(0) |
| 288 |
response = input('Is the LED off (y/n) ? ').upper() |
| 289 |
self.assertEqual(response,'Y') |
| 290 |
print("LED Brighten/fade test...") |
| 291 |
for i in range(0,3): |
| 292 |
for x in range(0,101,5): |
| 293 |
pwm.ChangeDutyCycle(x) |
| 294 |
time.sleep(0.1) |
| 295 |
for x in range(100,-1,-5): |
| 296 |
pwm.ChangeDutyCycle(x) |
| 297 |
time.sleep(0.1) |
| 298 |
pwm.stop() |
| 299 |
response = input('Did it work (y/n) ? ').upper() |
| 300 |
self.assertEqual(response,'Y') |
| 301 |
GPIO.cleanup() |
| 302 |
|
| 303 |
class TestSetWarnings(unittest.TestCase): |
| 304 |
def test_alreadyinuse(self): |
| 305 |
"""Test 'already in use' warning""" |
| 306 |
GPIO.setmode(GPIO.BOARD) |
| 307 |
GPIO.setwarnings(False) |
| 308 |
with open('/sys/class/gpio/export','wb') as f: |
| 309 |
f.write(str(LED_PIN_BCM).encode()) |
| 310 |
time.sleep(0.2) |
| 311 |
with open('/sys/class/gpio/gpio%s/direction'%LED_PIN_BCM,'wb') as f: |
| 312 |
f.write(b'out') |
| 313 |
with warnings.catch_warnings(record=True) as w: |
| 314 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 315 |
self.assertEqual(len(w),0) |
| 316 |
with open('/sys/class/gpio/unexport','wb') as f: |
| 317 |
f.write(str(LED_PIN_BCM).encode()) |
| 318 |
GPIO.cleanup() |
| 319 |
|
| 320 |
GPIO.setmode(GPIO.BOARD) |
| 321 |
GPIO.setwarnings(True) |
| 322 |
with open('/sys/class/gpio/export','wb') as f: |
| 323 |
f.write(str(LED_PIN_BCM).encode()) |
| 324 |
time.sleep(0.2) |
| 325 |
with open('/sys/class/gpio/gpio%s/direction'%LED_PIN_BCM,'wb') as f: |
| 326 |
f.write(b'out') |
| 327 |
with warnings.catch_warnings(record=True) as w: |
| 328 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 329 |
self.assertEqual(w[0].category, RuntimeWarning) |
| 330 |
with open('/sys/class/gpio/unexport','wb') as f: |
| 331 |
f.write(str(LED_PIN_BCM).encode()) |
| 332 |
GPIO.cleanup() |
| 333 |
|
| 334 |
def test_cleanupwarning(self): |
| 335 |
"""Test initial GPIO.cleanup() produces warning""" |
| 336 |
GPIO.setwarnings(False) |
| 337 |
GPIO.setmode(GPIO.BOARD) |
| 338 |
GPIO.setup(SWITCH_PIN, GPIO.IN) |
| 339 |
with warnings.catch_warnings(record=True) as w: |
| 340 |
GPIO.cleanup() |
| 341 |
self.assertEqual(len(w),0) |
| 342 |
GPIO.cleanup() |
| 343 |
self.assertEqual(len(w),0) |
| 344 |
|
| 345 |
GPIO.setwarnings(True) |
| 346 |
GPIO.setmode(GPIO.BOARD) |
| 347 |
GPIO.setup(SWITCH_PIN, GPIO.IN) |
| 348 |
with warnings.catch_warnings(record=True) as w: |
| 349 |
GPIO.cleanup() |
| 350 |
self.assertEqual(len(w),0) |
| 351 |
GPIO.cleanup() |
| 352 |
self.assertEqual(w[0].category, RuntimeWarning) |
| 353 |
|
| 354 |
class TestVersions(unittest.TestCase): |
| 355 |
def test_rpi_info(self): |
| 356 |
print('RPi Board Information') |
| 357 |
print('---------------------') |
| 358 |
for key,val in list(GPIO.RPI_INFO.items()): |
| 359 |
print('%s => %s'%(key,val)) |
| 360 |
response = input('\nIs this board info correct (y/n) ? ').upper() |
| 361 |
self.assertEqual(response, 'Y') |
| 362 |
|
| 363 |
def test_gpio_version(self): |
| 364 |
response = input('\nRPi.GPIO version %s - is this correct (y/n) ? '%GPIO.VERSION).upper() |
| 365 |
self.assertEqual(response, 'Y') |
| 366 |
|
| 367 |
class TestGPIOFunction(unittest.TestCase): |
| 368 |
def runTest(self): |
| 369 |
GPIO.setmode(GPIO.BCM) |
| 370 |
GPIO.setup(LED_PIN_BCM, GPIO.IN) |
| 371 |
self.assertEqual(GPIO.gpio_function(LED_PIN_BCM), GPIO.IN) |
| 372 |
GPIO.setup(LED_PIN_BCM, GPIO.OUT) |
| 373 |
self.assertEqual(GPIO.gpio_function(LED_PIN_BCM), GPIO.OUT) |
| 374 |
GPIO.cleanup() |
| 375 |
|
| 376 |
GPIO.setmode(GPIO.BOARD) |
| 377 |
GPIO.setup(LED_PIN, GPIO.IN) |
| 378 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN) |
| 379 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 380 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT) |
| 381 |
|
| 382 |
def tearDown(self): |
| 383 |
GPIO.cleanup() |
| 384 |
|
| 385 |
class TestSwitchBounce(unittest.TestCase): |
| 386 |
def __init__(self, *a, **k): |
| 387 |
unittest.TestCase.__init__(self, *a, **k) |
| 388 |
self.switchcount = 0 |
| 389 |
|
| 390 |
def cb(self,chan): |
| 391 |
self.switchcount += 1 |
| 392 |
print('Button press',self.switchcount) |
| 393 |
|
| 394 |
def setUp(self): |
| 395 |
GPIO.setmode(GPIO.BOARD) |
| 396 |
GPIO.setup(SWITCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) |
| 397 |
|
| 398 |
@unittest.skipIf(non_interactive, 'Non interactive mode') |
| 399 |
def test_switchbounce(self): |
| 400 |
self.switchcount = 0 |
| 401 |
print("\nSwitch bounce test. Press switch at least 10 times and count...") |
| 402 |
GPIO.add_event_detect(SWITCH_PIN, GPIO.FALLING, callback=self.cb, bouncetime=200) |
| 403 |
while self.switchcount < 10: |
| 404 |
time.sleep(1) |
| 405 |
GPIO.remove_event_detect(SWITCH_PIN) |
| 406 |
|
| 407 |
@unittest.skipIf(non_interactive, 'Non interactive mode') |
| 408 |
def test_event_detected(self): |
| 409 |
self.switchcount = 0 |
| 410 |
print("\nGPIO.event_detected() switch bounce test. Press switch at least 10 times and count...") |
| 411 |
GPIO.add_event_detect(SWITCH_PIN, GPIO.FALLING, bouncetime=200) |
| 412 |
while self.switchcount < 10: |
| 413 |
if GPIO.event_detected(SWITCH_PIN): |
| 414 |
self.switchcount += 1 |
| 415 |
print('Button press',self.switchcount) |
| 416 |
GPIO.remove_event_detect(SWITCH_PIN) |
| 417 |
|
| 418 |
def tearDown(self): |
| 419 |
GPIO.cleanup() |
| 420 |
|
| 421 |
class TestEdgeDetection(unittest.TestCase): |
| 422 |
def setUp(self): |
| 423 |
GPIO.setmode(GPIO.BOARD) |
| 424 |
GPIO.setup(LOOP_IN, GPIO.IN) |
| 425 |
GPIO.setup(LOOP_OUT, GPIO.OUT) |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
def testShellCmdWithWaitForEdge(self): |
| 431 |
self.finished = False |
| 432 |
def shellcmd(): |
| 433 |
for i in range(50): |
| 434 |
os.system('sleep 0') |
| 435 |
subprocess.call('sleep 0', shell=True) |
| 436 |
self.finished = True |
| 437 |
def makehigh(): |
| 438 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 439 |
|
| 440 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 441 |
t1 = Timer(0.1, shellcmd) |
| 442 |
t2 = Timer(0.5, makehigh) |
| 443 |
t1.start() |
| 444 |
t2.start() |
| 445 |
starttime = time.time() |
| 446 |
channel = GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, timeout=1000) |
| 447 |
endtime = time.time() |
| 448 |
self.assertGreater(endtime - starttime, 0.5) |
| 449 |
self.assertLess(endtime - starttime, 0.6) |
| 450 |
self.assertEqual(channel, LOOP_IN) |
| 451 |
|
| 452 |
|
| 453 |
while not self.finished: |
| 454 |
time.sleep(0.1) |
| 455 |
|
| 456 |
def testShellCmdWithEventCallback(self): |
| 457 |
self.run_cb = False |
| 458 |
|
| 459 |
def cb(channel): |
| 460 |
self.run_cb = True |
| 461 |
|
| 462 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 463 |
GPIO.add_event_detect(LOOP_IN, GPIO.RISING, callback=cb) |
| 464 |
time.sleep(0.01) |
| 465 |
|
| 466 |
for i in range(50): |
| 467 |
os.system('sleep 0') |
| 468 |
subprocess.call('sleep 0', shell=True) |
| 469 |
|
| 470 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 471 |
time.sleep(0.01) |
| 472 |
GPIO.remove_event_detect(LOOP_IN) |
| 473 |
self.assertEqual(self.run_cb, True) |
| 474 |
|
| 475 |
def testWaitForEdgeInLoop(self): |
| 476 |
def makelow(): |
| 477 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 478 |
|
| 479 |
count = 0 |
| 480 |
timestart = time.time() |
| 481 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 482 |
while True: |
| 483 |
t = Timer(0.1, makelow) |
| 484 |
t.start() |
| 485 |
starttime = time.time() |
| 486 |
channel = GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING, timeout=200) |
| 487 |
endtime = time.time() |
| 488 |
self.assertLess(endtime-starttime, 0.12) |
| 489 |
self.assertEqual(channel, LOOP_IN) |
| 490 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 491 |
count += 1 |
| 492 |
if time.time() - timestart > 5 or count > 150: |
| 493 |
break |
| 494 |
|
| 495 |
def testWaitForEdgeWithCallback(self): |
| 496 |
def cb(): |
| 497 |
raise Exception("Callback should not be called") |
| 498 |
def makehigh(): |
| 499 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 500 |
|
| 501 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 502 |
t = Timer(0.1, makehigh) |
| 503 |
|
| 504 |
GPIO.add_event_detect(LOOP_IN, GPIO.RISING) |
| 505 |
t.start() |
| 506 |
GPIO.wait_for_edge(LOOP_IN, GPIO.RISING) |
| 507 |
|
| 508 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 509 |
GPIO.add_event_callback(LOOP_IN, callback=cb) |
| 510 |
with self.assertRaises(RuntimeError): |
| 511 |
GPIO.wait_for_edge(LOOP_IN, GPIO.RISING) |
| 512 |
|
| 513 |
GPIO.remove_event_detect(LOOP_IN) |
| 514 |
|
| 515 |
def testWaitForEventSwitchbounce(self): |
| 516 |
self.finished = False |
| 517 |
def bounce(): |
| 518 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 519 |
time.sleep(0.01) |
| 520 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 521 |
time.sleep(0.01) |
| 522 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 523 |
time.sleep(0.01) |
| 524 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 525 |
time.sleep(0.2) |
| 526 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 527 |
time.sleep(0.01) |
| 528 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 529 |
time.sleep(0.01) |
| 530 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 531 |
time.sleep(0.01) |
| 532 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 533 |
self.finished = True |
| 534 |
|
| 535 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 536 |
t1 = Timer(0.1, bounce) |
| 537 |
t1.start() |
| 538 |
|
| 539 |
starttime = time.time() |
| 540 |
GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=100) |
| 541 |
GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=100) |
| 542 |
finishtime = time.time() |
| 543 |
self.assertGreater(finishtime-starttime, 0.2) |
| 544 |
while not self.finished: |
| 545 |
time.sleep(0.1) |
| 546 |
|
| 547 |
def testInvalidBouncetime(self): |
| 548 |
with self.assertRaises(ValueError): |
| 549 |
GPIO.add_event_detect(LOOP_IN, GPIO.RISING, bouncetime=-1) |
| 550 |
with self.assertRaises(ValueError): |
| 551 |
GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=-1) |
| 552 |
GPIO.add_event_detect(LOOP_IN, GPIO.RISING, bouncetime=123) |
| 553 |
with self.assertRaises(RuntimeError): |
| 554 |
GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, bouncetime=321) |
| 555 |
GPIO.remove_event_detect(LOOP_IN) |
| 556 |
|
| 557 |
def testAlreadyAdded(self): |
| 558 |
GPIO.add_event_detect(LOOP_IN, GPIO.RISING) |
| 559 |
with self.assertRaises(RuntimeError): |
| 560 |
GPIO.add_event_detect(LOOP_IN, GPIO.RISING) |
| 561 |
GPIO.remove_event_detect(LOOP_IN) |
| 562 |
|
| 563 |
def testHighLowEvent(self): |
| 564 |
with self.assertRaises(ValueError): |
| 565 |
GPIO.add_event_detect(LOOP_IN, GPIO.LOW) |
| 566 |
with self.assertRaises(ValueError): |
| 567 |
GPIO.add_event_detect(LOOP_IN, GPIO.HIGH) |
| 568 |
|
| 569 |
def testFallingEventDetected(self): |
| 570 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 571 |
GPIO.add_event_detect(LOOP_IN, GPIO.FALLING) |
| 572 |
time.sleep(0.01) |
| 573 |
self.assertEqual(GPIO.event_detected(LOOP_IN), False) |
| 574 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 575 |
time.sleep(0.01) |
| 576 |
self.assertEqual(GPIO.event_detected(LOOP_IN), True) |
| 577 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 578 |
time.sleep(0.01) |
| 579 |
self.assertEqual(GPIO.event_detected(LOOP_IN), False) |
| 580 |
GPIO.remove_event_detect(LOOP_IN) |
| 581 |
|
| 582 |
def testRisingEventDetected(self): |
| 583 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 584 |
GPIO.add_event_detect(LOOP_IN, GPIO.RISING) |
| 585 |
time.sleep(0.01) |
| 586 |
self.assertEqual(GPIO.event_detected(LOOP_IN), False) |
| 587 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 588 |
time.sleep(0.01) |
| 589 |
self.assertEqual(GPIO.event_detected(LOOP_IN), True) |
| 590 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 591 |
time.sleep(0.01) |
| 592 |
self.assertEqual(GPIO.event_detected(LOOP_IN), False) |
| 593 |
GPIO.remove_event_detect(LOOP_IN) |
| 594 |
|
| 595 |
def testBothEventDetected(self): |
| 596 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 597 |
GPIO.add_event_detect(LOOP_IN, GPIO.BOTH) |
| 598 |
time.sleep(0.01) |
| 599 |
self.assertEqual(GPIO.event_detected(LOOP_IN), False) |
| 600 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 601 |
time.sleep(0.01) |
| 602 |
self.assertEqual(GPIO.event_detected(LOOP_IN), True) |
| 603 |
self.assertEqual(GPIO.event_detected(LOOP_IN), False) |
| 604 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 605 |
time.sleep(0.01) |
| 606 |
self.assertEqual(GPIO.event_detected(LOOP_IN), True) |
| 607 |
GPIO.remove_event_detect(LOOP_IN) |
| 608 |
|
| 609 |
def testWaitForRising(self): |
| 610 |
def makehigh(): |
| 611 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 612 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 613 |
t = Timer(0.1, makehigh) |
| 614 |
t.start() |
| 615 |
GPIO.wait_for_edge(LOOP_IN, GPIO.RISING) |
| 616 |
|
| 617 |
def testWaitForFalling(self): |
| 618 |
def makelow(): |
| 619 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 620 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 621 |
t = Timer(0.1, makelow) |
| 622 |
t.start() |
| 623 |
GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING) |
| 624 |
|
| 625 |
def testExceptionInCallback(self): |
| 626 |
self.run_cb = False |
| 627 |
def cb(channel): |
| 628 |
with self.assertRaises(ZeroDivisionError): |
| 629 |
self.run_cb = True |
| 630 |
a = 1/0 |
| 631 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 632 |
GPIO.add_event_detect(LOOP_IN, GPIO.RISING, callback=cb) |
| 633 |
time.sleep(0.01) |
| 634 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 635 |
time.sleep(0.01) |
| 636 |
self.assertEqual(self.run_cb, True) |
| 637 |
GPIO.remove_event_detect(LOOP_IN) |
| 638 |
|
| 639 |
def testAddEventCallback(self): |
| 640 |
def cb(channel): |
| 641 |
self.callback_count += 1 |
| 642 |
|
| 643 |
|
| 644 |
self.callback_count = 0 |
| 645 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 646 |
GPIO.add_event_detect(LOOP_IN, GPIO.FALLING) |
| 647 |
GPIO.add_event_callback(LOOP_IN, cb) |
| 648 |
time.sleep(0.01) |
| 649 |
for i in range(2048): |
| 650 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 651 |
time.sleep(0.001) |
| 652 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 653 |
time.sleep(0.001) |
| 654 |
GPIO.remove_event_detect(LOOP_IN) |
| 655 |
self.assertEqual(self.callback_count, 2048) |
| 656 |
|
| 657 |
|
| 658 |
self.callback_count = 0 |
| 659 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 660 |
GPIO.add_event_detect(LOOP_IN, GPIO.RISING, callback=cb) |
| 661 |
time.sleep(0.01) |
| 662 |
for i in range(2048): |
| 663 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 664 |
time.sleep(0.001) |
| 665 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 666 |
time.sleep(0.001) |
| 667 |
GPIO.remove_event_detect(LOOP_IN) |
| 668 |
self.assertEqual(self.callback_count, 2048) |
| 669 |
|
| 670 |
|
| 671 |
self.callback_count = 0 |
| 672 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 673 |
GPIO.add_event_detect(LOOP_IN, GPIO.BOTH, callback=cb) |
| 674 |
time.sleep(0.01) |
| 675 |
for i in range(2048): |
| 676 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 677 |
time.sleep(0.001) |
| 678 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 679 |
time.sleep(0.001) |
| 680 |
GPIO.remove_event_detect(LOOP_IN) |
| 681 |
self.assertEqual(self.callback_count, 4096) |
| 682 |
|
| 683 |
def testEventOnOutput(self): |
| 684 |
with self.assertRaises(RuntimeError): |
| 685 |
GPIO.add_event_detect(LOOP_OUT, GPIO.FALLING) |
| 686 |
|
| 687 |
def testAlternateWaitForEdge(self): |
| 688 |
def makehigh(): |
| 689 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 690 |
def makelow(): |
| 691 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 692 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 693 |
t = Timer(0.1, makehigh) |
| 694 |
t2 = Timer(0.15, makelow) |
| 695 |
t.start() |
| 696 |
t2.start() |
| 697 |
GPIO.wait_for_edge(LOOP_IN, GPIO.RISING) |
| 698 |
GPIO.wait_for_edge(LOOP_IN, GPIO.FALLING) |
| 699 |
|
| 700 |
def testWaitForEdgeTimeout(self): |
| 701 |
def makehigh(): |
| 702 |
GPIO.output(LOOP_OUT, GPIO.HIGH) |
| 703 |
def makelow(): |
| 704 |
GPIO.output(LOOP_OUT, GPIO.LOW) |
| 705 |
|
| 706 |
with self.assertRaises(TypeError): |
| 707 |
GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, timeout="beer") |
| 708 |
|
| 709 |
with self.assertRaises(ValueError): |
| 710 |
GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, timeout=-1234) |
| 711 |
|
| 712 |
makelow() |
| 713 |
chan = GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, timeout=200) |
| 714 |
self.assertEqual(chan, None) |
| 715 |
|
| 716 |
t = Timer(0.1, makehigh) |
| 717 |
t.start() |
| 718 |
chan = GPIO.wait_for_edge(LOOP_IN, GPIO.RISING, timeout=200) |
| 719 |
self.assertEqual(chan, LOOP_IN) |
| 720 |
|
| 721 |
def tearDown(self): |
| 722 |
GPIO.cleanup() |
| 723 |
|
| 724 |
class TestCleanup(unittest.TestCase): |
| 725 |
def setUp(self): |
| 726 |
GPIO.setmode(GPIO.BOARD) |
| 727 |
|
| 728 |
def test_cleanall(self): |
| 729 |
GPIO.setup(LOOP_OUT, GPIO.OUT) |
| 730 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 731 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.OUT) |
| 732 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT) |
| 733 |
GPIO.cleanup() |
| 734 |
GPIO.setmode(GPIO.BOARD) |
| 735 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN) |
| 736 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN) |
| 737 |
|
| 738 |
def test_cleanone(self): |
| 739 |
GPIO.setup(LOOP_OUT, GPIO.OUT) |
| 740 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 741 |
GPIO.setup(SWITCH_PIN, GPIO.IN) |
| 742 |
GPIO.setup(LOOP_IN, GPIO.IN) |
| 743 |
GPIO.add_event_detect(SWITCH_PIN, GPIO.FALLING) |
| 744 |
GPIO.add_event_detect(LOOP_IN, GPIO.RISING) |
| 745 |
time.sleep(0.2) |
| 746 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.OUT) |
| 747 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT) |
| 748 |
self.assertEqual(GPIO.gpio_function(SWITCH_PIN), GPIO.IN) |
| 749 |
self.assertEqual(GPIO.gpio_function(LOOP_IN), GPIO.IN) |
| 750 |
self.assertTrue(os.path.exists('/sys/class/gpio/gpio%s'%SWITCH_PIN_BCM)) |
| 751 |
self.assertTrue(os.path.exists('/sys/class/gpio/gpio%s'%LOOP_IN_BCM)) |
| 752 |
GPIO.cleanup(SWITCH_PIN) |
| 753 |
time.sleep(0.2) |
| 754 |
self.assertFalse(os.path.exists('/sys/class/gpio/gpio%s'%SWITCH_PIN_BCM)) |
| 755 |
self.assertTrue(os.path.exists('/sys/class/gpio/gpio%s'%LOOP_IN_BCM)) |
| 756 |
GPIO.cleanup(LOOP_IN) |
| 757 |
time.sleep(0.2) |
| 758 |
self.assertFalse(os.path.exists('/sys/class/gpio/gpio%s'%SWITCH_PIN_BCM)) |
| 759 |
self.assertFalse(os.path.exists('/sys/class/gpio/gpio%s'%LOOP_IN_BCM)) |
| 760 |
GPIO.cleanup(LOOP_OUT) |
| 761 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN) |
| 762 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT) |
| 763 |
GPIO.cleanup(LED_PIN) |
| 764 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN) |
| 765 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN) |
| 766 |
|
| 767 |
def test_cleantuple(self): |
| 768 |
GPIO.setup(LOOP_OUT, GPIO.OUT) |
| 769 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 770 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.OUT) |
| 771 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT) |
| 772 |
GPIO.cleanup((LOOP_OUT,)) |
| 773 |
GPIO.setmode(GPIO.BOARD) |
| 774 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN) |
| 775 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT) |
| 776 |
GPIO.cleanup((LED_PIN,)) |
| 777 |
GPIO.setmode(GPIO.BOARD) |
| 778 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN) |
| 779 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN) |
| 780 |
GPIO.setup(LOOP_OUT, GPIO.OUT) |
| 781 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 782 |
GPIO.cleanup((LOOP_OUT,LED_PIN)) |
| 783 |
GPIO.setmode(GPIO.BOARD) |
| 784 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN) |
| 785 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN) |
| 786 |
|
| 787 |
def test_cleanlist(self): |
| 788 |
GPIO.setup(LOOP_OUT, GPIO.OUT) |
| 789 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 790 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.OUT) |
| 791 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT) |
| 792 |
GPIO.cleanup([LOOP_OUT]) |
| 793 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN) |
| 794 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.OUT) |
| 795 |
GPIO.cleanup([LED_PIN]) |
| 796 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN) |
| 797 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN) |
| 798 |
GPIO.setup(LOOP_OUT, GPIO.OUT) |
| 799 |
GPIO.setup(LED_PIN, GPIO.OUT) |
| 800 |
GPIO.cleanup([LOOP_OUT,LED_PIN]) |
| 801 |
self.assertEqual(GPIO.gpio_function(LOOP_OUT), GPIO.IN) |
| 802 |
self.assertEqual(GPIO.gpio_function(LED_PIN), GPIO.IN) |
| 803 |
|
| 804 |
if __name__ == '__main__': |
| 805 |
unittest.main() |