Class: AmstradGpt::Amstrad

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

Instance Method Summary collapse

Constructor Details

#initialize(tty:, base_sleep_duration: 0.1) ⇒ Amstrad

Returns a new instance of Amstrad.



5
6
7
8
9
# File 'lib/amstrad_gpt/amstrad.rb', line 5

def initialize(tty:, base_sleep_duration: 0.1)
  @tty = tty
  @base_sleep_duration = base_sleep_duration
  setup_mutable_state
end

Instance Method Details

#receive_messagesObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/amstrad_gpt/amstrad.rb', line 28

def receive_messages
  Thread.new do
    loop do
      break unless @running

      message = maybe_message?

      if message
        yield message if message.length.positive?
      end

      sleep(base_sleep_duration * rand)
    end
  end
end

#reply(message) ⇒ Object



22
23
24
25
26
# File 'lib/amstrad_gpt/amstrad.rb', line 22

def reply(message)
  mutex.synchronize do
    interface.write(message)
  end
end

#startObject



11
12
13
# File 'lib/amstrad_gpt/amstrad.rb', line 11

def start
  @reader_thread = Thread.new { read_bytes_loop }
end

#stopObject



15
16
17
18
19
20
# File 'lib/amstrad_gpt/amstrad.rb', line 15

def stop
  @running = false
  @reader_thread.join
  @reader_thread = nil
  interface.shutdown
end