-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrot_encoder_test.py
executable file
·81 lines (69 loc) · 2.44 KB
/
rot_encoder_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/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-11-13
# modified: 2020-11-13
#
import pytest
import sys, traceback
from colorama import init, Fore, Style
init()
from lib.i2c_scanner import I2CScanner
from lib.config_loader import ConfigLoader
from lib.rate import Rate
from lib.logger import Logger, Level
from lib.rotary_encoder import RotaryEncoder
# ..............................................................................
@pytest.mark.unit
def test_rot_encoder():
_log = Logger("rot-test", Level.INFO)
_i2c_scanner = I2CScanner(Level.WARN)
if not _i2c_scanner.has_address([0x16]):
_log.warning('test ignored: no rotary encoder found.')
return
_rot = None
try:
# read YAML configuration
_loader = ConfigLoader(Level.INFO)
filename = 'config.yaml'
_config = _loader.configure(filename)
_rot = RotaryEncoder(_config, 0x0F, Level.INFO)
_count = 0
_updates = 0
_update_led = True
_last_value = 0
_rate = Rate(20)
_log.info(Fore.WHITE + Style.BRIGHT + 'waiting for rotary encoder to make 10 ticks...')
while _updates < 10:
# _value = _rot.update() # original method
_value = _rot.read(_update_led) # improved method
if _value != _last_value:
_log.info(Style.BRIGHT + 'returned value: {:d}'.format(_value))
_updates += 1
_last_value = _value
_count += 1
if _count % 33 == 0:
_log.info(Fore.BLACK + Style.BRIGHT + 'waiting…')
_rate.wait()
finally:
if _rot:
_log.info('resetting rotary encoder...')
_rot.reset()
# main .........................................................................
_rot = None
def main(argv):
try:
test_rot_encoder()
except KeyboardInterrupt:
print(Fore.CYAN + 'caught Ctrl-C; exiting...' + Style.RESET_ALL)
except Exception:
print(Fore.RED + 'error starting ros: {}'.format(traceback.format_exc()) + Style.RESET_ALL)
# call main ....................................................................
if __name__== "__main__":
main(sys.argv[1:])
#EOF