Class: Tochka::TochkaMiniUI

Inherits:
Object
  • Object
show all
Defined in:
lib/tochka/command/tochka-miniui.rb

Constant Summary collapse

DEBUG =
true
DEFAULT_FONT_PATH =
"/usr/share/fonts/truetype/freefont/FreeSansBold.ttf"
DEFAULT_LOG_FILE =
"/var/log/tochka-miniui.log"
WIDTH =
320
HEIGHT =
240
COLOR =
24
CAPTION =
"TochikaMini"
COLOR_RED =
:red
COLOR_GREEN =
:green
COLOR_BLUE =
:blue
COLOR_WHITE =
:white
COLOR_BLACK =
:black
LEVEL1_BASE_X =
15
LEVEL1_BASE_Y =
5
LEVEL1_BASE_X_C2 =
160
VAR_OFFSET_X =
2
VAR_OFFSET_Y =
14
OFFSET_X =
110
OFFSET_Y =
35
LEVEL2_BASE_X =
LEVEL1_BASE_X
LEVEL2_BASE_Y =
LEVEL1_BASE_Y + OFFSET_Y
LEVEL3_BASE_X =
LEVEL1_BASE_X
LEVEL3_BASE_Y =
190
LEVEL4_BASE_X =
0
LEVEL4_BASE_Y =
HEIGHT-VAR_OFFSET_Y

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font_path) ⇒ TochkaMiniUI

Returns a new instance of TochkaMiniUI.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tochka/command/tochka-miniui.rb', line 49

def initialize font_path
  @font_path = font_path
  check_priviledge() # need to be root
  init_sdl()
  init_var()

  begin
    @pf = PidFile.new(:piddir=> "/var/run", :pidfile => "tochka-miniui.pid")
  rescue => e
    $log.err("pid file is in trouble (#{e})")
  end
end

Class Method Details

.default_optionsObject



42
43
44
45
46
47
# File 'lib/tochka/command/tochka-miniui.rb', line 42

def self.default_options
  return {
    :font_path => DEFAULT_FONT_PATH,
    :log_file => DEFAULT_LOG_FILE,
  }
end

Instance Method Details

#draw_allObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/tochka/command/tochka-miniui.rb', line 187

def draw_all
  #back ground
  @screen.fill_rect(0, 0, WIDTH, HEIGHT, @colors[COLOR_BLACK])

  draw_lattice
  draw_base_text

  # update text
  draw_var(1, 0, @date)
  draw_var(1, 1, @hostname)

  draw_var(2, 0, @state)
  draw_var(2, 1, @file_name)
  draw_var(2, 2, file_size_to_h(@file_size))
  draw_var(2, 3, duration_to_h(@duration))
  draw_var(2, 4, @current_channel)
  draw_var(2, 5, @channel_walk)
  draw_var(2, 6, file_size_to_h(@size_per_sec))
  draw_var(2, 7, "#{@utilization}% (#{@utilization_channel}ch)")

  draw_var(3, 0, "#{@disk_usage}GB (#{@disk_usage_perc}%)")
  draw_var(3, 1, "#{@mem_usage}MB (#{@mem_usage_perc}%)")
  draw_var(3, 2, "#{@cpu_usage_perc}%")
end

#draw_base_textObject



68
69
70
71
72
73
# File 'lib/tochka/command/tochka-miniui.rb', line 68

def draw_base_text
  draw_base_text_level1
  draw_base_text_level2
  draw_base_text_level3
  draw_base_text_level4
end

#draw_base_text_level1Object



80
81
82
83
# File 'lib/tochka/command/tochka-miniui.rb', line 80

def draw_base_text_level1
  draw_text(1, 0, "Date")
  draw_text(1, 1, "Host Name")
end

#draw_base_text_level2Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/tochka/command/tochka-miniui.rb', line 91

def draw_base_text_level2
  draw_text(2, 0, "State")
  draw_text(2, 1, "File Name")
  draw_text(2, 2, "File Size")
  draw_text(2, 3, "Duration")
  draw_text(2, 4, "Current Channel")
  draw_text(2, 5, "Channel Walk")
  draw_text(2, 6, "Size per sec")
  draw_text(2, 7, "Utilization")
end

#draw_base_text_level3Object



85
86
87
88
89
# File 'lib/tochka/command/tochka-miniui.rb', line 85

def draw_base_text_level3
  draw_text(3, 0, "Disk Usage")
  draw_text(3, 1, "Memory Usage")
  draw_text(3, 2, "CPU Usage")
end

#draw_base_text_level4Object



102
103
104
105
106
107
# File 'lib/tochka/command/tochka-miniui.rb', line 102

def draw_base_text_level4
  @font.draw_solid_utf8(@screen, "START | STOP | MODE1 | MODE2",
                        LEVEL4_BASE_X, LEVEL4_BASE_Y, 255, 255, 255)
  @font.draw_solid_utf8(@screen, "stop count => #{@stop_count}",
                        LEVEL4_BASE_X + 200, LEVEL4_BASE_Y, 255, 0, 0)
end

#draw_latticeObject



62
63
64
65
66
# File 'lib/tochka/command/tochka-miniui.rb', line 62

def draw_lattice
  @screen.draw_line(9, 35, 306 + 9, 35, @colors[COLOR_WHITE], true)
  @screen.draw_line(9, 185, 306 + 9, 185, @colors[COLOR_WHITE], true)
  @screen.draw_line(9, 225, 306 + 9, 225, @colors[COLOR_WHITE], true)
end

#draw_text(level, idx, text, color = COLOR_WHITE) ⇒ Object



75
76
77
78
# File 'lib/tochka/command/tochka-miniui.rb', line 75

def draw_text level, idx, text, color=COLOR_WHITE
  @font.draw_solid_utf8(@screen, text.to_s, *get_text_pos(level, idx),
                        255, 255, 255)
end

#draw_var(level, idx, var, color = COLOR_GREEN) ⇒ Object



182
183
184
185
# File 'lib/tochka/command/tochka-miniui.rb', line 182

def draw_var level, idx, var, color=COLOR_GREEN
  @font.draw_solid_utf8(@screen, var.to_s, *get_var_pos(level, idx),
                        0, 255, 0)
end

#get_level1_pos(idx) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/tochka/command/tochka-miniui.rb', line 109

def get_level1_pos idx
  # 0 1
  offset_x = LEVEL1_BASE_X
  offset_y = 0
  if idx % 2 == 1
    offset_x = 160
  end
  offset_y = LEVEL1_BASE_Y
  return [offset_x, offset_y]
end

#get_level1_var_pos(idx) ⇒ Object



154
155
156
157
# File 'lib/tochka/command/tochka-miniui.rb', line 154

def get_level1_var_pos idx
  x, y = get_level1_pos(idx)
  return [x + VAR_OFFSET_X, y + VAR_OFFSET_Y]
end

#get_level2_pos(idx) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/tochka/command/tochka-miniui.rb', line 120

def get_level2_pos idx
  # 0 1
  # 2 3
  # 4 5
  # 6 7
  offset_x = LEVEL1_BASE_X
  offset_y = 0
  if idx % 2 == 1
    offset_x += OFFSET_X
  end
  offset_y = LEVEL2_BASE_Y + OFFSET_Y * (idx / 2)
  return [offset_x, offset_y]
end

#get_level2_var_pos(idx) ⇒ Object



159
160
161
162
# File 'lib/tochka/command/tochka-miniui.rb', line 159

def get_level2_var_pos idx
  x, y = get_level2_pos(idx)
  return [x + VAR_OFFSET_X, y + VAR_OFFSET_Y]
end

#get_level3_pos(idx) ⇒ Object



134
135
136
137
138
139
# File 'lib/tochka/command/tochka-miniui.rb', line 134

def get_level3_pos idx
  # 0 1 2
  offset_x = LEVEL3_BASE_X + 100 * (idx % 3)
  offset_y = LEVEL3_BASE_Y
  return [offset_x, offset_y]
end

#get_level3_var_pos(idx) ⇒ Object



164
165
166
167
# File 'lib/tochka/command/tochka-miniui.rb', line 164

def get_level3_var_pos idx
  x, y = get_level3_pos(idx)
  return [x + VAR_OFFSET_X, y + VAR_OFFSET_Y]
end

#get_text_pos(level, idx) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/tochka/command/tochka-miniui.rb', line 141

def get_text_pos level, idx
  case level
  when 1
    return get_level1_pos(idx)
  when 2
    return get_level2_pos(idx)
  when 3
    return get_level3_pos(idx)
  else
    raise "idx #{idx} is invalid"
  end
end

#get_var_pos(level, idx) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/tochka/command/tochka-miniui.rb', line 169

def get_var_pos level, idx
  case level
  when 1
    return get_level1_var_pos(idx)
  when 2
    return get_level2_var_pos(idx)
  when 3
    return get_level3_var_pos(idx)
  else
    raise "idx #{idx} is invalid"
  end
end

#runObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/tochka/command/tochka-miniui.rb', line 212

def run
  draw_all
  # background
  @screen.fill_rect(0, 0, WIDTH, HEIGHT, @colors[COLOR_BLACK])
  draw_lattice
  draw_base_text
  prev = Time.now.to_i

  while true
    sleep 0.05

    while event = SDL::Event.poll
    end

    buttons = @pitft_button.button_all_edge()
    if buttons[0] == true
      @ca.start_capture()
    elsif buttons[1] == true
      case @stop_count
      when 0
        $log.info("TOCHKA-UI: stop stage 1")
        @stop_count = 1
      when 3
        $log.info("TOCHKA-UI: stop stage done, activate halt")
        @ca.stop_capture
        @stop_count = 0
      else
        @stop_count = 0
      end
    elsif buttons[2] == true
      case @stop_count
      when 1
        $log.info("TOCHKA-UI: stop stage 2")
        @stop_count = 2
      else
        @stop_count = 0
        @pitft_button.backlight_off()
      end
    elsif buttons[3] == true
      case @stop_count
      when 2
        $log.info("TOCHKA-UI: stop stage 3")
        @stop_count = 3
      else
        @stop_count = 0
        @pitft_button.backlight_on()
      end
    end

    now = Time.now.to_i
    next if prev == now
    prev = now

    update_var

    draw_all
    @screen.update_rect(0, 0, 0, 0)
  end
end