ID12 RFID library for reading 125KHz RFID tags

Dependents:   ID12RFID_HelloWorld MyFinalDerbot RFID_CatDoor TweetRFID ... more

Committer:
simon
Date:
Tue Nov 23 17:17:15 2010 +0000
Revision:
1:f04afa911cf5
Parent:
0:049d572aefe1
New revisin as a library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 1:f04afa911cf5 1 /* mbed ID12 RFID Library
simon 1:f04afa911cf5 2 * Copyright (c) 2007-2010, sford, http://mbed.org
simon 1:f04afa911cf5 3 *
simon 1:f04afa911cf5 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 1:f04afa911cf5 5 * of this software and associated documentation files (the "Software"), to deal
simon 1:f04afa911cf5 6 * in the Software without restriction, including without limitation the rights
simon 1:f04afa911cf5 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 1:f04afa911cf5 8 * copies of the Software, and to permit persons to whom the Software is
simon 1:f04afa911cf5 9 * furnished to do so, subject to the following conditions:
simon 1:f04afa911cf5 10 *
simon 1:f04afa911cf5 11 * The above copyright notice and this permission notice shall be included in
simon 1:f04afa911cf5 12 * all copies or substantial portions of the Software.
simon 1:f04afa911cf5 13 *
simon 1:f04afa911cf5 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 1:f04afa911cf5 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 1:f04afa911cf5 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 1:f04afa911cf5 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 1:f04afa911cf5 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 1:f04afa911cf5 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 1:f04afa911cf5 20 * THE SOFTWARE.
simon 1:f04afa911cf5 21 */
simon 1:f04afa911cf5 22
simon 1:f04afa911cf5 23 #include "ID12RFID.h"
simon 1:f04afa911cf5 24
simon 1:f04afa911cf5 25 #include "mbed.h"
simon 1:f04afa911cf5 26
simon 1:f04afa911cf5 27 ID12RFID::ID12RFID(PinName rx)
simon 1:f04afa911cf5 28 : _rfid(NC, rx) {
simon 1:f04afa911cf5 29 }
simon 1:f04afa911cf5 30
simon 1:f04afa911cf5 31 int ID12RFID::readable() {
simon 1:f04afa911cf5 32 return _rfid.readable();
simon 1:f04afa911cf5 33 }
simon 1:f04afa911cf5 34
simon 1:f04afa911cf5 35 int ID12RFID::read() {
simon 1:f04afa911cf5 36 while (_rfid.getc() != 2);
simon 1:f04afa911cf5 37
simon 1:f04afa911cf5 38 int v = 0;
simon 1:f04afa911cf5 39 _rfid.getc(); // drop 1st 2 bytes - we actually only read the lower 32-bits of the code
simon 1:f04afa911cf5 40 _rfid.getc();
simon 1:f04afa911cf5 41
simon 1:f04afa911cf5 42 for (int i=7; i>=0; i--) {
simon 1:f04afa911cf5 43 char c = _rfid.getc(); // a ascii hex char
simon 1:f04afa911cf5 44 int part = c - '0';
simon 1:f04afa911cf5 45 v |= part << (i * 4);
simon 1:f04afa911cf5 46 }
simon 1:f04afa911cf5 47
simon 1:f04afa911cf5 48 for (int i=0; i<5; i++) {
simon 1:f04afa911cf5 49 _rfid.getc();
simon 1:f04afa911cf5 50 }
simon 1:f04afa911cf5 51
simon 1:f04afa911cf5 52 return v;
simon 1:f04afa911cf5 53 }