Class: Msp430Bsl::Uart::PeripheralInterface
Constant Summary
collapse
- MIN_PACKET_SIZE =
6.freeze
0x80.freeze
1.freeze
- DATA_LEN_SIZE =
2.freeze
- CRC_SIZE =
2.freeze
- TOTAL_SIZE =
(HEADER_SIZE + DATA_LEN_SIZE + CRC_SIZE).freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
build_logger_from, crc16, crc8, normalize_log_level
Constructor Details
#initialize(header: nil, data_len: nil, data: nil, crc: nil) ⇒ PeripheralInterface
Returns a new instance of PeripheralInterface.
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 42
def initialize(header: nil, data_len: nil, data: nil, crc: nil)
raise Exceptions::PeripheralInterfaceDataNotArray if (data && !data.is_a?(Array))
@header =
@data_len = data_len
@data = data
@crc = crc ? crc : (data ? crc16(data) : nil)
@cmd_code
@partial_data = []
end
|
Instance Attribute Details
#cmd_kind ⇒ Object
Returns the value of attribute cmd_kind.
40
41
42
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40
def cmd_kind
@cmd_kind
end
|
#crc ⇒ Object
Returns the value of attribute crc.
40
41
42
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40
def crc
@crc
end
|
#data ⇒ Object
Returns the value of attribute data.
40
41
42
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40
def data
@data
end
|
#data_len ⇒ Object
Returns the value of attribute data_len.
40
41
42
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40
def data_len
@data_len
end
|
#errors ⇒ Object
Returns the value of attribute errors.
40
41
42
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40
def errors
@errors
end
|
Returns the value of attribute header.
40
41
42
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40
def
@header
end
|
#packet ⇒ Object
Returns the value of attribute packet.
40
41
42
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 40
def packet
@packet
end
|
Class Method Details
.parse(raw_data) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 27
def parse(raw_data)
raise Exceptions::PeripheralInterfaceParseRawDataNotArray unless raw_data.is_a?(Array)
raise Exceptions::PeripheralInterfaceSize, raw_data.size unless raw_data.size >= MIN_PACKET_SIZE
= raw_data[0]
data_len = raw_data[2] << 8 | raw_data[1]
data = raw_data[3, data_len]
crc = raw_data[-1] << 8 | raw_data[-2]
new header: , data_len: data_len, data: data, crc: crc
end
|
.wrap(command) ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 19
def wrap(command)
unless command.is_a?(Command)
raise Exceptions::PeripheralInterfaceWrapNotACommand, command
end
new header: OK_HEADER, data_len: command.packet.size, data: command.packet
end
|
Instance Method Details
#crc_ok? ⇒ Boolean
54
55
56
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 54
def crc_ok?
data && crc == crc16(data)
end
|
#data_len_ok? ⇒ Boolean
62
63
64
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 62
def data_len_ok?
data&.length == data_len
end
|
58
59
60
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 58
def
== OK_HEADER
end
|
#length ⇒ Object
66
67
68
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 66
def length
packet.length
end
|
#push(val) ⇒ Object
Also known as:
<<
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 81
def push(val)
@partial_data.append *val
if ! && @partial_data.size >= HEADER_SIZE
@header = @partial_data.shift
end
if && !data_len && @partial_data.size >= DATA_LEN_SIZE
values = @partial_data.shift DATA_LEN_SIZE
@data_len = values[0] | (values[1] << 8)
end
if data_len && (data.nil? || data.empty?) && @partial_data.size >= data_len
@data = @partial_data.shift data_len
end
if data && !crc && @partial_data.size >= CRC_SIZE
values = @partial_data.shift CRC_SIZE
@crc = values[0] | (values[1] << 8)
end
end
|
#to_hex_ary_str ⇒ Object
106
107
108
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 106
def to_hex_ary_str
packet.to_hex
end
|
#to_response ⇒ Object
114
115
116
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 114
def to_response
Response.new data
end
|
#to_uart ⇒ Object
110
111
112
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 110
def to_uart
packet.to_chr_string
end
|
#valid? ⇒ Boolean
118
119
120
121
122
123
124
125
|
# File 'lib/msp430_bsl/uart/peripheral_interface.rb', line 118
def valid?
@errors = []
@errors << [:header, 'Header NOK'] unless
@errors << [:data_len, "'data_len' value (#{data_len}) differs from current data length (#{data&.length})"] unless data_len_ok?
@errors << [:crc, 'CRC NOK'] unless crc_ok?
@errors.empty?
end
|