Skip to content

Commit e06a103

Browse files
committed
ic
0 parents  commit e06a103

19 files changed

+596
-0
lines changed

Diff for: .DS_Store

6 KB
Binary file not shown.

Diff for: .Rbuildignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$

Diff for: .gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.Ruserdata
5+
src/*.o
6+
src/*.so
7+
src/*.dll

Diff for: DESCRIPTION

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Package: arduinor
2+
Type: Package
3+
Title: What the Package Does in One 'Title Case' Line
4+
Version: 1.0
5+
Date: 2018-02-11
6+
Author: Your Name
7+
Maintainer: Your Name <[email protected]>
8+
Description: One paragraph description of what the package does as one or
9+
more full sentences.
10+
License: GPL (>= 2)
11+
Imports: Rcpp (>= 0.12.15)
12+
LinkingTo: Rcpp

Diff for: NAMESPACE

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
useDynLib(arduinor, .registration=TRUE)
2+
exportPattern("^[[:alpha:]]+")
3+
importFrom(Rcpp, evalCpp)

Diff for: R/RcppExports.R

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
2+
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3+
4+
rcpp_hello_world <- function() {
5+
.Call(`_arduinor_rcpp_hello_world`)
6+
}
7+

Diff for: Read-and-delete-me

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
* Edit the help file skeletons in 'man', possibly combining help files for
2+
multiple functions.
3+
* Edit the exports in 'NAMESPACE', and add necessary imports.
4+
* Put any C/C++/Fortran code in 'src'.
5+
* If you have compiled code, add a useDynLib() directive to 'NAMESPACE'.
6+
* Run R CMD build to build the package tarball.
7+
* Run R CMD check to check the package tarball.
8+
9+
Read "Writing R Extensions" for more information.

Diff for: arduinor.Rproj

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
14+
15+
BuildType: Package
16+
PackageUseDevtools: Yes
17+
PackageInstallArgs: --no-multiarch --with-keep.source

Diff for: man/arduinor-package.Rd

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
\name{arduinor-package}
2+
\alias{arduinor-package}
3+
\alias{arduinor}
4+
\docType{package}
5+
\title{
6+
A short title line describing what the package does
7+
}
8+
\description{
9+
A more detailed description of what the package does. A length
10+
of about one to five lines is recommended.
11+
}
12+
\details{
13+
This section should provide a more detailed overview of how to use the
14+
package, including the most important functions.
15+
}
16+
\author{
17+
Your Name, email optional.
18+
19+
Maintainer: Your Name <your@email.com>
20+
}
21+
\references{
22+
This optional section can contain literature or other references for
23+
background information.
24+
}
25+
\keyword{ package }
26+
\seealso{
27+
Optional links to other man pages
28+
}
29+
\examples{
30+
\dontrun{
31+
## Optional simple examples of the most important functions
32+
## These can be in \dontrun{} and \donttest{} blocks.
33+
}
34+
}

Diff for: man/rcpp_hello_world.Rd

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
\name{rcpp_hello_world}
2+
\alias{rcpp_hello_world}
3+
\docType{package}
4+
\title{
5+
Simple function using Rcpp
6+
}
7+
\description{
8+
Simple function using Rcpp
9+
}
10+
\usage{
11+
rcpp_hello_world()
12+
}
13+
\examples{
14+
\dontrun{
15+
rcpp_hello_world()
16+
}
17+
}

Diff for: src/.DS_Store

6 KB
Binary file not shown.

Diff for: src/RcppExports.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
2+
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3+
4+
#include <Rcpp.h>
5+
6+
using namespace Rcpp;
7+
8+
// rcpp_hello_world
9+
List rcpp_hello_world();
10+
RcppExport SEXP _arduinor_rcpp_hello_world() {
11+
BEGIN_RCPP
12+
Rcpp::RObject rcpp_result_gen;
13+
Rcpp::RNGScope rcpp_rngScope_gen;
14+
rcpp_result_gen = Rcpp::wrap(rcpp_hello_world());
15+
return rcpp_result_gen;
16+
END_RCPP
17+
}
18+
19+
static const R_CallMethodDef CallEntries[] = {
20+
{"_arduinor_rcpp_hello_world", (DL_FUNC) &_arduinor_rcpp_hello_world, 0},
21+
{NULL, NULL, 0}
22+
};
23+
24+
RcppExport void R_init_arduinor(DllInfo *dll) {
25+
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
26+
R_useDynamicSymbols(dll, FALSE);
27+
}

Diff for: src/arduino-serial/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Tod E. Kurt
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Diff for: src/arduino-serial/arduino-serial-lib.cpp

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
//
2+
// arduino-serial-lib -- simple library for reading/writing serial ports
3+
//
4+
// 2006-2013, Tod E. Kurt, http://todbot.com/blog/
5+
//
6+
// Modified by Hao Zhu, 2018
7+
//
8+
9+
#include <Rcpp.h>
10+
using namespace Rcpp;
11+
12+
#include "arduino-serial-lib.h"
13+
14+
#include <stdio.h> // Standard input/output definitions
15+
#include <unistd.h> // UNIX standard function definitions
16+
#include <fcntl.h> // File control definitions
17+
#include <errno.h> // Error number definitions
18+
#include <termios.h> // POSIX terminal control definitions
19+
#include <string.h> // String function definitions
20+
#include <sys/ioctl.h>
21+
#include <vector>
22+
23+
// uncomment this to debug reads
24+
//#define SERIALPORTDEBUG
25+
26+
// takes the string name of the serial port (e.g. "/dev/tty.usbserial","COM1")
27+
// and a baud rate (bps) and connects to that port at that speed and 8N1.
28+
// opens the port in fully raw mode so you can send binary data.
29+
// returns valid fd, or -1 on error
30+
int serialport_init(const char* serialport, int baud)
31+
{
32+
struct termios toptions;
33+
int fd;
34+
35+
//fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
36+
fd = open(serialport, O_RDWR | O_NONBLOCK );
37+
38+
if (fd == -1) {
39+
perror("serialport_init: Unable to open port ");
40+
return -1;
41+
}
42+
43+
//int iflags = TIOCM_DTR;
44+
//ioctl(fd, TIOCMBIS, &iflags); // turn on DTR
45+
//ioctl(fd, TIOCMBIC, &iflags); // turn off DTR
46+
47+
if (tcgetattr(fd, &toptions) < 0) {
48+
perror("serialport_init: Couldn't get term attributes");
49+
return -1;
50+
}
51+
speed_t brate = baud; // let you override switch below if needed
52+
switch(baud) {
53+
case 4800: brate=B4800; break;
54+
case 9600: brate=B9600; break;
55+
#ifdef B14400
56+
case 14400: brate=B14400; break;
57+
#endif
58+
case 19200: brate=B19200; break;
59+
#ifdef B28800
60+
case 28800: brate=B28800; break;
61+
#endif
62+
case 38400: brate=B38400; break;
63+
case 57600: brate=B57600; break;
64+
case 115200: brate=B115200; break;
65+
}
66+
cfsetispeed(&toptions, brate);
67+
cfsetospeed(&toptions, brate);
68+
69+
// 8N1
70+
toptions.c_cflag &= ~PARENB;
71+
toptions.c_cflag &= ~CSTOPB;
72+
toptions.c_cflag &= ~CSIZE;
73+
toptions.c_cflag |= CS8;
74+
// no flow control
75+
toptions.c_cflag &= ~CRTSCTS;
76+
77+
//toptions.c_cflag &= ~HUPCL; // disable hang-up-on-close to avoid reset
78+
79+
toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
80+
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
81+
82+
toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
83+
toptions.c_oflag &= ~OPOST; // make raw
84+
85+
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
86+
toptions.c_cc[VMIN] = 0;
87+
toptions.c_cc[VTIME] = 0;
88+
//toptions.c_cc[VTIME] = 20;
89+
90+
tcsetattr(fd, TCSANOW, &toptions);
91+
if( tcsetattr(fd, TCSAFLUSH, &toptions) < 0) {
92+
perror("init_serialport: Couldn't set term attributes");
93+
return -1;
94+
}
95+
96+
return fd;
97+
}
98+
99+
//
100+
int serialport_close( int fd )
101+
{
102+
return close( fd );
103+
}
104+
105+
//
106+
int serialport_writebyte( int fd, uint8_t b)
107+
{
108+
int n = write(fd,&b,1);
109+
if( n!=1)
110+
return -1;
111+
return 0;
112+
}
113+
114+
//
115+
int serialport_write(int fd, const char* str)
116+
{
117+
int len = strlen(str);
118+
int n = write(fd, str, len);
119+
if( n!=len ) {
120+
perror("serialport_write: couldn't write whole string\n");
121+
return -1;
122+
}
123+
return 0;
124+
}
125+
126+
//
127+
int serialport_read_until(int fd, char* buf, char until, int buf_max, int timeout)
128+
{
129+
char b[1]; // read expects an array, so we give it a 1-byte array
130+
int i=0;
131+
do {
132+
int n = read(fd, b, 1); // read a char at a time
133+
if( n==-1) return -1; // couldn't read
134+
if( n==0 ) {
135+
usleep( 1 * 1000 ); // wait 1 msec try again
136+
timeout--;
137+
if( timeout==0 ) return -2;
138+
continue;
139+
}
140+
#ifdef SERIALPORTDEBUG
141+
printf("serialport_read_until: i=%d, n=%d b='%c'\n",i,n,b[0]); // debug
142+
#endif
143+
buf[i] = b[0];
144+
i++;
145+
} while( b[0] != until && i < buf_max && timeout>0 );
146+
147+
buf[i] = 0; // null terminate the string
148+
return 0;
149+
}
150+
151+
//
152+
int serialport_flush(int fd)
153+
{
154+
sleep(2); //required to make flush work, for some reason
155+
return tcflush(fd, TCIOFLUSH);
156+
}
157+
158+
159+
// =====================================
160+
// [[Rcpp::export]]
161+
Rcpp::StringVector arduino_read(const char* serialport, int baudrate = 9600,
162+
int times = 10, char eolchar = '\n',
163+
int timeout = 5000, const int buf_max = 256)
164+
{
165+
166+
int fd;
167+
char buf[buf_max];
168+
std::vector<std::string> out(times);
169+
170+
fd = serialport_init(serialport, baudrate);
171+
memset(buf,0,buf_max); //
172+
serialport_read_until(fd, buf, eolchar, buf_max, timeout);
173+
for (int i=0; i<times; ++i) {
174+
serialport_read_until(fd, buf, eolchar, buf_max, timeout);
175+
out[i] = buf;
176+
memset(&buf[0], 0, sizeof(buf));
177+
}
178+
179+
serialport_close(fd);
180+
Rcpp::StringVector out2(times);
181+
out2 = out;
182+
return out2;
183+
}

Diff for: src/arduino-serial/arduino-serial-lib.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// arduino-serial-lib -- simple library for reading/writing serial ports
3+
//
4+
// 2006-2013, Tod E. Kurt, http://todbot.com/blog/
5+
//
6+
7+
8+
#ifndef __ARDUINO_SERIAL_LIB_H__
9+
#define __ARDUINO_SERIAL_LIB_H__
10+
11+
#include <stdint.h> // Standard types
12+
13+
int serialport_init(const char* serialport, int baud);
14+
int serialport_close(int fd);
15+
int serialport_writebyte( int fd, uint8_t b);
16+
int serialport_write(int fd, const char* str);
17+
int serialport_read_until(int fd, char* buf, char until, int buf_max,int timeout);
18+
int serialport_flush(int fd);
19+
20+
#endif
21+

0 commit comments

Comments
 (0)