ant-wireless 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/History.md +8 -0
- data/lib/ant/bitvector.rb +197 -0
- data/lib/ant.rb +1 -1
- data/spec/bitvector_spec.rb +141 -0
- data.tar.gz.sig +0 -0
- metadata +4 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abd16b62c0eb3b3e7e683fd46cca0382fa6b0400f23d8ae8a12de4455e66599a
|
4
|
+
data.tar.gz: c0e7b0571d2cb8285f072b23027b226ac4c472e0b2147faa34c1bd13d4c78aef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 994058a2faa922f5d5469b9ab04aa5eecefaa6fdac4921a035a9bfe4f0ca58bea90a32f225c54d695f93b5cc27bcaf87ce2442b80160984944e7a04234eb0036
|
7
|
+
data.tar.gz: 11f91861b51651d3cfba2309334ac4e9253a4ea9bc313bd0434f42eaa4a86e05d3659b11cfed13a85b162761b1913afb2da13d99faf58f1b14cf53b52fb03da3
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/History.md
CHANGED
@@ -0,0 +1,197 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
# vim: set nosta noet ts=4 sw=4:
|
4
|
+
|
5
|
+
require 'ant' unless defined?( Ant )
|
6
|
+
|
7
|
+
# Ant::BitVector -- a convenience class for manipulating and
|
8
|
+
# comparing bit vectors.
|
9
|
+
#
|
10
|
+
# This is a slightly-modified version of the same utility in StaticCling.
|
11
|
+
#
|
12
|
+
# == Synopsis
|
13
|
+
#
|
14
|
+
# require 'ant/bitvector'
|
15
|
+
#
|
16
|
+
# vector = Ant::BitVector.new
|
17
|
+
#
|
18
|
+
# vector.on( 4 ) # => 16
|
19
|
+
# vector.on( 12 ) # => 4112
|
20
|
+
# vector.toggle( 4 ) # => 4096
|
21
|
+
# vector.on?( 4 ) # => false
|
22
|
+
# vector.size # => 13
|
23
|
+
# vector.to_hex # => 0x1000
|
24
|
+
#
|
25
|
+
# vector2 = Ant::BitVector.new( 5 )
|
26
|
+
# vector > vector2 # => true
|
27
|
+
#
|
28
|
+
# vector2.each_with_index do |bit, i|
|
29
|
+
# puts "Bit %d is %s" % [ i + 1, bit.zero? ? 'off' : 'on' ]
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# Bit 1 is on
|
33
|
+
# Bit 2 is off
|
34
|
+
# Bit 3 is on
|
35
|
+
#
|
36
|
+
# == Version
|
37
|
+
#
|
38
|
+
# $Id$
|
39
|
+
#
|
40
|
+
# == Author
|
41
|
+
#
|
42
|
+
# * Mahlon E. Smith <mahlon@martini.nu>
|
43
|
+
#
|
44
|
+
# == License
|
45
|
+
#
|
46
|
+
# Copyright (c) 2000-2013, Mahlon E. Smith <mahlon@martini.nu>
|
47
|
+
#
|
48
|
+
# All rights reserved.
|
49
|
+
#
|
50
|
+
# Redistribution and use in source and binary forms, with or without
|
51
|
+
# modification, are permitted provided that the following conditions are met:
|
52
|
+
#
|
53
|
+
# * Redistributions of source code must retain the above copyright notice,
|
54
|
+
# this list of conditions and the following disclaimer.
|
55
|
+
#
|
56
|
+
# * Redistributions in binary form must reproduce the above copyright
|
57
|
+
# notice, this list of conditions and the following disclaimer in the
|
58
|
+
# documentation and/or other materials provided with the distribution.
|
59
|
+
#
|
60
|
+
# * Neither the name of the author, nor the names of contributors may be
|
61
|
+
# used to endorse or promote products derived from this software without
|
62
|
+
# specific prior written permission.
|
63
|
+
#
|
64
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
65
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
66
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
67
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
68
|
+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
69
|
+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
70
|
+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
71
|
+
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
72
|
+
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
73
|
+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
74
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
75
|
+
#
|
76
|
+
class Ant::BitVector
|
77
|
+
include Enumerable,
|
78
|
+
Comparable
|
79
|
+
|
80
|
+
### Create a new bit vector object, optionally from a pre-existing
|
81
|
+
### +init+ number (Any number that ruby supports natively should be
|
82
|
+
### fine -- 0b, 0x, or decimal.)
|
83
|
+
def initialize( init=0 )
|
84
|
+
unless init.respond_to?( :to_i )
|
85
|
+
raise ArgumentError, "I don't know what to do with a %s object." % [ init.class.name ]
|
86
|
+
end
|
87
|
+
|
88
|
+
@bv = init.to_i
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
######
|
93
|
+
public
|
94
|
+
######
|
95
|
+
|
96
|
+
# let any additional methods fall through to Fixnum/Bignum objs,
|
97
|
+
# and return new vector objects. This allows for doing bitwise math
|
98
|
+
# or simple addition/subtraction on two BitVector objects.
|
99
|
+
%w{ % & * ** + - / << >> ^ | ~ }.each do |op|
|
100
|
+
define_method( op.to_sym ) do |arg|
|
101
|
+
res = @bv.send( op.to_sym, arg.bv )
|
102
|
+
return self.class.new( res )
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
##
|
108
|
+
# Allow external access to the underlying Fixnum/Bignum
|
109
|
+
attr_reader :bv
|
110
|
+
|
111
|
+
|
112
|
+
### Return the bit vector as a decimal.
|
113
|
+
def to_i
|
114
|
+
return @bv
|
115
|
+
end
|
116
|
+
alias_method :to_int, :to_i
|
117
|
+
alias_method :to_dec, :to_i
|
118
|
+
|
119
|
+
|
120
|
+
### Return the bit vector as a binary string.
|
121
|
+
def to_bin
|
122
|
+
return "0b%s" % @bv.to_s(2)
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
### Return the bit vector as a hexidecimal string.
|
127
|
+
def to_hex
|
128
|
+
return "0x%04x" % @bv
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
### Return the length of the vector in bytes.
|
133
|
+
def size
|
134
|
+
return @bv.to_s(2).length
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
### Switch a +bit+ on.
|
139
|
+
def on( bit )
|
140
|
+
@bv = @bv | ( 1 << bit )
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
### Return boolean true if given +bit+ is currently on.
|
145
|
+
def on?( bit )
|
146
|
+
return @bv[ bit ].zero? ? false : true
|
147
|
+
end
|
148
|
+
alias_method :[], :on?
|
149
|
+
|
150
|
+
|
151
|
+
### Switch a +bit+ off.
|
152
|
+
def off( bit )
|
153
|
+
@bv = @bv & ~( 1 << bit )
|
154
|
+
end
|
155
|
+
|
156
|
+
|
157
|
+
### Return boolean true if given +bit+ is currently +off+.
|
158
|
+
def off?( bit )
|
159
|
+
return ! self.on?( bit )
|
160
|
+
end
|
161
|
+
|
162
|
+
|
163
|
+
### Swap the current state of the given +bit+.
|
164
|
+
def toggle( bit )
|
165
|
+
@bv = @bv ^ ( 1 << bit )
|
166
|
+
end
|
167
|
+
alias_method :flip, :toggle
|
168
|
+
|
169
|
+
|
170
|
+
### Set a +bit+ to +bool+ -- either true (on) or false (off).
|
171
|
+
### Any value other than nil or false is treated as true.
|
172
|
+
### This form also accepts ranges of bits, a la: vector[ 1..4 ] = true
|
173
|
+
def []=( bit, bool )
|
174
|
+
if bit.respond_to?( :each )
|
175
|
+
bit.each { |b| bool ? self.on( b ) : self.off( b ) }
|
176
|
+
else
|
177
|
+
bool ? self.on( bit ) : self.off( bit )
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
|
182
|
+
### Yield each binary position, least significant +bit+ first.
|
183
|
+
def each
|
184
|
+
@bv.to_s(2).reverse.each_byte do |bit|
|
185
|
+
yield bit.chr.to_i
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
### Comparision operator for Comparable mixin, fallthrough to
|
191
|
+
### Fixnum/Bignum. Compares current state against +cmp+.
|
192
|
+
def <=>( cmp )
|
193
|
+
@bv <=> cmp.bv
|
194
|
+
end
|
195
|
+
|
196
|
+
end # class Ant::BitVector
|
197
|
+
|
data/lib/ant.rb
CHANGED
@@ -0,0 +1,141 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative 'spec_helper'
|
5
|
+
|
6
|
+
require 'ant/bitvector'
|
7
|
+
|
8
|
+
|
9
|
+
RSpec.describe( Ant::BitVector ) do
|
10
|
+
|
11
|
+
context 'when first instantiated' do
|
12
|
+
|
13
|
+
it "is empty when created without arguments" do
|
14
|
+
v = described_class.new
|
15
|
+
expect( v.to_i ).to eq( 0 )
|
16
|
+
expect( v.size ).to eq( 1 )
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
it "accepts a decimal argument" do
|
21
|
+
v = described_class.new( 242 )
|
22
|
+
expect( v.to_hex ).to eq( '0x00f2' )
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
it "accepts a hexadecimal argument" do
|
27
|
+
v = described_class.new( 0x00f2 )
|
28
|
+
expect( v.to_i ).to eq( 242 )
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
it "accepts a bit string argument" do
|
33
|
+
v = described_class.new( 0b11110010 )
|
34
|
+
expect( v.to_i ).to eq( 242 )
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
it "accepts a different BitVector argument" do
|
39
|
+
v = described_class.new( described_class.new( 242 ) )
|
40
|
+
expect( v.to_i ).to eq( 242 )
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
it "accepts a string argument" do
|
45
|
+
v = described_class.new( "242" )
|
46
|
+
expect( v.to_i ).to eq( 242 )
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
it "rejects objects that don't have a 'to_i' method" do
|
51
|
+
expect {
|
52
|
+
described_class.new( Class )
|
53
|
+
}.to raise_error( ArgumentError, /don't know what to do/i )
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
context 'after modified with a value' do
|
60
|
+
|
61
|
+
let( :bv ) { described_class.new( 242 ) }
|
62
|
+
|
63
|
+
|
64
|
+
it "can be converted into various formats" do
|
65
|
+
expect( bv.to_int ).to eq( 242 )
|
66
|
+
expect( bv.to_hex ).to eq( '0x00f2' )
|
67
|
+
expect( bv.to_bin ).to eq( '0b11110010' )
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
it "knows the size of its own bit string" do
|
72
|
+
expect( bv.size ).to eq( 8 )
|
73
|
+
bv.toggle( 9 )
|
74
|
+
expect( bv.size ).to eq( 10 )
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
it "can switch specific bits on and off" do
|
79
|
+
expect( bv.on?( 12 ) ).to be_falsey
|
80
|
+
|
81
|
+
bv.on( 12 )
|
82
|
+
expect( bv[12] ).to be_truthy
|
83
|
+
|
84
|
+
bv.off( 12 )
|
85
|
+
expect( bv.off?( 12 ) ).to be_truthy
|
86
|
+
|
87
|
+
bv[12] = true
|
88
|
+
expect( bv.on?( 12 ) ).to be_truthy
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
it "can set bits in a range" do
|
93
|
+
expect( bv[ 8] ).to be_falsey
|
94
|
+
expect( bv[ 9] ).to be_falsey
|
95
|
+
expect( bv[10] ).to be_falsey
|
96
|
+
expect( bv[11] ).to be_falsey
|
97
|
+
expect( bv[12] ).to be_falsey
|
98
|
+
|
99
|
+
bv[8..12] = true
|
100
|
+
|
101
|
+
expect( bv[ 8] ).to be_truthy
|
102
|
+
expect( bv[ 9] ).to be_truthy
|
103
|
+
expect( bv[10] ).to be_truthy
|
104
|
+
expect( bv[11] ).to be_truthy
|
105
|
+
expect( bv[12] ).to be_truthy
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
it "delegates math operations to the underlying integer" do
|
110
|
+
bv2 = described_class.new( 4112 )
|
111
|
+
|
112
|
+
bv3 = bv + bv2
|
113
|
+
expect( bv3.to_int ).to eq( 4354 )
|
114
|
+
|
115
|
+
bv3 = bv | bv2
|
116
|
+
expect( bv3.to_int ).to eq( 4338 )
|
117
|
+
|
118
|
+
bv3 = bv & bv2
|
119
|
+
expect( bv3.to_int ).to eq( 16 )
|
120
|
+
|
121
|
+
expect( bv3 ).to be_a( described_class )
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
it "can compare different bitvector objects" do
|
126
|
+
bv1 = described_class.new( bv )
|
127
|
+
bv2 = described_class.new( 4112 )
|
128
|
+
expect( bv ).to eq( bv1 )
|
129
|
+
expect( bv ).to be < bv2
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
it "is enumerable" do
|
134
|
+
bits = bv.to_a
|
135
|
+
expect( bits ).to eq( [0, 1, 0, 0, 1, 1, 1, 1] )
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ant-wireless
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Granger
|
@@ -34,7 +34,7 @@ cert_chain:
|
|
34
34
|
MCh97sQ/Z/MOusb5+QddBmB+k8EicXyGNl4b5L4XpL7fIQu+Y96TB3JEJlShxFD9
|
35
35
|
k9FjI4d9EP54gS/4
|
36
36
|
-----END CERTIFICATE-----
|
37
|
-
date: 2021-09-
|
37
|
+
date: 2021-09-30 00:00:00.000000000 Z
|
38
38
|
dependencies:
|
39
39
|
- !ruby/object:Gem::Dependency
|
40
40
|
name: rake-compiler
|
@@ -106,6 +106,7 @@ files:
|
|
106
106
|
- ext/ant_ext/version.h
|
107
107
|
- lib/ant-wireless.rb
|
108
108
|
- lib/ant.rb
|
109
|
+
- lib/ant/bitvector.rb
|
109
110
|
- lib/ant/channel.rb
|
110
111
|
- lib/ant/channel/event_callbacks.rb
|
111
112
|
- lib/ant/message.rb
|
@@ -113,6 +114,7 @@ files:
|
|
113
114
|
- lib/ant/response_callbacks.rb
|
114
115
|
- lib/ant/wireless.rb
|
115
116
|
- spec/ant_spec.rb
|
117
|
+
- spec/bitvector_spec.rb
|
116
118
|
- spec/spec_helper.rb
|
117
119
|
homepage: https://sr.ht/~ged/ruby-ant-wireless/
|
118
120
|
licenses:
|
metadata.gz.sig
CHANGED
Binary file
|