From: W. Trevor King Date: Sat, 4 Oct 2008 22:34:58 +0000 (-0400) Subject: Cleaned up single_dio.py X-Git-Tag: 0.1 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=4e0bfd8e949b03ad678fdcdc84c93da2d2187078;p=pycomedi.git Cleaned up single_dio.py --- diff --git a/pycomedi/single_dio.py b/pycomedi/single_dio.py index e3fd9c0..3a0cea8 100644 --- a/pycomedi/single_dio.py +++ b/pycomedi/single_dio.py @@ -8,14 +8,22 @@ generate/aquire signals at 1 second and greater timescales. import comedi as c -VERSION = 0.0 +VERSION = 0.1 class dioError (Exception) : "Digital IO error" pass -class dio_obj : +class DIO_port : def __init__(self, filename="/dev/comedi0", subdevice=2, chan=(0,1,2,3), aref=0, range=0, output=True) : + """inputs: + filename: comedi device file for your device ("/dev/comedi0"). + subdevice: the digital IO subdevice (2) + chan: an iterable of the channels you wish to control ((0,1,2,3)) + aref: the analog reference for these channels (0) + range: the range for these channels (0) + output: whether to use the lines as output (vs input) (True) + """ self.filename = filename self.subdev = subdevice self.chan = chan @@ -33,24 +41,35 @@ class dio_obj : else : self.set_to_input() def set_to_output(self) : + "switch all the channels associated with this object to be outputs" for chan in self.chan : rc = c.comedi_dio_config(self.dev, self.subdev, chan, c.COMEDI_OUTPUT) if rc != 1 : # yes, comedi_dio_config returns 1 on success, -1 on failure, as of comedilib-0.8.1 raise dioError, 'comedi_dio_config("%s", %d, %d, %d) returned %d' % (self.filename, self.subdev, chan, c.COMEDI_OUTPUT, rc) self.output = True def set_to_input(self) : + "switch all the channels associated with this object to be inputs" for chan in self.chan : rc = c.comedi_dio_config(self.dev, self.subdev, chan, c.COMEDI_INPUT) if rc != 1 : raise dioError, 'comedi_dio_config("%s", %d, %d, %d) returned %d' % (self.filename, self.subdev, chan, c.COMEDI_INPUT, rc) self.output = False def write_chan_index(self, chan_index, data) : + """inputs: + chan_index: the channel you wish to write to + data: the value you wish to write to that channel + """ if self.output != True : raise dioError, "Must be an output to write" rc = c.comedi_data_write(self.dev, self.subdev, self.chan[chan_index], self.range, self.aref, data); if rc != 1 : # the number of samples written raise dioError, "comedi_data_write returned %d" % rc def read_chan_index(self, chan_index) : + """inputs: + chan_index: the channel you wish to read from + outputs: + data: the value read from that channel + """ if self.output == True : raise dioError, "Must be an input to read" rc, data = c.comedi_data_read(self.dev, self.subdev, self.chan[chan_index], self.range, self.aref); @@ -58,21 +77,37 @@ class dio_obj : raise dioError, "comedi_data_read returned %d" % rc return data def write_port(self, data) : - "Channel significance increases with array index" + """inputs: + data: decimal number representing data to write + For example, setting data=6 will write + 0 to chan[0] + 1 to chan[1] + 1 to chan[2] + 0 to higher channels... + """ for i in range(len(self.chan)) : self.write_chan_index(i, (data >> i) % 2) def read_port(self) : + """outputs: + data: decimal number representing data read + For example, data=6 represents + 0 on chan[0] + 1 on chan[1] + 1 on chan[2] + 0 on higher channels... + """ data = 0 for i in range(len(self.chan)) : data += self.read_chan_index(i) << i return data -class write_dig_port (dio_obj) : +class DO_port (DIO_port) : + "A simple wrapper on dio_obj to make writing easier" def __call__(self, data) : self.write_port(data) -def _test_dio_obj() : - d = dio_obj() +def _test_DIO_port() : + d = DIO_port() d.set_to_output() d.write_chan_index(0, 1) d.write_chan_index(0, 0) @@ -84,15 +119,15 @@ def _test_dio_obj() : print "port value is %d" % data print "dio_obj success" -def _test_write_dig_port() : - p = write_dig_port() +def _test_DO_port() : + p = DO_port for data in [0, 1, 2, 3, 4, 5, 6, 7] : p(data) print "write_dig_port success" def test() : - _test_dio_obj() - _test_write_dig_port() + _test_DIO_port() + _test_DO_port() if __name__ == "__main__" : test()