Class: Oschii::SerialDevice

Inherits:
Device
  • Object
show all
Defined in:
lib/oschii/serial_device.rb

Constant Summary collapse

BAUD_RATE =
115200
CancelSerialQuery =
Class.new(StandardError)

Instance Attribute Summary collapse

Attributes inherited from Device

#log_lines

Instance Method Summary collapse

Methods inherited from Device

#clear!, #config, #config_description, #config_name, #description, #device_details, #logger, #poke!, #raw_config=, #raw_device_details, #raw_status, #refresh, #save_config, #settings, #settings=, #tail, #update!, #upload_config, #uptime

Constructor Details

#initialize(serial) ⇒ SerialDevice

Returns a new instance of SerialDevice.



7
8
9
10
11
# File 'lib/oschii/serial_device.rb', line 7

def initialize(serial)
  @serial = serial
  @capturing_serial = false
  super()
end

Instance Attribute Details

#capturing_serialObject

Returns the value of attribute capturing_serial.



14
15
16
# File 'lib/oschii/serial_device.rb', line 14

def capturing_serial
  @capturing_serial
end

#serialObject (readonly)

Returns the value of attribute serial.



13
14
15
# File 'lib/oschii/serial_device.rb', line 13

def serial
  @serial
end

#serial_line_bufferObject

Returns the value of attribute serial_line_buffer.



14
15
16
# File 'lib/oschii/serial_device.rb', line 14

def serial_line_buffer
  @serial_line_buffer
end

Instance Method Details

#clear_logObject



185
186
187
# File 'lib/oschii/serial_device.rb', line 185

def clear_log
  serial_query 'clear log'
end

#config=(new_config) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/oschii/serial_device.rb', line 65

def config=(new_config)
  puts 'Querying serial port....'
  if serial_query('config=') == '>> Enter new configuration <<'
    puts 'Uploading configuration....'
    puts serial_query new_config.to_json
  end
  refresh
end

#inspectObject



193
194
195
# File 'lib/oschii/serial_device.rb', line 193

def inspect
  "<#{self.class.name}[#{name}] #{serial} (v#{version})>"
end

#ipObject



35
36
37
# File 'lib/oschii/serial_device.rb', line 35

def ip
  serial_query 'ip'
end

#logObject



181
182
183
# File 'lib/oschii/serial_device.rb', line 181

def log
  serial_query 'print log'
end

#nameObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/oschii/serial_device.rb', line 43

def name
  return @name if @name

  attempts = 3
  while attempts > 0
    @name = serial_query 'name'
    if @name.empty?
      attempts -= 1
    else
      attempts = 0
    end
  end
end

#name=(new_name) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/oschii/serial_device.rb', line 57

def name=(new_name)
  puts 'Querying serial port....'
  if serial_query('name=') == '>> Enter new name <<'
    puts serial_query new_name
  end
  refresh
end

#pokeObject



16
17
18
19
# File 'lib/oschii/serial_device.rb', line 16

def poke
  start_serial_capture
  serial_query('poke', timeout: 2) == 'Tickles!'
end

#purge_serialObject



110
111
112
113
# File 'lib/oschii/serial_device.rb', line 110

def purge_serial
  log_lines.clear
  self.serial_line_buffer = ''
end

#raw_configObject



74
75
76
# File 'lib/oschii/serial_device.rb', line 74

def raw_config
  serial_query 'config', timeout: 3
end

#raw_settingsObject



95
96
97
# File 'lib/oschii/serial_device.rb', line 95

def raw_settings
  serial_query 'settings'
end

#restartObject



39
40
41
# File 'lib/oschii/serial_device.rb', line 39

def restart
  serial_query 'restart'
end

#serial_portObject



177
178
179
# File 'lib/oschii/serial_device.rb', line 177

def serial_port
  @serial_port ||= Serial.new serial, BAUD_RATE
end

#serial_query(query, timeout: 3) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/oschii/serial_device.rb', line 99

def serial_query(query, timeout: 3)
  # puts "#> #{query}"
  purge_serial
  serial_port.write (query.empty? ? "\n" : query)
  sleep timeout
  result = log_lines.join
  log_lines.clear
  # puts "   #{result}"
  result
end

#start_ethernetObject



169
170
171
# File 'lib/oschii/serial_device.rb', line 169

def start_ethernet
  serial_query 'start ethernet'
end

#start_serial_captureObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/oschii/serial_device.rb', line 115

def start_serial_capture
  return if capturing_serial

  Thread.new do
    self.capturing_serial = true
    self.serial_line_buffer = ''
    log_lines.clear
    while capturing_serial
      input = serial_port.read 100
      input.each_char do |c|
        if c == "\n"
          log_lines << serial_line_buffer.gsub("\r", '')
          self.serial_line_buffer = ''
        else
          serial_line_buffer << c
        end
      end
      sleep 0.01
    end
  end
end

#start_wifi(ssid = nil, password = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/oschii/serial_device.rb', line 141

def start_wifi(ssid = nil, password = nil)
  begin
    if ssid.nil?
      ssid = prompt 'Enter SSID'
    end

    if password.nil?
      password = prompt 'Enter password', obscure: true
    end

    puts 'Querying serial port...'
    if serial_query('start wifi') == '>> Enter new Wifi SSID <<'
      puts 'Sending SSID...'
      if serial_query(ssid) == '>> Enter new Wifi Password <<'
        puts 'Sending password...'
        puts serial_query(password)
      end
    end

  rescue CancelSerialQuery
    puts 'Cancelled'
  end
end

#statusObject



78
79
80
# File 'lib/oschii/serial_device.rb', line 78

def status
  JSON.parse(serial_query('status'))
end

#stop_ethernetObject



173
174
175
# File 'lib/oschii/serial_device.rb', line 173

def stop_ethernet
  serial_query 'stop ethernet'
end

#stop_serial_captureObject



137
138
139
# File 'lib/oschii/serial_device.rb', line 137

def stop_serial_capture
  self.capturing_serial = false
end

#stop_wifiObject



165
166
167
# File 'lib/oschii/serial_device.rb', line 165

def stop_wifi
  serial_query 'stop wifi'
end

#to_sObject



189
190
191
# File 'lib/oschii/serial_device.rb', line 189

def to_s
  inspect
end

#update_settings(new_settings = nil, file: nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/oschii/serial_device.rb', line 82

def update_settings(new_settings = nil, file: nil)
  unless file.nil?
    puts 'Reading file....'
    new_settings = JSON.parse(File.read(file))
  end
  puts 'Querying serial port....'
  if serial_query('settings=') == '>> Enter new settings <<'
    puts 'Uploading settings....'
    puts serial_query new_settings.to_json
    refresh
  end
end

#versionObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/oschii/serial_device.rb', line 21

def version
  return @version if @version

  attempts = 3
  while attempts > 0
    @version = serial_query 'version'
    if @version.empty?
      attempts -= 1
    else
      attempts = 0
    end
  end
end