-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtoggle_test.py
executable file
·61 lines (52 loc) · 1.84 KB
/
toggle_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2020-2021 by Murray Altheim. All rights reserved. This file is part
# of the Robot Operating System project, released under the MIT License. Please
# see the LICENSE file included as part of this package.
#
# author: Murray Altheim
# created: 2020-03-16
# modified: 2021-01-19
#
# This tests the Toggle, which reads the state of the toggle switch.
#
import pytest
import os, sys, signal, time
from colorama import init, Fore, Style
init()
from lib.toggle import Toggle
from lib.logger import Logger, Level
def get_label(state):
_label = 'on' if state else 'off'
return _label
_log = Logger("toggle-test", Level.INFO)
_toggle = None
# ..............................................................................
@pytest.mark.unit
def test_toggle():
_log.info('starting test...')
_pin = 6
_toggle = Toggle(_pin, Level.INFO)
_initial_state = _toggle.state
while _initial_state == _toggle.state:
_log.info('waiting for change to toggle switch from' + Fore.YELLOW + ' {}'.format(get_label(_initial_state)) \
+ Fore.CYAN + ' on pin {:d}...'.format(_pin))
time.sleep(1.0)
while _initial_state != _toggle.state:
_log.info('waiting for change to toggle switch from' + Fore.YELLOW + ' {}'.format(get_label(not _initial_state)) \
+ Fore.CYAN + ' on pin {:d}...'.format(_pin))
time.sleep(1.0)
# main .........................................................................
def main(argv):
try:
test_toggle()
except KeyboardInterrupt:
_log.info("caught Ctrl-C.")
finally:
_log.info("closing toggle switch...")
if _toggle:
_toggle.close()
# call main ....................................................................
if __name__== "__main__":
main(sys.argv[1:])