Class: Tamashii::Agent::LCD

Inherits:
Component show all
Defined in:
lib/tamashii/agent/lcd.rb

Defined Under Namespace

Classes: LineAnimator

Instance Method Summary collapse

Methods inherited from Component

#check_new_event, #handle_new_event, #restart_current_component_async, #run, #run!, #run_worker_loop, #send_event, #stop, #stop_threads, #worker_loop

Methods included from Common::Loggable

#logger, #progname

Constructor Details

#initialize(master) ⇒ LCD

Returns a new instance of LCD.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tamashii/agent/lcd.rb', line 80

def initialize(master)
  super
  load_lcd_device
  @device_line_count = @lcd.class::LINE_COUNT
  @device_lock = Mutex.new
  create_line_animators
  set_idle_text("[Tamashii]\nIdle...")
  logger.debug "Using LCD instance: #{@lcd.class}"
  print_message("Initializing\nPlease wait...")
  schedule_to_print_idle
end

Instance Method Details

#clean_upObject



187
188
189
190
# File 'lib/tamashii/agent/lcd.rb', line 187

def clean_up
  clear_screen
  super
end

#clear_screenObject



183
184
185
# File 'lib/tamashii/agent/lcd.rb', line 183

def clear_screen
  print_message("")
end

#compute_idle_textObject



150
151
152
153
154
155
156
157
# File 'lib/tamashii/agent/lcd.rb', line 150

def compute_idle_text
  result = @idle_text_raw.clone
  if @has_time_hint
    result.gsub!(Tamashii::AgentHint::TIME, Time.now.localtime(Config.localtime).strftime("%m/%d(%a) %H:%M"))
  end
  @idle_text = result
  logger.debug "Idle text updated to #{@idle_text}"
end

#create_line_animatorsObject



92
93
94
95
96
97
# File 'lib/tamashii/agent/lcd.rb', line 92

def create_line_animators
  LineAnimator.line_width = @lcd.class::WIDTH
  LineAnimator.handler_print_line = method(:print_line)
  @line_animators = [] 
  @device_line_count.times {|i| @line_animators << LineAnimator.new(i)}
end

#load_lcd_deviceObject



99
100
101
102
103
104
105
# File 'lib/tamashii/agent/lcd.rb', line 99

def load_lcd_device
  @lcd = Adapter::LCD.object
rescue => e
  logger.error "Unable to load LCD instance: #{Adapter::LCD.current_class}"
  logger.error "Use #{Adapter::LCD.fake_class} instead"
  @lcd = Adapter::LCD.fake_class.new
end


163
164
165
# File 'lib/tamashii/agent/lcd.rb', line 163

def print_idle
  print_message(@idle_text)
end


114
115
116
117
118
# File 'lib/tamashii/agent/lcd.rb', line 114

def print_line(*args)
  @device_lock.synchronize do
    @lcd.print_line(*args)
  end
end


107
108
109
110
111
112
# File 'lib/tamashii/agent/lcd.rb', line 107

def print_message(message)
  lines = message.lines.map{|l| l.delete("\n")} 
  @device_line_count.times do |line_count|
    @line_animators[line_count].set_text(lines[line_count])
  end
end

#process_event(event) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/tamashii/agent/lcd.rb', line 167

def process_event(event)
  case event.type
  when Event::LCD_MESSAGE
    logger.debug "Show message: #{event.body}"
    @back_to_idle_task&.cancel
    print_message(event.body)
    @device_lock.synchronize do
      schedule_to_print_idle
    end
  when Event::LCD_SET_IDLE_TEXT
    logger.debug "Idle text set to #{event.body}"
    set_idle_text(event.body)
    print_idle
  end
end

#schedule_to_print_idle(delay = 5) ⇒ Object



159
160
161
# File 'lib/tamashii/agent/lcd.rb', line 159

def schedule_to_print_idle(delay = 5)
  @back_to_idle_task = Concurrent::ScheduledTask.execute(delay, &method(:print_idle))
end

#set_idle_text(text) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/tamashii/agent/lcd.rb', line 120

def set_idle_text(text)
  @idle_text_raw = text
  @auto_update_interval = 0
  # Time hint
  if @idle_text_raw.include?(Tamashii::AgentHint::TIME)
    @has_time_hint = true
    @auto_update_interval = [@auto_update_interval, 30].max
  else
    @has_time_hint = false
  end
  # clear auto update timer
  @idle_text_timer_task.shutdown if @idle_text_timer_task
  if @auto_update_interval > 0
    setup_idle_text_auto_update
  else
    # one-time setup
    compute_idle_text
  end
end

#setup_idle_text_auto_updateObject



140
141
142
143
144
145
146
147
148
# File 'lib/tamashii/agent/lcd.rb', line 140

def setup_idle_text_auto_update
  @idle_text_timer_task = Concurrent::TimerTask.new(run_now: true) do 
    compute_idle_text
    print_idle
  end
  @idle_text_timer_task.execution_interval = @auto_update_interval
  @idle_text_timer_task.timeout_interval = @auto_update_interval
  @idle_text_timer_task.execute
end