Skip to content
Browse files

lot of stuff

  • Loading branch information...
1 parent 8457d73 commit 2f0ff1b38d51914be179df5d6c822a251d395881 @nofxx committed
View
2 Rakefile
@@ -6,7 +6,7 @@ begin
Jeweler::Tasks.new do |gem|
gem.name = "subduino"
gem.summary = "Arduino Ruby Helpers"
- gem.description = "Interface, compile, upload, play with arduino/ruby"
+ gem.description = "Interface, compile, upload... Play with arduino on ruby!"
gem.email = "x@nofxx.com"
gem.homepage = "http://github.com/nofxx/subduino"
gem.authors = ["Marcos Piccinini"]
View
40 bin/subduino
@@ -2,45 +2,53 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'rubygems'
require 'optparse'
-require 'subduino'
-require 'subduino/os'
-require 'faye'
+
Debug = false
+if File.exists?(f = ARGV.join)
+ return unless f =~ /\.yml$/
+ puts "Using config file #{f}"
+ AppConfig = YAML.load(File.read(f))["opts"]
+else
+ AppConfig = { :log_interval => 30 }
+end
+
OptionParser.new { |op|
op.on('-d', '--debug') { |d| Debug = true }
+ op.on('-b', '--bauds=BAUDS', Integer) { |b| AppConfig[:bauds] = b }
op.on('-e env') { |val| set :environment, val.to_sym }
op.on('-s server') { |val| set :server, val }
op.on('-p port') { |val| set :port, val.to_i }
-}.parse!(ARGV)
+ op.parse!(ARGV)
+}
+
+require 'subduino'
+require 'subduino/os'
+require 'faye'
+# Cubduino.write("hahah")
+# puts Cubduino.read
# set :run, true
-if Debug
- puts "Starting on #{OS}"
-end
if ARGV.empty? || ARGV.join =~ /yml/
LastRun = {:all => 0}
- if File.exists?(f = ARGV.join)
- puts "Using config file #{f}"
- AppConfig = YAML.load(File.read(f))["opts"]
- else
- AppConfig = { "log_interval" => 30, "bauds" => 115200 }
- end
client = Faye::Client.new('http://localhost:8000/faye')
EM.run do
+ if Debug
+ puts "Starting on #{OS}"
+ end
Subduino.start do |read|
# Subduino::ArdIO.write("hi")
if Debug
- # puts "--------------------- #{Time.now}"
- p read
+ #puts "--------------------- #{Time.now}"
+ #p read
end
- if LastRun[:all] < (Time.now.to_i - AppConfig["log_interval"])
+ if LastRun[:all] < (Time.now.to_i - AppConfig[:log_interval])
LastRun[:all] = Time.now.to_i
client.publish('/stats', 'data' => read)
Subduino::Store.add_csv_to_store(read, true)
View
23 duino/duino.pde
@@ -112,9 +112,11 @@ void read_commands() {
while( Serial.available() && c != '\n' ) { // buffer up a line
c = Serial.read();
if (c == '\n' || c == ',') {
+ Serial.println("<<");
int pin; int val;
pin = (ctoi(cmdbuf[0])*10) + ctoi(cmdbuf[1]);
val = (ctoi(cmdbuf[2])*100) + (ctoi(cmdbuf[3])*10) + ctoi(cmdbuf[4]);
+ Serial.println("<<");
analogWrite(pin, val);
i = 0;
} else if (c == 'x') {
@@ -128,13 +130,20 @@ void read_commands() {
void setup() {
// Atmega defaults INPUT
pinMode(infoPin, OUTPUT);
- pinMode(d2, OUTPUT);
- pinMode(d3, OUTPUT);
- pinMode(d4, OUTPUT);
- pinMode(d5, OUTPUT);
- pinMode(d6, OUTPUT);
- pinMode(d7, OUTPUT);
- pinMode(d10, OUTPUT);
+ pinMode(rxPin, OUTPUT);
+ pinMode(txPin, OUTPUT);
+ for (int k=0;k<=7;k++)
+ pinMode(k,OUTPUT);
+ // pinMode(d2, OUTPUT);
+ // pinMode(d3, OUTPUT);
+ // pinMode(d4, OUTPUT);
+ // pinMode(d5, OUTPUT);
+ // pinMode(d6, OUTPUT);
+ // pinMode(d7, OUTPUT);
+ // pinMode(d10, OUTPUT);
+ // digitalWrite(rxPin, 0); FAIL
+ // digitalWrite(txPin, 0);
+
// attachInterrupt(0, btnLed, CHANGE);
Serial.begin(115200);
View
45 examples/duin
@@ -0,0 +1,45 @@
+#!/usr/bin/env ruby
+
+# usage: usb-power bus/device port on|off
+#
+# example:
+# usb-power 004/006 2 on
+# usb-power 004/006 2 off
+
+require 'usb'
+require 'optparse'
+
+USB_RT_PORT = USB::USB_TYPE_CLASS | USB::USB_RECIP_OTHER
+USB_PORT_FEAT_POWER = 8
+
+ USB.devices.find_all {|d|
+ # p d
+ 0x200 <= d.bcdDevice &&
+ d.bDeviceClass == USB::USB_CLASS_HUB
+ }
+
+# require 'pp'
+
+# def power_on(h, port)
+# h.usb_control_msg(USB_RT_PORT, USB::USB_REQ_SET_FEATURE, USB_PORT_FEAT_POWER, port, "", 0)
+# end
+
+# def power_off(h, port)
+# h.usb_control_msg(USB_RT_PORT, USB::USB_REQ_CLEAR_FEATURE, USB_PORT_FEAT_POWER, port, "", 0)
+# end
+
+# bus_device = ARGV.shift
+# port = ARGV.shift.to_i
+# on_off = ARGV.shift
+
+# %r{/} =~ bus_device
+# bus = $`.to_i
+# device = $'.to_i
+
+# USB.find_bus(bus).find_device(device).open {|h|
+# if on_off == 'off'
+# power_off(h, port)
+# else
+# power_on(h, port)
+# end
+# }
View
187 ext/subduino/Makefile
@@ -0,0 +1,187 @@
+
+SHELL = /bin/sh
+
+#### Start of system configuration section. ####
+
+srcdir = .
+topdir = /usr/include/ruby-1.9.1
+hdrdir = /usr/include/ruby-1.9.1
+arch_hdrdir = /usr/include/ruby-1.9.1/$(arch)
+VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
+prefix = $(DESTDIR)/usr
+rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
+exec_prefix = $(prefix)
+vendorhdrdir = $(rubyhdrdir)/vendor_ruby
+sitehdrdir = $(rubyhdrdir)/site_ruby
+rubyhdrdir = $(includedir)/$(RUBY_BASE_NAME)-$(ruby_version)
+vendordir = $(rubylibprefix)/vendor_ruby
+sitedir = $(rubylibprefix)/site_ruby
+ridir = $(datarootdir)/$(RI_BASE_NAME)
+mandir = $(datarootdir)/man
+localedir = $(datarootdir)/locale
+libdir = $(exec_prefix)/lib
+psdir = $(docdir)
+pdfdir = $(docdir)
+dvidir = $(docdir)
+htmldir = $(docdir)
+infodir = $(datarootdir)/info
+docdir = $(datarootdir)/doc/$(PACKAGE)
+oldincludedir = $(DESTDIR)/usr/include
+includedir = $(prefix)/include
+localstatedir = $(prefix)/var
+sharedstatedir = $(prefix)/com
+sysconfdir = $(prefix)/etc
+datadir = $(datarootdir)
+datarootdir = $(prefix)/share
+libexecdir = $(exec_prefix)/libexec
+sbindir = $(exec_prefix)/sbin
+bindir = $(exec_prefix)/bin
+rubylibdir = $(rubylibprefix)/$(ruby_version)
+archdir = $(rubylibdir)/$(arch)
+sitelibdir = $(sitedir)/$(ruby_version)
+sitearchdir = $(sitelibdir)/$(sitearch)
+vendorlibdir = $(vendordir)/$(ruby_version)
+vendorarchdir = $(vendorlibdir)/$(sitearch)
+
+CC = gcc
+CXX = g++
+LIBRUBY = $(LIBRUBY_SO)
+LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
+LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME)
+LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static
+OUTFLAG = -o
+COUTFLAG = -o
+
+RUBY_EXTCONF_H =
+cflags = $(optflags) $(debugflags) $(warnflags)
+optflags = -O3
+debugflags = -ggdb
+warnflags = -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long
+CFLAGS = -fPIC -march=x86-64 -mtune=generic -O3 -pipe -fPIC -std=c99 -Wall -I.
+INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
+DEFS =
+CPPFLAGS = $(DEFS) $(cppflags)
+CXXFLAGS = $(CFLAGS) -march=x86-64 -mtune=generic -O2 -pipe
+ldflags = -L. -rdynamic -Wl,-export-dynamic
+dldflags =
+ARCH_FLAG =
+DLDFLAGS = $(ldflags) $(dldflags)
+LDSHARED = $(CC) -shared
+LDSHAREDXX = $(CXX) -shared
+AR = ar
+EXEEXT =
+
+RUBY_BASE_NAME = ruby
+RUBY_INSTALL_NAME = ruby
+RUBY_SO_NAME = ruby
+arch = x86_64-linux
+sitearch = $(arch)
+ruby_version = 1.9.1
+ruby = /usr/bin/ruby
+RUBY = $(ruby)
+RM = rm -f
+RM_RF = $(RUBY) -run -e rm -- -rf
+RMDIRS = $(RUBY) -run -e rmdir -- -p
+MAKEDIRS = /bin/mkdir -p
+INSTALL = /bin/install -c
+INSTALL_PROG = $(INSTALL) -m 0755
+INSTALL_DATA = $(INSTALL) -m 644
+COPY = cp
+
+#### End of system configuration section. ####
+
+preload =
+
+libpath = . $(libdir)
+LIBPATH = -L. -L$(libdir)
+DEFFILE =
+
+CLEANFILES = mkmf.log
+DISTCLEANFILES =
+DISTCLEANDIRS =
+
+extout =
+extout_prefix =
+target_prefix =
+LOCAL_LIBS =
+LIBS = $(LIBRUBYARG_SHARED) -lpthread -lrt -ldl -lcrypt -lm -lc
+SRCS = cubduino.c
+OBJS = cubduino.o
+TARGET = cubduino
+DLLIB = $(TARGET).so
+EXTSTATIC =
+STATIC_LIB =
+
+BINDIR = $(bindir)
+RUBYCOMMONDIR = $(sitedir)$(target_prefix)
+RUBYLIBDIR = $(sitelibdir)$(target_prefix)
+RUBYARCHDIR = $(sitearchdir)$(target_prefix)
+HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
+ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
+
+TARGET_SO = $(DLLIB)
+CLEANLIBS = $(TARGET).so
+CLEANOBJS = *.o *.bak
+
+all: $(DLLIB)
+static: $(STATIC_LIB)
+.PHONY: all install static install-so install-rb
+.PHONY: clean clean-so clean-rb
+
+clean-rb-default::
+clean-rb::
+clean-so::
+clean: clean-so clean-rb-default clean-rb
+ @-$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES)
+
+distclean-rb-default::
+distclean-rb::
+distclean-so::
+distclean: clean distclean-so distclean-rb-default distclean-rb
+ @-$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
+ @-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
+ @-$(RMDIRS) $(DISTCLEANDIRS)
+
+realclean: distclean
+install: install-so install-rb
+
+install-so: $(RUBYARCHDIR)
+install-so: $(RUBYARCHDIR)/$(DLLIB)
+$(RUBYARCHDIR)/$(DLLIB): $(DLLIB)
+ @-$(MAKEDIRS) $(@D)
+ $(INSTALL_PROG) $(DLLIB) $(@D)
+install-rb: pre-install-rb install-rb-default
+install-rb-default: pre-install-rb-default
+pre-install-rb: Makefile
+pre-install-rb-default: Makefile
+$(RUBYARCHDIR):
+ $(MAKEDIRS) $@
+
+site-install: site-install-so site-install-rb
+site-install-so: install-so
+site-install-rb: install-rb
+
+.SUFFIXES: .c .m .cc .cxx .cpp .C .o
+
+.cc.o:
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
+
+.cxx.o:
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
+
+.cpp.o:
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
+
+.C.o:
+ $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
+
+.c.o:
+ $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
+
+$(DLLIB): $(OBJS) Makefile
+ @-$(RM) $(@)
+ $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
+
+
+
+$(OBJS): $(hdrdir)/ruby.h $(hdrdir)/ruby/defines.h $(arch_hdrdir)/ruby/config.h
View
242 ext/subduino/cubduino.c
@@ -0,0 +1,242 @@
+/*
+ * Arduino-serial
+ * --------------
+ *
+ * A simple command-line example program showing how a computer can
+ * communicate with an Arduino board. Works on any POSIX system (Mac/Unix/PC)
+ *
+ *
+ * Compile with something like:
+ * gcc -o arduino-serial arduino-serial.c
+ *
+ * Created 5 December 2006
+ * Copyleft (c) 2006, Tod E. Kurt, tod@todbot.com
+ * http://todbot.com/blog/
+ *
+ *
+ * Updated 8 December 2006:
+ * Justin McBride discoevered B14400 & B28800 aren't in Linux's termios.h.
+ * I've included his patch, but commented out for now. One really needs a
+ * real make system when doing cross-platform C and I wanted to avoid that
+ * for this little program. Those baudrates aren't used much anyway. :)
+ *
+ * Updated 26 December 2007:
+ * Added ability to specify a delay (so you can wait for Arduino Diecimila)
+ * Added ability to send a binary byte number
+ *
+ * Update 31 August 2008:
+ * Added patch to clean up odd baudrates from Andy at hexapodia.org
+ *
+ */
+
+#include "cubduino.h"
+
+
+
+// Ruby 1.x compatibility
+#ifndef RSTRING_PTR
+#define RSTRING_PTR(s) (RSTRING(s)->ptr)
+#endif
+#ifndef RSTRING_LEN
+#define RSTRING_LEN(s) (RSTRING(s)->len)
+#endif
+
+VALUE Cubduino;
+
+VALUE sBaud, sDataBits, sStopBits, sParity; /* strings */
+VALUE sRts, sDtr, sCts, sDsr, sDcd, sRi;
+
+
+void Init_cubduino();
+//VALUE method_cwrite(VALUE self);
+
+
+
+void usage(void);
+int serialport_init(const char* serialport, int baud);
+int serialport_writebyte(int fd, uint8_t b);
+int serialport_write(int fd, const char* str);
+int serialport_read_until(int fd, char* buf, char until);
+
+
+int main(int argc, char *argv[])
+{
+ int rc,n;
+ int fd = 0;
+ int baudrate = B9600; // default
+ char serialport[256];
+ char buf[256];
+
+ /* parse options */
+ int option_index = 0, opt;
+ static struct option loptions[] = {
+ {"help", no_argument, 0, 'h'},
+ {"port", required_argument, 0, 'p'},
+ {"baud", required_argument, 0, 'b'},
+ {"send", required_argument, 0, 's'},
+ {"receive", no_argument, 0, 'r'},
+ {"num", required_argument, 0, 'n'},
+ {"delay", required_argument, 0, 'd'}
+ };
+
+ while(1) {
+ opt = getopt_long (argc, argv, "hp:b:s:rn:d:",
+ loptions, &option_index);
+ if (opt==-1) break;
+ switch (opt) {
+ case '0': break;
+ case 'd':
+ n = strtol(optarg,NULL,10);
+ usleep(n * 1000 ); // sleep milliseconds
+ break;
+ case 'h':
+ usage();
+ break;
+ case 'b':
+ baudrate = strtol(optarg,NULL,10);
+ break;
+ case 'p':
+ strcpy(serialport,optarg);
+ fd = serialport_init(optarg, baudrate);
+ if(fd==-1) return -1;
+ break;
+ case 'n':
+ n = strtol(optarg, NULL, 10); // convert string to number
+ rc = serialport_writebyte(fd, (uint8_t)n);
+ if(rc==-1) return -1;
+ break;
+ case 's':
+ strcpy(buf,optarg);
+ rc = serialport_write(fd, buf);
+ if(rc==-1) return -1;
+ break;
+ case 'r':
+ serialport_read_until(fd, buf, '\n');
+ printf("read: %s\n",buf);
+ break;
+ }
+ }
+
+ exit(EXIT_SUCCESS);
+} // end main
+
+int serialport_writebyte( int fd, uint8_t b)
+{
+ int n = write(fd,&b,1);
+ if( n!=1)
+ return -1;
+ return 0;
+}
+
+int serialport_write(int fd, const char* str)
+{
+ int len = strlen(str);
+ int n = write(fd, str, len);
+ if( n!=len )
+ return -1;
+ return 0;
+}
+
+int serialport_read_until(int fd, char* buf, char until)
+{
+ char b[1];
+ int i=0;
+ do {
+ int n = read(fd, b, 1); // read a char at a time
+ if( n==-1) return -1; // couldn't read
+ if( n==0 ) {
+ usleep( 10 * 1000 ); // wait 10 msec try again
+ continue;
+ }
+ buf[i] = b[0]; i++;
+ } while( b[0] != until );
+
+ buf[i] = 0; // null terminate the string
+ return 0;
+}
+
+// takes the string name of the serial port (e.g. "/dev/tty.usbserial","COM1")
+// and a baud rate (bps) and connects to that port at that speed and 8N1.
+// opens the port in fully raw mode so you can send binary data.
+// returns valid fd, or -1 on error
+int serialport_init(const char* serialport, int baud)
+{
+ struct termios toptions;
+ int fd;
+
+ //fprintf(stderr,"init_serialport: opening port %s @ %d bps\n",
+ // serialport,baud);
+
+ fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
+ if (fd == -1) {
+ perror("init_serialport: Unable to open port ");
+ return -1;
+ }
+
+ if (tcgetattr(fd, &toptions) < 0) {
+ perror("init_serialport: Couldn't get term attributes");
+ return -1;
+ }
+ speed_t brate = baud; // let you override switch below if needed
+ switch(baud) {
+ case 4800: brate=B4800; break;
+ case 9600: brate=B9600; break;
+#ifdef B14400
+ case 14400: brate=B14400; break;
+#endif
+ case 19200: brate=B19200; break;
+#ifdef B28800
+ case 28800: brate=B28800; break;
+#endif
+ case 38400: brate=B38400; break;
+ case 57600: brate=B57600; break;
+ case 115200: brate=B115200; break;
+ }
+ cfsetispeed(&toptions, brate);
+ cfsetospeed(&toptions, brate);
+
+ // 8N1
+ toptions.c_cflag &= ~PARENB;
+ toptions.c_cflag &= ~CSTOPB;
+ toptions.c_cflag &= ~CSIZE;
+ toptions.c_cflag |= CS8;
+ // no flow control
+ toptions.c_cflag &= ~CRTSCTS;
+
+ toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
+ toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
+
+ toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
+ toptions.c_oflag &= ~OPOST; // make raw
+
+ // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
+ toptions.c_cc[VMIN] = 0;
+ toptions.c_cc[VTIME] = 20;
+
+ if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
+ perror("init_serialport: Couldn't set term attributes");
+ return -1;
+ }
+
+ return fd;
+}
+
+static VALUE method_write(VALUE class, VALUE arg) {
+ printf("%s", RSTRING_PTR(arg));
+ return Qnil;
+}
+
+static VALUE method_read() {
+ int fd = 0;
+ int baudrate = 9600;
+ fd = serialport_init(optarg, baudrate);
+ char buf[256];
+ serialport_read_until(fd, buf, '\n');
+ return rb_str_new2(buf);
+}
+
+void Init_cubduino() {
+ Cubduino = rb_define_module("Cubduino");
+ rb_define_singleton_method(Cubduino, "read", method_write, 0);
+ rb_define_singleton_method(Cubduino, "write", method_write, 1);
+}
View
12 ext/subduino/cubduino.h
@@ -0,0 +1,12 @@
+#include <ruby.h>
+
+#include <stdio.h> /* Standard input/output definitions */
+#include <stdlib.h>
+#include <stdint.h> /* Standard types */
+#include <string.h> /* String function definitions */
+#include <unistd.h> /* UNIX standard function definitions */
+#include <fcntl.h> /* File control definitions */
+#include <errno.h> /* Error number definitions */
+#include <termios.h> /* POSIX terminal control definitions */
+#include <sys/ioctl.h>
+#include <getopt.h>
View
BIN ext/subduino/cubduino.o
Binary file not shown.
View
BIN ext/subduino/cubduino.so
Binary file not shown.
View
38 ext/subduino/extconf.rb
@@ -0,0 +1,38 @@
+require 'mkmf'
+
+def crash(s)
+ puts "--------------------------------------------------"
+ puts " extconf failure: #{s}"
+ puts "--------------------------------------------------"
+ exit 1
+end
+
+unless find_executable("pkg-config")
+ crash("pkg-config needed")
+end
+
+$CFLAGS += " -std=c99 -Wall -I. " #+ `pkg-config --cflags glib-2.0`.strip
+# $LIBS += " " + `pkg-config --libs glib-2.0`
+# $CFLAGS += " -DOS_#{os.upcase}"
+# $CFLAGS += " -DRUBY_1_9" if RUBY_VERSION =~ /^1\.9/
+
+
+# unless have_library('glib-2.0')
+# crash "libglib-2.0 needed"
+# end
+
+if ARGV.include?("-d")
+ $CFLAGS += " -D CUBDUINO_DEBUG"
+end
+
+if ARGV.include?("-O0")
+ $CFLAGS.gsub!(/-O./, "-O0")
+else
+ $CFLAGS.gsub!(/-O./, "-O3")
+end
+
+if ARGV.include?("-gdb")
+ $CFLAGS += " -g -gdwarf-2 -g3"
+end
+
+create_makefile("cubduino")
View
5 lib/subduino.rb
@@ -4,6 +4,8 @@
#
#
#
+require File.dirname(__FILE__) + '/../ext/subduino/cubduino'
+#require "subduino/cubduino"
require 'serialport'
require 'eventmachine'
require 'stringio'
@@ -20,8 +22,7 @@
Thread.current.abort_on_exception = false
module Subduino
- Log = Logger.new("out.log")
- BAUDS = 115200 #BAUDS = 9600
+ Log = Logger.new(Debug ? STDOUT : "out.log")
# BAUDS = [300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200]
Sensors = [:temp, :lux]
# DATA_BITS = 8
View
14 lib/subduino/ard_io.rb
@@ -9,7 +9,7 @@ class << self
# Direct access to the SerialPort instance.
#
def sp
- @sp ||= SerialPort.new(Arduino.find_usb, BAUDS) #, DATA_BITS, DATA_STOP, parity)
+ @sp ||= SerialPort.new(Arduino.find_usb, AppConfig[:bauds] || 115200) #, DATA_BITS, DATA_STOP, parity)
# @sp.read_timeout = 10;# @sp.write_timeout = 10
end
@@ -24,6 +24,7 @@ def read(&proc)
Log.info "[USB] Starting USB Connect..." + sp.get_modem_params.map { |k,v| "#{k}: #{v}" }.join(" ")
Log.info "[USB] Read Timeout #{sp.read_timeout}" # {sp.write_timeout}"
+ if sp
@iothread ||= Thread.new do
Thread.current.abort_on_exception = false
icache = []
@@ -31,8 +32,11 @@ def read(&proc)
begin
while char = sp.getc
if !char.valid_encoding?
- puts "Bad char #{char}"
+ bytes = char.bytes.to_a
+ hexes = bytes.map { |b| b.to_s(16) }
+ puts " - Bad char #{char} - (Hex: #{char.unpack('H')} | Byte(s) #{bytes} | Hexe(s) #{hexes}"
elsif char !~ /\n|\r/
+ # print char if Debug
icache << char
else
data = icache.join(""); icache = []
@@ -49,6 +53,8 @@ def read(&proc)
end
end
end
+ end
+
end
#
@@ -60,8 +66,8 @@ def read(&proc)
#
def write(msg)
Log.info "[IO TX] #{msg}"
- txt = msg.gsub("\r", "\n")
- txt += "\n" unless txt =~ /^\\n/
+ txt = msg.gsub("\r|\n", "")
+ # txt += "\n" unless txt =~ /^\\n/
puts "=> Sending #{txt.inspect}" if Debug
sp.puts(msg)
end
View
7 lib/subduino/store.rb
@@ -14,7 +14,7 @@ def redis
end
def read(key)
- redis.get key
+ redis.get key.to_s
end
def timestamp
@@ -23,10 +23,9 @@ def timestamp
def write(k, v, stamp = false)
return unless redis #.connected?
- stamp ? redis.rpush("log:inputs:#{k}", "#{timestamp}:#{v}") : redis.set("now:inputs:"+k, v)
+ stamp ? redis.rpush("log:inputs:#{k}", "#{timestamp}:#{v}") : redis.set("now:inputs:#{k}", v)
end
-
def add_to_store(key, val=nil)
if val
write key, val
@@ -38,7 +37,7 @@ def add_to_store(key, val=nil)
end
def add_csv_to_store(csv, stamp = false)
- Log.info "[STORE] CSV #{Time.now.to_i}"
+ # Log.info "[STORE] CSV #{Time.now.to_i}"
csv.split(",").each do |d|
comm, value = d.split(":")
write(comm, value, stamp)
View
1 spec/subduino/ard_io_spec.rb
@@ -3,6 +3,7 @@
describe ArdIO do
it "should start serial port" do
+ Arduino.stub!(:find_usb).and_return("/dev/ttyUSB0")
SerialPort.should_receive(:new).with("/dev/ttyUSB0", 115200)
ArdIO.sp
end
View
10 spec/subduino/cubduino_spec.rb
@@ -0,0 +1,10 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe "Cubduino" do
+
+ it "should define module" do
+ Subduino.const_defined?("Cubduino").should be_true
+ end
+
+
+end
View
4 spec/subduino/store_spec.rb
@@ -5,11 +5,11 @@
it "should write things" do
Store.flush
Store.write(:temp, 18)
- Store.count.should eql(2)
+ Store.count.should eql(1)
end
it "should read" do
- Store.read(:temp).should eql("18")
+ Store.read("now:inputs:temp").should eql("18")
end
it "should read array"
View
6 subduino.gemspec
@@ -9,10 +9,11 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Marcos Piccinini"]
- s.date = %q{2010-09-30}
+ s.date = %q{2010-10-21}
s.description = %q{Interface, compile, upload, play with arduino/ruby}
s.email = %q{x@nofxx.com}
s.executables = ["subduino-cli", "subduino"]
+ s.extensions = ["ext/subduino/extconf.rb"]
s.files = [
".document",
".gitignore",
@@ -65,7 +66,8 @@ Gem::Specification.new do |s|
"spec/spec_helper.rb",
"spec/subduino/ard_io_spec.rb",
"spec/subduino/parse_spec.rb",
- "spec/subduino/store_spec.rb"
+ "spec/subduino/store_spec.rb",
+ "spec/subduino/cubduino_spec.rb"
]
if s.respond_to? :specification_version then

0 comments on commit 2f0ff1b

Please sign in to comment.
Something went wrong with that request. Please try again.