ant-wireless 0.1.0.pre.20210810141303 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,8 +13,8 @@ require 'ant/mixins'
13
13
  # Refs:
14
14
  # * 9.5.6.1 Channel Response / Event (0x40) [ANT Message Protocol and Usage, Rev 5.1]
15
15
  module Ant::Channel::EventCallbacks
16
- extend Loggability
17
- include Ant::DataUtilities
16
+ extend Loggability,
17
+ Ant::DataUtilities
18
18
 
19
19
 
20
20
  # Mapping of response message IDs to handler methods
@@ -62,13 +62,9 @@ module Ant::Channel::EventCallbacks
62
62
  end
63
63
 
64
64
 
65
- ###############
66
- module_function
67
- ###############
68
-
69
65
  ### Default callback hook -- handles event callbacks.
70
66
  def handle_event_callback( channel_num, event_id, data )
71
- handler_method = HANDLER_METHODS[ event_id ] or
67
+ handler_method = Ant::Channel::EventCallbacks::HANDLER_METHODS[ event_id ] or
72
68
  raise "Unhandled channel event %p" % [ event_id ]
73
69
 
74
70
  if self.respond_to?( handler_method )
@@ -81,10 +77,10 @@ module Ant::Channel::EventCallbacks
81
77
 
82
78
  ### Handle an TX event.
83
79
  def on_event_tx( channel_num, data )
84
- # self.log.info "Broadcast message on channel %d was transmitted." % [ channel_num ]
85
- data = SecureRandom.bytes( 8 )
86
- self.log.info "Sending our own broadcast data: \n%s" % [ hexdump(data) ]
87
- self.send_broadcast_data( data )
80
+ channel = Ant::Channel.registry[ channel_num ]
81
+ self.log.debug "%p ready for transmission." % [ channel ]
82
+ ident = [ 1, 33 ].pack( "CC" )
83
+ channel.send_broadcast_data( ident )
88
84
  end
89
85
 
90
86
 
@@ -148,7 +144,7 @@ module Ant::Channel::EventCallbacks
148
144
  usDeviceNumber = data.bytes[10] | (data.bytes[11] << 8)
149
145
  ucDeviceType = data.bytes[12]
150
146
  ucTransmissionType = data.bytes[13]
151
- self.log.info "Got an acknowledge on Chan ID(%d/%d/%d)" %
147
+ self.log.debug "Got an acknowledge on Chan ID(%d/%d/%d)" %
152
148
  [usDeviceNumber, ucDeviceType, ucTransmissionType]
153
149
  end
154
150
 
@@ -162,7 +158,7 @@ module Ant::Channel::EventCallbacks
162
158
  usDeviceNumber = data.bytes[10] | (data.bytes[11] << 8)
163
159
  ucDeviceType = data.bytes[12]
164
160
  ucTransmissionType = data.bytes[13]
165
- self.log.info "Got a burst on Chan ID(%d/%d/%d)" %
161
+ self.log.debug "Got a burst on Chan ID(%d/%d/%d)" %
166
162
  [usDeviceNumber, ucDeviceType, ucTransmissionType]
167
163
  end
168
164
 
@@ -176,7 +172,7 @@ module Ant::Channel::EventCallbacks
176
172
  usDeviceNumber = data.bytes[10] | (data.bytes[11] << 8)
177
173
  ucDeviceType = data.bytes[12]
178
174
  ucTransmissionType = data.bytes[13]
179
- self.log.info "Got a broadcast on Chan ID(%d/%d/%d)" %
175
+ self.log.debug "Got a broadcast on Chan ID(%d/%d/%d)" %
180
176
  [usDeviceNumber, ucDeviceType, ucTransmissionType]
181
177
  end
182
178
 
@@ -185,7 +181,7 @@ module Ant::Channel::EventCallbacks
185
181
 
186
182
 
187
183
  def on_event_rx_acknowledged( channel_num, data )
188
- self.log.info "Acknowledged: Rx:\n%s" % [ hexdump(data[1..9]) ]
184
+ self.log.debug "Acknowledged: Rx [%d]:\n%s" % [ data.bytes[0], Ant::DataUtilities.hexdump(data[1..9]) ]
189
185
  end
190
186
 
191
187
 
@@ -193,12 +189,12 @@ module Ant::Channel::EventCallbacks
193
189
  channel = (data.bytes[0] & CHANNEL_NUMBER_MASK) >> 5
194
190
  sequence_num = data.bytes[0] & SEQUENCE_NUMBER_MASK
195
191
 
196
- self.log.info "Burst (0x%02x): Rx: %d:\n%s" % [ channel, sequence_num, hexdump(data[1..9]) ]
192
+ self.log.debug "Burst (0x%02x): Rx: %d:\n%s" % [ channel, sequence_num, Ant::DataUtilities.hexdump(data[1..9]) ]
197
193
  end
198
194
 
199
195
 
200
196
  def on_event_rx_broadcast( channel_num, data )
201
- self.log.info "Broadcast: Rx:\n%s" % [ hexdump(data[1..9]) ]
197
+ self.log.debug "Broadcast: Rx [%d]:\n%s" % [ data.bytes[0], Ant::DataUtilities.hexdump(data[1..9]) ]
202
198
  end
203
199
 
204
200
 
data/lib/ant/channel.rb CHANGED
@@ -9,6 +9,7 @@ require 'ant' unless defined?( Ant )
9
9
  class Ant::Channel
10
10
  extend Loggability
11
11
 
12
+
12
13
  # The default network number
13
14
  DEFAULT_NETWORK_NUMBER = 0
14
15
 
@@ -20,7 +21,8 @@ class Ant::Channel
20
21
  # Autoloads
21
22
  #
22
23
 
23
- autoload :EventCallbacks, 'ant/channel/event_callbacks'
24
+ require 'ant/channel/event_callbacks'
25
+ include Ant::Channel::EventCallbacks
24
26
 
25
27
 
26
28
  # Loggability API -- log to the Ant logger
@@ -44,23 +46,68 @@ class Ant::Channel
44
46
 
45
47
 
46
48
  ### Set up the given +mod+ as the handler module for channel events.
47
- def set_event_handlers( object=Ant::Channel::EventCallbacks )
49
+ def set_event_handlers( object=self )
48
50
  self.on_event( &object.method(:handle_event_callback) )
49
51
  end
50
52
 
51
53
 
54
+ ### Return the ANT channel ID if one has been assigned.
55
+ def channel_id
56
+ device_number = self.device_number or return nil
57
+ device_type = self.device_type & 0x7f
58
+ pairing_bit = self.device_type & 0x80
59
+ transmission_type = self.transmission_type
60
+
61
+ return "%d/%d/%d%s" % [
62
+ device_number,
63
+ device_type,
64
+ transmission_type,
65
+ pairing_bit.nonzero? ? '+' : '',
66
+ ]
67
+ end
68
+
69
+
70
+ ### Return a human-readable description of the channel type.
71
+ def channel_type_description
72
+ case self.channel_type
73
+ when Ant::PARAMETER_RX_NOT_TX
74
+ return :slave
75
+ when Ant::PARAMETER_TX_NOT_RX
76
+ return :master
77
+ when Ant::PARAMETER_SHARED_CHANNEL
78
+ return :shared
79
+ when Ant::PARAMETER_NO_TX_GUARD_BAND
80
+ return :no_tx_guard_band
81
+ when Ant::PARAMETER_ALWAYS_RX_WILD_CARD_SEARCH_ID
82
+ return :always_rx_wild_card_search_id
83
+ when Ant::PARAMETER_RX_ONLY
84
+ return :rx_only
85
+ else
86
+ return nil
87
+ end
88
+ end
89
+
90
+
91
+ ### Returns +true+ if the channel is not closed.
92
+ def open?
93
+ return !self.closed?
94
+ end
95
+
96
+
52
97
  ### Return a human-readable version of the object suitable for debugging.
53
98
  def inspect
54
- return "#<%p:%#x {%d} %#02x on network %d: %d%s>" % [
99
+ return "#<%p:%#x %s {%s} #%d @%dMHz on network %d%s>" % [
55
100
  self.class,
56
101
  self.object_id,
102
+ self.channel_type_description,
103
+ self.channel_id || '-',
57
104
  self.channel_number,
58
- self.channel_type,
105
+ self.rf_frequency + 2400,
59
106
  self.network_number,
60
- self.extended_options,
61
107
  self.closed? ? " (closed)" : "",
62
108
  ]
63
109
  end
64
110
 
65
111
  end # class Ant::Channel
66
112
 
113
+