can_messenger 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6aab35f0ccd4fcbd716ddb9cb553555c1c524eef3ca917d5cf43539b8bb55ff0
4
- data.tar.gz: 1f2ebb1af8cf741ceba5c1e2033faa807ed4f8f58e1c44dcbacfe7adba60a9dc
3
+ metadata.gz: d80a738eccd9344c8de4dac35f0e965555cbb88d4eda7ead334acba2ee8e63c1
4
+ data.tar.gz: c7a8ce5a726fd04e959b74cbfe92ee292fac37962e98251e6ffdce8102ab39bc
5
5
  SHA512:
6
- metadata.gz: 4f20347545fdfa495b80fc7475e611e0aa80bcc47a8a4bca26844f07a677327949001dd28b24422a402a49bd864ce2ecdde00968cc0ee9f150da4a968d1617a6
7
- data.tar.gz: 712bbb367ea096d2a9e57d1649bafb75c90b51652a789a907cdc1f8984ea965959b24f46ac083340816364ac023cf0efae10c1b7e378f4535c40e3324045f248
6
+ metadata.gz: a5e48323462fd6657e36b55bb7078f6a3499467bb8c3ea98d214a35f58d88f4070adbec9af33571f4ffaa8b8b2dd73d6a6fcc2f215ba0227ae34793b0da00d90
7
+ data.tar.gz: 1364b4df8e59903104fdc063d47629a948503020f0685d4d0babbb1de7bb61cde68a53665fbe395fa7a1d1e77e4b724c675dafb33fda6ca6b567b7ec407c0f53
data/README.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # CanMessenger
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/can_messenger.svg?icon=si%3Arubygems&icon_color=%23e77682)](https://badge.fury.io/rb/can_messenger)
4
+ [![Build Status](https://github.com/fk1018/can_messenger/actions/workflows/ruby.yml/badge.svg)](https://github.com/fk1018/can_messenger/actions)
5
+ [![Test Coverage](https://codecov.io/gh/fk1018/can_messenger/branch/main/graph/badge.svg)](https://codecov.io/gh/fk1018/can_messenger)
6
+ ![Status](https://img.shields.io/badge/status-stable-green)
7
+ [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
8
+ ![Gem Total Downloads](https://img.shields.io/gem/dt/can_messenger)
9
+
3
10
  `can_messenger` is a Ruby gem that provides an interface for communicating over the CAN bus, allowing users to send and receive CAN messages. This gem is designed for developers who need an easy way to interact with CAN-enabled devices.
4
11
 
5
12
  ## Installation
@@ -54,8 +61,38 @@ To listen for incoming messages, set up a listener:
54
61
 
55
62
  ```ruby
56
63
  messenger.start_listening do |message|
57
- puts "Received: ID=#{message[:id]}, Data=#{message[:data]}"
64
+ puts "Received: ID=#{message[:id]}, Data=#{message[:data]}"
65
+ end
66
+ ```
67
+
68
+ #### Listening with Filters
69
+
70
+ The `start_listening` method supports filtering incoming messages based on CAN ID:
71
+
72
+ Single CAN ID:
73
+
74
+ ```ruby
75
+ messenger.start_listening(filter: 0x123) do |message|
76
+ puts "Received filtered message: #{message}"
77
+ end
78
+ ```
79
+
80
+ Range of CAN IDs:
81
+
82
+ ```ruby
83
+ messenger.start_listening(filter: 0x100..0x200) do |message|
84
+ puts "Received filtered message: #{message}"
85
+ end
86
+
87
+ ```
88
+
89
+ Array of CAN IDs:
90
+
91
+ ```ruby
92
+ messenger.start_listening(filter: [0x123, 0x456, 0x789]) do |message|
93
+ puts "Received filtered message: #{message}"
58
94
  end
95
+
59
96
  ```
60
97
 
61
98
  ### Stopping the Listener
@@ -92,4 +129,4 @@ The gem is available as open-source under the terms of the MIT License.
92
129
 
93
130
  ## Author
94
131
 
95
- Developed by Fredrick Khoury.
132
+ Developed by fk1018.
@@ -43,16 +43,23 @@ module CanMessenger
43
43
 
44
44
  # Continuously listens for CAN messages on the specified interface.
45
45
  #
46
- # Yields each message as a hash with keys :id and :data.
46
+ # This method listens for incoming CAN messages and applies an optional filter.
47
+ # The filter can be a specific CAN ID or a range of IDs. Only messages that match
48
+ # the filter are yielded to the provided block.
49
+ #
50
+ # @param [Integer, Range, nil] filter Optional filter for CAN IDs. Pass a single ID (e.g., 0x123)
51
+ # or a range (e.g., 0x100..0x200). If no filter is provided, all messages are processed.
47
52
  # @yield [Hash] Each received message in the format { id: Integer, data: Array<Integer> }.
48
53
  # @return [void]
49
- def start_listening
54
+ def start_listening(filter: nil)
50
55
  @logger.info("Started listening on #{@can_interface}")
51
56
  socket = create_socket
52
57
  while @listening
53
58
  message = receive_message(socket)
54
59
  break unless message # Exit loop if no message
55
60
 
61
+ next if filter && !matches_filter?(message[:id], filter) # Apply filter if specified
62
+
56
63
  yield(message)
57
64
  end
58
65
  ensure
@@ -115,5 +122,29 @@ module CanMessenger
115
122
  @logger.error("Error parsing CAN frame: #{e}")
116
123
  nil
117
124
  end
125
+
126
+ # Checks if a message ID matches the given filter.
127
+ #
128
+ # Supported filters:
129
+ # - A single ID (e.g., 0x123)
130
+ # - A range of IDs (e.g., 0x100..0x200)
131
+ # - An array of IDs (e.g., [0x123, 0x200, 0x300])
132
+ #
133
+ # @param [Integer] message_id The ID of the incoming CAN message.
134
+ # @param [Integer, Range, Array<Integer>, nil] filter The filter to apply.
135
+ # If nil, the method always returns true.
136
+ # @return [Boolean] True if the message ID matches the filter, false otherwise.
137
+ def matches_filter?(message_id, filter)
138
+ case filter
139
+ when Integer
140
+ message_id == filter
141
+ when Range
142
+ filter.cover?(message_id)
143
+ when Array
144
+ filter.include?(message_id)
145
+ else
146
+ true # No filter or unrecognized filter
147
+ end
148
+ end
118
149
  end
119
150
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CanMessenger
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: can_messenger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - fk1018
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-11-11 00:00:00.000000000 Z
11
+ date: 2024-12-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: CanMessenger provides an interface to send and receive messages over
14
14
  the CAN bus, useful for applications requiring CAN communication in Ruby.
@@ -29,7 +29,7 @@ metadata:
29
29
  allowed_push_host: https://rubygems.org
30
30
  homepage_uri: https://github.com/fk1018/can_messenger
31
31
  source_code_uri: https://github.com/fk1018/can_messenger
32
- changelog_uri: https://github.com/fk1018/can_messenger/CHANGELOG.md
32
+ changelog_uri: https://github.com/fk1018/can_messenger/blob/main/CHANGELOG.md
33
33
  post_install_message:
34
34
  rdoc_options: []
35
35
  require_paths: