Module: Msp430Bsl::Utils

Included in:
HexLine, Msp430Bsl::Uart::PeripheralInterface, Msp430Bsl::Uart::PeripheralInterface
Defined in:
lib/msp430_bsl/utils.rb

Instance Method Summary collapse

Instance Method Details

#build_logger_from(opts) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/msp430_bsl/utils.rb', line 24

def build_logger_from(opts)
  logto = if opts[:logfile]
            File.expand_path opts[:logfile]
          else
            STDOUT
          end

  Logger.new logto, level: normalize_log_level(opts[:loglevel])
end

#crc16(data) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/msp430_bsl/utils.rb', line 6

def crc16(data)
  raise ArgumentError, 'data must be an Array' unless data.is_a?(Array)

  crc = 0xFFFF

  data.each do |byte|
    x = (crc >> 8 ^ byte) & 0xFF
    x ^= x >> 4
    crc = (crc << 8) ^ (x << 12) ^ (x << 5) ^ x
  end

  crc & 0xFFFF
end

#crc8(data) ⇒ Object



20
21
22
# File 'lib/msp430_bsl/utils.rb', line 20

def crc8(data)
  (~data.reduce(:+) + 1) & 0xFF
end

#normalize_log_level(level) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/msp430_bsl/utils.rb', line 34

def normalize_log_level(level)
  case level
  when :debug, ::Logger::DEBUG, 'debug', 'd' then ::Logger::DEBUG
  when :info,  ::Logger::INFO,  'info', 'i'  then ::Logger::INFO
  when :warn,  ::Logger::WARN,  'warn', 'w'  then ::Logger::WARN
  when :error, ::Logger::ERROR, 'error', 'e' then ::Logger::ERROR
  when :fatal, ::Logger::FATAL, 'fatal', 'f' then ::Logger::FATAL
  else
    ENV['LOG_LEVEL'] || Logger::DEBUG
  end
end