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, #display_name, #get_device_instance, #handle_new_event, #initialize_device, #load_default_device, #restart_current_component_async, #run, #run!, #run_worker_loop, #send_event, #stop, #stop_threads, #worker_loop

Methods included from Common::Loggable

#display_name, #logger, #progname

Constructor Details

#initialize(name, master, options = {}) ⇒ Lcd

Returns a new instance of Lcd.



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

def initialize(name, master, options = {})
  super
  @lcd = initialize_device
  @device_line_count = @lcd.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



185
186
187
188
# File 'lib/tamashii/agent/lcd.rb', line 185

def clean_up
  super
  @lcd.shutdown
end

#clear_screenObject



181
182
183
# File 'lib/tamashii/agent/lcd.rb', line 181

def clear_screen
  print_message("")
end

#compute_idle_textObject



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

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



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

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

#default_device_nameObject



90
91
92
# File 'lib/tamashii/agent/lcd.rb', line 90

def default_device_name
  'Dummy'
end

#get_device_class_name(device_name) ⇒ Object



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

def get_device_class_name(device_name)
  "Lcd::#{device_name}"
end


161
162
163
# File 'lib/tamashii/agent/lcd.rb', line 161

def print_idle
  print_message(@idle_text)
end


112
113
114
115
116
# File 'lib/tamashii/agent/lcd.rb', line 112

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


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

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



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

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



157
158
159
# File 'lib/tamashii/agent/lcd.rb', line 157

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

#set_idle_text(text) ⇒ Object



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

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



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

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