gv_fsm 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d5cf386d941c15a1f9c6efb216b87d53e1d2836db174974f29290873c53a86b
4
- data.tar.gz: 9d1bcf0cd2d1bf32ca797d76f4e776a6462dbedc565399da20ef1c581b66fa47
3
+ metadata.gz: 61d5d6c1ef593ce7d516c48ab905c210d3005081fdd241b47298c55c5340236c
4
+ data.tar.gz: d8bc69220e149140bd150ad3890dbf11e33651352bf22df483b7941c9a9911c7
5
5
  SHA512:
6
- metadata.gz: 22269db6b746cba79aace36079d7857850f67d2f484b4661b738ce10b9f15f659f6e6fa1092c8a67d02d8b392ccdce12f2a78dfa41c9d8980e5fae91aaea59bb
7
- data.tar.gz: f0408ea83e6cd16a94b8b82916185eec48d779600c93d4f79555edd2ff8268f232c20310abc0c5606014a502ac8ce33b98727815663ffdaf61888109a88c039e
6
+ metadata.gz: c92f9ebb2275bfb8fd4fa23060cc22c8d34fe2e7eb219e9833f60517417ca11a420758c99fb0bee82cc58795f184a44d230bf03ae947072e1a72271bad34b2c9
7
+ data.tar.gz: 7a2898d7f153fe7c80a9eb046c54bd4076ffa15bf69987f069d94a70708a17085641fc7dff765fd7816df839f469ce363e7793122e5d09538e882667d5e83650
data/bin/gv_fsm CHANGED
@@ -44,7 +44,7 @@ op = OptionParser.new do |parser|
44
44
  sm.ino = true
45
45
  end
46
46
 
47
- parser.on("-l", "--no-syslog", "Omit syslog calls in stub functions") do
47
+ parser.on("-l", "--no-log", "Omit log calls in stub functions") do
48
48
  sm.syslog = false
49
49
  end
50
50
 
@@ -60,28 +60,27 @@ op.parse!
60
60
  unless ARGV[0]
61
61
  STDERR.puts "ERROR: I need the path to a Graphviz file!\n\n"
62
62
  STDERR.puts op
63
- exit
63
+ exit 1
64
64
  end
65
- unless File.extname(ARGV[0]) == ".dot"
65
+ if !File.extname(ARGV[0]) == ".dot" or !File.exist? ARGV[0] then
66
66
  STDERR.puts "ERROR: #{ARGV[0]} does not look like a Graphviz file!\n\n"
67
67
  STDERR.puts op
68
- exit
68
+ exit 2
69
69
  end
70
70
 
71
- sm.parse(ARGV[0])
71
+ unless sm.parse(ARGV[0]) then
72
+ puts "Error parsing the file #{ARGV[0]}: #{sm.error}"
73
+ exit 3
74
+ end
72
75
 
73
76
  puts "Parsed #{sm.dotfile}.\nGenerating C stub for states: #{sm.states_list.join(", ")}."
74
- if sm.ino then
75
- sm.generate_ino
76
- puts "Generated .ino file #{sm.cname}.ino"
77
- else
78
- if options[:header] then
79
- sm.generate_h
80
- puts "Generated header #{sm.cname}.h"
81
- end
82
- if options[:source] then
83
- sm.generate_c
84
- puts "Generated source #{sm.cname}.c"
85
- end
77
+
78
+ if options[:header] then
79
+ name = sm.generate_h
80
+ puts "Generated header #{name}"
81
+ end
82
+ if options[:source] then
83
+ name = sm.generate_c
84
+ puts "Generated source #{name}"
86
85
  end
87
86
 
@@ -8,7 +8,7 @@ require File.expand_path("../version.rb", __FILE__)
8
8
 
9
9
  module GV_FSM
10
10
  class FSM
11
- attr_reader :states, :transitions, :dotfile, :prefix
11
+ attr_reader :states, :transitions, :dotfile, :prefix, :error
12
12
  attr_accessor :project_name, :description, :cname, :syslog, :ino
13
13
  include GV_FSM::Templates
14
14
 
@@ -16,6 +16,7 @@ module GV_FSM
16
16
  @prefix = ""
17
17
  @syslog = true
18
18
  @ino = false
19
+ @error = nil
19
20
  parse(filename) if filename
20
21
  end
21
22
 
@@ -29,7 +30,20 @@ module GV_FSM
29
30
  @dotfile = filename
30
31
  @states = []
31
32
  @transitions = []
32
- GraphViz.parse(filename) do |g|
33
+ graph = GraphViz.parse(filename) do |g|
34
+ if g.graph_count > 1 then
35
+ @error = "Only one graph in the dot file is permitted"
36
+ return nil
37
+ end
38
+ unless g.type == "digraph" then
39
+ @error = "Graph is not directed"
40
+ return nil
41
+ end
42
+ if g.node_count == 0 then
43
+ @error = "Graph is empty"
44
+ return nil
45
+ end
46
+ @description = g.name
33
47
  g.each_node do |id|
34
48
  n = g.get_node(id)
35
49
  if n[:label].source.empty? or
@@ -58,6 +72,11 @@ module GV_FSM
58
72
  @transitions << {from: from, to: to, function: label ? @prefix+label : nil}
59
73
  end
60
74
  end
75
+ unless graph then
76
+ @error = "Parsing error"
77
+ return nil
78
+ end
79
+ return graph
61
80
  end
62
81
 
63
82
  def state_functions_list
@@ -108,27 +127,24 @@ module GV_FSM
108
127
  end
109
128
 
110
129
  def generate_c(filename = @cname)
111
- File.open("#{filename}.c", "w") do |f|
130
+ ext = @ino ? "cpp" : "c"
131
+ fname = "#{filename}.#{ext}"
132
+ File.open(fname, "w") do |f|
112
133
  f.puts ERB.new(HEADER, 0, "<>").result(binding)
113
134
  f.puts ERB.new(CC, 0, "<>").result(binding)
114
135
  end
136
+ return fname
115
137
  end
116
138
 
117
139
  def generate_h(filename = @cname)
118
- File.open("#{filename}.h", "w") do |f|
140
+ fname = "#{filename}.h"
141
+ File.open(fname, "w") do |f|
119
142
  f.puts ERB.new(HEADER, 0, "<>").result(binding)
120
143
  f.puts ERB.new(HH, 0, "<>").result(binding)
121
144
  end
145
+ return fname
122
146
  end
123
147
 
124
- def generate_ino(filename=@cname)
125
- @syslog = false
126
- File.open("#{filename}.ino", "w") do |f|
127
- f.puts ERB.new(HEADER, 0, "<>").result(binding)
128
- f.puts ERB.new(HH, 0, "<>").result(binding)
129
- f.puts ERB.new(CC, 0, "<>").result(binding)
130
- end
131
- end
132
148
  end
133
149
 
134
150
  end
@@ -26,6 +26,8 @@ module GV_FSM
26
26
  #ifndef <%= @cname.upcase %>_H
27
27
  #define <%= @cname.upcase %>_H
28
28
  #include <stdlib.h>
29
+ <% else %>
30
+ #include <arduino.h>
29
31
  <% end %>
30
32
 
31
33
  // State data object
@@ -100,10 +102,13 @@ module GV_FSM
100
102
  CC =<<~EOC
101
103
  <% if !@ino then %>
102
104
  <% if @syslog then %>
105
+ <% log = :syslog %>
103
106
  #include <syslog.h>
104
107
  <% end %>
105
- #include "<%= @cname %>.h"
108
+ <% else %>
109
+ <% if @syslog then log = :ino end %>
106
110
  <% end %>
111
+ #include "<%= @cname %>.h"
107
112
 
108
113
  <% placeholder = "Your Code Here" %>
109
114
  // SEARCH FOR <%= placeholder %> FOR CODE INSERTION POINTS!
@@ -159,8 +164,10 @@ module GV_FSM
159
164
  <%= @prefix %>state_t <%= s[:function] %>(<%= @prefix %>state_data_t *data) {
160
165
  <%= @prefix %>state_t next_state = <%= dest[s[:id]].first %>;
161
166
 
162
- <% if @syslog then %>
167
+ <% if log == :syslog then %>
163
168
  syslog(LOG_INFO, "[FSM] In state <%= s[:id] %>");
169
+ <% elsif log == :ino then %>
170
+ Serial.println("[FSM] In state <%= s[:id] %>");
164
171
  <% end %>
165
172
  /* <%= placeholder %> */
166
173
 
@@ -170,8 +177,12 @@ module GV_FSM
170
177
  <% end %>
171
178
  break;
172
179
  default:
173
- <% if @syslog then %>
180
+ <% if log == :syslog then %>
174
181
  syslog(LOG_WARNING, "[FSM] Cannot pass from <%= s[:id] %> to %s, remaining in this state", state_names[next_state]);
182
+ <% elsif log == :ino then %>
183
+ Serial.print("[FSM] Cannot pass from <%= s[:id] %> to ");
184
+ Serial.print(state_names[next_state]);
185
+ Serial.println(", remaining in this state");
175
186
  <% end %>
176
187
  next_state = <%= @prefix.upcase %>NO_CHANGE;
177
188
  }
@@ -202,8 +213,10 @@ module GV_FSM
202
213
  // <%= i+1 %>. from <%= e[:from] %> to <%= e[:to] %>
203
214
  <% end %>
204
215
  void <%= t %>(<%= @prefix %>state_data_t *data) {
205
- <% if @syslog then %>
216
+ <% if log == :syslog then %>
206
217
  syslog(LOG_INFO, "[FSM] State transition <%= t %>");
218
+ <% elsif log == :ino then %>
219
+ Serial.println("[FSM] State transition <%= t %>");
207
220
  <% end %>
208
221
  /* <%= placeholder %> */
209
222
  }
@@ -237,11 +250,17 @@ module GV_FSM
237
250
 
238
251
  <% if @ino then %>
239
252
  /* Example usage:
253
+ <%= @prefix %>state_data_t data = {count: 1};
254
+
255
+ void loop() {
256
+ static <%= @prefix %>state_t cur_state = <%= @prefix.upcase %>STATE_INIT;
257
+ cur_state = <%= @prefix %>run_state(cur_state, &data);
258
+ }
259
+ */
240
260
  <% else %>
241
261
  #ifdef TEST_MAIN
242
262
  #include <unistd.h>
243
263
  int main() {
244
- <% end %>
245
264
  <%= @prefix %>state_t cur_state = <%= @prefix.upcase %>STATE_INIT;
246
265
  <% if @syslog then %>
247
266
  openlog("SM", LOG_PID | LOG_PERROR, LOG_USER);
@@ -251,12 +270,9 @@ module GV_FSM
251
270
  cur_state = <%= @prefix %>run_state(cur_state, NULL);
252
271
  sleep(1);
253
272
  } while (cur_state != <%= @prefix.upcase %>STATE_STOP);
254
- <% if !@ino then %>
255
273
  return 0;
256
274
  }
257
275
  #endif
258
- <% else %>
259
- */
260
276
  <% end %>
261
277
  EOC
262
278
  end
@@ -1,3 +1,3 @@
1
1
  module GV_FSM
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gv_fsm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paolo Bosetti