Class: ChiplotleCliWrapper::Plotter

Inherits:
Object
  • Object
show all
Defined in:
lib/chiplotle-cli-wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlotter

Returns a new instance of Plotter.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chiplotle-cli-wrapper.rb', line 16

def initialize

  @log            = Logger.new 'chiplotle-cli-wrapper'
  @log.outputters = Outputter.stdout

  # TODO: check for chiplotle installation
  @chiplotle, @stdout, @stderr = popen3("chiplotle")

  # plotter initialization takes 8 seconds
  begin
    Timeout::timeout(10) do
      @stdout.each do |line|

        # this is the last line of the banner output on successul initialization of plotter
        break if line =~ /Buffer Size:.*/

        log.info "waiting..."
        sleep 0.5
      end
    end
  rescue Timeout::Error => e
    raise Timeout::Error, "#{e}: No plotter detected!"
  end

  log.info "plotter detected!"

  log.info "setting some defaults..."
  self.reset_label_size
  self.label_direction(1,0)
  self.slant(0)
  self.position(0,0)
  self.pen_number(1)
  self.replace_pen

  log.info "starting stderr/stdout logging threads..."
  Thread.new do
    loop do
      log.info @stdout.gets
    end
  end

  Thread.new do
    loop do
      log.info @stderr.gets
    end
  end

  log.info "initialization complete!"
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



14
15
16
# File 'lib/chiplotle-cli-wrapper.rb', line 14

def log
  @log
end

Instance Method Details

#character_size(width, height) ⇒ Object



94
95
96
# File 'lib/chiplotle-cli-wrapper.rb', line 94

def character_size(width, height)
  plotter "hpgl.SI(#{width}, #{height})"
end

#closeObject



66
67
68
69
70
71
# File 'lib/chiplotle-cli-wrapper.rb', line 66

def close
  # TODO: perform any reset actions...

  @chiplotle.close
  #@log.close
end

#convert_hpgl_to_chiplotle_functions(hpgl) ⇒ Object

FIXME



157
158
159
160
161
# File 'lib/chiplotle-cli-wrapper.rb', line 157

def convert_hpgl_to_chiplotle_functions(hpgl)
  hpgl.map do |statement|
    statement.gsub!(/([A-Za-z]{2})(.*)/, "\1(\2)")
  end
end

#draw_hpgl(hpgl) ⇒ Object

FIXME



164
165
166
167
168
169
170
171
# File 'lib/chiplotle-cli-wrapper.rb', line 164

def draw_hpgl(hpgl)

  chiplotle_functions = convert_hpgl_to_chiplotle_functions(hpgl)

  chiplotle_functions.each do |command|
    plotter("hpgl.#{command}")
  end
end

#label(text) ⇒ Object



90
91
92
# File 'lib/chiplotle-cli-wrapper.rb', line 90

def label(text)
  plotter "hpgl.LB('#{text}')"
end

#label_direction(run, rise) ⇒ Object



102
103
104
# File 'lib/chiplotle-cli-wrapper.rb', line 102

def label_direction(run,rise)
  plotter "hpgl.DI(#{run}, #{rise})"
end

#pen_number(pen_number) ⇒ Object



81
82
83
# File 'lib/chiplotle-cli-wrapper.rb', line 81

def pen_number(pen_number)
  plotter "hpgl.SP(#{pen_number})"
end

#plotter(command) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/chiplotle-cli-wrapper.rb', line 73

def plotter(command)
  log "plotter.write(#{command})"

  @chiplotle.puts "plotter.write(#{command})"

  #"oh no! #{@log.read}" if @log.read != "chiplotle> "
end

#position(x, y) ⇒ Object

device will continue from last position if no values are supplied



86
87
88
# File 'lib/chiplotle-cli-wrapper.rb', line 86

def position(x, y)
  plotter "hpgl.PA([(#{x}, #{y})])"
end

#replace_penObject



110
111
112
# File 'lib/chiplotle-cli-wrapper.rb', line 110

def replace_pen
  pen_number(0)
end

#reset_label_sizeObject



98
99
100
# File 'lib/chiplotle-cli-wrapper.rb', line 98

def reset_label_size
  plotter "hpgl.SI()"
end

#slant(slant) ⇒ Object



106
107
108
# File 'lib/chiplotle-cli-wrapper.rb', line 106

def slant(slant)
  plotter "hpgl.SL(#{slant})"
end

#write_file(hpgl) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/chiplotle-cli-wrapper.rb', line 173

def write_file(hpgl)
  temp_file = Tempfile.new('chiplotle-cli-wrapper')

  hpgl.gsub!(/\\r?\\n/, '')

  log "hpgl: #{hpgl}"

  temp_file.write(hpgl)
  temp_file.close

  log "temp_file.path: #{temp_file.path}"

  log %x{cat #{temp_file.path}}

  @chiplotle.puts("plotter.write_file('#{temp_file.path}')")

  # FIXME - fork a sleep remove to prevent file being removed before chiplotle has read it?
  #temp_file.unlink
end

#write_text(text: '', pen_number: '', slant: '', direction_run: '', direction_rise: '', position_x: '', position_y: '', character_width: '', character_height: '') ⇒ Object

TODO: insert new lines when plotter is going to run out of horizontal space FIXME: maybe use nil rather than defaul values to leave setting unchanged..



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chiplotle-cli-wrapper.rb', line 116

def write_text(
  text:             '',
  pen_number:       '',
  slant:            '',
  direction_run:    '',
  direction_rise:   '',
  position_x:       '',
  position_y:       '',
  character_width:  '',
  character_height: ''
)
  if pen_number != ''
    self.pen_number(pen_number)
  end

  if direction_run != '' and direction_rise != ''
    self.label_direction(direction_run, direction_rise)
  end

  if slant !=  ''
    self.slant(slant)
  end

  if position_x != '' and position_y != ''
    self.position(position_x, position_y)
  end

  if character_width != '' and character_height != ''
    self.character_size(character_width, character_height)
  end

  if text != ''
    label(text)
  end

  reset_label_size

  replace_pen
end