From: <ean...@us...> - 2012-09-02 22:39:49
|
Revision: 10954 http://octave.svn.sourceforge.net/octave/?rev=10954&view=rev Author: eandrius Date: 2012-09-02 22:39:41 +0000 (Sun, 02 Sep 2012) Log Message: ----------- instrument-control: merged serial and i2c packages Added Paths: ----------- trunk/octave-forge/main/instrument-control/ trunk/octave-forge/main/instrument-control/COPYING trunk/octave-forge/main/instrument-control/DESCRIPTION trunk/octave-forge/main/instrument-control/INDEX trunk/octave-forge/main/instrument-control/NEWS trunk/octave-forge/main/instrument-control/PKG_ADD trunk/octave-forge/main/instrument-control/src/ trunk/octave-forge/main/instrument-control/src/Makefile trunk/octave-forge/main/instrument-control/src/i2c/ trunk/octave-forge/main/instrument-control/src/i2c/Makefile 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 trunk/octave-forge/main/instrument-control/src/serial/ trunk/octave-forge/main/instrument-control/src/serial/Makefile trunk/octave-forge/main/instrument-control/src/serial/serial.cc trunk/octave-forge/main/instrument-control/src/serial/serial.h trunk/octave-forge/main/instrument-control/src/serial/srl_baudrate.cc trunk/octave-forge/main/instrument-control/src/serial/srl_bytesize.cc trunk/octave-forge/main/instrument-control/src/serial/srl_close.cc trunk/octave-forge/main/instrument-control/src/serial/srl_flush.cc trunk/octave-forge/main/instrument-control/src/serial/srl_parity.cc trunk/octave-forge/main/instrument-control/src/serial/srl_read.cc trunk/octave-forge/main/instrument-control/src/serial/srl_stopbits.cc trunk/octave-forge/main/instrument-control/src/serial/srl_timeout.cc trunk/octave-forge/main/instrument-control/src/serial/srl_write.cc Removed Paths: ------------- trunk/octave-forge/main/i2c/ trunk/octave-forge/main/serial/ Added: trunk/octave-forge/main/instrument-control/COPYING =================================================================== --- trunk/octave-forge/main/instrument-control/COPYING (rev 0) +++ trunk/octave-forge/main/instrument-control/COPYING 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. Added: trunk/octave-forge/main/instrument-control/DESCRIPTION =================================================================== --- trunk/octave-forge/main/instrument-control/DESCRIPTION (rev 0) +++ trunk/octave-forge/main/instrument-control/DESCRIPTION 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,11 @@ +Name: instrument-control +Version: 0.1.0 +Date: 2012-09-02 +Author: Andrius Sutas <and...@gm...> +Maintainer: Andrius Sutas <and...@gm...> +Title: Instrument Control Toolbox +Description: Low level I/O functions for serial, i2c and parallel interfaces. +Categories: instrument-control +Depends: octave (>= 3.2.0) +Autoload: no +License: LGPLv3+ Added: trunk/octave-forge/main/instrument-control/INDEX =================================================================== --- trunk/octave-forge/main/instrument-control/INDEX (rev 0) +++ trunk/octave-forge/main/instrument-control/INDEX 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,12 @@ +instrument-control >> Low level I/O functions + +Serial + serial + srl_read + srl_write + srl_close + +I2C + i2c + i2c_read + i2c_write Added: trunk/octave-forge/main/instrument-control/NEWS =================================================================== --- trunk/octave-forge/main/instrument-control/NEWS (rev 0) +++ trunk/octave-forge/main/instrument-control/NEWS 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,9 @@ +Summary of important user-visible changes for instrument-control 0.1.0: +------------------------------------------------------------------- + + ** Initial release + + ** Support for parallel port I/O + + ** The following functions are new: + parport Added: trunk/octave-forge/main/instrument-control/PKG_ADD =================================================================== --- trunk/octave-forge/main/instrument-control/PKG_ADD (rev 0) +++ trunk/octave-forge/main/instrument-control/PKG_ADD 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,18 @@ +# Serial +autoload ("srl_timeout", "instrument-control.oct"); +autoload ("serial", "instrument-control.oct"); +autoload ("srl_stopbits", "instrument-control.oct"); +autoload ("srl_read", "instrument-control.oct"); +autoload ("srl_parity", "instrument-control.oct"); +autoload ("srl_close", "instrument-control.oct"); +autoload ("srl_bytesize", "instrument-control.oct"); +autoload ("srl_write", "instrument-control.oct"); +autoload ("srl_flush", "instrument-control.oct"); +autoload ("srl_baudrate", "instrument-control.oct"); + +# I2C +autoload ("i2c_addr", "instrument-control.oct"); +autoload ("i2c", "instrument-control.oct"); +autoload ("i2c_read", "instrument-control.oct"); +autoload ("i2c_close", "instrument-control.oct"); +autoload ("i2c_write", "instrument-control.oct"); Added: trunk/octave-forge/main/instrument-control/src/Makefile =================================================================== --- trunk/octave-forge/main/instrument-control/src/Makefile (rev 0) +++ trunk/octave-forge/main/instrument-control/src/Makefile 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,12 @@ +OCT = instrument-control +SUBDIRS = i2c serial + +MKOCTFILE ?= mkoctfile + +all: + @for dir in $(SUBDIRS); do $(MAKE) -C $$dir; done + $(MKOCTFILE) i2c/*.o serial/*.o -o $(OCT) + +clean: + @for dir in $(SUBDIRS); do $(MAKE) -C $$dir clean; done + rm -f *.oct Added: trunk/octave-forge/main/instrument-control/src/i2c/Makefile =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/Makefile (rev 0) +++ trunk/octave-forge/main/instrument-control/src/i2c/Makefile 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,21 @@ +OCT := i2c.oct +OBJ := i2c.o i2c_close.o i2c_addr.o i2c_write.o i2c_read.o + +MKOCTFILE ?= mkoctfile + +all: $(OBJ) + +oct: $(OCT) + +%.oct: $(OBJ) + $(MKOCTFILE) $^ + +%.o: %.cc + $(MKOCTFILE) -c -s $< + + +clean: + rm -f *.oct *.o + + +.PHONY: all clean Added: trunk/octave-forge/main/instrument-control/src/i2c/i2c.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c.cc (rev 0) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c.cc 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,122 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// 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> +#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" + +DEFINE_OCTAVE_ALLOCATOR (octave_i2c); +DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_i2c, "octave_i2c", "octave_i2c"); + +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; +} + +octave_i2c::octave_i2c(string path, int flags) +{ + this->fd = open(path.c_str(), flags, 0); +} + +octave_i2c::~octave_i2c() +{ + this->i2c_close(); +} + +int octave_i2c::i2c_get_fd() +{ + return this->fd; +} + +void octave_i2c::print (std::ostream& os, bool pr_as_read_syntax ) const +{ + print_raw(os, pr_as_read_syntax); + newline(os); +} + +void octave_i2c::print_raw (std::ostream& os, bool pr_as_read_syntax) const +{ + os << this->fd; +} + +// PKG_ADD: autoload ("i2c", "instrument-control.oct"); +DEFUN_DLD (i2c, args, nargout, "i2c() function has one mandatory output argument") +{ +#ifdef __WIN32__ + error("i2c: Windows platform support is not yet implemented, go away..."); + return octave_value(); +#endif + + int nargin = args.length(); + + // Default values + int oflags = O_RDWR; + string path("/dev/i2c-0"); + + // Do not open interface if return value is not assigned + if (nargout != 1) + { + print_usage(); + return octave_value(); + } + + // Open the interface + octave_i2c* retval = new octave_i2c(path, oflags); + + if (retval->i2c_get_fd() < 0) + { + error("i2c: Error opening the interface: %s\n", strerror(errno)); + return octave_value(); + } + + return octave_value(retval); +} Added: trunk/octave-forge/main/instrument-control/src/i2c/i2c.h =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c.h (rev 0) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c.h 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,72 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +#ifndef i2c_H +#define i2c_H + +#include <octave/oct.h> +#include <octave/ov-int32.h> + +#include <string> + +using std::string; + +class octave_i2c : public octave_base_value +{ +public: + octave_i2c(); + octave_i2c(string, int); + ~octave_i2c(); + + int i2c_get_fd(); + int i2c_close(); + + int i2c_set_addr(int); + int i2c_get_addr(); + + // Simple i2c commands + int i2c_write(unsigned char*, int); + int i2c_read(char*, unsigned int); + + + // Overloaded base functions + double i2c_value() const + { + return (double)this->fd; + } + + virtual double scalar_value (bool frc_str_conv = false) const + { + return (double)this->fd; + } + + void print (std::ostream& os, bool pr_as_read_syntax = false) const; + void print_raw (std::ostream& os, bool pr_as_read_syntax) const; + + // Properties + bool is_constant (void) const { return true;} + bool is_defined (void) const { return true;} + bool print_as_scalar (void) const { return true;} + +private: + int fd; + int addr; + + DECLARE_OCTAVE_ALLOCATOR + DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA +}; + + +#endif Added: trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc (rev 0) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_addr.cc 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,89 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// 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> +#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_addr", "instrument-control.oct"); +DEFUN_DLD (i2c_addr, args, nargout, "Hello World Help String") +{ + if (args.length() > 2 || + args(0).type_id() != octave_i2c::static_type_id()) + { + print_usage(); + return octave_value(-1); + } + + octave_i2c* i2c = NULL; + + const octave_base_value& rep = args(0).get_rep(); + i2c = &((octave_i2c &)rep); + + + // Setting new slave address + if (args.length() > 1) + { + if ( !(args(1).is_integer_type() || args(1).is_float_type()) ) + { + print_usage(); + return octave_value(-1); + } + + i2c->i2c_set_addr(args(1).int_value()); + + return octave_value(); + } + + // Returning current slave address + return octave_value(i2c->i2c_get_addr()); +} + +int octave_i2c::i2c_set_addr(int addr) +{ + + if (::ioctl(i2c_get_fd(), I2C_SLAVE, addr) < 0) + { + error("i2c: Error setting slave address: %s\n", strerror(errno)); + return false; + } + + return true; +} + +int octave_i2c::i2c_get_addr() +{ + return this->addr; +} \ No newline at end of file Added: trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc (rev 0) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_close.cc 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,66 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// 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> +#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") +{ + if (args.length() != 1 || args(0).type_id() != octave_i2c::static_type_id()) + { + print_usage(); + return octave_value(-1); + } + + octave_i2c* i2c = NULL; + + const octave_base_value& rep = args(0).get_rep(); + i2c = &((octave_i2c &)rep); + + i2c->i2c_close(); + + return octave_value(); +} + +int octave_i2c::i2c_close() +{ + int retval = ::close(this->i2c_get_fd()); + this->fd = -1; + return retval; +} \ No newline at end of file Added: trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc (rev 0) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_read.cc 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,101 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// 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> + +#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") +{ + if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_i2c::static_type_id()) + { + print_usage(); + return octave_value(-1); + } + + char *buffer = NULL; + unsigned int buffer_len = 1; + + if (args.length() > 1) + { + if ( !(args(1).is_integer_type() || args(1).is_float_type()) ) + { + print_usage(); + return octave_value(-1); + } + + buffer_len = args(1).int_value(); + } + + buffer = new char [buffer_len + 1]; + + if (buffer == NULL) + { + error("i2c_read: cannot allocate requested memory..."); + return octave_value(-1); + } + + octave_i2c* i2c = NULL; + + const octave_base_value& rep = args(0).get_rep(); + i2c = &((octave_i2c &)rep); + + int retval; + + retval = i2c->i2c_read(buffer, buffer_len); + + octave_value_list return_list; + uint8NDArray data(retval); + + for (int i = 0; i < retval; i++) + data(i) = buffer[i]; + + return_list(1) = retval; + return_list(0) = data; + + delete[] buffer; + + return return_list; +} + +int octave_i2c::i2c_read(char *buf, unsigned int len) +{ + int retval = ::read(i2c_get_fd(), buf, len); + + if (retval != len) + error("i2c: Failed to read from the i2c bus: %s\n", strerror(errno)); + + return retval; +} \ No newline at end of file Added: trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc (rev 0) +++ trunk/octave-forge/main/instrument-control/src/i2c/i2c_write.cc 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,90 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// 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> + +#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") +{ + if (args.length() != 2 || args(0).type_id() != octave_i2c::static_type_id()) + { + print_usage(); + return octave_value(-1); + } + + octave_i2c* i2c = NULL; + + const octave_base_value& rep = args(0).get_rep(); + i2c = &((octave_i2c &)rep); + + const octave_base_value& data = args(1).get_rep(); + int retval; + + if (data.is_string()) + { + string buf = data.string_value(); + retval = i2c->i2c_write((unsigned char*)buf.c_str(), buf.length()); + } + else if (data.byte_size() == data.numel()) + { + NDArray dtmp = data.array_value(); + unsigned char* buf = new unsigned char [dtmp.length()]; + + for (int i = 0; i < dtmp.length(); i++) + buf[i] = (unsigned char)dtmp(i); + + retval = i2c->i2c_write(buf, data.byte_size()); + + delete[] buf; + } + else + { + print_usage(); + return octave_value(-1); + } + + return octave_value(retval); +} + +int octave_i2c::i2c_write(unsigned char *buf, int len) +{ + int retval = ::write(i2c_get_fd(), buf, len); + + if (retval < 0) + error("i2c: Failed to write to the i2c bus: %s\n", strerror(errno)); + + return retval; +} \ No newline at end of file Added: trunk/octave-forge/main/instrument-control/src/serial/Makefile =================================================================== --- trunk/octave-forge/main/instrument-control/src/serial/Makefile (rev 0) +++ trunk/octave-forge/main/instrument-control/src/serial/Makefile 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,21 @@ +OCT := serial.oct +OBJ := serial.o srl_baudrate.o srl_timeout.o srl_bytesize.o srl_flush.o \ + srl_parity.o srl_stopbits.o srl_write.o srl_close.o srl_read.o + +MKOCTFILE ?= mkoctfile + +all: $(OBJ) + +oct: $(OCT) + +%.oct: $(OBJ) + $(MKOCTFILE) $^ + +%.o: %.cc + $(MKOCTFILE) -c -s $< + +clean: + rm -f *.oct *.o + + +.PHONY: all clean Added: trunk/octave-forge/main/instrument-control/src/serial/serial.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/serial/serial.cc (rev 0) +++ trunk/octave-forge/main/instrument-control/src/serial/serial.cc 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,187 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +// TODO: Implement Flow Control +// TODO: Implement H/W handshaking +// TODO: Check if interface is opened first + +#include <octave/oct.h> +#include <octave/ov-int32.h> + +#include <iostream> +#include <string> +#include <algorithm> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +DEFINE_OCTAVE_ALLOCATOR (octave_serial); +DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_serial, "octave_serial", "octave_serial"); + +static bool type_loaded = false; + +octave_serial::octave_serial() +{ + this->fd = -1; +} + +octave_serial::octave_serial(string path, int flags) +{ + this->fd = open(path.c_str(), flags, 0); + + if (this->fd > 0) + { + tcgetattr(this->fd, &this->config); + this->blocking_read = true; + } +} + +octave_serial::~octave_serial() +{ + this->close(); +} + +void octave_serial::print (std::ostream& os, bool pr_as_read_syntax ) const +{ + print_raw(os, pr_as_read_syntax); + newline(os); +} + +void octave_serial::print_raw (std::ostream& os, bool pr_as_read_syntax) const +{ + os << this->fd; +} + +// PKG_ADD: autoload ("serial", "instrument-control.oct"); +DEFUN_DLD (serial, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {@var{serial} = } serial ([@var{path}], [@var{baudrate}], [@var{timeout}])\n \ +\n\ +Open serial interface.\n \ +\n\ +@var{path} - the interface path of type String. If omitted defaults to '/dev/ttyUSB0'. @*\ +@var{baudrate} - the baudrate of interface. If omitted defaults to 115200. @*\ +@var{timeout} - the interface timeout value. If omitted defaults to blocking call.\n \ +\n\ +The serial() shall return instance of @var{octave_serial} class as the result @var{serial}.\n \ +@end deftypefn") +{ +#ifdef __WIN32__ + error("serial: Windows platform support is not yet implemented, go away..."); + return octave_value(); +#endif + + int nargin = args.length(); + + // Do not open interface if return value is not assigned + if (nargout != 1) + { + print_usage(); + return octave_value(); + } + + // Default values + string path("/dev/ttyUSB0"); + unsigned int baud_rate = 115200; + short timeout = -1; + + unsigned short bytesize = 8; + string parity("N"); + unsigned short stopbits = 1; + int oflags = O_RDWR | O_NOCTTY | O_SYNC; + // O_SYNC - All writes immediately effective, no buffering + + if (!type_loaded) + { + octave_serial::register_type(); + type_loaded = true; + } + + // Parse the function arguments + if (args.length() > 0) + { + if (args(0).is_string()) + { + path = args(0).string_value(); + } + else + { + print_usage(); + return octave_value(); + } + + } + + // is_float_type() is or'ed to allow expression like ("", 123), without user + // having to use ("", int32(123)), as we still only take "int_value" + if (args.length() > 1) + { + if (args(1).is_integer_type() || args(1).is_float_type()) + { + baud_rate = args(1).int_value(); + } + else + { + print_usage(); + return octave_value(); + } + } + + if (args.length() > 2) + { + if (args(2).is_integer_type() || args(2).is_float_type()) + { + timeout = args(2).int_value(); + } + else + { + print_usage(); + return octave_value(); + } + } + + // Open the interface + octave_serial* retval = new octave_serial(path, oflags); + + if (retval->get_fd() < 0) + { + error("serial: Error opening the interface: %s\n", strerror(errno)); + return octave_value(); + } + + retval->set_baudrate(baud_rate); + + if (timeout >= 0) { + retval->set_timeout(timeout); + } + + retval->set_parity(parity); + retval->set_bytesize(bytesize); + retval->set_stopbits(stopbits); + + //retval->flush(2); + + return octave_value(retval); +} Added: trunk/octave-forge/main/instrument-control/src/serial/serial.h =================================================================== --- trunk/octave-forge/main/instrument-control/src/serial/serial.h (rev 0) +++ trunk/octave-forge/main/instrument-control/src/serial/serial.h 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,89 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +#ifndef SERIAL_H +#define SERIAL_H + +#include <octave/oct.h> +#include <octave/ov-int32.h> + +#include <string> + +#define BITMASK_SET(x,y) ((x) |= (y)) +#define BITMASK_CLEAR(x,y) ((x) &= (~(y))) +#define BITMASK_TOGGLE(x,y) ((x) ^= (y)) +#define BITMASK_CHECK(x,y) ((x) & (y)) + +class octave_serial : public octave_base_value +{ +public: + octave_serial(); + octave_serial(string, int); + ~octave_serial(); + + int write(string); + int write(unsigned char*, int); + + int read(char *, unsigned int); + + int close(); + + int flush(unsigned short); + + int set_timeout(short); + int get_timeout(); + + int set_baudrate(unsigned int); + int get_baudrate(); + + int set_bytesize(unsigned short); + int get_bytesize(); + + int set_parity(string); + string get_parity(); + + int set_stopbits(unsigned short); + int get_stopbits(); + + int get_fd() { return this->fd; } + + // Overloaded base functions + double serial_value() const { return (double)this->fd; } + + virtual double scalar_value (bool frc_str_conv = false) const + { + return (double)this->fd; + } + + void print (std::ostream& os, bool pr_as_read_syntax = false) const; + void print_raw (std::ostream& os, bool pr_as_read_syntax) const; + + // Properties + bool is_constant (void) const { return true;} + bool is_defined (void) const { return true;} + bool print_as_scalar (void) const { return true;} + +private: + int fd; + struct termios config; + + bool blocking_read; + + DECLARE_OCTAVE_ALLOCATOR + DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA +}; + + +#endif Added: trunk/octave-forge/main/instrument-control/src/serial/srl_baudrate.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/serial/srl_baudrate.cc (rev 0) +++ trunk/octave-forge/main/instrument-control/src/serial/srl_baudrate.cc 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,193 @@ +#include <octave/oct.h> +#include <octave/ov-int32.h> +//#include <octave/ops.h> +//#include <octave/ov-typeinfo.h> + +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see <http://www.gnu.org/licenses/>. + +#include <iostream> +#include <string> +#include <algorithm> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// PKG_ADD: autoload ("srl_baudrate", "instrument-control.oct"); +DEFUN_DLD (srl_baudrate, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} srl_baudrate (@var{serial}, @var{baudrate})\n \ +@deftypefnx {Loadable Function} {@var{br} = } srl_baudrate (@var{serial})\n \ +\n\ +Set new or get existing serial interface baudrate parameter. Only standard values are supported.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@var{baudrate} - the baudrate value used. Supported values: 0, 50, 75, 110, \ +134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600 19200, 38400, 57600, \ +115200 and 230400.\n \ +\n\ +If @var{baudrate} parameter is omitted, the srl_baudrate() shall return current baudrate value as the result @var{br}.\n \ +@end deftypefn") +{ + if (args.length() < 1 || args.length() > 2 || + args(0).type_id() != octave_serial::static_type_id()) + { + print_usage(); + return octave_value(-1); + } + + octave_serial* serial = NULL; + + const octave_base_value& rep = args(0).get_rep(); + serial = &((octave_serial &)rep); + + // Setting new baudrate + if (args.length() > 1) + { + if ( !(args(1).is_integer_type() || args(1).is_float_type()) ) + { + error("srl_baudrate: expecting second argument of type integer..."); + return octave_value(-1); + } + + serial->set_baudrate(args(1).int_value()); + + return octave_value(); + } + + // Returning current baud rate + return octave_value(serial->get_baudrate()); +} + +int octave_serial::set_baudrate(unsigned int baud) +{ + speed_t baud_rate = 0; + + switch (baud) + { + case 0: + baud_rate = B0; break; + case 50: + baud_rate = B50; break; + case 75: + baud_rate = B75; break; + case 110: + baud_rate = B110; break; + case 134: + baud_rate = B134; break; + case 150: + baud_rate = B150; break; + case 200: + baud_rate = B200; break; + case 300: + baud_rate = B300; break; + case 600: + baud_rate = B600; break; + case 1200: + baud_rate = B1200; break; + case 1800: + baud_rate = B1800; break; + case 2400: + baud_rate = B2400; break; + case 4800: + baud_rate = B4800; break; + case 9600: + baud_rate = B9600; break; + case 19200: + baud_rate = B19200; break; + case 38400: + baud_rate = B38400; break; + case 57600: + baud_rate = B57600; break; + case 115200: + baud_rate = B115200; break; + case 230400: + baud_rate = B230400; break; + default: + error("srl_baudrate: currently only 0, 50, 75, 110, \ + 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, \ + 9600 19200, 38400, 57600, 115200 and 230400 baud rates are supported..."); + return false; + } + + cfsetispeed(&this->config, baud_rate); + cfsetospeed(&this->config, baud_rate); + + if (tcsetattr(this->get_fd(), TCSANOW, &this->config) < 0) { + error("srl_baudrate: error setting baud rate: %s\n", strerror(errno)); + return false; + } + + return true; +} + +int octave_serial::get_baudrate() +{ + int retval = -1; + + speed_t baudrate = cfgetispeed(&this->config); + + if (baudrate == B0) + retval = 0; + else if (baudrate == B50) + retval = 50; + else if (baudrate == B75) + retval = 75; + else if (baudrate == B110) + retval = 110; + else if (baudrate == B134) + retval = 134; + else if (baudrate == B150) + retval = 150; + else if (baudrate == B200) + retval = 200; + else if (baudrate == B300) + retval = 300; + else if (baudrate == B600) + retval = 600; + else if (baudrate == B1200) + retval = 1200; + else if (baudrate == B1800) + retval = 1800; + else if (baudrate == B2400) + retval = 2400; + else if (baudrate == B4800) + retval = 4800; + else if (baudrate == B9600) + retval = 9600; + else if (baudrate == B19200) + retval = 19200; + else if (baudrate == B38400) + retval = 38400; + else if (baudrate == B57600) + retval = 57600; + else if (baudrate == B115200) + retval = 115200; + else if (baudrate == B230400) + retval = 230400; + + return retval; + +} \ No newline at end of file Added: trunk/octave-forge/main/instrument-control/src/serial/srl_bytesize.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/serial/srl_bytesize.cc (rev 0) +++ trunk/octave-forge/main/instrument-control/src/serial/srl_bytesize.cc 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,125 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// 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 <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// PKG_ADD: autoload ("srl_bytesize", "instrument-control.oct"); +DEFUN_DLD (srl_bytesize, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} srl_bytesize (@var{serial}, @var{bsize})\n \ +@deftypefnx {Loadable Function} {@var{bs} = } srl_bytesize (@var{serial})\n \ +\n\ +Set new or get existing serial interface byte size parameter.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@var{bsize} - byte size of type Integer. Supported values: 5/6/7/8.\n \ +\n\ +If @var{bsize} parameter is omitted, the srl_bytesize() shall return current byte size value or in case of unsupported setting -1, as the result @var{bs}.\n \ +@end deftypefn") +{ + if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id()) + { + print_usage(); + return octave_value(-1); + } + + octave_serial* serial = NULL; + + const octave_base_value& rep = args(0).get_rep(); + serial = &((octave_serial &)rep); + + // Setting new byte size + if (args.length() > 1) + { + if ( !(args(1).is_integer_type() || args(1).is_float_type()) ) + { + print_usage(); + return octave_value(-1); + } + + serial->set_bytesize(args(1).int_value()); + + return octave_value(); + } + + // Returning current byte size + return octave_value(serial->get_bytesize()); +} + +int octave_serial::set_bytesize(unsigned short bytesize) +{ + tcflag_t c_bytesize = 0; + + switch (bytesize) + { + case 5: c_bytesize = CS5; break; + case 6: c_bytesize = CS6; break; + case 7: c_bytesize = CS7; break; + case 8: c_bytesize = CS8; break; + + default: + error("srl_bytesize: expecting value between [5..8]..."); + return false; + } + + // Clear bitmask CSIZE + BITMASK_CLEAR(this->config.c_cflag, CSIZE); + + // Apply new + BITMASK_SET(this->config.c_cflag, c_bytesize); + + if (tcsetattr(this->get_fd(), TCSANOW, &this->config) < 0) { + error("srl_bytesize: error setting byte size: %s\n", strerror(errno)); + return false; + } + + return true; +} + +int octave_serial::get_bytesize() +{ + int retval = -1; + + if (BITMASK_CHECK(this->config.c_cflag, CS5)) + retval = 5; + else if (BITMASK_CHECK(this->config.c_cflag, CS6)) + retval = 6; + else if (BITMASK_CHECK(this->config.c_cflag, CS7)) + retval = 7; + else if (BITMASK_CHECK(this->config.c_cflag, CS8)) + retval = 8; + + return retval; +} \ No newline at end of file Added: trunk/octave-forge/main/instrument-control/src/serial/srl_close.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/serial/srl_close.cc (rev 0) +++ trunk/octave-forge/main/instrument-control/src/serial/srl_close.cc 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,67 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// 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> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// PKG_ADD: autoload ("srl_close", "instrument-control.oct"); +DEFUN_DLD (srl_close, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} srl_close (@var{serial})\n \ +\n\ +Close the interface and release a file descriptor.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@end deftypefn") +{ + if (args.length() != 1 || args(0).type_id() != octave_serial::static_type_id()) + { + print_usage(); + return octave_value(-1); + } + + octave_serial* serial = NULL; + + const octave_base_value& rep = args(0).get_rep(); + serial = &((octave_serial &)rep); + + serial->close(); + + return octave_value(); +} + +int octave_serial::close() +{ + int retval = ::close(this->get_fd()); + this->fd = -1; + return retval; +} \ No newline at end of file Added: trunk/octave-forge/main/instrument-control/src/serial/srl_flush.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/serial/srl_flush.cc (rev 0) +++ trunk/octave-forge/main/instrument-control/src/serial/srl_flush.cc 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,100 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// 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> + +#ifndef __WIN32__ +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// PKG_ADD: autoload ("srl_flush", "instrument-control.oct"); +DEFUN_DLD (srl_flush, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} srl_flush (@var{serial}, [@var{q}])\n \ +\n\ +Flush the pending input/output.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@var{q} - queue selector of type Integer. Supported values: 0 - flush untransmitted output, \ +1 - flush pending input, 2 - flush both pending input and untransmitted output.\n \ +\n\ +If @var{q} parameter is omitted, the srl_flush() shall flush both, input and output buffers.\n \ +@end deftypefn") +{ + int queue_selector = 2; // Input and Output + + if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id()) + { + print_usage(); + return octave_value(-1); + } + + if (args.length() > 1) + { + if (!(args(1).is_integer_type() || args(1).is_float_type())) + { + print_usage(); + return octave_value(-1); + } + + queue_selector = args(1).int_value(); + } + + octave_serial* serial = NULL; + + const octave_base_value& rep = args(0).get_rep(); + serial = &((octave_serial &)rep); + + serial->flush(queue_selector); + + return octave_value(); +} + +int octave_serial::flush(unsigned short queue_selector) +{ + /* + * TCIOFLUSH Flush both pending input and untransmitted output. + * TCOFLUSH Flush untransmitted output. + * TCIFLUSH Flush pending input. + */ + + int flag; + + switch (queue_selector) + { + case 0: flag = TCOFLUSH; break; + case 1: flag = TCIFLUSH; break; + case 2: flag = TCIOFLUSH; break; + default: + error("srl_flush: only [0..2] values are accepted..."); + return false; + } + + return ::tcflush(this->get_fd(), flag); +} \ No newline at end of file Added: trunk/octave-forge/main/instrument-control/src/serial/srl_parity.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/serial/srl_parity.cc (rev 0) +++ trunk/octave-forge/main/instrument-control/src/serial/srl_parity.cc 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,136 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// 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 <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#endif + +using std::string; + +#include "serial.h" + +// PKG_ADD: autoload ("srl_parity", "instrument-control.oct"); +DEFUN_DLD (srl_parity, args, nargout, +"-*- texinfo -*-\n\ +@deftypefn {Loadable Function} {} srl_parity (@var{serial}, @var{parity})\n \ +@deftypefnx {Loadable Function} {@var{p} = } srl_parity (@var{serial})\n \ +\n\ +Set new or get existing serial interface parity parameter. Even/Odd/None values are supported.\n \ +\n\ +@var{serial} - instance of @var{octave_serial} class.@*\ +@var{parity} - parity value of type String. Supported values: Even/Odd/None (case insensitive, can be abbreviated to the first letter only).\n \ +\n\ +If @var{parity} parameter is omitted, the srl_parity() shall return current parity value as the result @var{p}.\n \ +@end deftypefn") +{ + if (args.length() < 1 || args.length() > 2 || args(0).type_id() != octave_serial::static_type_id()) + { + print_usage(); + return octave_value(-1); + } + + octave_serial* serial = NULL; + + const octave_base_value& rep = args(0).get_rep(); + serial = &((octave_serial &)rep); + + // Setting new parity + if (args.length() > 1) + { + if ( !(args(1).is_string()) ) + { + print_usage(); + return octave_value(-1); + } + + serial->set_parity(args(1).string_value()); + + return octave_value(); + } + + // Returning current parity + return octave_value(serial->get_parity()); +} + +int octave_serial::set_parity(string parity) +{ + // Convert string to lowercase + std::transform(parity.begin(), parity.end(), parity.begin(), ::tolower); + + /* + * PARENB Enable parity generation on output and parity checking for input. + * PARODD If set, then parity for input and output is odd; otherwise even parity is used. + */ + + if (parity == "n" || parity == "none") + { + // Disable parity generation/checking + BITMASK_CLEAR(this->config.c_cflag, PARENB); + } + else if (parity == "e" || parity == "even") + { + // Enable parity generation/checking + BITMASK_SET(this->config.c_cflag, PARENB); + + // Set to Even + BITMASK_CLEAR(this->config.c_cflag, PARODD); + + } + else if (parity == "o" || parity == "odd") + { + // Enable parity generation/checking + BITMASK_SET(this->config.c_cflag, PARENB); + + // Set to Odd + BITMASK_SET(this->config.c_cflag, PARODD); + + } + else + { + error("srl_parity: Only [N]one, [E]ven or [O]dd parities are supported..."); + return false; + } + + if (tcsetattr(this->get_fd(), TCSANOW, &this->config) < 0) { + error("srl_parity: error setting parity: %s\n", strerror(errno)); + return false; + } + + return true; +} + +string octave_serial::get_parity() +{ + if (!BITMASK_CHECK(this->config.c_cflag, PARENB)) + return "None"; + else if (BITMASK_CHECK(this->config.c_cflag, PARODD)) + return "Odd"; + else + return "Even"; +} \ No newline at end of file Added: trunk/octave-forge/main/instrument-control/src/serial/srl_read.cc =================================================================== --- trunk/octave-forge/main/instrument-control/src/serial/srl_read.cc (rev 0) +++ trunk/octave-forge/main/instrument-control/src/serial/srl_read.cc 2012-09-02 22:39:41 UTC (rev 10954) @@ -0,0 +1,110 @@ +// Copyright (C) 2012 Andrius Sutas <and...@gm...> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this progra... [truncated message content] |