subduino 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (267) hide show
  1. data/VERSION +1 -1
  2. data/bin/subduino +18 -5
  3. data/duino/Makefile +2 -3
  4. data/duino/duino.pde +97 -66
  5. data/lib/subduino/ard_io.rb +33 -14
  6. data/lib/subduino/ard_ps.rb +47 -24
  7. data/lib/subduino/os.rb +49 -0
  8. data/lib/subduino/parse/bool.rb +1 -1
  9. data/lib/subduino/parse/energy.rb +18 -0
  10. data/lib/subduino/parse/flow.rb +13 -0
  11. data/lib/subduino/parse/lux.rb +1 -1
  12. data/lib/subduino/parse/presence.rb +9 -0
  13. data/lib/subduino/parse/pressure.rb +13 -0
  14. data/lib/subduino/parse/temp.rb +1 -1
  15. data/lib/subduino/parse.rb +3 -3
  16. data/lib/subduino/scaffold/Makefile +72 -73
  17. data/lib/subduino/scaffold/scaffold.pde +77 -0
  18. data/lib/subduino/store.rb +1 -1
  19. data/spec/subduino/parse_spec.rb +1 -1
  20. data/subduino.gemspec +8 -249
  21. metadata +8 -249
  22. data/webapp/Gemfile +0 -8
  23. data/webapp/Gemfile.lock +0 -14
  24. data/webapp/Rakefile +0 -49
  25. data/webapp/Readme.textile +0 -45
  26. data/webapp/VERSION +0 -1
  27. data/webapp/config.yml +0 -20
  28. data/webapp/lib/app.rb +0 -103
  29. data/webapp/lib/duino.rb +0 -206
  30. data/webapp/lib/environment.rb +0 -36
  31. data/webapp/lib/messenger.rb +0 -16
  32. data/webapp/public/app.css +0 -119
  33. data/webapp/public/app.js +0 -34
  34. data/webapp/public/custom.js +0 -187
  35. data/webapp/public/date_input.css +0 -149
  36. data/webapp/public/facebox.css +0 -83
  37. data/webapp/public/facebox.js +0 -319
  38. data/webapp/public/favicon.ico +0 -0
  39. data/webapp/public/faye.js +0 -1
  40. data/webapp/public/icons/alarm-clock-blue.png +0 -0
  41. data/webapp/public/icons/alarm-clock.png +0 -0
  42. data/webapp/public/icons/balloon-left.png +0 -0
  43. data/webapp/public/icons/bandaid.png +0 -0
  44. data/webapp/public/icons/bell-disable.png +0 -0
  45. data/webapp/public/icons/bell.png +0 -0
  46. data/webapp/public/icons/big_icon.png +0 -0
  47. data/webapp/public/icons/bomb.png +0 -0
  48. data/webapp/public/icons/bookmark.png +0 -0
  49. data/webapp/public/icons/box-label.png +0 -0
  50. data/webapp/public/icons/brightness-control-up.png +0 -0
  51. data/webapp/public/icons/brightness-control.png +0 -0
  52. data/webapp/public/icons/brightness-small.png +0 -0
  53. data/webapp/public/icons/broom.png +0 -0
  54. data/webapp/public/icons/bug.png +0 -0
  55. data/webapp/public/icons/calculator.png +0 -0
  56. data/webapp/public/icons/calendar-day.png +0 -0
  57. data/webapp/public/icons/camera.png +0 -0
  58. data/webapp/public/icons/cards-address.png +0 -0
  59. data/webapp/public/icons/chart.png +0 -0
  60. data/webapp/public/icons/clock-select.png +0 -0
  61. data/webapp/public/icons/color.png +0 -0
  62. data/webapp/public/icons/compass.png +0 -0
  63. data/webapp/public/icons/control-power-small.png +0 -0
  64. data/webapp/public/icons/control-power.png +0 -0
  65. data/webapp/public/icons/control-record-small.png +0 -0
  66. data/webapp/public/icons/cpus.png +0 -0
  67. data/webapp/public/icons/credit-card.png +0 -0
  68. data/webapp/public/icons/cross-small.png +0 -0
  69. data/webapp/public/icons/current.png +0 -0
  70. data/webapp/public/icons/dashboard.png +0 -0
  71. data/webapp/public/icons/database.png +0 -0
  72. data/webapp/public/icons/databases.png +0 -0
  73. data/webapp/public/icons/day.png +0 -0
  74. data/webapp/public/icons/door-big.png +0 -0
  75. data/webapp/public/icons/door-open-in.png +0 -0
  76. data/webapp/public/icons/door-open-out.png +0 -0
  77. data/webapp/public/icons/door-open.png +0 -0
  78. data/webapp/public/icons/door.png +0 -0
  79. data/webapp/public/icons/drive-globe.png +0 -0
  80. data/webapp/public/icons/equalizer.png +0 -0
  81. data/webapp/public/icons/exclamation-diamond.png +0 -0
  82. data/webapp/public/icons/exclamation.png +0 -0
  83. data/webapp/public/icons/eye-disable.png +0 -0
  84. data/webapp/public/icons/eye.png +0 -0
  85. data/webapp/public/icons/false.png +0 -0
  86. data/webapp/public/icons/favicon.png +0 -0
  87. data/webapp/public/icons/gear-small.png +0 -0
  88. data/webapp/public/icons/gear.png +0 -0
  89. data/webapp/public/icons/groups.png +0 -0
  90. data/webapp/public/icons/heart.png +0 -0
  91. data/webapp/public/icons/heart_empty.png +0 -0
  92. data/webapp/public/icons/info.png +0 -0
  93. data/webapp/public/icons/irrigation.png +0 -0
  94. data/webapp/public/icons/key.png +0 -0
  95. data/webapp/public/icons/lamp-big.png +0 -0
  96. data/webapp/public/icons/lightbulb.png +0 -0
  97. data/webapp/public/icons/lightbulb_off.png +0 -0
  98. data/webapp/public/icons/lightning-disable.png +0 -0
  99. data/webapp/public/icons/lightning-small.png +0 -0
  100. data/webapp/public/icons/lightning.png +0 -0
  101. data/webapp/public/icons/lock-unlock.png +0 -0
  102. data/webapp/public/icons/lock.png +0 -0
  103. data/webapp/public/icons/locked.png +0 -0
  104. data/webapp/public/icons/lux.png +0 -0
  105. data/webapp/public/icons/marker.png +0 -0
  106. data/webapp/public/icons/media-player-phone.png +0 -0
  107. data/webapp/public/icons/megaphone.png +0 -0
  108. data/webapp/public/icons/mem.png +0 -0
  109. data/webapp/public/icons/microphone.png +0 -0
  110. data/webapp/public/icons/monitor.png +0 -0
  111. data/webapp/public/icons/navigation.png +0 -0
  112. data/webapp/public/icons/night.png +0 -0
  113. data/webapp/public/icons/noise.png +0 -0
  114. data/webapp/public/icons/off.png +0 -0
  115. data/webapp/public/icons/on.png +0 -0
  116. data/webapp/public/icons/onoff.png +0 -0
  117. data/webapp/public/icons/pin.png +0 -0
  118. data/webapp/public/icons/plug--exclamation.png +0 -0
  119. data/webapp/public/icons/plug-disable.png +0 -0
  120. data/webapp/public/icons/plug.png +0 -0
  121. data/webapp/public/icons/rain.png +0 -0
  122. data/webapp/public/icons/refrigeration.png +0 -0
  123. data/webapp/public/icons/restart.png +0 -0
  124. data/webapp/public/icons/ruby.png +0 -0
  125. data/webapp/public/icons/server.png +0 -0
  126. data/webapp/public/icons/shield-disable.png +0 -0
  127. data/webapp/public/icons/shield.png +0 -0
  128. data/webapp/public/icons/socket--exclamation.png +0 -0
  129. data/webapp/public/icons/socket-disable.png +0 -0
  130. data/webapp/public/icons/socket.png +0 -0
  131. data/webapp/public/icons/start.png +0 -0
  132. data/webapp/public/icons/stop.png +0 -0
  133. data/webapp/public/icons/switch--exclamation.png +0 -0
  134. data/webapp/public/icons/switch-disable.png +0 -0
  135. data/webapp/public/icons/switch-small.png +0 -0
  136. data/webapp/public/icons/switch.png +0 -0
  137. data/webapp/public/icons/target.png +0 -0
  138. data/webapp/public/icons/television-off.png +0 -0
  139. data/webapp/public/icons/television.png +0 -0
  140. data/webapp/public/icons/temp.png +0 -0
  141. data/webapp/public/icons/terminal.png +0 -0
  142. data/webapp/public/icons/tick-small.png +0 -0
  143. data/webapp/public/icons/traffic-light-off.png +0 -0
  144. data/webapp/public/icons/traffic-light.png +0 -0
  145. data/webapp/public/icons/traffic.png +0 -0
  146. data/webapp/public/icons/true.png +0 -0
  147. data/webapp/public/icons/umbrella.png +0 -0
  148. data/webapp/public/icons/unlocked.png +0 -0
  149. data/webapp/public/icons/unmonitor.png +0 -0
  150. data/webapp/public/icons/unmonitored.png +0 -0
  151. data/webapp/public/icons/users.png +0 -0
  152. data/webapp/public/icons/vcard.png +0 -0
  153. data/webapp/public/icons/wall.png +0 -0
  154. data/webapp/public/icons/wall_brick.png +0 -0
  155. data/webapp/public/icons/wall_disable.png +0 -0
  156. data/webapp/public/icons/wand-disable.png +0 -0
  157. data/webapp/public/icons/wand.png +0 -0
  158. data/webapp/public/icons/warn.png +0 -0
  159. data/webapp/public/icons/weather_clouds.png +0 -0
  160. data/webapp/public/icons/weather_cloudy.png +0 -0
  161. data/webapp/public/icons/weather_lightning.png +0 -0
  162. data/webapp/public/icons/weather_rain.png +0 -0
  163. data/webapp/public/icons/weather_snow.png +0 -0
  164. data/webapp/public/icons/weather_sun.png +0 -0
  165. data/webapp/public/icons/wrench-screwdriver.png +0 -0
  166. data/webapp/public/icons/wrench.png +0 -0
  167. data/webapp/public/images/ajax-loader.gif +0 -0
  168. data/webapp/public/images/b.png +0 -0
  169. data/webapp/public/images/bendl.gif +0 -0
  170. data/webapp/public/images/bendr.gif +0 -0
  171. data/webapp/public/images/bendsb.gif +0 -0
  172. data/webapp/public/images/bg.jpg +0 -0
  173. data/webapp/public/images/bhead.gif +0 -0
  174. data/webapp/public/images/bheadl.gif +0 -0
  175. data/webapp/public/images/bheadr.gif +0 -0
  176. data/webapp/public/images/bl.png +0 -0
  177. data/webapp/public/images/bnd.gif +0 -0
  178. data/webapp/public/images/br.png +0 -0
  179. data/webapp/public/images/bread.gif +0 -0
  180. data/webapp/public/images/btnb.gif +0 -0
  181. data/webapp/public/images/btnb_.gif +0 -0
  182. data/webapp/public/images/btnm.gif +0 -0
  183. data/webapp/public/images/btnm_.gif +0 -0
  184. data/webapp/public/images/btns.gif +0 -0
  185. data/webapp/public/images/btns_.gif +0 -0
  186. data/webapp/public/images/cal.jpg +0 -0
  187. data/webapp/public/images/close.png +0 -0
  188. data/webapp/public/images/closelabel.gif +0 -0
  189. data/webapp/public/images/error.gif +0 -0
  190. data/webapp/public/images/ft.gif +0 -0
  191. data/webapp/public/images/hdr.gif +0 -0
  192. data/webapp/public/images/hdrl.gif +0 -0
  193. data/webapp/public/images/hdrr.gif +0 -0
  194. data/webapp/public/images/hld.jpg +0 -0
  195. data/webapp/public/images/imgb.gif +0 -0
  196. data/webapp/public/images/imgo.gif +0 -0
  197. data/webapp/public/images/imgt.gif +0 -0
  198. data/webapp/public/images/info.gif +0 -0
  199. data/webapp/public/images/jquery.wysiwyg.gif +0 -0
  200. data/webapp/public/images/li.gif +0 -0
  201. data/webapp/public/images/mbg.png +0 -0
  202. data/webapp/public/images/nsp.gif +0 -0
  203. data/webapp/public/images/phs.gif +0 -0
  204. data/webapp/public/images/sdd.jpg +0 -0
  205. data/webapp/public/images/sdd_.jpg +0 -0
  206. data/webapp/public/images/sidebar.gif +0 -0
  207. data/webapp/public/images/sorta.gif +0 -0
  208. data/webapp/public/images/sortd.gif +0 -0
  209. data/webapp/public/images/srch.gif +0 -0
  210. data/webapp/public/images/srch_.gif +0 -0
  211. data/webapp/public/images/success.gif +0 -0
  212. data/webapp/public/images/thumb1.jpg +0 -0
  213. data/webapp/public/images/thumb2.jpg +0 -0
  214. data/webapp/public/images/thumb3.jpg +0 -0
  215. data/webapp/public/images/tiny.gif +0 -0
  216. data/webapp/public/images/tiny_.gif +0 -0
  217. data/webapp/public/images/tl.png +0 -0
  218. data/webapp/public/images/tr.png +0 -0
  219. data/webapp/public/images/upload.gif +0 -0
  220. data/webapp/public/images/warning.gif +0 -0
  221. data/webapp/public/iui/backButton.png +0 -0
  222. data/webapp/public/iui/blueButton.png +0 -0
  223. data/webapp/public/iui/cancel.png +0 -0
  224. data/webapp/public/iui/grayButton.png +0 -0
  225. data/webapp/public/iui/greenButton.png +0 -0
  226. data/webapp/public/iui/iui-logo-touch-icon.png +0 -0
  227. data/webapp/public/iui/iui.css +0 -396
  228. data/webapp/public/iui/iui.js +0 -511
  229. data/webapp/public/iui/iuix.css +0 -1
  230. data/webapp/public/iui/iuix.js +0 -1
  231. data/webapp/public/iui/listArrow.png +0 -0
  232. data/webapp/public/iui/listArrowSel.png +0 -0
  233. data/webapp/public/iui/listGroup.png +0 -0
  234. data/webapp/public/iui/loading.gif +0 -0
  235. data/webapp/public/iui/pinstripes.png +0 -0
  236. data/webapp/public/iui/redButton.png +0 -0
  237. data/webapp/public/iui/selection.png +0 -0
  238. data/webapp/public/iui/thumb.png +0 -0
  239. data/webapp/public/iui/toggle.png +0 -0
  240. data/webapp/public/iui/toggleOn.png +0 -0
  241. data/webapp/public/iui/toolButton.png +0 -0
  242. data/webapp/public/iui/toolbar.png +0 -0
  243. data/webapp/public/iui/whiteButton.png +0 -0
  244. data/webapp/public/iui/yellowButton.png +0 -0
  245. data/webapp/public/jquery.img.preload.js +0 -152
  246. data/webapp/public/jquery.js +0 -154
  247. data/webapp/public/jquery.sparkline.js +0 -85
  248. data/webapp/public/jquery.tablesorter.min.js +0 -2
  249. data/webapp/public/jquery.visualize.js +0 -452
  250. data/webapp/public/jquery.wysiwyg.css +0 -56
  251. data/webapp/public/layout.css +0 -33
  252. data/webapp/public/right.js +0 -9
  253. data/webapp/public/rt.js +0 -7
  254. data/webapp/public/sparkline.js +0 -85
  255. data/webapp/public/style.css +0 -881
  256. data/webapp/public/visualize.css +0 -32
  257. data/webapp/spec/duino_spec.rb +0 -8
  258. data/webapp/spec/spec_helper.rb +0 -10
  259. data/webapp/views/command.haml +0 -4
  260. data/webapp/views/icon.haml +0 -6
  261. data/webapp/views/index.haml +0 -367
  262. data/webapp/views/layout.haml +0 -88
  263. data/webapp/views/mobile.haml +0 -21
  264. data/webapp/views/switch.haml +0 -56
  265. data/webapp/views/template.haml +0 -676
  266. data/webapp/views/top.haml +0 -4
  267. data/webapp/views/watch.haml +0 -26
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
data/bin/subduino CHANGED
@@ -3,16 +3,23 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'rubygems'
4
4
  require 'optparse'
5
5
  require 'subduino'
6
+ require 'subduino/os'
6
7
  require 'faye'
7
8
 
9
+ Debug = false
10
+
8
11
  OptionParser.new { |op|
12
+ op.on('-d', '--debug') { |d| Debug = true }
9
13
  op.on('-e env') { |val| set :environment, val.to_sym }
10
14
  op.on('-s server') { |val| set :server, val }
11
15
  op.on('-p port') { |val| set :port, val.to_i }
12
- }.parse!(ARGV.dup)
16
+ }.parse!(ARGV)
13
17
 
14
18
  # set :run, true
15
19
 
20
+ if Debug
21
+ puts "Starting on #{OS}"
22
+ end
16
23
 
17
24
  if ARGV.empty? || ARGV.join =~ /yml/
18
25
  LastRun = {:all => 0}
@@ -20,7 +27,7 @@ if ARGV.empty? || ARGV.join =~ /yml/
20
27
  puts "Using config file #{f}"
21
28
  AppConfig = YAML.load(File.read(f))["opts"]
22
29
  else
23
- AppConfig = { "log_interval" => 30, "bauds" => 57600 }
30
+ AppConfig = { "log_interval" => 30, "bauds" => 115200 }
24
31
  end
25
32
 
26
33
  client = Faye::Client.new('http://localhost:8000/faye')
@@ -28,9 +35,12 @@ if ARGV.empty? || ARGV.join =~ /yml/
28
35
  EM.run do
29
36
 
30
37
  Subduino.start do |read|
31
- puts "[IO] #{read}"
38
+ # Subduino::ArdIO.write("hi")
39
+ if Debug
40
+ # puts "--------------------- #{Time.now}"
41
+ p read
42
+ end
32
43
  if LastRun[:all] < (Time.now.to_i - AppConfig["log_interval"])
33
- p "Syncing...."
34
44
  LastRun[:all] = Time.now.to_i
35
45
  client.publish('/stats', 'data' => read)
36
46
  Subduino::Store.add_csv_to_store(read, true)
@@ -40,6 +50,10 @@ if ARGV.empty? || ARGV.join =~ /yml/
40
50
  end
41
51
 
42
52
  end
53
+ elsif ARGV.join =~ /send/
54
+ txt = ARGV[1..-1].join
55
+ puts "Writing #{txt}"
56
+ Subduino::ArdPS.write(txt)
43
57
  else
44
58
  require "subduino/scaffold/generator"
45
59
  dir = ARGV[0]
@@ -47,4 +61,3 @@ else
47
61
  `mkdir #{dir}` unless File.exists?(dir)
48
62
  Subduino::Generator.copy_files(dir)
49
63
  end
50
-
data/duino/Makefile CHANGED
@@ -15,7 +15,7 @@
15
15
  #
16
16
  TARGET = duino
17
17
  ARDUINO_DIR = /usr/share/arduino
18
- ARDUINO_LIBS = Messenger
18
+ # ARDUINO_LIBS = ExtraLib
19
19
 
20
20
  MCU = atmega328p
21
21
  F_CPU = 16000000
@@ -25,7 +25,6 @@ ARDUINO_PORT = /dev/ttyUSB*
25
25
  AVR_TOOLS_PATH = /usr/bin
26
26
  AVRDUDE_CONF = /usr/share/arduino/hardware/tools/avrdude.conf
27
27
 
28
- # INC_DIRS = ./vendor/Messenger
29
28
 
30
29
  # Hopefully these will be self-explanatory but in case they're not:
31
30
  #
@@ -136,7 +135,7 @@ ifeq ($(strip $(NO_CORE)),)
136
135
  ifdef ARDUINO_CORE_PATH
137
136
  CORE_C_SRCS = $(wildcard $(ARDUINO_CORE_PATH)/*.c)
138
137
  CORE_CPP_SRCS = $(wildcard $(ARDUINO_CORE_PATH)/*.cpp)
139
- EXTRA_CPP_SRCS = $(wildcard $(ARDUINO_LIB_PATH)/Messenger/*.cpp)
138
+ # EXTRA_CPP_SRCS = $(wildcard $(ARDUINO_LIB_PATH)/ExtraLib/*.cpp)
140
139
  CORE_OBJ_FILES = $(CORE_C_SRCS:.c=.o) $(CORE_CPP_SRCS:.cpp=.o) $(EXTRA_CPP_SRCS:.cpp=.o)
141
140
  CORE_OBJS = $(patsubst $(ARDUINO_CORE_PATH)/%, $(OBJDIR)/%,$(CORE_OBJ_FILES))
142
141
  endif
data/duino/duino.pde CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Arduin
2
+ * Arduin v2
3
3
  *
4
4
  *
5
5
  * /home/nofxx/projects/ardo/control
@@ -7,12 +7,11 @@
7
7
  *
8
8
  */
9
9
 
10
- #include <Messenger.h>
11
-
12
- #define sec 1000
13
- #define min 60000
14
- #define hour 3600000
15
- #define day 86400000
10
+ #define SEC 1000
11
+ #define MIN 60000
12
+ #define HOUR 3600000
13
+ #define DAY 86400000
14
+ #define MAX 50
16
15
 
17
16
  // Output
18
17
  const int rxPin = 0;
@@ -43,55 +42,87 @@ const int d11 = 11;
43
42
  const int d12 = 12;
44
43
 
45
44
  // Intervals (s)
46
- unsigned long shoot = 1;
47
- unsigned long sync = 5 * sec;
48
- unsigned long time_now, last_shoot, last_sync;
45
+ unsigned long sync = 5 * SEC;
46
+ unsigned long time_now, last_sync;
49
47
 
48
+ int i = 0;
50
49
  int btnState = 0;
51
50
  int touchState = 0;
52
51
  int motorState = 0;
53
52
 
54
- char buffer [50];
53
+ char outbuf[MAX];
54
+ char cmdbuf[MAX];
55
55
 
56
56
  volatile int rx_state = LOW;
57
57
  volatile int tx_state = LOW;
58
58
 
59
- Messenger message = Messenger();
60
-
61
- // Create the callback function
62
59
 
63
60
  unsigned long mintomilli(unsigned long m) {
64
61
  return(m * 60000);
65
62
  }
66
63
 
67
- void btnLed() {
68
- if(digitalRead(d2) == 1) {
69
- Serial.println("BUTTON");
70
- digitalWrite(infoPin, HIGH);
71
- analogWrite(d3, 250);
72
- if (motorState != 0) {
73
- motorState = 0;
74
- } else {
75
- motorState = 1;
76
- }
77
- } else {
78
- digitalWrite(infoPin, LOW);
79
- analogWrite(d3, LOW);
80
- }
64
+ int ctoi( int c )
65
+ {
66
+ return c - '0';
67
+ }
68
+
69
+ /*
81
70
 
71
+ Read Sensors Data
72
+
73
+ */
74
+ void read_sensors() {
75
+ sprintf(outbuf, "i0:%d,i1:%d,i2:%d,i3:%d,i4:%d,i5:%d,d11:%d,d12:%d",
76
+ analogRead(i0),
77
+ analogRead(i1),
78
+ analogRead(i2),
79
+ analogRead(i3),
80
+ analogRead(i4),
81
+ analogRead(i5),
82
+ digitalRead(d11),
83
+ digitalRead(d12));
84
+
85
+ Serial.println(outbuf);
82
86
  }
83
87
 
84
- void messageReady() {
85
- int pin = 2;
86
- int val = 0;
87
- // Loop through all the available elements of the message
88
- while ( message.available() && pin <= infoPin ) {
89
- val = message.readInt();
90
- // Set the pin as determined by the message
91
- analogWrite(pin, val);
92
- pin = pin + 1;
93
- }
88
+ void switch_pin(int pin) {
89
+ analogWrite(pin, digitalRead(pin) == 0 ? 999 : 0);
90
+ }
91
+
92
+ void pulse_pin(int pin) {
93
+ analogWrite(pin, HIGH);
94
+ delay(500);
95
+ analogWrite(pin, LOW);
96
+ }
94
97
 
98
+ /*
99
+
100
+ Command Protocol -> NNvvv
101
+ 2 bytes pin number NN
102
+ 3 bytes value vvv
103
+ Split commands with ','
104
+
105
+ 13500,130,13999
106
+
107
+ Pin 13 -> 500 -> 000 -> 999
108
+
109
+ */
110
+ void read_commands() {
111
+ char c;
112
+ while( Serial.available() && c != '\n' ) { // buffer up a line
113
+ c = Serial.read();
114
+ if (c == '\n' || c == ',') {
115
+ int pin; int val;
116
+ pin = (ctoi(cmdbuf[0])*10) + ctoi(cmdbuf[1]);
117
+ val = (ctoi(cmdbuf[2])*100) + (ctoi(cmdbuf[3])*10) + ctoi(cmdbuf[4]);
118
+ analogWrite(pin, val);
119
+ i = 0;
120
+ } else if (c == 'x') {
121
+ read_sensors();
122
+ } else {
123
+ cmdbuf[i++] = c;
124
+ }
125
+ }
95
126
  }
96
127
 
97
128
  void setup() {
@@ -99,39 +130,50 @@ void setup() {
99
130
  pinMode(infoPin, OUTPUT);
100
131
  pinMode(d2, OUTPUT);
101
132
  pinMode(d3, OUTPUT);
133
+ pinMode(d4, OUTPUT);
134
+ pinMode(d5, OUTPUT);
102
135
  pinMode(d6, OUTPUT);
136
+ pinMode(d7, OUTPUT);
103
137
  pinMode(d10, OUTPUT);
104
138
 
105
- // testFalse();
106
- shoot = mintomilli(shoot);
107
-
108
- attachInterrupt(0, btnLed, CHANGE);
139
+ // attachInterrupt(0, btnLed, CHANGE);
109
140
  Serial.begin(115200);
110
- //Serial.begin(9600);
111
- message.attach(messageReady);
112
141
  }
113
142
 
114
143
 
115
144
  void loop() {
116
-
117
145
  time_now = millis();
118
146
 
147
+ read_commands();
148
+
119
149
  // Sync over wire
120
150
  if ( abs(time_now - last_sync) >= sync) {
121
151
  last_sync = time_now;
122
- sprintf(buffer, "i0:%d,i1:%d,i2:%d,i3:%d,i4:%d,i5:%d,d11:%d,d12:%d",
123
- analogRead(i0),
124
- analogRead(i1),
125
- analogRead(i2),
126
- analogRead(i3),
127
- analogRead(i4),
128
- analogRead(i5),
129
- digitalRead(d11),
130
- digitalRead(d12));
131
- Serial.println(buffer);
152
+ read_sensors();
132
153
  }
133
154
 
134
155
 
156
+ }
157
+
158
+
159
+ // void btnLed() {
160
+ // if(digitalRead(d2) == 1) {
161
+ // Serial.println("BUTTON");
162
+ // digitalWrite(infoPin, HIGH);
163
+ // analogWrite(d3, 250);
164
+ // if (motorState != 0) {
165
+ // motorState = 0;
166
+ // } else {
167
+ // motorState = 1;
168
+ // }
169
+ // } else {
170
+ // digitalWrite(infoPin, LOW);
171
+ // analogWrite(d3, LOW);
172
+ // }
173
+
174
+ // }
175
+
176
+
135
177
  // if (motorState != 0) {
136
178
  // analogWrite(d10, (analogRead(i1)/5));
137
179
  // } else {
@@ -141,14 +183,6 @@ void loop() {
141
183
  // noInterrupts();
142
184
  // interrupts();
143
185
 
144
- // if ( abs(time_now - last_shoot) >= shoot) {
145
- // last_shoot = time_now;
146
- // // Serial.println("SHOOTING");
147
- // digitalWrite(d6, HIGH); // turn the d6 on
148
- // delay(400); // stop the program for some time
149
- // digitalWrite(d6, LOW); // turn the d6 off
150
- // }
151
-
152
186
  // Terminate message with a linefeed and a carriage return
153
187
  // Serial.print(13,BYTE);
154
188
  // Serial.print(10,BYTE);
@@ -172,9 +206,6 @@ void loop() {
172
206
  // }
173
207
 
174
208
 
175
- while ( Serial.available() ) message.process(Serial.read());
176
-
177
- }
178
209
 
179
210
  // void pulseJob(pin) {
180
211
  // analogWrite(pin, 250);
@@ -3,39 +3,46 @@ module Subduino
3
3
  class ArdIO
4
4
  class << self
5
5
 
6
+ #
7
+ # Serial Port
8
+ #
9
+ # Direct access to the SerialPort instance.
10
+ #
6
11
  def sp
7
12
  @sp ||= SerialPort.new(Arduino.find_usb, BAUDS) #, DATA_BITS, DATA_STOP, parity)
8
13
  # @sp.read_timeout = 10;# @sp.write_timeout = 10
9
14
  end
10
15
 
16
+ #
17
+ # Read I/O
18
+ #
19
+ # Starts a thread that loops fetching serial bytes.
20
+ # It'll buffer it up until a \n or \r char are found.
21
+ # Feed it with a block to read the text.
22
+ #
11
23
  def read(&proc)
12
24
  Log.info "[USB] Starting USB Connect..." + sp.get_modem_params.map { |k,v| "#{k}: #{v}" }.join(" ")
13
25
  Log.info "[USB] Read Timeout #{sp.read_timeout}" # {sp.write_timeout}"
14
26
 
15
- #
16
- # Read I/O
17
- #
18
27
  @iothread ||= Thread.new do
19
28
  Thread.current.abort_on_exception = false
20
29
  icache = []
21
-
22
30
  loop do
23
31
  begin
24
32
  while char = sp.getc
25
- if char !~ /\n|\r/
33
+ if !char.valid_encoding?
34
+ puts "Bad char #{char}"
35
+ elsif char !~ /\n|\r/
26
36
  icache << char
27
37
  else
28
38
  data = icache.join(""); icache = []
29
- if data =~ /:/
39
+ unless data.empty?
40
+ Log.info "[IO RX] #{data}"
30
41
  proc.call(data)
31
- Log.info "[SENSOR] #{data}"
32
- else
33
- proc.call(data) unless data.empty?
34
- # Log.info "[INPUT] Done."
35
42
  end
36
43
  end
44
+ # sleep 1
37
45
  end
38
- sleep 1
39
46
  rescue => e
40
47
  Log.error "[USB] Error #{e}"
41
48
  Log.error e.backtrace.join("\n")
@@ -44,13 +51,25 @@ module Subduino
44
51
  end
45
52
  end
46
53
 
54
+ #
55
+ # Write I/O
56
+ #
57
+ # Prints bytes to the serial port.
58
+ # It'll use subduino convention of \n to end the message.
59
+ # Use #sp to write directly to the port. `ArdIO.sp.puts ('hi')`
60
+ #
47
61
  def write(msg)
48
62
  Log.info "[IO TX] #{msg}"
49
- txt = msg.gsub("\n", "\r")
50
- txt += "\r" unless txt =~ /^\\r/
51
- sp.write(txt)
63
+ txt = msg.gsub("\r", "\n")
64
+ txt += "\n" unless txt =~ /^\\n/
65
+ puts "=> Sending #{txt.inspect}" if Debug
66
+ sp.puts(msg)
52
67
  end
53
68
 
69
+
70
+ #
71
+ # Finish Him!
72
+ #
54
73
  def stop!
55
74
  sp.close
56
75
  end
@@ -1,38 +1,61 @@
1
1
  module Subduino
2
+
2
3
  class ArdPS
4
+ class << self
3
5
 
4
- def self.redis
5
- @redis ||= Redis.new(:timeout => 0) rescue false
6
- end
6
+ #
7
+ # Redis PubSub
8
+ #
9
+ # Direct access to the Redis instance.
10
+ #
11
+ def redis
12
+ @redis ||= Redis.new(:timeout => 0) rescue false
13
+ end
7
14
 
8
- def self.read
9
- return Log.warn "[PubSub] Not started..." unless @redis
10
- Thread.new do
11
- begin
12
- redis.subscribe('subduin') do |on|
13
- on.subscribe {|klass, num_subs| Log.info "[PubSub] Subscribed to #{klass} (#{num_subs} subscriptions)" }
14
- on.message do |klass, msg|
15
- Log.info "[PubSub] #{klass} - #{msg}"
16
- ArdIO.write msg
17
- # @redis.unsubscribe if msg == 'exit'
15
+ #
16
+ # Read PubSub
17
+ #
18
+ # Join 'subduino' channel and send messages to I/O
19
+ #
20
+ def read
21
+ return Log.warn "[PubSub] Not started..." unless redis
22
+ Thread.new do
23
+ begin
24
+ redis.subscribe('subduino') do |on|
25
+ on.subscribe {|klass, num_subs| Log.info "[PubSub] Subscribed to #{klass} (#{num_subs} subscriptions)" }
26
+ on.message do |klass, msg|
27
+ Log.info "[PubSub] #{klass} - #{msg}"
28
+ ArdIO.write msg
29
+ # @redis.unsubscribe if msg == 'exit'
30
+ end
31
+ on.unsubscribe {|klass, num_subs| Log.info "[PubSub] Unsubscribed to #{klass} (#{num_subs} subscriptions)" }
18
32
  end
19
- on.unsubscribe {|klass, num_subs| Log.info "[PubSub] Unsubscribed to #{klass} (#{num_subs} subscriptions)" }
33
+ rescue => e
34
+ Log.error "[PubSub] Error #{e}"
35
+ Log.error e.backtrace.join("\n")
20
36
  end
21
- rescue => e
22
- Log.error "[PubSub] Error #{e}"
23
- Log.error e.backtrace.join("\n")
37
+
24
38
  end
39
+ end
25
40
 
41
+ #
42
+ # Write PubSub
43
+ #
44
+ # Write a message to the 'subduino' PubSub channel.
45
+ #
46
+ def write(msg)
47
+ redis.publish('subduino', msg)
26
48
  end
27
- end
28
49
 
29
- def self.write(msg)
30
- redis.publish('subdout', msg)
31
- end
32
50
 
33
- def self.stop!
34
- redis.disconnect if @redis
35
- end
51
+ #
52
+ # Fatality
53
+ #
54
+ def stop!
55
+ redis.disconnect if redis
56
+ end
36
57
 
58
+ end
37
59
  end
60
+
38
61
  end
@@ -0,0 +1,49 @@
1
+ # From
2
+ # http://rbjl.net/
3
+ # Ruby Zucker
4
+ require 'rbconfig'; RbConfig::CONFIG['host_os']
5
+
6
+ module OS
7
+ class << self
8
+ def is?(what)
9
+ what === RbConfig::CONFIG['host_os']
10
+ end
11
+ alias is is?
12
+
13
+ def to_s
14
+ RbConfig::CONFIG['host_os']
15
+ end
16
+ end
17
+
18
+ module_function
19
+
20
+ def linux?
21
+ OS.is? /linux|cygwin/
22
+ end
23
+
24
+ def mac?
25
+ OS.is? /mac|darwin/
26
+ end
27
+
28
+ def bsd?
29
+ OS.is? /bsd/
30
+ end
31
+
32
+ def windows?
33
+ OS.is? /mswin|win|mingw/
34
+ end
35
+
36
+ def solaris?
37
+ OS.is? /solaris|sunos/
38
+ end
39
+
40
+ def posix?
41
+ linux? or mac? or bsd? or solaris? or Process.respond_to?(:fork)
42
+ end
43
+
44
+ #def symbian?
45
+ #TODO who knows what symbian returns?
46
+ #end
47
+
48
+ # ...
49
+ end
@@ -1,6 +1,6 @@
1
1
  module Subduino
2
2
  module Parse
3
- class Bool < DigParser
3
+ class Bool < Digital
4
4
  def parse
5
5
  @v.zero? ? false : true
6
6
  end
@@ -0,0 +1,18 @@
1
+ module Subduino
2
+ module Parse
3
+ class Energy < Analogic
4
+ AC = 220
5
+ def parse
6
+ (@v / 10).to_i
7
+ end
8
+
9
+ def kwh
10
+ AC * parse
11
+ end
12
+
13
+ def to_s
14
+ "#{parse} A"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ module Subduino
2
+ module Parse
3
+ class Flow < Analogic
4
+ def parse
5
+ (@v * 0.5).to_i
6
+ end
7
+
8
+ def to_s
9
+ "#{parse} cm3/h"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,6 +1,6 @@
1
1
  module Subduino
2
2
  module Parse
3
- class Lux < AnaParser
3
+ class Lux < Analogic
4
4
  def parse
5
5
  (@v * 0.5).to_i
6
6
  end
@@ -0,0 +1,9 @@
1
+
2
+ module Subduino
3
+ module Parse
4
+ class Presence < Digital
5
+
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module Subduino
2
+ module Parse
3
+ class Pressure < Analogic
4
+ def parse
5
+ (@v * 0.5).to_i
6
+ end
7
+
8
+ def to_s
9
+ "#{parse} psi"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -2,7 +2,7 @@
2
2
  module Subduino
3
3
  module Parse
4
4
 
5
- class Temp < AnaParser
5
+ class Temp < Analogic
6
6
  def parse
7
7
  @v * 0.035
8
8
  end
@@ -6,7 +6,7 @@ module Subduino
6
6
  const_get(klass).new(v, name, id)
7
7
  end
8
8
 
9
- class DigParser
9
+ class Digital
10
10
  def initialize(v,n=nil,id=nil)
11
11
  @v = v.to_i
12
12
  @id = id
@@ -31,7 +31,7 @@ module Subduino
31
31
 
32
32
  end
33
33
 
34
- class AnaParser < DigParser
34
+ class Analogic < Digital
35
35
  def digital?; false; end
36
36
  def parse; @v; end
37
37
  def ratio
@@ -40,7 +40,7 @@ module Subduino
40
40
  end
41
41
  end
42
42
 
43
- class Knob < AnaParser
43
+ class Knob < Analogic
44
44
  end
45
45
 
46
46