lib/rfbeam/cli.rb in rfbeam-0.5.1 vs lib/rfbeam/cli.rb in rfbeam-0.5.8
- old
+ new
@@ -1,6 +1,7 @@
# frozen_string_literal: true
+# rubocop:disable all
require 'thor'
require 'tty-table'
require 'tty-logger'
require 'tty-spinner'
@@ -13,35 +14,40 @@
desc 'list', 'List available radar modules'
def list
logger = TTY::Logger.new
devices = RfBeam.connected
- return logger.warn 'No Radar modules found.' if devices.empty?
+ logger.info 'No Radar modules found.' unless devices.count.positive?
table = TTY::Table.new(header: %w[id Path Version])
- devices.each.with_index { |path, index| table << [index.to_s, path, radar(index).sw_version] }
+ devices.each.with_index do |path, index|
+ init_radar(index)
+ table << ["#{index}", path, @radar.sw_version]
+ end
puts table.render(:ascii)
end
- desc 'config [RADAR_ID]', 'Shows the parameter setting for the Radar module'
+ desc 'config <radar_id>', 'Shows the parameter setting for the Radar module'
def config(radar_id)
- puts radar(radar_id).config
+ init_radar(radar_id)
+ puts @radar.config
end
- desc 'reset [RADAR_ID]', 'Shows the parameter setting for the Radar module'
+ desc 'reset <radar_id>', 'Shows the parameter setting for the Radar module'
def reset(radar_id)
- @logger.success 'Radar reset to factory defaults' if radar(radar_id).reset
+ init_radar(radar_id)
+ @logger.success 'Radar reset to factory defaults' if @radar.reset
end
- desc 'set_param [RADAR_ID] [KEY] [VALUE]', 'Set radar parameters, see readme for KEYS'
+ desc 'set_param <radar_id> <key> <value>', 'Set radar parameters, see readme for keys'
def set_param(radar_id, param, value)
- return @logger.warn("Invalid param: '#{param}'") unless Kld7::RADAR_PARAMETERS.include?(param.to_sym)
+ init_radar radar_id
+ return @logger.warn("Invalid param: '#{param}'") unless RfBeam::KLD7::RADAR_PARAMETERS.include?(param.to_sym)
- r = radar(radar_id)
- r.send("#{param}=", value.to_i)
- @logger.success r.formatted_parameter(param.to_sym)
+ @radar.send("#{param}=", value.to_i)
+ @logger.success "Set #{@radar.formatted_parameter(param.to_sym)}"
end
desc 'ddat <radar_id>', 'stream any valid detections, stop stream with q and enter'
option :stream, type: :boolean, aliases: '-s', desc: 'Stream the data from the device'
def ddat(radar_id)
@@ -61,47 +67,50 @@
else
puts "\n#{@radar.ddat}"
end
end
- desc 'tdat [RADAR_ID]', 'Display tracked target data'
- option :raw, type: :boolean, aliases: '-r', desc: 'Display raw data'
- def tdat(radar_id)
- cli = RfBeam::Kld7::CliOutput.new(radar_id)
- cli.display(:tdat, options)
- end
-
- desc 'pdat [RADAR_ID]', 'Display Tracked Targets'
+ desc 'pdat <radar_id>', 'Display Tracked Targets'
def pdat(radar_id)
- cli = RfBeam::Kld7::CliOutput.new(radar_id)
- cli.display(:pdat, options)
+ init_radar radar_id
+ puts @radar.pdat
end
desc 'rfft <radar_id>', 'Display the dopplar radar data as a plot'
option :stream, type: :boolean, aliases: '-s', desc: 'Stream the data from the device'
option :raw, type: :boolean, aliases: '-r', desc: 'Display raw data'
def rfft(radar_id)
- plotter = RfBeam::Kld7::CliOutput.new(radar_id)
- if options[:raw]
- print radar(radar_id).rfft
+ init_radar(radar_id)
+
+ case
+ when options[:stream]
+ streamer = RfBeam::KLD7::Streamer.new(@radar)
+ streamer.rfft
+ when options[:raw]
+ print @radar.rfft
else
- plotter.plot(:rfft, stream: options[:stream])
+ plot = rfft_plot(@radar)
+ p plot.render
end
end
private
- def radar(id)
+ def init_radar(id)
devices = RfBeam.connected
@logger = TTY::Logger.new
- return @logger.warning 'No Radar modules found.' unless devices.count.positive?
+ return @logger.warn 'No Radar modules found.' unless devices.count.positive?
@radar = RfBeam::K_ld7.new(devices[id.to_i])
end
def plot_data(data)
- { x: Array(-128...128), series1: data.shift(256).map { |value| value / 100 }, series2: data.shift(256).map { |value| value.to_i / 100 } }
+ {
+ x: Array(-128...128),
+ series1: data.shift(256).map { |value| value / 100 },
+ series2: data.shift(256).map { |value| value.to_i / 100 }
+ }
end
def monitor_keypress
@stop_streaming = false
loop do
@@ -114,10 +123,9 @@
end
def rfft_plot(radar)
speed = radar.max_speed
speed_label = radar.formatted_parameter(:max_speed)
- xlim = [speed - speed * 2, speed]
data = plot_data(radar.rfft)
plot =
UnicodePlot.lineplot(
data[:x],
data[:series1],