class Device::IO

Constants

ALPHA
BACK
CANCEL
CLEAR
DEFAULT_TIMEOUT
DOWN
EIGHT_NUMBER
ENTER
F1
F2
F3
F4
FIVE_NUMBER
FOUR_NUMBER
FUNC
IO_INPUT_ALPHA
IO_INPUT_DECIMAL
IO_INPUT_LETTERS
IO_INPUT_MASK
IO_INPUT_MONEY
IO_INPUT_NUMBERS
IO_INPUT_SECRET
KEYBOARD_DEFAULT
KEY_TIMEOUT
MASK_ALPHA
MASK_LETTERS
MASK_NUMBERS
NINE_NUMBER
NUMBERS
ONE_NUMBER
SEVEN_NUMBER
SHARP
SIX_NUMBER
THREE_NUMBER
TWO_NUMBER
UP
ZERO_NUMBER

Attributes

back_key[RW]
back_key_label[RW]
forward_key[RW]
forward_key_label[RW]
keys_range[RW]
keys_valid[RW]
timeout[RW]

Public Class Methods

change_next(text, mask_type = Device::IO::MASK_ALPHA) click to toggle source
# File lib/device/io.rb, line 241
def self.change_next(text, mask_type = Device::IO::MASK_ALPHA)
  char = text[-1]
  if char
    range = self.keys_range[mask_type].detect { |range| range.include?(char) }
    if range
      index = range.index(char)
      new_value = range[index+1]
      if new_value
        text[-1] = new_value
      else
        text[-1] = range[0]
      end
    end
  end
  text
end
check_mask(char) click to toggle source
# File lib/device/io.rb, line 295
def self.check_mask(char)
  case char
  when "9"
    Device::IO::MASK_NUMBERS
  when "A"
    Device::IO::MASK_LETTERS
  else # "a"
    Device::IO::MASK_ALPHA
  end
end
check_mask_type(text, options) click to toggle source
# File lib/device/io.rb, line 227
def self.check_mask_type(text, options)
  if options[:mode] == Device::IO::IO_INPUT_ALPHA
    Device::IO::MASK_ALPHA
  elsif options[:mode] == Device::IO::IO_INPUT_LETTERS
    Device::IO::MASK_LETTERS
  elsif options[:mode] == Device::IO::IO_INPUT_NUMBERS
    Device::IO::MASK_NUMBERS
  elsif options[:mode] == Device::IO::IO_INPUT_MASK
    check_mask(options[:mask_clean].to_s[text.size - 1])
  else
    Device::IO::MASK_ALPHA
  end
end
format(string, options) click to toggle source
# File lib/device/io.rb, line 267
def self.format(string, options)
  options[:label].to_s +
  if options[:mode] == IO_INPUT_MONEY || options[:mode] == IO_INPUT_DECIMAL
    number_to_currency(string, options)
  elsif options[:mode] == IO_INPUT_SECRET
    "*" * string.size
  elsif options[:mode] == IO_INPUT_MASK
    string.to_mask(options[:mask])
  else
    string
  end
end
get_format(min, max, options = {}) click to toggle source

Restricted to terminals, get strings and numbers. The switch method between uppercase, lowercase and number characters is to keep pressing a same button quickly. The timeout of this operation is 1 second.

@param min [Fixnum] Minimum length of the input string. @param max [Fixnum] Maximum length of the input string (127 bytes maximum). @param options [Hash]

:value - Represent the current value, to be initially used.

:precision - Sets the level of precision (defaults to 2).

:separator - Sets the separator between the units (defaults to “.”).

:delimiter - Sets the thousands delimiter (defaults to “,”).

:label - Sets the label display before currency, eg.: “U$:”, “R$:”

:mask - If mode IO_INPUT_MASK a mask should send

(only numbers and letters are allowed), eg.: "9999-AAAA"

:mode - Define input modes:

:numbers (IO_INPUT_NUMBERS) - Only number.
:letters (IO_INPUT_LETTERS) - Only Letters.
:alpha (IO_INPUT_ALPHA) - Letters and numbers.
:secret (IO_INPUT_SECRET) - Secret *.
:decimal (IO_INPUT_DECIMAL) - Decimal input, only number.
:money (IO_INPUT_MONEY) - Money input, only number.
:mask (IO_INPUT_MASK) - Custom mask.

@return [String] buffer read from keyboard

# File lib/device/io.rb, line 137
def self.get_format(min, max, options = {})
  set_default_format_option(options)
  key = text = options[:value] || ""

  while key != CANCEL
    Device::Display.clear options[:line]
    Device::Display.print_line format(text, options), options[:line], options[:column]
    key = getc
    if key == BACK
      text = text[0..-2]
    elsif key == KEY_TIMEOUT
      return KEY_TIMEOUT
    elsif key == ENTER
      return text
    elsif key == CANCEL
      return CANCEL
    elsif key == F1 || key == DOWN || key == UP || key == ALPHA
      change_next(text, check_mask_type(text, options))
      next
    elsif text.size >= max
      next
    elsif insert_key?(key, options)
      text << key
    end
  end
end
get_format_or_touchscreen_action(max, touch_map, options = {}) click to toggle source
# File lib/device/io.rb, line 164
def self.get_format_or_touchscreen_action(max, touch_map, options = {})
  set_default_format_option(options)
  key = text = options[:value] || ""
  time = Time.now + (options[:timeout] || 30000) / 1000
  ret = {}
  touch_clear
  Device::Display.clear options[:line]
  Device::Display.print_line format(text, options), options[:line], options[:column]
  loop do
    line_x, line_y = getxy_stream(100)
    if line_x && line_y
      ret = parse_touchscreen(touch_map, line_x, line_y)
      break(ret) if ret.include?(:touch_action)
    else
      key = getc(100)
      if key == BACK
        text = text[0..-2]
        Device::Display.clear options[:line]
        Device::Display.print_line format(text, options), options[:line], options[:column]
      elsif options[:timeout_enabled] && time < Time.now
        ret[:timeout] = Device::IO::KEY_TIMEOUT
        break(ret)
      elsif key == ENTER
        ret[:text] = text
        break(ret)
      elsif key == CANCEL
        ret[:cancel] = Device::IO::CANCEL
        break(ret)
      elsif key == F1 || key == DOWN || key == UP || key == ALPHA
        change_next(text, check_mask_type(text, options))
        next
      elsif text.size >= max
        next
      elsif insert_key?(key, options)
        text << key
        Device::Display.clear options[:line]
        Device::Display.print_line format(text, options), options[:line], options[:column]
      end
    end
  end
end
getc(timeout = self.timeout) click to toggle source

Read 1 byte on keyboard, wait until be pressed

@param timeout [Fixnum] Timeout in milliseconds to wait for key. If not sent the default timeout is 30_000. If nil should be blocking.

@return [String] key read from keyboard

Calls superclass method
# File lib/device/io.rb, line 265
def self.getc(timeout = self.timeout); super(timeout); end
getxy() click to toggle source

> {“return” => 1, “x” => 10, “y” => 50}

Calls superclass method
# File lib/device/io.rb, line 281
def self.getxy
  super
end
insert_key?(key, options) click to toggle source
# File lib/device/io.rb, line 285
def self.insert_key?(key, options)
  if options[:mode] == IO_INPUT_MONEY || options[:mode] == IO_INPUT_DECIMAL || options[:mode] == IO_INPUT_NUMBERS
    NUMBERS.include?(key)
  elsif options[:mode] != IO_INPUT_NUMBERS && options[:mode] != IO_INPUT_MONEY && options[:mode] != IO_INPUT_DECIMAL
    self.keys_valid.include? key
  else
    false
  end
end
parse_touchscreen(touch_map, line_x, line_y) click to toggle source
# File lib/device/io.rb, line 206
def self.parse_touchscreen(touch_map, line_x, line_y)
  ret = {}
  touch_map.each do |key, value|
    if value[:x].include?(line_x) && value[:y].include?(line_y)
      Device::Audio.beep(7, 60)
      ret[:touch_action] = key
    end
  end
  ret
end
set_default_format_option(options) click to toggle source
# File lib/device/io.rb, line 217
def self.set_default_format_option(options)
  options[:mode]   ||= IO_INPUT_LETTERS
  options[:line]   ||= 2
  options[:column] ||= 0

  if options[:mask]
    options[:mask_clean] = options[:mask].chars.reject{|ch| ch.match(/[^0-9A-Za-z]/) }.join
  end
end
setup_keyboard(map, options = {}) click to toggle source

Setup Keyboard Map

@param map [Array] contains the key map from 1 to 0 (0..9) @return [NilClass] nil.

@example

one_letters   = "qzQZ _,."
two_letters   = "abcABC"
three_letters = "defDEF"
four_letters  = "ghiGHI"
five_letters  = "jklJKL"
six_letters   = "mnoMNO"
seven_letters = "prsPRS"
eight_letters = "tuvTUV"
nine_letters  = "wxyWXY"
zero_letters  = "spSP"
map = [one_letters, two_letters, three_letters, four_letters, five_letters,
six_letters, seven_letters, eight_letters, nine_letters, zero_letters]
Device::IO.setup_keyboard(map)
# File lib/device/io.rb, line 74
def self.setup_keyboard(map, options = {})
  one_letters, two_letters, three_letters, four_letters, five_letters,
    six_letters, seven_letters, eight_letters, nine_letters, zero_letters =
    map

  range_number  = [
    ONE_NUMBER , TWO_NUMBER   , THREE_NUMBER , FOUR_NUMBER , FIVE_NUMBER ,
    SIX_NUMBER , SEVEN_NUMBER , EIGHT_NUMBER , NINE_NUMBER , ZERO_NUMBER
  ]
  range_letters = [
    one_letters, two_letters, three_letters, four_letters, five_letters,
    six_letters, seven_letters, eight_letters, nine_letters, zero_letters
  ]
  range_alpha = [
    ONE_NUMBER   + one_letters, TWO_NUMBER     + two_letters,
    THREE_NUMBER + three_letters, FOUR_NUMBER  + four_letters,
    FIVE_NUMBER  + five_letters, SIX_NUMBER    + six_letters,
    SEVEN_NUMBER + seven_letters, EIGHT_NUMBER + eight_letters,
    NINE_NUMBER  + nine_letters, ZERO_NUMBER   + zero_letters
  ]
  @keys_range = {MASK_ALPHA => range_alpha, MASK_LETTERS => range_letters, MASK_NUMBERS => range_number}
  @keys_valid = range_alpha.flatten.join

  self.back_key          = options[:back_key]          || Device::IO::F1
  self.back_key_label    = options[:back_key_label]    || " F1 "
  self.forward_key       = options[:forward_key]       || Device::IO::F2
  self.forward_key_label = options[:forward_key_label] || " F2 "
end