mfrc522 2.0.0 → 3.0.0

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.
Files changed (4) hide show
  1. checksums.yaml +5 -5
  2. data/lib/mfrc522.rb +30 -50
  3. data/lib/picc.rb +4 -4
  4. metadata +8 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4c15749e6896a2e58865f9e6639bc829fa717068
4
- data.tar.gz: e340cba7fdc0810a3d581b8a14d329bba72886e4
2
+ SHA256:
3
+ metadata.gz: b180534b3c1fff35f759792d021ba7005b460838e82b43782341f358d314ae5c
4
+ data.tar.gz: 77f1e85d84082d57ed27dbba18aea6b6e1e9949327f62ed135e2ec1389e12efe
5
5
  SHA512:
6
- metadata.gz: 7b88ed0c68070b2bafe8e3bd7e25304852a24bd88c609b1688b2c5e1ce830114c3540801482c7aee5276cd357226dfb49884fea1c9c79fba56e6a9c47a73d293
7
- data.tar.gz: 94e20de85a0185679b75a2902516406f6e5ed499433f5195f26493621ddac779932ca3c61b88a2c487a044db1e0ac56a489d9ec4a21aae47b842b190d782d98f
6
+ metadata.gz: d558480f55924d0e55e6028f87186008ed51346ab8cb1720dd2330613fe3c34ec1c6c8c33575d4e9bc228e9a9bd3be2f6826da3349c67ab12a4006c744e5ea49
7
+ data.tar.gz: 75554b9bcb105507d74f18ae96552bc1dd93b1d5b813f0dc5fba1e5cfdbf3ace56a54f25b623a5948ee268c1ea936b87466e87101900efba49da2d94ac763168
data/lib/mfrc522.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'pi_piper'
1
+ require 'fubuki'
2
2
 
3
3
  require 'openssl'
4
4
  require 'securerandom'
@@ -16,8 +16,6 @@ require 'mifare/ultralight'
16
16
  require 'mifare/ultralight_c'
17
17
  require 'mifare/ultralight_ev1'
18
18
 
19
- include PiPiper
20
-
21
19
  class MFRC522
22
20
 
23
21
  # PICC commands used by the PCD to manage communication with several PICCs (ISO 14443-3, Type A, section 6.4)
@@ -99,19 +97,11 @@ class MFRC522
99
97
  TestDAC2Reg = 0x3A # defines the test value for TestDAC2
100
98
  TestADCReg = 0x3B # shows the value of ADC I and Q channels
101
99
 
102
- def initialize(nrstpd = 24, chip = 0, spd = 1000000, timer = 256)
103
- chip_option = { 0 => PiPiper::Spi::CHIP_SELECT_0,
104
- 1 => PiPiper::Spi::CHIP_SELECT_1,
105
- 2 => PiPiper::Spi::CHIP_SELECT_BOTH,
106
- 3 => PiPiper::Spi::CHIP_SELECT_NONE }
107
- @spi_chip = chip_option[chip]
108
- @spi_spd = spd
109
- @timer = timer
110
-
111
- # Power it up
112
- nrstpd_pin = PiPiper::Pin.new(pin: nrstpd, direction: :out)
113
- nrstpd_pin.on
114
- sleep 1.0 / 20.0 # Wait 50ms
100
+ def initialize(spi_bus: 0, spi_chip: 0, spi_spd: 1_000_000, spi_delay: 1, pcd_timer: 256)
101
+ @spi_driver = Fubuki::SPI.new(spi_bus, spi_chip)
102
+ @spi_speed = spi_spd
103
+ @spi_delay = spi_delay
104
+ @pcd_timer = pcd_timer
115
105
 
116
106
  soft_reset # Perform software reset
117
107
 
@@ -148,7 +138,7 @@ class MFRC522
148
138
  transceiver_baud_rate(:rx, 0)
149
139
 
150
140
  # Set PCD timer value for 302us default timer
151
- internal_timer(@timer)
141
+ internal_timer(@pcd_timer)
152
142
  end
153
143
 
154
144
  # Control transceive timeout value
@@ -268,13 +258,7 @@ class MFRC522
268
258
  # Validate buffer content against non-numeric classes and incorrect size
269
259
  buffer = buffer[0..5]
270
260
  dirty_buffer = buffer.size != 6
271
- dirty_buffer ||= buffer.any? do |byte|
272
- if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.0')
273
- !byte.is_a?(Numeric)
274
- else
275
- !byte.is_a?(Fixnum)
276
- end
277
- end
261
+ dirty_buffer ||= buffer.any?{|byte| !byte.is_a?(Integer) }
278
262
 
279
263
  # Retry reading UID when buffer is dirty, but don't reset loop count to prevent infinite loop
280
264
  if dirty_buffer
@@ -443,35 +427,33 @@ class MFRC522
443
427
  return received_data, valid_bits
444
428
  end
445
429
 
446
- private
447
-
448
430
  # Read from SPI communication
449
431
  def read_spi(reg)
450
- output = 0
451
- PiPiper::Spi.begin do |spi|
452
- spi.chip_select_active_low(true)
453
- spi.bit_order Spi::MSBFIRST
454
- spi.clock @spi_spd
455
-
456
- spi.chip_select(@spi_chip) do
457
- spi.write((reg << 1) & 0x7E | 0x80)
458
- output = spi.read
459
- end
460
- end
461
- output
432
+ read_spi_bulk(reg).first
433
+ end
434
+
435
+ def read_spi_bulk(*regs)
436
+ regs.flatten!
437
+
438
+ payload = regs.map{ |reg| ((reg & 0x3F) << 1) | 0x80 }
439
+ payload << 0x00
440
+
441
+ result = @spi_driver.transfer(payload, @spi_speed, @spi_delay)
442
+
443
+ # discard first byte
444
+ result.shift
445
+
446
+ result
462
447
  end
463
448
 
464
449
  # Write to SPI communication
465
450
  def write_spi(reg, values)
466
- PiPiper::Spi.begin do |spi|
467
- spi.chip_select_active_low(true)
468
- spi.bit_order Spi::MSBFIRST
469
- spi.clock @spi_spd
451
+ spi_addr = (reg & 0x3F) << 1
452
+ payload = [spi_addr, *values]
470
453
 
471
- spi.chip_select(@spi_chip) do
472
- spi.write((reg << 1) & 0x7E, *values)
473
- end
474
- end
454
+ @spi_driver.transfer(payload, @spi_speed, @spi_delay)
455
+
456
+ true
475
457
  end
476
458
 
477
459
  # Set bits by mask
@@ -522,10 +504,8 @@ class MFRC522
522
504
  # Receiving data
523
505
  received_data = []
524
506
  data_length = read_spi(FIFOLevelReg)
525
- while data_length > 0 do
526
- data = read_spi(FIFODataReg)
527
- received_data << data
528
- data_length -=1
507
+ if data_length > 0
508
+ received_data = read_spi_bulk(Array.new(data_length, FIFODataReg))
529
509
  end
530
510
  valid_bits = read_spi(ControlReg) & 0x07
531
511
 
data/lib/picc.rb CHANGED
@@ -92,11 +92,11 @@ class PICC
92
92
  # Collect INF from chain
93
93
  inf = []
94
94
  received_chained_data.each do |data|
95
- inf_position = 1
96
- inf_position += 1 if data[0] & 0x08 != 0 # CID present
97
- inf_position += 1 if data[0] & 0x04 != 0 # NAD present
95
+ flag = data.shift
96
+ data.shift if flag & 0x08 != 0 # CID present
97
+ data.shift if flag & 0x04 != 0 # NAD present
98
98
 
99
- inf.concat(data[inf_position..-1])
99
+ inf.concat(data)
100
100
  end
101
101
  inf
102
102
  end
metadata CHANGED
@@ -1,35 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mfrc522
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - atitan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-17 00:00:00.000000000 Z
11
+ date: 2021-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: pi_piper
14
+ name: fubuki
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 2.0.0
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 2.0.0
19
+ version: 1.0.1
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: 2.0.0
30
- - - ">="
24
+ - - '='
31
25
  - !ruby/object:Gem::Version
32
- version: 2.0.0
26
+ version: 1.0.1
33
27
  description:
34
28
  email: commit@atifans.net
35
29
  executables: []
@@ -70,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
64
  version: '0'
71
65
  requirements: []
72
66
  rubyforge_project:
73
- rubygems_version: 2.6.8
67
+ rubygems_version: 2.7.6.2
74
68
  signing_key:
75
69
  specification_version: 4
76
70
  summary: MFRC522 RFID Reader Library for RaspberryPi