Class: Tamashii::Agent::LCD::LineAnimator

Inherits:
Object
  • Object
show all
Includes:
Common::Loggable
Defined in:
lib/tamashii/agent/lcd.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::Loggable

#logger, #progname

Constructor Details

#initialize(line) ⇒ LineAnimator

Returns a new instance of LineAnimator.



24
25
26
27
28
29
# File 'lib/tamashii/agent/lcd.rb', line 24

def initialize(line)
  @line = line
  @text = ""
  @pos = -1
  @stop_animation = false
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



14
15
16
# File 'lib/tamashii/agent/lcd.rb', line 14

def text
  @text
end

Class Method Details

.handler_print_line=(value) ⇒ Object



20
21
22
# File 'lib/tamashii/agent/lcd.rb', line 20

def self.handler_print_line=(value)
  @@handler_print_line = value
end

.line_width=(value) ⇒ Object



16
17
18
# File 'lib/tamashii/agent/lcd.rb', line 16

def self.line_width=(value)
  @@line_width = value
end

Instance Method Details

#animation_loopObject



57
58
59
60
61
62
63
64
# File 'lib/tamashii/agent/lcd.rb', line 57

def animation_loop
  loop do
    sleep Config.lcd_animation_delay
    animation_show_text
    break if @stop_animation
    sleep Config.lcd_animation_delay if @pos == 0 || @pos == @max_pos
  end
end

#animation_show_textObject



42
43
44
45
46
47
# File 'lib/tamashii/agent/lcd.rb', line 42

def animation_show_text
  text = @text[@pos, @@line_width]
  @pos += 1
  @pos = 0 if @pos > @max_pos
  print_text(text)
end


75
76
77
# File 'lib/tamashii/agent/lcd.rb', line 75

def print_text(text)
  @@handler_print_line&.call(text, @line)
end

#set_text(text) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/tamashii/agent/lcd.rb', line 31

def set_text(text)
  return if text == @text
  stop_animation
  @text = text || ""
  if @text.size > @@line_width
    start_animation
  else
    print_text(@text)
  end
end

#start_animationObject



49
50
51
52
53
54
55
# File 'lib/tamashii/agent/lcd.rb', line 49

def start_animation
  @pos = 0
  @max_pos = @text.size - @@line_width
  @stop_animation = false
  logger.debug "Start animation for line #{@line}: #{@text}"
  @animation_thread = Thread.new { animation_loop }
end

#stop_animationObject



66
67
68
69
70
71
72
73
# File 'lib/tamashii/agent/lcd.rb', line 66

def stop_animation
  @stop_animation = true
  if @animation_thread
    @animation_thread.join(Config.lcd_animation_delay * 3)
    @animation_thread.exit 
    @animation_thread = nil
  end
end