subduino 0.2.2 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ begin
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "subduino"
8
8
  gem.summary = "Arduino Ruby Helpers"
9
- gem.description = "Interface, compile, upload, play with arduino/ruby"
9
+ gem.description = "Interface, compile, upload... Play with arduino on ruby!"
10
10
  gem.email = "x@nofxx.com"
11
11
  gem.homepage = "http://github.com/nofxx/subduino"
12
12
  gem.authors = ["Marcos Piccinini"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.1
data/bin/subduino CHANGED
@@ -2,45 +2,53 @@
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'rubygems'
4
4
  require 'optparse'
5
- require 'subduino'
6
- require 'subduino/os'
7
- require 'faye'
5
+
8
6
 
9
7
  Debug = false
10
8
 
9
+ if File.exists?(f = ARGV.join)
10
+ # return unless f =~ /\.yml$/
11
+ puts "Using config file #{f}"
12
+ AppConfig = YAML.load(File.read(f))["opts"]
13
+ else
14
+ AppConfig = { :log_interval => 30 }
15
+ end
16
+
11
17
  OptionParser.new { |op|
12
18
  op.on('-d', '--debug') { |d| Debug = true }
19
+ op.on('-b', '--bauds=BAUDS', Integer) { |b| AppConfig[:bauds] = b }
13
20
  op.on('-e env') { |val| set :environment, val.to_sym }
14
21
  op.on('-s server') { |val| set :server, val }
15
22
  op.on('-p port') { |val| set :port, val.to_i }
16
- }.parse!(ARGV)
23
+ op.parse!(ARGV)
24
+ }
25
+
26
+ require 'subduino'
27
+ require 'subduino/os'
28
+ require 'faye'
29
+ # Cubduino.write("hahah")
30
+ # puts Cubduino.read
17
31
 
18
32
  # set :run, true
19
33
 
20
- if Debug
21
- puts "Starting on #{OS}"
22
- end
23
34
 
24
35
  if ARGV.empty? || ARGV.join =~ /yml/
25
36
  LastRun = {:all => 0}
26
- if File.exists?(f = ARGV.join)
27
- puts "Using config file #{f}"
28
- AppConfig = YAML.load(File.read(f))["opts"]
29
- else
30
- AppConfig = { "log_interval" => 30, "bauds" => 115200 }
31
- end
32
37
 
33
38
  client = Faye::Client.new('http://localhost:8000/faye')
34
39
 
35
40
  EM.run do
41
+ if Debug
42
+ puts "Starting on #{OS}"
43
+ end
36
44
 
37
45
  Subduino.start do |read|
38
46
  # Subduino::ArdIO.write("hi")
39
47
  if Debug
40
- # puts "--------------------- #{Time.now}"
41
- p read
48
+ #puts "--------------------- #{Time.now}"
49
+ #p read
42
50
  end
43
- if LastRun[:all] < (Time.now.to_i - AppConfig["log_interval"])
51
+ if LastRun[:all] < (Time.now.to_i - AppConfig[:log_interval])
44
52
  LastRun[:all] = Time.now.to_i
45
53
  client.publish('/stats', 'data' => read)
46
54
  Subduino::Store.add_csv_to_store(read, true)
data/duino/duino.pde CHANGED
@@ -112,9 +112,11 @@ void read_commands() {
112
112
  while( Serial.available() && c != '\n' ) { // buffer up a line
113
113
  c = Serial.read();
114
114
  if (c == '\n' || c == ',') {
115
+ Serial.println("<<");
115
116
  int pin; int val;
116
117
  pin = (ctoi(cmdbuf[0])*10) + ctoi(cmdbuf[1]);
117
118
  val = (ctoi(cmdbuf[2])*100) + (ctoi(cmdbuf[3])*10) + ctoi(cmdbuf[4]);
119
+ Serial.println("<<");
118
120
  analogWrite(pin, val);
119
121
  i = 0;
120
122
  } else if (c == 'x') {
@@ -128,13 +130,20 @@ void read_commands() {
128
130
  void setup() {
129
131
  // Atmega defaults INPUT
130
132
  pinMode(infoPin, OUTPUT);
131
- pinMode(d2, OUTPUT);
132
- pinMode(d3, OUTPUT);
133
- pinMode(d4, OUTPUT);
134
- pinMode(d5, OUTPUT);
135
- pinMode(d6, OUTPUT);
136
- pinMode(d7, OUTPUT);
137
- pinMode(d10, OUTPUT);
133
+ pinMode(rxPin, OUTPUT);
134
+ pinMode(txPin, OUTPUT);
135
+ for (int k=0;k<=7;k++)
136
+ pinMode(k,OUTPUT);
137
+ // pinMode(d2, OUTPUT);
138
+ // pinMode(d3, OUTPUT);
139
+ // pinMode(d4, OUTPUT);
140
+ // pinMode(d5, OUTPUT);
141
+ // pinMode(d6, OUTPUT);
142
+ // pinMode(d7, OUTPUT);
143
+ // pinMode(d10, OUTPUT);
144
+ // digitalWrite(rxPin, 0); FAIL
145
+ // digitalWrite(txPin, 0);
146
+
138
147
 
139
148
  // attachInterrupt(0, btnLed, CHANGE);
140
149
  Serial.begin(115200);
data/examples/duin ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # usage: usb-power bus/device port on|off
4
+ #
5
+ # example:
6
+ # usb-power 004/006 2 on
7
+ # usb-power 004/006 2 off
8
+
9
+ require 'usb'
10
+ require 'optparse'
11
+
12
+ USB_RT_PORT = USB::USB_TYPE_CLASS | USB::USB_RECIP_OTHER
13
+ USB_PORT_FEAT_POWER = 8
14
+
15
+ USB.devices.find_all {|d|
16
+ # p d
17
+ 0x200 <= d.bcdDevice &&
18
+ d.bDeviceClass == USB::USB_CLASS_HUB
19
+ }
20
+
21
+ # require 'pp'
22
+
23
+ # def power_on(h, port)
24
+ # h.usb_control_msg(USB_RT_PORT, USB::USB_REQ_SET_FEATURE, USB_PORT_FEAT_POWER, port, "", 0)
25
+ # end
26
+
27
+ # def power_off(h, port)
28
+ # h.usb_control_msg(USB_RT_PORT, USB::USB_REQ_CLEAR_FEATURE, USB_PORT_FEAT_POWER, port, "", 0)
29
+ # end
30
+
31
+ # bus_device = ARGV.shift
32
+ # port = ARGV.shift.to_i
33
+ # on_off = ARGV.shift
34
+
35
+ # %r{/} =~ bus_device
36
+ # bus = $`.to_i
37
+ # device = $'.to_i
38
+
39
+ # USB.find_bus(bus).find_device(device).open {|h|
40
+ # if on_off == 'off'
41
+ # power_off(h, port)
42
+ # else
43
+ # power_on(h, port)
44
+ # end
45
+ # }
@@ -0,0 +1,187 @@
1
+
2
+ SHELL = /bin/sh
3
+
4
+ #### Start of system configuration section. ####
5
+
6
+ srcdir = .
7
+ topdir = /usr/include/ruby-1.9.1
8
+ hdrdir = /usr/include/ruby-1.9.1
9
+ arch_hdrdir = /usr/include/ruby-1.9.1/$(arch)
10
+ VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
11
+ prefix = $(DESTDIR)/usr
12
+ rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
13
+ exec_prefix = $(prefix)
14
+ vendorhdrdir = $(rubyhdrdir)/vendor_ruby
15
+ sitehdrdir = $(rubyhdrdir)/site_ruby
16
+ rubyhdrdir = $(includedir)/$(RUBY_BASE_NAME)-$(ruby_version)
17
+ vendordir = $(rubylibprefix)/vendor_ruby
18
+ sitedir = $(rubylibprefix)/site_ruby
19
+ ridir = $(datarootdir)/$(RI_BASE_NAME)
20
+ mandir = $(datarootdir)/man
21
+ localedir = $(datarootdir)/locale
22
+ libdir = $(exec_prefix)/lib
23
+ psdir = $(docdir)
24
+ pdfdir = $(docdir)
25
+ dvidir = $(docdir)
26
+ htmldir = $(docdir)
27
+ infodir = $(datarootdir)/info
28
+ docdir = $(datarootdir)/doc/$(PACKAGE)
29
+ oldincludedir = $(DESTDIR)/usr/include
30
+ includedir = $(prefix)/include
31
+ localstatedir = $(prefix)/var
32
+ sharedstatedir = $(prefix)/com
33
+ sysconfdir = $(prefix)/etc
34
+ datadir = $(datarootdir)
35
+ datarootdir = $(prefix)/share
36
+ libexecdir = $(exec_prefix)/libexec
37
+ sbindir = $(exec_prefix)/sbin
38
+ bindir = $(exec_prefix)/bin
39
+ rubylibdir = $(rubylibprefix)/$(ruby_version)
40
+ archdir = $(rubylibdir)/$(arch)
41
+ sitelibdir = $(sitedir)/$(ruby_version)
42
+ sitearchdir = $(sitelibdir)/$(sitearch)
43
+ vendorlibdir = $(vendordir)/$(ruby_version)
44
+ vendorarchdir = $(vendorlibdir)/$(sitearch)
45
+
46
+ CC = gcc
47
+ CXX = g++
48
+ LIBRUBY = $(LIBRUBY_SO)
49
+ LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
50
+ LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME)
51
+ LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static
52
+ OUTFLAG = -o
53
+ COUTFLAG = -o
54
+
55
+ RUBY_EXTCONF_H =
56
+ cflags = $(optflags) $(debugflags) $(warnflags)
57
+ optflags = -O3
58
+ debugflags = -ggdb
59
+ warnflags = -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long
60
+ CFLAGS = -fPIC -march=x86-64 -mtune=generic -O3 -pipe -fPIC -std=c99 -Wall -I.
61
+ INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
62
+ DEFS =
63
+ CPPFLAGS = $(DEFS) $(cppflags)
64
+ CXXFLAGS = $(CFLAGS) -march=x86-64 -mtune=generic -O2 -pipe
65
+ ldflags = -L. -rdynamic -Wl,-export-dynamic
66
+ dldflags =
67
+ ARCH_FLAG =
68
+ DLDFLAGS = $(ldflags) $(dldflags)
69
+ LDSHARED = $(CC) -shared
70
+ LDSHAREDXX = $(CXX) -shared
71
+ AR = ar
72
+ EXEEXT =
73
+
74
+ RUBY_BASE_NAME = ruby
75
+ RUBY_INSTALL_NAME = ruby
76
+ RUBY_SO_NAME = ruby
77
+ arch = x86_64-linux
78
+ sitearch = $(arch)
79
+ ruby_version = 1.9.1
80
+ ruby = /usr/bin/ruby
81
+ RUBY = $(ruby)
82
+ RM = rm -f
83
+ RM_RF = $(RUBY) -run -e rm -- -rf
84
+ RMDIRS = $(RUBY) -run -e rmdir -- -p
85
+ MAKEDIRS = /bin/mkdir -p
86
+ INSTALL = /bin/install -c
87
+ INSTALL_PROG = $(INSTALL) -m 0755
88
+ INSTALL_DATA = $(INSTALL) -m 644
89
+ COPY = cp
90
+
91
+ #### End of system configuration section. ####
92
+
93
+ preload =
94
+
95
+ libpath = . $(libdir)
96
+ LIBPATH = -L. -L$(libdir)
97
+ DEFFILE =
98
+
99
+ CLEANFILES = mkmf.log
100
+ DISTCLEANFILES =
101
+ DISTCLEANDIRS =
102
+
103
+ extout =
104
+ extout_prefix =
105
+ target_prefix =
106
+ LOCAL_LIBS =
107
+ LIBS = $(LIBRUBYARG_SHARED) -lpthread -lrt -ldl -lcrypt -lm -lc
108
+ SRCS = cubduino.c
109
+ OBJS = cubduino.o
110
+ TARGET = cubduino
111
+ DLLIB = $(TARGET).so
112
+ EXTSTATIC =
113
+ STATIC_LIB =
114
+
115
+ BINDIR = $(bindir)
116
+ RUBYCOMMONDIR = $(sitedir)$(target_prefix)
117
+ RUBYLIBDIR = $(sitelibdir)$(target_prefix)
118
+ RUBYARCHDIR = $(sitearchdir)$(target_prefix)
119
+ HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
120
+ ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
121
+
122
+ TARGET_SO = $(DLLIB)
123
+ CLEANLIBS = $(TARGET).so
124
+ CLEANOBJS = *.o *.bak
125
+
126
+ all: $(DLLIB)
127
+ static: $(STATIC_LIB)
128
+ .PHONY: all install static install-so install-rb
129
+ .PHONY: clean clean-so clean-rb
130
+
131
+ clean-rb-default::
132
+ clean-rb::
133
+ clean-so::
134
+ clean: clean-so clean-rb-default clean-rb
135
+ @-$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES)
136
+
137
+ distclean-rb-default::
138
+ distclean-rb::
139
+ distclean-so::
140
+ distclean: clean distclean-so distclean-rb-default distclean-rb
141
+ @-$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
142
+ @-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
143
+ @-$(RMDIRS) $(DISTCLEANDIRS)
144
+
145
+ realclean: distclean
146
+ install: install-so install-rb
147
+
148
+ install-so: $(RUBYARCHDIR)
149
+ install-so: $(RUBYARCHDIR)/$(DLLIB)
150
+ $(RUBYARCHDIR)/$(DLLIB): $(DLLIB)
151
+ @-$(MAKEDIRS) $(@D)
152
+ $(INSTALL_PROG) $(DLLIB) $(@D)
153
+ install-rb: pre-install-rb install-rb-default
154
+ install-rb-default: pre-install-rb-default
155
+ pre-install-rb: Makefile
156
+ pre-install-rb-default: Makefile
157
+ $(RUBYARCHDIR):
158
+ $(MAKEDIRS) $@
159
+
160
+ site-install: site-install-so site-install-rb
161
+ site-install-so: install-so
162
+ site-install-rb: install-rb
163
+
164
+ .SUFFIXES: .c .m .cc .cxx .cpp .C .o
165
+
166
+ .cc.o:
167
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
168
+
169
+ .cxx.o:
170
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
171
+
172
+ .cpp.o:
173
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
174
+
175
+ .C.o:
176
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
177
+
178
+ .c.o:
179
+ $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
180
+
181
+ $(DLLIB): $(OBJS) Makefile
182
+ @-$(RM) $(@)
183
+ $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
184
+
185
+
186
+
187
+ $(OBJS): $(hdrdir)/ruby.h $(hdrdir)/ruby/defines.h $(arch_hdrdir)/ruby/config.h
@@ -0,0 +1,242 @@
1
+ /*
2
+ * Arduino-serial
3
+ * --------------
4
+ *
5
+ * A simple command-line example program showing how a computer can
6
+ * communicate with an Arduino board. Works on any POSIX system (Mac/Unix/PC)
7
+ *
8
+ *
9
+ * Compile with something like:
10
+ * gcc -o arduino-serial arduino-serial.c
11
+ *
12
+ * Created 5 December 2006
13
+ * Copyleft (c) 2006, Tod E. Kurt, tod@todbot.com
14
+ * http://todbot.com/blog/
15
+ *
16
+ *
17
+ * Updated 8 December 2006:
18
+ * Justin McBride discoevered B14400 & B28800 aren't in Linux's termios.h.
19
+ * I've included his patch, but commented out for now. One really needs a
20
+ * real make system when doing cross-platform C and I wanted to avoid that
21
+ * for this little program. Those baudrates aren't used much anyway. :)
22
+ *
23
+ * Updated 26 December 2007:
24
+ * Added ability to specify a delay (so you can wait for Arduino Diecimila)
25
+ * Added ability to send a binary byte number
26
+ *
27
+ * Update 31 August 2008:
28
+ * Added patch to clean up odd baudrates from Andy at hexapodia.org
29
+ *
30
+ */
31
+
32
+ #include "cubduino.h"
33
+
34
+
35
+
36
+ // Ruby 1.x compatibility
37
+ #ifndef RSTRING_PTR
38
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
39
+ #endif
40
+ #ifndef RSTRING_LEN
41
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
42
+ #endif
43
+
44
+ VALUE Cubduino;
45
+
46
+ VALUE sBaud, sDataBits, sStopBits, sParity; /* strings */
47
+ VALUE sRts, sDtr, sCts, sDsr, sDcd, sRi;
48
+
49
+
50
+ void Init_cubduino();
51
+ //VALUE method_cwrite(VALUE self);
52
+
53
+
54
+
55
+ void usage(void);
56
+ int serialport_init(const char* serialport, int baud);
57
+ int serialport_writebyte(int fd, uint8_t b);
58
+ int serialport_write(int fd, const char* str);
59
+ int serialport_read_until(int fd, char* buf, char until);
60
+
61
+
62
+ int main(int argc, char *argv[])
63
+ {
64
+ int rc,n;
65
+ int fd = 0;
66
+ int baudrate = B9600; // default
67
+ char serialport[256];
68
+ char buf[256];
69
+
70
+ /* parse options */
71
+ int option_index = 0, opt;
72
+ static struct option loptions[] = {
73
+ {"help", no_argument, 0, 'h'},
74
+ {"port", required_argument, 0, 'p'},
75
+ {"baud", required_argument, 0, 'b'},
76
+ {"send", required_argument, 0, 's'},
77
+ {"receive", no_argument, 0, 'r'},
78
+ {"num", required_argument, 0, 'n'},
79
+ {"delay", required_argument, 0, 'd'}
80
+ };
81
+
82
+ while(1) {
83
+ opt = getopt_long (argc, argv, "hp:b:s:rn:d:",
84
+ loptions, &option_index);
85
+ if (opt==-1) break;
86
+ switch (opt) {
87
+ case '0': break;
88
+ case 'd':
89
+ n = strtol(optarg,NULL,10);
90
+ usleep(n * 1000 ); // sleep milliseconds
91
+ break;
92
+ case 'h':
93
+ usage();
94
+ break;
95
+ case 'b':
96
+ baudrate = strtol(optarg,NULL,10);
97
+ break;
98
+ case 'p':
99
+ strcpy(serialport,optarg);
100
+ fd = serialport_init(optarg, baudrate);
101
+ if(fd==-1) return -1;
102
+ break;
103
+ case 'n':
104
+ n = strtol(optarg, NULL, 10); // convert string to number
105
+ rc = serialport_writebyte(fd, (uint8_t)n);
106
+ if(rc==-1) return -1;
107
+ break;
108
+ case 's':
109
+ strcpy(buf,optarg);
110
+ rc = serialport_write(fd, buf);
111
+ if(rc==-1) return -1;
112
+ break;
113
+ case 'r':
114
+ serialport_read_until(fd, buf, '\n');
115
+ printf("read: %s\n",buf);
116
+ break;
117
+ }
118
+ }
119
+
120
+ exit(EXIT_SUCCESS);
121
+ } // end main
122
+
123
+ int serialport_writebyte( int fd, uint8_t b)
124
+ {
125
+ int n = write(fd,&b,1);
126
+ if( n!=1)
127
+ return -1;
128
+ return 0;
129
+ }
130
+
131
+ int serialport_write(int fd, const char* str)
132
+ {
133
+ int len = strlen(str);
134
+ int n = write(fd, str, len);
135
+ if( n!=len )
136
+ return -1;
137
+ return 0;
138
+ }
139
+
140
+ int serialport_read_until(int fd, char* buf, char until)
141
+ {
142
+ char b[1];
143
+ int i=0;
144
+ do {
145
+ int n = read(fd, b, 1); // read a char at a time
146
+ if( n==-1) return -1; // couldn't read
147
+ if( n==0 ) {
148
+ usleep( 10 * 1000 ); // wait 10 msec try again
149
+ continue;
150
+ }
151
+ buf[i] = b[0]; i++;
152
+ } while( b[0] != until );
153
+
154
+ buf[i] = 0; // null terminate the string
155
+ return 0;
156
+ }
157
+
158
+ // takes the string name of the serial port (e.g. "/dev/tty.usbserial","COM1")
159
+ // and a baud rate (bps) and connects to that port at that speed and 8N1.
160
+ // opens the port in fully raw mode so you can send binary data.
161
+ // returns valid fd, or -1 on error
162
+ int serialport_init(const char* serialport, int baud)
163
+ {
164
+ struct termios toptions;
165
+ int fd;
166
+
167
+ //fprintf(stderr,"init_serialport: opening port %s @ %d bps\n",
168
+ // serialport,baud);
169
+
170
+ fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
171
+ if (fd == -1) {
172
+ perror("init_serialport: Unable to open port ");
173
+ return -1;
174
+ }
175
+
176
+ if (tcgetattr(fd, &toptions) < 0) {
177
+ perror("init_serialport: Couldn't get term attributes");
178
+ return -1;
179
+ }
180
+ speed_t brate = baud; // let you override switch below if needed
181
+ switch(baud) {
182
+ case 4800: brate=B4800; break;
183
+ case 9600: brate=B9600; break;
184
+ #ifdef B14400
185
+ case 14400: brate=B14400; break;
186
+ #endif
187
+ case 19200: brate=B19200; break;
188
+ #ifdef B28800
189
+ case 28800: brate=B28800; break;
190
+ #endif
191
+ case 38400: brate=B38400; break;
192
+ case 57600: brate=B57600; break;
193
+ case 115200: brate=B115200; break;
194
+ }
195
+ cfsetispeed(&toptions, brate);
196
+ cfsetospeed(&toptions, brate);
197
+
198
+ // 8N1
199
+ toptions.c_cflag &= ~PARENB;
200
+ toptions.c_cflag &= ~CSTOPB;
201
+ toptions.c_cflag &= ~CSIZE;
202
+ toptions.c_cflag |= CS8;
203
+ // no flow control
204
+ toptions.c_cflag &= ~CRTSCTS;
205
+
206
+ toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
207
+ toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
208
+
209
+ toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
210
+ toptions.c_oflag &= ~OPOST; // make raw
211
+
212
+ // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
213
+ toptions.c_cc[VMIN] = 0;
214
+ toptions.c_cc[VTIME] = 20;
215
+
216
+ if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
217
+ perror("init_serialport: Couldn't set term attributes");
218
+ return -1;
219
+ }
220
+
221
+ return fd;
222
+ }
223
+
224
+ static VALUE method_write(VALUE class, VALUE arg) {
225
+ printf("%s", RSTRING_PTR(arg));
226
+ return Qnil;
227
+ }
228
+
229
+ static VALUE method_read() {
230
+ int fd = 0;
231
+ int baudrate = 9600;
232
+ fd = serialport_init(optarg, baudrate);
233
+ char buf[256];
234
+ serialport_read_until(fd, buf, '\n');
235
+ return rb_str_new2(buf);
236
+ }
237
+
238
+ void Init_cubduino() {
239
+ Cubduino = rb_define_module("Cubduino");
240
+ rb_define_singleton_method(Cubduino, "read", method_write, 0);
241
+ rb_define_singleton_method(Cubduino, "write", method_write, 1);
242
+ }
@@ -0,0 +1,12 @@
1
+ #include <ruby.h>
2
+
3
+ #include <stdio.h> /* Standard input/output definitions */
4
+ #include <stdlib.h>
5
+ #include <stdint.h> /* Standard types */
6
+ #include <string.h> /* String function definitions */
7
+ #include <unistd.h> /* UNIX standard function definitions */
8
+ #include <fcntl.h> /* File control definitions */
9
+ #include <errno.h> /* Error number definitions */
10
+ #include <termios.h> /* POSIX terminal control definitions */
11
+ #include <sys/ioctl.h>
12
+ #include <getopt.h>
Binary file
Binary file
@@ -0,0 +1,38 @@
1
+ require 'mkmf'
2
+
3
+ def crash(s)
4
+ puts "--------------------------------------------------"
5
+ puts " extconf failure: #{s}"
6
+ puts "--------------------------------------------------"
7
+ exit 1
8
+ end
9
+
10
+ unless find_executable("pkg-config")
11
+ crash("pkg-config needed")
12
+ end
13
+
14
+ $CFLAGS += " -std=c99 -Wall -I. " #+ `pkg-config --cflags glib-2.0`.strip
15
+ # $LIBS += " " + `pkg-config --libs glib-2.0`
16
+ # $CFLAGS += " -DOS_#{os.upcase}"
17
+ # $CFLAGS += " -DRUBY_1_9" if RUBY_VERSION =~ /^1\.9/
18
+
19
+
20
+ # unless have_library('glib-2.0')
21
+ # crash "libglib-2.0 needed"
22
+ # end
23
+
24
+ if ARGV.include?("-d")
25
+ $CFLAGS += " -D CUBDUINO_DEBUG"
26
+ end
27
+
28
+ if ARGV.include?("-O0")
29
+ $CFLAGS.gsub!(/-O./, "-O0")
30
+ else
31
+ $CFLAGS.gsub!(/-O./, "-O3")
32
+ end
33
+
34
+ if ARGV.include?("-gdb")
35
+ $CFLAGS += " -g -gdwarf-2 -g3"
36
+ end
37
+
38
+ create_makefile("cubduino")
@@ -9,7 +9,7 @@ module Subduino
9
9
  # Direct access to the SerialPort instance.
10
10
  #
11
11
  def sp
12
- @sp ||= SerialPort.new(Arduino.find_usb, BAUDS) #, DATA_BITS, DATA_STOP, parity)
12
+ @sp ||= SerialPort.new(Arduino.find_usb, AppConfig[:bauds] || 57600) #, DATA_BITS, DATA_STOP, parity)
13
13
  # @sp.read_timeout = 10;# @sp.write_timeout = 10
14
14
  end
15
15
 
@@ -21,18 +21,23 @@ module Subduino
21
21
  # Feed it with a block to read the text.
22
22
  #
23
23
  def read(&proc)
24
- Log.info "[USB] Starting USB Connect..." + sp.get_modem_params.map { |k,v| "#{k}: #{v}" }.join(" ")
24
+ Log.info "[USB] Found Device...#{Arduino.find_usb}"
25
+ Log.info "[USB] Starting Connect..." + sp.get_modem_params.map { |k,v| "#{k}: #{v}" }.join(" ")
25
26
  Log.info "[USB] Read Timeout #{sp.read_timeout}" # {sp.write_timeout}"
26
27
 
28
+ if sp
27
29
  @iothread ||= Thread.new do
28
- Thread.current.abort_on_exception = false
30
+ # Thread.current.abort_on_exception = false
29
31
  icache = []
30
32
  loop do
31
33
  begin
32
34
  while char = sp.getc
33
35
  if !char.valid_encoding?
34
- puts "Bad char #{char}"
36
+ bytes = char.bytes.to_a
37
+ hexes = bytes.map { |b| b.to_s(16) }
38
+ puts " - Bad char #{char} - (Hex: #{char.unpack('H')} | Byte(s) #{bytes} | Hexe(s) #{hexes}"
35
39
  elsif char !~ /\n|\r/
40
+ # print char if Debug
36
41
  icache << char
37
42
  else
38
43
  data = icache.join(""); icache = []
@@ -46,9 +51,13 @@ module Subduino
46
51
  rescue => e
47
52
  Log.error "[USB] Error #{e}"
48
53
  Log.error e.backtrace.join("\n")
54
+ stop!
55
+ exit 1
49
56
  end
50
57
  end
51
58
  end
59
+ end
60
+
52
61
  end
53
62
 
54
63
  #
@@ -60,8 +69,8 @@ module Subduino
60
69
  #
61
70
  def write(msg)
62
71
  Log.info "[IO TX] #{msg}"
63
- txt = msg.gsub("\r", "\n")
64
- txt += "\n" unless txt =~ /^\\n/
72
+ txt = msg.gsub("\r|\n", "")
73
+ # txt += "\n" unless txt =~ /^\\n/
65
74
  puts "=> Sending #{txt.inspect}" if Debug
66
75
  sp.puts(msg)
67
76
  end
@@ -72,6 +81,8 @@ module Subduino
72
81
  #
73
82
  def stop!
74
83
  sp.close
84
+ Thread.kill @iothread
85
+ Log.info "[IO] K.I.A"
75
86
  end
76
87
 
77
88
  end
@@ -9,7 +9,7 @@ module Subduino
9
9
  # Direct access to the Redis instance.
10
10
  #
11
11
  def redis
12
- @redis ||= Redis.new(:timeout => 0) rescue false
12
+ @redis ||= Redis.new(:timeout => 0)
13
13
  end
14
14
 
15
15
  #
@@ -19,7 +19,7 @@ module Subduino
19
19
  #
20
20
  def read
21
21
  return Log.warn "[PubSub] Not started..." unless redis
22
- Thread.new do
22
+ @psthread = Thread.new do
23
23
  begin
24
24
  redis.subscribe('subduino') do |on|
25
25
  on.subscribe {|klass, num_subs| Log.info "[PubSub] Subscribed to #{klass} (#{num_subs} subscriptions)" }
@@ -33,6 +33,7 @@ module Subduino
33
33
  rescue => e
34
34
  Log.error "[PubSub] Error #{e}"
35
35
  Log.error e.backtrace.join("\n")
36
+ exit 1
36
37
  end
37
38
 
38
39
  end
@@ -52,7 +53,9 @@ module Subduino
52
53
  # Fatality
53
54
  #
54
55
  def stop!
56
+ Thread.kill @psthread
55
57
  redis.disconnect if redis
58
+ Log.info "[PubSub] K.I.A"
56
59
  end
57
60
 
58
61
  end
@@ -139,8 +139,16 @@ ifeq ($(strip $(NO_CORE)),)
139
139
  ifdef ARDUINO_CORE_PATH
140
140
  CORE_C_SRCS = $(wildcard $(ARDUINO_CORE_PATH)/*.c)
141
141
  CORE_CPP_SRCS = $(wildcard $(ARDUINO_CORE_PATH)/*.cpp)
142
- # EXTRA_CPP_SRCS = $(wildcard $(ARDUINO_LIB_PATH)/ExtraLib/*.cpp)
143
- CORE_OBJ_FILES = $(CORE_C_SRCS:.c=.o) $(CORE_CPP_SRCS:.cpp=.o) $(EXTRA_CPP_SRCS:.cpp=.o)
142
+
143
+ # FIXME: Dynamic way
144
+ ifdef ARDUINO_LIBS
145
+ EXTRA_C_SRCS = $(wildcard $(ARDUINO_LIB_PATH)/ExtraLib/*.c)
146
+ EXTRA_CPP_SRCS = $(wildcard $(ARDUINO_LIB_PATH)/ExtraLib/*.cpp)
147
+ CORE_OBJ_FILES = $(CORE_C_SRCS:.c=.o) $(CORE_CPP_SRCS:.cpp=.o) $(EXTRA_C_SRCS:.c=.o) $(EXTRA_CPP_SRCS:.cpp=.o)
148
+ endif
149
+ ifndef ARDUINO_LIBS
150
+ CORE_OBJ_FILES = $(CORE_C_SRCS:.c=.o) $(CORE_CPP_SRCS:.cpp=.o)
151
+ endif
144
152
  CORE_OBJS = $(patsubst $(ARDUINO_CORE_PATH)/%, $(OBJDIR)/%,$(CORE_OBJ_FILES))
145
153
  endif
146
154
  endif
@@ -14,7 +14,7 @@ module Subduino
14
14
  end
15
15
 
16
16
  def read(key)
17
- redis.get key
17
+ redis.get key.to_s
18
18
  end
19
19
 
20
20
  def timestamp
@@ -23,10 +23,9 @@ module Subduino
23
23
 
24
24
  def write(k, v, stamp = false)
25
25
  return unless redis #.connected?
26
- stamp ? redis.rpush("log:inputs:#{k}", "#{timestamp}:#{v}") : redis.set("now:inputs:"+k, v)
26
+ stamp ? redis.rpush("log:inputs:#{k}", "#{timestamp}:#{v}") : redis.set("now:inputs:#{k}", v)
27
27
  end
28
28
 
29
-
30
29
  def add_to_store(key, val=nil)
31
30
  if val
32
31
  write key, val
@@ -38,7 +37,7 @@ module Subduino
38
37
  end
39
38
 
40
39
  def add_csv_to_store(csv, stamp = false)
41
- Log.info "[STORE] CSV #{Time.now.to_i}"
40
+ # Log.info "[STORE] CSV #{Time.now.to_i}"
42
41
  csv.split(",").each do |d|
43
42
  comm, value = d.split(":")
44
43
  write(comm, value, stamp)
data/lib/subduino.rb CHANGED
@@ -4,6 +4,8 @@
4
4
  #
5
5
  #
6
6
  #
7
+ require File.dirname(__FILE__) + '/../ext/subduino/cubduino'
8
+ #require "subduino/cubduino"
7
9
  require 'serialport'
8
10
  require 'eventmachine'
9
11
  require 'stringio'
@@ -17,11 +19,10 @@ require 'subduino/parse'
17
19
  require 'subduino/store'
18
20
  require 'subduino/arduino'
19
21
 
20
- Thread.current.abort_on_exception = false
22
+ #Thread.current.abort_on_exception = false
21
23
 
22
24
  module Subduino
23
- Log = Logger.new("out.log")
24
- BAUDS = 115200 #BAUDS = 9600
25
+ Log = Logger.new(const_defined?("DEBUG") ? STDOUT : "subduino-debug.log")
25
26
  # BAUDS = [300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200]
26
27
  Sensors = [:temp, :lux]
27
28
  # DATA_BITS = 8
@@ -32,7 +33,9 @@ module Subduino
32
33
  trap(:TERM) { stop! }
33
34
  trap(:INT) { stop! }
34
35
  # Start some threads...
36
+ Log.info "[IO] Boot!"
35
37
  ArdIO.read &proc
38
+ Log.info "[PubSub] Boot!"
36
39
  ArdPS.read
37
40
 
38
41
  # Be a daemon. Should be a better way..
@@ -40,7 +43,9 @@ module Subduino
40
43
  end
41
44
 
42
45
  def self.stop!
46
+ Log.info "[IO] Shutting I/O down..."
43
47
  ArdIO.stop!
48
+ Log.info "[PubSub] Shutting PubSub down..."
44
49
  ArdPS.stop!
45
50
  exit 0
46
51
  end
@@ -3,6 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
3
  describe ArdIO do
4
4
 
5
5
  it "should start serial port" do
6
+ Arduino.stub!(:find_usb).and_return("/dev/ttyUSB0")
6
7
  SerialPort.should_receive(:new).with("/dev/ttyUSB0", 115200)
7
8
  ArdIO.sp
8
9
  end
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Cubduino" do
4
+
5
+ it "should define module" do
6
+ Subduino.const_defined?("Cubduino").should be_true
7
+ end
8
+
9
+
10
+ end
@@ -5,11 +5,11 @@ describe "Store" do
5
5
  it "should write things" do
6
6
  Store.flush
7
7
  Store.write(:temp, 18)
8
- Store.count.should eql(2)
8
+ Store.count.should eql(1)
9
9
  end
10
10
 
11
11
  it "should read" do
12
- Store.read(:temp).should eql("18")
12
+ Store.read("now:inputs:temp").should eql("18")
13
13
  end
14
14
 
15
15
  it "should read array"
data/subduino.gemspec CHANGED
@@ -9,10 +9,11 @@ Gem::Specification.new do |s|
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marcos Piccinini"]
12
- s.date = %q{2010-09-30}
12
+ s.date = %q{2010-10-21}
13
13
  s.description = %q{Interface, compile, upload, play with arduino/ruby}
14
14
  s.email = %q{x@nofxx.com}
15
15
  s.executables = ["subduino-cli", "subduino"]
16
+ s.extensions = ["ext/subduino/extconf.rb"]
16
17
  s.files = [
17
18
  ".document",
18
19
  ".gitignore",
@@ -65,7 +66,8 @@ Gem::Specification.new do |s|
65
66
  "spec/spec_helper.rb",
66
67
  "spec/subduino/ard_io_spec.rb",
67
68
  "spec/subduino/parse_spec.rb",
68
- "spec/subduino/store_spec.rb"
69
+ "spec/subduino/store_spec.rb",
70
+ "spec/subduino/cubduino_spec.rb"
69
71
  ]
70
72
 
71
73
  if s.respond_to? :specification_version then
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 2
9
- version: 0.2.2
7
+ - 3
8
+ - 1
9
+ version: 0.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Marcos Piccinini
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-30 00:00:00 -03:00
17
+ date: 2011-08-29 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -58,18 +58,17 @@ dependencies:
58
58
  version: 1.2.9
59
59
  type: :development
60
60
  version_requirements: *id003
61
- description: Interface, compile, upload, play with arduino/ruby
61
+ description: Interface, compile, upload... Play with arduino on ruby!
62
62
  email: x@nofxx.com
63
63
  executables:
64
- - subduino-cli
65
64
  - subduino
66
- extensions: []
67
-
65
+ - subduino-cli
66
+ extensions:
67
+ - ext/subduino/extconf.rb
68
68
  extra_rdoc_files: []
69
69
 
70
70
  files:
71
71
  - .document
72
- - .gitignore
73
72
  - Rakefile
74
73
  - Readme.textile
75
74
  - VERSION
@@ -80,6 +79,13 @@ files:
80
79
  - duino/duino.pde
81
80
  - duino/duino.rb
82
81
  - duino/methods.pde
82
+ - examples/duin
83
+ - ext/subduino/Makefile
84
+ - ext/subduino/cubduino.c
85
+ - ext/subduino/cubduino.h
86
+ - ext/subduino/cubduino.o
87
+ - ext/subduino/cubduino.so
88
+ - ext/subduino/extconf.rb
83
89
  - lib/subduino.rb
84
90
  - lib/subduino/ard_io.rb
85
91
  - lib/subduino/ard_ps.rb
@@ -104,6 +110,7 @@ files:
104
110
  - node/vendor/faye.js
105
111
  - spec/spec_helper.rb
106
112
  - spec/subduino/ard_io_spec.rb
113
+ - spec/subduino/cubduino_spec.rb
107
114
  - spec/subduino/parse_spec.rb
108
115
  - spec/subduino/store_spec.rb
109
116
  - spec/subduino_spec.rb
@@ -113,8 +120,8 @@ homepage: http://github.com/nofxx/subduino
113
120
  licenses: []
114
121
 
115
122
  post_install_message:
116
- rdoc_options:
117
- - --charset=UTF-8
123
+ rdoc_options: []
124
+
118
125
  require_paths:
119
126
  - lib
120
127
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -140,9 +147,5 @@ rubygems_version: 1.3.7
140
147
  signing_key:
141
148
  specification_version: 3
142
149
  summary: Arduino Ruby Helpers
143
- test_files:
144
- - spec/subduino_spec.rb
145
- - spec/spec_helper.rb
146
- - spec/subduino/ard_io_spec.rb
147
- - spec/subduino/parse_spec.rb
148
- - spec/subduino/store_spec.rb
150
+ test_files: []
151
+
data/.gitignore DELETED
@@ -1,26 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC
22
- *.log
23
- *.gem
24
- *.rdb
25
- *.cache
26
- webapp/.bundle/*