ID12RFID library, quickly modified to use interrupt driven reception instead so it doesn\'t block

Dependents:   m3pi_WiiRacing m3pi_RFIDReader

Committer:
simon
Date:
Mon Apr 11 23:02:00 2011 +0000
Revision:
0:a740ea2424e5

        

Who changed what in which revision?

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