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. |