Skip to content

Commit 082bfff

Browse files
authored
Add logging support, turn off unconditional debug output
1 parent 059d0f9 commit 082bfff

File tree

1 file changed

+93
-66
lines changed

1 file changed

+93
-66
lines changed

Adafruit_BBIO/Encoder.py

Lines changed: 93 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/python
22

3-
from subprocess import call
3+
from subprocess import check_output, STDOUT, CalledProcessError
44
import os
5+
import logging
6+
57

68
class QEP :
79

@@ -48,48 +50,64 @@ class RotaryEncoder(object):
4850
EQEP1 = 1
4951
EQEP2 = 2
5052
EQEP2b = 3
51-
53+
54+
def _run_cmd(self, cmd):
55+
'''Runs a command. If not successful (i.e. error code different than zero),
56+
print the stderr output as a warning.
57+
'''
58+
59+
try:
60+
output = check_output(cmd, stderr=STDOUT)
61+
self._logger.info("_run_cmd(): cmd='{}' return code={} output={}".format(
62+
" ".join(cmd), 0, output))
63+
except CalledProcessError as e:
64+
self._logger.warning(
65+
"_run_cmd(): cmd='{}' return code={} output={}".format(
66+
" ".join(cmd), e.returncode, e.output))
67+
5268
def config_pin(self, pin):
5369
'''
5470
config_pin()
5571
Config pin for QEP
5672
'''
57-
result = call(["config-pin", pin, "qep"])
58-
print("config_pin> pin={0} result={1}".format(pin, result))
59-
return result
60-
73+
74+
self._run_cmd(["config-pin", pin, "qep"])
75+
6176
def cat_file(self, path):
6277
'''
6378
cat_file()
6479
Print contents of file
6580
'''
66-
result = call(["cat", path])
67-
print("cat_file> path={0} result={1}".format(path, result))
68-
return result
69-
81+
82+
self._run_cmd(["cat", path])
83+
7084
def __init__(self, eqep_num):
7185
'''
7286
RotaryEncoder(eqep_num)
73-
Creates an instance of the class RotaryEncoder.
74-
eqep_num determines which eQEP pins are set up.
87+
Creates an instance of the class RotaryEncoder.
88+
eqep_num determines which eQEP pins are set up.
7589
eqep_num can be: EQEP0, EQEP1, EQEP2 or EQEP2b based on which pins \
7690
the rotary encoder is connected to.
7791
'''
78-
print(">>>>>>>> TEST CALL BEGIN")
7992

80-
###################################
81-
print(">>>>>> eqep0: P9_27, P9_92")
93+
self._logger = logging.getLogger(__name__)
94+
self._logger.addHandler(logging.NullHandler())
95+
#self._logger.setLevel(logging.DEBUG)
96+
97+
# Configure eqep0
98+
self._logger.info("Configuring eqep0, pins: P9.27, P9.92")
99+
82100
pin = "P9_27"
83101
self.config_pin(pin)
84102

85103
pin = "P9_92"
86104
self.config_pin(pin)
87105

88106
path = "/sys/devices/platform/ocp/48300000.epwmss/48300180.eqep/position"
89-
self.cat_file(path);
107+
self.cat_file(path)
90108

91-
###################################
92-
print(">>>>>>> eqep1: P8.33, P8.35")
109+
# Configure eqep1
110+
self._logger.info("Configuring eqep1, pins: P8.33, P8.35")
93111

94112
pin = "P8.33"
95113
self.config_pin(pin)
@@ -100,8 +118,8 @@ def __init__(self, eqep_num):
100118
path = "/sys/devices/platform/ocp/48302000.epwmss/48302180.eqep/position"
101119
self.cat_file(path);
102120

103-
###################################
104-
print(">>>>>>> eqep2: P8.11, P8.12")
121+
# Configure eqep2
122+
self._logger.info("Configuring eqep2, pins: P8.11, P8.12")
105123

106124
pin = "P8.11"
107125
self.config_pin(pin)
@@ -112,8 +130,8 @@ def __init__(self, eqep_num):
112130
path = "/sys/devices/platform/ocp/48304000.epwmss/48304180.eqep/position"
113131
self.cat_file(path);
114132

115-
###################################
116-
print(">>>>>>> eqep2b: P8.41, P8.42")
133+
# Configure eqep2b
134+
self._logger.info("Configuring eqep2, pins: P8.41, P8.42")
117135

118136
pin = "P8.41"
119137
self.config_pin(pin)
@@ -124,17 +142,17 @@ def __init__(self, eqep_num):
124142
path = "/sys/devices/platform/ocp/48304000.epwmss/48304180.eqep/position"
125143
self.cat_file(path);
126144

127-
###################################
128-
print(">>>>>>>> TEST CALL END")
145+
self._logger.debug("RotaryEncoder(): eqep_num: {0}".format(eqep_num))
146+
self._logger.debug("RotaryEncoder(): self._eqep_dirs[0]: {0}".format(self._eqep_dirs[0]))
147+
self._logger.debug("RotaryEncoder(): self._eqep_dirs[1]: {0}".format(self._eqep_dirs[1]))
148+
self._logger.debug("RotaryEncoder(): self._eqep_dirs[2]: {0}".format(self._eqep_dirs[2]))
149+
self._logger.debug("RotaryEncoder(): self._eqep_dirs[eqep_num: {0}]: {1}".format(eqep_num, self._eqep_dirs[eqep_num]))
129150

130-
print("RotaryEncoder(): eqep_num: {0}".format(eqep_num))
131-
print("RotaryEncoder(): self._eqep_dirs[0]: {0}".format(self._eqep_dirs[0]))
132-
print("RotaryEncoder(): self._eqep_dirs[1]: {0}".format(self._eqep_dirs[1]))
133-
print("RotaryEncoder(): self._eqep_dirs[2]: {0}".format(self._eqep_dirs[2]))
134-
print("RotaryEncoder(): self._eqep_dirs[eqep_num: {0}]: {1}".format(eqep_num, self._eqep_dirs[eqep_num]))
135151
assert 0 <= eqep_num <= 3 , "eqep_num must be between 0 and 3"
152+
136153
self.base_dir = self._eqep_dirs[eqep_num]
137-
print("RotaryEncoder(): self.base_dir: {0}".format(self.base_dir))
154+
self._logger.debug("RotaryEncoder(): self.base_dir: {0}".format(self.base_dir))
155+
138156
self.enable()
139157

140158
def enable(self):
@@ -143,93 +161,102 @@ def enable(self):
143161
Turns the eQEP hardware ON
144162
'''
145163
enable_file = "%s/enabled" % self.base_dir
146-
print("enable(): enable_file: {0}".format(enable_file))
147-
print("enable(): TODO: write 1 to enable_file")
148-
#return sysfs.kernelFileIO(enable_file, '1')
149-
164+
self._logger.debug("enable(): enable_file: {0}".format(enable_file))
165+
self._logger.warning(
166+
"enable(): TODO: not implemented, write 1 to {}".format(enable_file))
167+
#return sysfs.kernelFileIO(enable_file, '1')
168+
150169
def disable(self):
151170
'''
152171
disable()
153172
Turns the eQEP hardware OFF
154173
'''
155174
enable_file = "%s/enabled" % self.base_dir
156-
print("disable(): enable_file: {0}".format(enable_file))
157-
print("disable(): TODO: write 0 to enable_file")
175+
self._logger.debug("disable(): enable_file: {0}".format(enable_file))
176+
self._logger.warning(
177+
"disable(): TODO: not implemented, write 0 to {}".format(enable_file))
158178
#return sysfs.kernelFileIO(enable_file, '0')
159179

160180
def setAbsolute(self):
161181
'''
162182
setAbsolute()
163183
Set mode as Absolute
164-
The position starts at zero and is incremented or
184+
The position starts at zero and is incremented or
165185
decremented by the encoder's movement
166186
'''
167187
mode_file = "%s/mode" % self.base_dir
168-
print("setAbsolute(): mode_file: {0}".format(mode_file))
169-
print("setAbsolute(): TODO: write 0 to mode_file")
188+
self._logger.debug("setAbsolute(): mode_file: {0}".format(mode_file))
189+
self._logger.warning(
190+
"setAbsolute(): TODO: not implemented, write 0 to {}".format(mode_file))
170191
#return sysfs.kernelFileIO(mode_file, '0')
171-
192+
172193
def setRelative(self):
173194
'''
174195
setRelative()
175196
Set mode as Relative
176197
The position is reset when the unit timer overflows.
177198
'''
178199
mode_file = "%s/mode" % self.base_dir
179-
print("setRelative(): mode_file: {0}".format(mode_file))
180-
print("setRelative(): TODO: write 1 to mode_file")
200+
self._logger.debug("setRelative(): mode_file: {0}".format(mode_file))
201+
self._logger.warning(
202+
"setRelative(): TODO: not implemented, write 1 to {}".format(mode_file))
181203
#return sysfs.kernelFileIO(mode_file, '1')
182-
204+
183205
def getMode(self):
184206
'''
185207
getMode()
186208
Returns the mode the eQEP hardware is in.
187209
'''
188210
mode_file = "%s/mode" % self.base_dir
189-
print("getMode(): mode_file: {0}".format(mode_file))
190-
print("getMode(): TODO: read mode_file")
211+
self._logger.debug("getMode(): mode_file: {0}".format(mode_file))
212+
self._logger.warning("getMode(): TODO: read mode_file")
191213
#return sysfs.kernelFileIO(mode_file)
192214

193215
def getPosition(self):
194216
'''
195217
getPosition()
196218
Get the current position of the encoder.
197-
In absolute mode, this attribute represents the current position
198-
of the encoder.
199-
In relative mode, this attribute represents the position of the
219+
In absolute mode, this attribute represents the current position
220+
of the encoder.
221+
In relative mode, this attribute represents the position of the
200222
encoder at the last unit timer overflow.
201223
'''
202224
position_file = "%s/position" % self.base_dir
203-
print("getPosition(): position_file: {0}".format(position_file))
225+
self._logger.debug("getPosition(): position_file: {0}".format(position_file))
204226
position_handle = open(position_file, 'r')
205-
print("getPosition(): position_handle: {0}".format(position_handle))
227+
self._logger.debug("getPosition(): position_handle: {0}".format(position_handle))
206228
position = position_handle.read()
207-
print("getPosition(): position: {0}".format(position))
229+
self._logger.debug("getPosition(): position: {0}".format(position))
208230
#return sysfs.kernelFileIO(position_file)
231+
209232
return position
210-
211-
def setFrequency(self,freq):
233+
234+
def setFrequency(self, freq):
212235
'''
213236
setFrequency(freq)
214237
Set the frequency in Hz at which the driver reports new positions.
215238
'''
216239
period_file = "%s/period" % self.base_dir
217-
print("setFrequency(): period_file: {0}".format(period_file))
218-
print("setFrequency(): freq: {0}".format(period_file))
219-
print("setFrequency(): freq: {0}".format(freq))
220-
print("setFrequency(): 1000000000/freq: {0}".format(1000000000/freq))
221-
print("setFrequency(): str(1000000000/freq)): {0}".format(str(1000000000/freq)))
222-
print("setFrequency(): TODO: set period_file: {0}".format(str(1000000000/freq)))
240+
self._logger.debug("setFrequency(): period_file: {0}".format(period_file))
241+
self._logger.debug("setFrequency(): freq: {0}".format(freq))
242+
self._logger.debug("setFrequency(): 1000000000/freq: {0}".format(1000000000/freq))
243+
self._logger.debug("setFrequency(): str(1000000000/freq)): {0}".format(str(1000000000/freq)))
244+
self._logger.warning(
245+
"setFrequency(): TODO: not implemented, set {} to {}".format(
246+
period_file, str(1000000000/freq)))
223247
#return sysfs.kernelFileIO(period_file, str(1000000000/freq))
224-
225-
def setPosition(self,val):
226-
'''
248+
249+
def setPosition(self, val):
250+
'''
227251
setPosition(value)
228252
Give a new value to the current position
229253
'''
230254
position_file = "%s/position" % self.base_dir
255+
self._logger.warning(
256+
"setPosition(): TODO: not implemented, write position to {}".format(
257+
position_file))
231258
#return sysfs.kernelFileIO(position_file, str(val))
232-
259+
233260
def zero(self):
234261
'''
235262
zero()s
@@ -239,7 +266,7 @@ def zero(self):
239266

240267

241268
#"""
242-
# encoder_test.py
269+
# encoder_test.py
243270
# Rekha Seethamraju
244271
# An example to demonstrate the use of the eQEP library
245272
# for PyBBIO.
@@ -253,9 +280,9 @@ def zero(self):
253280
#def setup():
254281
# encoder.setAbsolute()
255282
# encoder.zero()
256-
#
283+
#
257284
#def loop():
258285
# print("encoder position : "+encoder.getPosition())
259286
# delay(1000)
260-
#
287+
#
261288
#run(setup, loop)

0 commit comments

Comments
 (0)