Class: MorsecodeKeypad

Inherits:
Object
  • Object
show all
Defined in:
lib/morsecode_keypad.rb

Constant Summary collapse

DASH =
'1'
DOT =
'2'
SEPARATOR =
'4'

Instance Method Summary collapse

Constructor Details

#initialize(dash: 4, dot: 17, separator: 27, sendx: 22, notifier: nil) ⇒ MorsecodeKeypad

Returns a new instance of MorsecodeKeypad.



14
15
16
17
18
19
20
21
22
23
# File 'lib/morsecode_keypad.rb', line 14

def initialize(dash: 4, dot: 17, separator: 27, sendx: 22, notifier: nil)

  @pins = {dash: dash, dot: dot, separator: separator, sendx: sendx}
  
  @notifier = notifier
  @dash_pin, @dot_pin, @separator_pin, @send_pin = \
    [dash, dot, separator, sendx].map {|pin| RPiPinIn.new(pin, pull: :up)}
  @mc = ''
  
end

Instance Method Details

#startObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/morsecode_keypad.rb', line 25

def start()

  Thread.new do
    @dash_pin.watch_high do |v|

      puts "dash: " +  Time.now.to_s if @notifier.nil?
      on_dash()

    end
  end

  Thread.new do
    @dot_pin.watch_high do |v|

      puts "dot: " +  Time.now.to_s if @notifier.nil?
      on_dot()

    end
  end

  Thread.new do
    @separator_pin.watch_high do |v|

      puts "separator: " +  Time.now.to_s if @notifier.nil?
      on_separator()

    end
  end

  @send_pin.watch_high do |v|

    puts "send: " +  Time.now.to_s if @notifier.nil?
    on_send()

  end

end

#testObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/morsecode_keypad.rb', line 68

def test()
  
  pins = i(dash dot separator sendx)
  buttons = {}
  i = 0

  Thread.new do 
    @dash_pin.watch_high do |v| 
      
      buttons[pins[i]] = @pins[:dash]
      puts pins[i].to_s + ": GPIO " +  buttons[pins[i]].to_s 
      i += 1
      
      puts buttons.inspect if i > 3
      Thread.stop
      
    end
  end
  
  Thread.new do 
    @dot_pin.watch_high do |v| 
      
      buttons[pins[i]] = @pins[:dot]
      puts pins[i].to_s + ": GPIO " +  buttons[pins[i]].to_s
      i += 1
      
      puts buttons.inspect if i > 3
      Thread.stop
      
    end
  end    
  
  Thread.new do 
    @separator_pin.watch_high do |v| 
      
      buttons[pins[i]] = @pins[:separator]
      puts pins[i].to_s + ": GPIO " +  buttons[pins[i]].to_s
      i += 1
      
      puts buttons.inspect if i > 3
      Thread.stop
      
    end
  end      
  
  Thread.new do 
    @send_pin.watch_high do |v| 
      
      buttons[pins[i]] = @pins[:sendx]
      puts pins[i].to_s + ": GPIO " +  buttons[pins[i]].to_s
      i += 1
      
      puts buttons.inspect if i > 3
      Thread.stop
      
    end
  end      

end