This program demonstrates how to use a MicroNFCBoard as a peripheral to read and decode NFC tags.
Dependencies: MicroNFCBoardAPI mbed
Fork of MicroNFCBoardAPI_Blink by
main.cpp@2:b14afa59da26, 2015-05-14 (annotated)
- Committer:
- AppNearMe
- Date:
- Thu May 14 17:29:46 2015 +0000
- Revision:
- 2:b14afa59da26
- Parent:
- 1:5b804c0c8aa8
Tag reader fork
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AppNearMe | 0:e6bbb5a92267 | 1 | /* |
AppNearMe | 0:e6bbb5a92267 | 2 | MicroNFCBoard mbed API |
AppNearMe | 0:e6bbb5a92267 | 3 | |
AppNearMe | 0:e6bbb5a92267 | 4 | Copyright (c) 2014-2015 AppNearMe Ltd |
AppNearMe | 0:e6bbb5a92267 | 5 | |
AppNearMe | 0:e6bbb5a92267 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
AppNearMe | 0:e6bbb5a92267 | 7 | you may not use this file except in compliance with the License. |
AppNearMe | 0:e6bbb5a92267 | 8 | You may obtain a copy of the License at |
AppNearMe | 0:e6bbb5a92267 | 9 | |
AppNearMe | 0:e6bbb5a92267 | 10 | http://www.apache.org/licenses/LICENSE-2.0 |
AppNearMe | 0:e6bbb5a92267 | 11 | |
AppNearMe | 0:e6bbb5a92267 | 12 | Unless required by applicable law or agreed to in writing, software |
AppNearMe | 0:e6bbb5a92267 | 13 | distributed under the License is distributed on an "AS IS" BASIS, |
AppNearMe | 0:e6bbb5a92267 | 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AppNearMe | 0:e6bbb5a92267 | 15 | See the License for the specific language governing permissions and |
AppNearMe | 0:e6bbb5a92267 | 16 | limitations under the License. |
AppNearMe | 0:e6bbb5a92267 | 17 | */ |
AppNearMe | 0:e6bbb5a92267 | 18 | |
AppNearMe | 0:e6bbb5a92267 | 19 | #include "mbed.h" |
AppNearMe | 0:e6bbb5a92267 | 20 | #include "micronfcboard.h" |
AppNearMe | 0:e6bbb5a92267 | 21 | |
AppNearMe | 0:e6bbb5a92267 | 22 | MicroNFCBoard nfc(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, D9); |
AppNearMe | 0:e6bbb5a92267 | 23 | |
AppNearMe | 0:e6bbb5a92267 | 24 | int main() { |
AppNearMe | 0:e6bbb5a92267 | 25 | nfc.init(); |
AppNearMe | 2:b14afa59da26 | 26 | |
AppNearMe | 2:b14afa59da26 | 27 | //Start polling |
AppNearMe | 2:b14afa59da26 | 28 | while(true) |
AppNearMe | 0:e6bbb5a92267 | 29 | { |
AppNearMe | 2:b14afa59da26 | 30 | printf("Poll\r\n"); |
AppNearMe | 2:b14afa59da26 | 31 | nfc.startPolling(true, false, false); |
AppNearMe | 2:b14afa59da26 | 32 | |
AppNearMe | 2:b14afa59da26 | 33 | while( nfc.polling() ); |
AppNearMe | 2:b14afa59da26 | 34 | |
AppNearMe | 2:b14afa59da26 | 35 | if( nfc.type2Tag() ) |
AppNearMe | 2:b14afa59da26 | 36 | { |
AppNearMe | 2:b14afa59da26 | 37 | printf("Connected to type 2 tag\r\n"); |
AppNearMe | 2:b14afa59da26 | 38 | } |
AppNearMe | 2:b14afa59da26 | 39 | else |
AppNearMe | 2:b14afa59da26 | 40 | { |
AppNearMe | 2:b14afa59da26 | 41 | continue; |
AppNearMe | 2:b14afa59da26 | 42 | } |
AppNearMe | 2:b14afa59da26 | 43 | |
AppNearMe | 2:b14afa59da26 | 44 | bool ndefRead = false; |
AppNearMe | 2:b14afa59da26 | 45 | bool ndefReadingStarted = false; |
AppNearMe | 2:b14afa59da26 | 46 | while( nfc.connected() ) |
AppNearMe | 2:b14afa59da26 | 47 | { |
AppNearMe | 2:b14afa59da26 | 48 | if( !ndefReadingStarted && nfc.ndefReadable() ) |
AppNearMe | 2:b14afa59da26 | 49 | { |
AppNearMe | 2:b14afa59da26 | 50 | printf("Reading tag\r\n"); |
AppNearMe | 2:b14afa59da26 | 51 | ndefReadingStarted = true; |
AppNearMe | 2:b14afa59da26 | 52 | nfc.ndefRead(); |
AppNearMe | 2:b14afa59da26 | 53 | } |
AppNearMe | 2:b14afa59da26 | 54 | if( !ndefRead && nfc.ndefPresent() ) |
AppNearMe | 2:b14afa59da26 | 55 | { |
AppNearMe | 2:b14afa59da26 | 56 | printf("Got message\r\n"); |
AppNearMe | 2:b14afa59da26 | 57 | char buf[512]; |
AppNearMe | 2:b14afa59da26 | 58 | if( nfc.readNdefUri(buf, sizeof(buf)) ) |
AppNearMe | 2:b14afa59da26 | 59 | { |
AppNearMe | 2:b14afa59da26 | 60 | printf("Got URI: %s\r\n", buf); |
AppNearMe | 2:b14afa59da26 | 61 | } |
AppNearMe | 2:b14afa59da26 | 62 | if( nfc.readNdefText(buf, sizeof(buf)) ) |
AppNearMe | 2:b14afa59da26 | 63 | { |
AppNearMe | 2:b14afa59da26 | 64 | printf("Got Text: %s\r\n", buf); |
AppNearMe | 2:b14afa59da26 | 65 | } |
AppNearMe | 2:b14afa59da26 | 66 | ndefRead = true; |
AppNearMe | 2:b14afa59da26 | 67 | } |
AppNearMe | 2:b14afa59da26 | 68 | } |
AppNearMe | 2:b14afa59da26 | 69 | |
AppNearMe | 2:b14afa59da26 | 70 | printf("Disconnected\r\n"); |
AppNearMe | 0:e6bbb5a92267 | 71 | } |
AppNearMe | 0:e6bbb5a92267 | 72 | } |