Class: AgriController::Bit

Inherits:
Object
  • Object
show all
Defined in:
lib/agri-controller/bit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bit = 0) ⇒ Bit

Returns a new instance of Bit.



27
28
29
# File 'lib/agri-controller/bit.rb', line 27

def initialize(bit=0)
  @bit=bit
end

Instance Attribute Details

#bitObject

Returns the value of attribute bit.



26
27
28
# File 'lib/agri-controller/bit.rb', line 26

def bit
  @bit
end

Instance Method Details

#bit_off(bit) ⇒ Object Also known as: off



66
67
68
69
70
71
# File 'lib/agri-controller/bit.rb', line 66

def bit_off(bit)
  if on?(bit)
    @bit=@bit - 2**bit
  end
  @bit
end

#bit_on(bit) ⇒ Object Also known as: on



43
44
45
46
47
48
# File 'lib/agri-controller/bit.rb', line 43

def bit_on(bit)
  unless on?(bit)
    @bit+= 2**bit
  end
  @bit
end


39
40
41
# File 'lib/agri-controller/bit.rb', line 39

def blink(i)
  boolbit(!on?(i),i)
end

#boolbit(bool, bit) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/agri-controller/bit.rb', line 31

def boolbit(bool,bit)
  if bool
    bit_on(bit)
  else
    bit_off(bit)
  end
end

#helpObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/agri-controller/bit.rb', line 87

def help
  "##
  #class Bit
  #          is simple bit culcurater.
  #  if on or off
  #        on(bit) # => on bit
  #        on(1)   # =>2 (if @bit==0)
  #        on(1)   # =>3 (if @bit==1)
  #        on(1)   # =>7 (if @bit==5)
  #    bit_on(bit) # => on bit
  #
  #        off(1)  # =>0 (if @bit==2)
  #        off(1)  # =>1 (if @bit==3)
  #        off(1)  # =>5 (if @bit==7)
  #    bit_off(bit)# => off bit
  #
  #  if check_bit
  #     on?(bit)# =>true or false
  #    off?(bit)# =>true or false
  #
  #  boolbit(true,bit) # ==on(bit)
  #  boolbit(false,bit)# ==off(bit)
  #
  "
end

#off?(bit) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/agri-controller/bit.rb', line 84

def off?(bit)
  not(on?(bit))
end

#on?(bit) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/agri-controller/bit.rb', line 73

def on?(bit)
  str=@bit.to_s(2)
    case str.slice(-1-bit,1)
    when "1"
      true
    when "0"
      false
    else
      nil
    end
end

#tos(int = 6, bit_num = 16) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/agri-controller/bit.rb', line 50

def tos(int=6,bit_num=16)
  x=@bit.to_s(bit_num)
  
  # 17.tos(1,16) # => "1"
  # 17.tos(2,16) # => "11"
  (int-x.size).times do
    x="0"+x
  end
  
  if x.size > int
    x=x.slice(x.size-int,int)
  end
  
  return x.upcase
end