mfrc522 1.0.4 → 1.0.6

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 +4 -4
  2. data/lib/iso144434.rb +4 -2
  3. data/lib/mfrc522.rb +42 -17
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3bc5f3921bd767b79efbdbd0d21a6ee673c1ac15
4
- data.tar.gz: 564343a2457082eb7d394a0a8f6d7f7dfe0f2e94
3
+ metadata.gz: 6970a8238b5e32859472bec37f4a0c7327dd8bf7
4
+ data.tar.gz: cbeac674373f82caa74e5fc91179176bb2c40c86
5
5
  SHA512:
6
- metadata.gz: 38314cbd1525c65bd8f4a797b9c38b3cff3e1bd2dd36ec32c720da3921eff070d6ca4eb8f4ba07a5b75413bd0f1d98f45c61292d312dcc801c5324d9d4eda39c
7
- data.tar.gz: 77b667701217f597d8b1fdcd22fde1b98cef93d00646583de55be5e9fe229d77da5d5894733c9d511481cab09b73a6e27c5f0b0397e008ac3752bdb355f9b5ee
6
+ metadata.gz: edbb612b4d103e712eff156431ebdcc55b601cbf74a666d39a34b8246f027d5cfd5d2a938fcb4b33612f7970187bd68983e5d391c05b3eb586a33374e8b9f02e
7
+ data.tar.gz: 3affc064c30c146115c0c0d1894fd1e71a519af1557700178b07ba1a5e3655e55a0102e989f6c3e4a3da04e1164dbe7a56604b997d46e0c5364aa3a6ba30a753
data/lib/iso144434.rb CHANGED
@@ -169,9 +169,11 @@ class ISO144434 < PICC
169
169
  ds = (ta >> 4) & 0x07 # PICC to PCD baud rate
170
170
 
171
171
  # Convert fastest baud rate to PCD setting
172
- dr = convert_iso_baud_rate_to_pcd_setting(dr)
173
- ds = convert_iso_baud_rate_to_pcd_setting(ds)
172
+ # dr = convert_iso_baud_rate_to_pcd_setting(dr)
173
+ # ds = convert_iso_baud_rate_to_pcd_setting(ds)
174
174
 
175
+ # FIXME: baud rate fixed to 106kBd
176
+ # until author can confirm negotiation works
175
177
  dr = 0
176
178
  ds = 0
177
179
  end
data/lib/mfrc522.rb CHANGED
@@ -100,7 +100,7 @@ class MFRC522
100
100
  TestDAC2Reg = 0x3A # defines the test value for TestDAC2
101
101
  TestADCReg = 0x3B # shows the value of ADC I and Q channels
102
102
 
103
- def initialize(nrstpd = 24, chip = 0, spd = 8000000, timer = 256)
103
+ def initialize(nrstpd = 24, chip = 0, spd = 1000000, timer = 256)
104
104
  chip_option = { 0 => PiPiper::Spi::CHIP_SELECT_0,
105
105
  1 => PiPiper::Spi::CHIP_SELECT_1,
106
106
  2 => PiPiper::Spi::CHIP_SELECT_BOTH,
@@ -248,15 +248,37 @@ class MFRC522
248
248
  current_level_known_bits = 0
249
249
  received_data = []
250
250
  valid_bits = 0
251
+ timeout = true
251
252
 
252
- # Maxmimum loop is defined in ISO spec
253
+ # Maxmimum loop count is defined in ISO spec
253
254
  32.times do
254
255
  if current_level_known_bits >= 32 # Prepare to do a complete select if we knew everything
255
- buffer = buffer[0..5] if buffer.size != 6
256
+ # Validate buffer content against non-numeric classes and incorrect size
257
+ buffer = buffer[0..5]
258
+ dirty_buffer = buffer.size != 6
259
+ dirty_buffer ||= buffer.any? do |byte|
260
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.0')
261
+ !byte.is_a?(Numeric)
262
+ else
263
+ !byte.is_a?(Fixnum)
264
+ end
265
+ end
266
+
267
+ # Retry reading UID when buffer is dirty, but don't reset loop count to prevent infinite loop
268
+ if dirty_buffer
269
+ # Reinitialize all variables
270
+ buffer = [cascade_level]
271
+ current_level_known_bits = 0
272
+ received_data = []
273
+ valid_bits = 0
274
+
275
+ # Continue to next loop
276
+ next
277
+ end
256
278
 
257
279
  tx_last_bits = 0
258
280
  buffer[1] = 0x70 # NVB - We're sending full length byte[0..6]
259
- buffer << (buffer[2] ^ buffer[3] ^ buffer[4] ^ buffer[5]) # Block Check Character
281
+ buffer[6] = (buffer[2] ^ buffer[3] ^ buffer[4] ^ buffer[5]) # Block Check Character
260
282
 
261
283
  # Append CRC to buffer
262
284
  buffer = append_crc(buffer)
@@ -265,6 +287,9 @@ class MFRC522
265
287
  uid_full_byte = current_level_known_bits / 8
266
288
  all_full_byte = 2 + uid_full_byte # length of SEL + NVB + UID
267
289
  buffer[1] = (all_full_byte << 4) + tx_last_bits # NVB
290
+
291
+ buffer_length = all_full_byte + (tx_last_bits > 0 ? 1 : 0)
292
+ buffer = buffer[0...buffer_length]
268
293
  end
269
294
 
270
295
  framing_bit = (tx_last_bits << 4) + tx_last_bits
@@ -284,11 +309,10 @@ class MFRC522
284
309
  if current_level_known_bits < 32
285
310
  # Check for last collision
286
311
  if tx_last_bits != 0
287
- buffer[-1] &= (0xFF >> (8 - tx_last_bits))
288
312
  buffer[-1] |= received_data.shift
289
313
  end
290
314
 
291
- buffer = buffer[0...all_full_byte] + received_data
315
+ buffer += received_data
292
316
  end
293
317
 
294
318
  # Handle collision
@@ -301,26 +325,27 @@ class MFRC522
301
325
  collision_position = collision & 0x1F
302
326
  collision_position = 32 if collision_position == 0 # Values 0-31, 0 means bit 32
303
327
  raise CollisionError if collision_position <= current_level_known_bits
304
-
328
+
305
329
  # Calculate positioin
306
330
  current_level_known_bits = collision_position
307
- uid_bit = current_level_known_bits % 8
308
- uid_byte = (current_level_known_bits + 7) / 8
331
+ uid_bit = (current_level_known_bits - 1) % 8
309
332
 
310
333
  # Mark the collision bit
311
- buffer = buffer[0..(uid_byte + 1)]
312
- if uid_bit == 0
313
- buffer << 0x01
314
- else
315
- buffer[-1] &= (0xFF >> (8 - uid_bit))
316
- buffer[-1] |= (1 << uid_bit)
317
- end
334
+ buffer[-1] |= (1 << uid_bit)
318
335
  else
319
- break if current_level_known_bits >= 32
336
+ if current_level_known_bits >= 32
337
+ timeout = false
338
+ break
339
+ end
320
340
  current_level_known_bits = 32 # We've already known all bits, loop again for a complete select
321
341
  end
322
342
  end
323
343
 
344
+ # Handle timeout after 32 loops
345
+ if timeout
346
+ raise UnexpectedDataError, 'Keep receiving incomplete UID until timeout'
347
+ end
348
+
324
349
  # We've finished current cascade level
325
350
  # Check and collect all uid stored in buffer
326
351
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mfrc522
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - atitan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-31 00:00:00.000000000 Z
11
+ date: 2017-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pi_piper
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
66
  version: '0'
67
67
  requirements: []
68
68
  rubyforge_project:
69
- rubygems_version: 2.5.1
69
+ rubygems_version: 2.6.8
70
70
  signing_key:
71
71
  specification_version: 4
72
72
  summary: MFRC522 RFID Reader Library for RaspberryPi