From: <ean...@us...> - 2012-09-09 20:56:31
|
Revision: 10990 http://octave.svn.sourceforge.net/octave/?rev=10990&view=rev Author: eandrius Date: 2012-09-09 20:56:23 +0000 (Sun, 09 Sep 2012) Log Message: ----------- instrument-control: i2c code refactoring Modified Paths: -------------- trunk/octave-forge/main/instrument-control/src/i2c/i2c.cc trunk/octave-forge/main/instrument-control/src/i2c/i2c.h trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c.cc 2012-09-09 20:31:57 UTC (rev 10989) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c.cc 2012-09-09 20:56:23 UTC (rev 10990) @@ -14,24 +14,15 @@ // along with this program; if not, see <http://www.gnu.org/licenses/>. #include <octave/oct.h> -#include <octave/ov-int32.h> -//#include <octave/ops.h> -//#include <octave/ov-typeinfo.h> +//#include <octave/ov-int32.h> -#include <iostream> +#include <stdio.h> +#include <stdlib.h> #include <string> -#include <algorithm> #ifndef __WIN32__ #include <errno.h> -#include <string.h> -#include <stdio.h> -#include <stdlib.h> #include <unistd.h> -#include <linux/i2c-dev.h> -#include <sys/ioctl.h> -#include <sys/types.h> -#include <sys/stat.h> #include <fcntl.h> #endif @@ -44,19 +35,6 @@ static bool type_loaded = false; - - -DEFUN_DLD (helloworld, args, nargout, - "Hello World Help String") - { - int nargin = args.length (); - octave_stdout << "Hello World has " << nargin - << " input arguments and " - << nargout << " output arguments.\n"; - return octave_value_list (); - } - - octave_i2c::octave_i2c() { this->fd = -1; @@ -69,10 +47,10 @@ octave_i2c::~octave_i2c() { - this->i2c_close(); + this->close(); } -int octave_i2c::i2c_get_fd() +int octave_i2c::get_fd() { return this->fd; } @@ -88,8 +66,7 @@ os << this->fd; } -// PKG_ADD: autoload ("i2c", "instrument-control.oct"); -DEFUN_DLD (i2c, args, nargout, "i2c() function has one mandatory output argument") +DEFUN_DLD (i2c, args, nargout, "") { #ifdef __WIN32__ error("i2c: Windows platform support is not yet implemented, go away..."); @@ -112,7 +89,7 @@ // Open the interface octave_i2c* retval = new octave_i2c(path, oflags); - if (retval->i2c_get_fd() < 0) + if (retval->get_fd() < 0) { error("i2c: Error opening the interface: %s\n", strerror(errno)); return octave_value(); Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c.h =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c.h 2012-09-09 20:31:57 UTC (rev 10989) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c.h 2012-09-09 20:56:23 UTC (rev 10990) @@ -17,7 +17,6 @@ #define i2c_H #include <octave/oct.h> -#include <octave/ov-int32.h> #include <string> @@ -30,15 +29,15 @@ octave_i2c(string, int); ~octave_i2c(); - int i2c_get_fd(); - int i2c_close(); + int get_fd(); + int close(); - int i2c_set_addr(int); - int i2c_get_addr(); + int set_addr(int); + int get_addr(); // Simple i2c commands - int i2c_write(unsigned char*, int); - int i2c_read(char*, unsigned int); + int write(unsigned char*, int); + int read(char*, unsigned int); // Overloaded base functions Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc 2012-09-09 20:31:57 UTC (rev 10989) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc 2012-09-09 20:56:23 UTC (rev 10990) @@ -14,31 +14,19 @@ // along with this program; if not, see <http://www.gnu.org/licenses/>. #include <octave/oct.h> -#include <octave/ov-int32.h> -//#include <octave/ops.h> -//#include <octave/ov-typeinfo.h> -#include <iostream> -#include <string> -#include <algorithm> - -#ifndef __WIN32__ -#include <errno.h> -#include <string.h> #include <stdio.h> #include <stdlib.h> -#include <unistd.h> + +#ifndef __WIN32__ #include <linux/i2c-dev.h> +#include <errno.h> #include <sys/ioctl.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> #endif #include "i2c.h" -// PKG_ADD: autoload ("i2c_addr", "instrument-control.oct"); -DEFUN_DLD (i2c_addr, args, nargout, "Hello World Help String") +DEFUN_DLD (i2c_addr, args, nargout, "") { if (args.length() > 2 || args(0).type_id() != octave_i2c::static_type_id()) @@ -62,28 +50,28 @@ return octave_value(-1); } - i2c->i2c_set_addr(args(1).int_value()); + i2c->set_addr(args(1).int_value()); return octave_value(); } // Returning current slave address - return octave_value(i2c->i2c_get_addr()); + return octave_value(i2c->get_addr()); } -int octave_i2c::i2c_set_addr(int addr) +int octave_i2c::set_addr(int addr) { - if (::ioctl(i2c_get_fd(), I2C_SLAVE, addr) < 0) + if (::ioctl(this->get_fd(), I2C_SLAVE, addr) < 0) { error("i2c: Error setting slave address: %s\n", strerror(errno)); - return false; + return -1; } - return true; + return 1; } -int octave_i2c::i2c_get_addr() +int octave_i2c::get_addr() { return this->addr; } \ No newline at end of file Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc 2012-09-09 20:31:57 UTC (rev 10989) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc 2012-09-09 20:56:23 UTC (rev 10990) @@ -14,33 +14,18 @@ // along with this program; if not, see <http://www.gnu.org/licenses/>. #include <octave/oct.h> -#include <octave/ov-int32.h> -//#include <octave/ops.h> -//#include <octave/ov-typeinfo.h> -#include <iostream> -#include <string> -#include <algorithm> +#include <stdio.h> +#include <stdlib.h> #ifndef __WIN32__ #include <errno.h> -#include <string.h> -#include <stdio.h> -#include <stdlib.h> #include <unistd.h> -#include <linux/i2c-dev.h> -#include <sys/ioctl.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> #endif -using std::string; - #include "i2c.h" -// PKG_ADD: autoload ("i2c_close", "instrument-control.oct"); -DEFUN_DLD (i2c_close, args, nargout, "Hello World Help String") +DEFUN_DLD (i2c_close, args, nargout, "") { if (args.length() != 1 || args(0).type_id() != octave_i2c::static_type_id()) { @@ -53,14 +38,15 @@ const octave_base_value& rep = args(0).get_rep(); i2c = &((octave_i2c &)rep); - i2c->i2c_close(); + i2c->close(); return octave_value(); } -int octave_i2c::i2c_close() +int octave_i2c::close() { - int retval = ::close(this->i2c_get_fd()); + int retval = ::close(this->get_fd()); this->fd = -1; + return retval; } \ No newline at end of file Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc 2012-09-09 20:31:57 UTC (rev 10989) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc 2012-09-09 20:56:23 UTC (rev 10990) @@ -14,30 +14,18 @@ // along with this program; if not, see <http://www.gnu.org/licenses/>. #include <octave/oct.h> -#include <octave/ov-int32.h> -#include <iostream> -#include <string> -#include <algorithm> +#include <stdio.h> +#include <stdlib.h> #ifndef __WIN32__ #include <errno.h> -#include <string.h> -#include <stdio.h> -#include <stdlib.h> #include <unistd.h> -#include <linux/i2c-dev.h> -#include <sys/ioctl.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> #endif - #include "i2c.h" -// PKG_ADD: autoload ("i2c_read", "instrument-control.oct"); -DEFUN_DLD (i2c_read, args, nargout, "Hello World Help String") +DEFUN_DLD (i2c_read, args, nargout, "") { if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_i2c::static_type_id()) { @@ -74,7 +62,7 @@ int retval; - retval = i2c->i2c_read(buffer, buffer_len); + retval = i2c->read(buffer, buffer_len); octave_value_list return_list; uint8NDArray data(retval); @@ -90,9 +78,9 @@ return return_list; } -int octave_i2c::i2c_read(char *buf, unsigned int len) +int octave_i2c::read(char *buf, unsigned int len) { - int retval = ::read(i2c_get_fd(), buf, len); + int retval = ::read(this->get_fd(), buf, len); if (retval != len) error("i2c: Failed to read from the i2c bus: %s\n", strerror(errno)); Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc 2012-09-09 20:31:57 UTC (rev 10989) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc 2012-09-09 20:56:23 UTC (rev 10990) @@ -14,30 +14,21 @@ // along with this program; if not, see <http://www.gnu.org/licenses/>. #include <octave/oct.h> -#include <octave/ov-int32.h> -#include <iostream> #include <string> -#include <algorithm> +#include <stdio.h> +#include <stdlib.h> #ifndef __WIN32__ #include <errno.h> -#include <string.h> -#include <stdio.h> -#include <stdlib.h> #include <unistd.h> -#include <linux/i2c-dev.h> -#include <sys/ioctl.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> #endif - #include "i2c.h" -// PKG_ADD: autoload ("i2c_write", "instrument-control.oct"); -DEFUN_DLD (i2c_write, args, nargout, "Hello World Help String") +using std::string; + +DEFUN_DLD (i2c_write, args, nargout, "") { if (args.length() != 2 || args(0).type_id() != octave_i2c::static_type_id()) { @@ -56,7 +47,7 @@ if (data.is_string()) { string buf = data.string_value(); - retval = i2c->i2c_write((unsigned char*)buf.c_str(), buf.length()); + retval = i2c->write((unsigned char*)buf.c_str(), buf.length()); } else if (data.byte_size() == data.numel()) { @@ -66,7 +57,7 @@ for (int i = 0; i < dtmp.length(); i++) buf[i] = (unsigned char)dtmp(i); - retval = i2c->i2c_write(buf, data.byte_size()); + retval = i2c->write(buf, data.byte_size()); delete[] buf; } @@ -79,9 +70,9 @@ return octave_value(retval); } -int octave_i2c::i2c_write(unsigned char *buf, int len) +int octave_i2c::write(unsigned char *buf, int len) { - int retval = ::write(i2c_get_fd(), buf, len); + int retval = ::write(this->get_fd(), buf, len); if (retval < 0) error("i2c: Failed to write to the i2c bus: %s\n", strerror(errno)); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ean...@us...> - 2012-09-09 20:59:22
|
Revision: 10991 http://octave.svn.sourceforge.net/octave/?rev=10991&view=rev Author: eandrius Date: 2012-09-09 20:59:15 +0000 (Sun, 09 Sep 2012) Log Message: ----------- instrument-control: i2c, check if interface is opened before operating on it Modified Paths: -------------- trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc 2012-09-09 20:56:23 UTC (rev 10990) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc 2012-09-09 20:59:15 UTC (rev 10991) @@ -61,6 +61,11 @@ int octave_i2c::set_addr(int addr) { + if (this->get_fd() < 0) + { + error("i2c: Interface must be open first..."); + return -1; + } if (::ioctl(this->get_fd(), I2C_SLAVE, addr) < 0) { @@ -73,5 +78,11 @@ int octave_i2c::get_addr() { + if (this->get_fd() < 0) + { + error("i2c: Interface must be open first..."); + return -1; + } + return this->addr; } \ No newline at end of file Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc 2012-09-09 20:56:23 UTC (rev 10990) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc 2012-09-09 20:59:15 UTC (rev 10991) @@ -45,6 +45,12 @@ int octave_i2c::close() { + if (this->get_fd() < 0) + { + error("i2c: Interface must be open first..."); + return -1; + } + int retval = ::close(this->get_fd()); this->fd = -1; Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc 2012-09-09 20:56:23 UTC (rev 10990) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc 2012-09-09 20:59:15 UTC (rev 10991) @@ -80,6 +80,12 @@ int octave_i2c::read(char *buf, unsigned int len) { + if (this->get_fd() < 0) + { + error("i2c: Interface must be open first..."); + return -1; + } + int retval = ::read(this->get_fd(), buf, len); if (retval != len) Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc 2012-09-09 20:56:23 UTC (rev 10990) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc 2012-09-09 20:59:15 UTC (rev 10991) @@ -72,6 +72,12 @@ int octave_i2c::write(unsigned char *buf, int len) { + if (this->get_fd() < 0) + { + error("i2c: Interface must be open first..."); + return -1; + } + int retval = ::write(this->get_fd(), buf, len); if (retval < 0) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ean...@us...> - 2012-09-23 15:41:05
|
Revision: 11089 http://octave.svn.sourceforge.net/octave/?rev=11089&view=rev Author: eandrius Date: 2012-09-23 15:40:53 +0000 (Sun, 23 Sep 2012) Log Message: ----------- instrument-control: i2c, texinfo help strings Modified Paths: -------------- trunk/octave-forge/main/instrument-control/src/i2c/i2c.cc trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c.cc 2012-09-23 08:54:28 UTC (rev 11088) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c.cc 2012-09-23 15:40:53 UTC (rev 11089) @@ -66,7 +66,17 @@ os << this->fd; } -DEFUN_DLD (i2c, args, nargout, "") +DEFUN_DLD (i2c, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {@var{i2c} = } i2c ([@var{path}], [@var{address}])\n \ +\n\ +Open i2c interface.\n \ +\n\ +@var{path} - the interface path of type String. If omitted defaults to '/dev/i2c-0'. @*\ +@var{address} - the slave device address. If omitted must be set using i2c_addr() call.\n \ +\n\ +The i2c() shall return instance of @var{octave_i2c} class as the result @var{i2c}.\n \ +@end deftypefn") { #ifdef __WIN32__ error("i2c: Windows platform support is not yet implemented, go away..."); Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc 2012-09-23 08:54:28 UTC (rev 11088) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc 2012-09-23 15:40:53 UTC (rev 11089) @@ -26,7 +26,20 @@ #include "i2c.h" -DEFUN_DLD (i2c_addr, args, nargout, "") +DEFUN_DLD (i2c_addr, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} i2c_addr (@var{i2c}, @var{address})\n \ +@deftypefnx {Loadable Function} {@var{addr} = } i2c_addr (@var{i2c})\n \ +\n\ +Set new or get existing i2c slave device address.\n \ +\n\ +@var{i2c} - instance of @var{octave_i2c} class.@*\ +@var{address} - i2c slave device address of type Integer. \ +The address is passed in the 7 or 10 lower bits of the argument.\n \ +\n\ +If @var{address} parameter is omitted, the i2c_addr() shall return \ +current i2c slave device address as the result @var{addr}.\n \ +@end deftypefn") { if (args.length() > 2 || args(0).type_id() != octave_i2c::static_type_id()) Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc 2012-09-23 08:54:28 UTC (rev 11088) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc 2012-09-23 15:40:53 UTC (rev 11089) @@ -25,7 +25,14 @@ #include "i2c.h" -DEFUN_DLD (i2c_close, args, nargout, "") +DEFUN_DLD (i2c_close, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} i2c_close (@var{i2c})\n \ +\n\ +Close the interface and release a file descriptor.\n \ +\n\ +@var{i2c} - instance of @var{octave_i2c} class.@*\ +@end deftypefn") { if (args.length() != 1 || args(0).type_id() != octave_i2c::static_type_id()) { Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc 2012-09-23 08:54:28 UTC (rev 11088) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc 2012-09-23 15:40:53 UTC (rev 11089) @@ -26,7 +26,17 @@ #include "i2c.h" -DEFUN_DLD (i2c_read, args, nargout, "") +DEFUN_DLD (i2c_read, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {[@var{data}, @var{count}] = } i2c_read (@var{i2c}, @var{n})\n \ +\n\ +Read from i2c slave device.\n \ +\n\ +@var{i2c} - instance of @var{octave_i2c} class.@*\ +@var{n} - number of bytes to attempt to read of type Integer.\n \ +\n\ +The i2c_read() shall return number of bytes successfully read in @var{count} as Integer and the bytes themselves in @var{data} as uint8 array.\n \ +@end deftypefn") { if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_i2c::static_type_id()) { Modified: trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc 2012-09-23 08:54:28 UTC (rev 11088) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc 2012-09-23 15:40:53 UTC (rev 11089) @@ -28,7 +28,17 @@ using std::string; -DEFUN_DLD (i2c_write, args, nargout, "") +DEFUN_DLD (i2c_write, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {@var{n} = } i2c_write (@var{i2c}, @var{data})\n \ +\n\ +Write data to a i2c slave device.\n \ +\n\ +@var{i2c} - instance of @var{octave_i2c} class.@*\ +@var{data} - data to be written to the slave device. Can be either of String or uint8 type.\n \ +\n\ +Upon successful completion, i2c_write() shall return the number of bytes written as the result @var{n}.\n \ +@end deftypefn") { if (args.length() != 2 || args(0).type_id() != octave_i2c::static_type_id()) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |