This program demonstrates how to use a MicroNFCBoard as a peripheral to send emulate a NFC tag.
Dependencies: MicroNFCBoardAPI mbed
Fork of MicroNFCBoardAPI_Blink by
main.cpp@2:1f0b5d04ed11, 2015-05-14 (annotated)
- Committer:
- AppNearMe
- Date:
- Thu May 14 17:28:20 2015 +0000
- Revision:
- 2:1f0b5d04ed11
- Parent:
- 1:5b804c0c8aa8
Tag emulator 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:1f0b5d04ed11 | 26 | |
AppNearMe | 2:1f0b5d04ed11 | 27 | //Start polling |
AppNearMe | 2:1f0b5d04ed11 | 28 | while(true) |
AppNearMe | 0:e6bbb5a92267 | 29 | { |
AppNearMe | 2:1f0b5d04ed11 | 30 | if( !nfc.connected() ) |
AppNearMe | 2:1f0b5d04ed11 | 31 | { |
AppNearMe | 2:1f0b5d04ed11 | 32 | printf("Writing message\r\n"); |
AppNearMe | 2:1f0b5d04ed11 | 33 | nfc.writeNdefText("en", "AppNearMe"); |
AppNearMe | 2:1f0b5d04ed11 | 34 | } |
AppNearMe | 2:1f0b5d04ed11 | 35 | |
AppNearMe | 2:1f0b5d04ed11 | 36 | printf("Poll\r\n"); |
AppNearMe | 2:1f0b5d04ed11 | 37 | nfc.startPolling(false, true, false); |
AppNearMe | 2:1f0b5d04ed11 | 38 | |
AppNearMe | 2:1f0b5d04ed11 | 39 | while( nfc.polling() ); |
AppNearMe | 2:1f0b5d04ed11 | 40 | |
AppNearMe | 2:1f0b5d04ed11 | 41 | if( nfc.type4Emulator() ) |
AppNearMe | 2:1f0b5d04ed11 | 42 | { |
AppNearMe | 2:1f0b5d04ed11 | 43 | printf("Emulating type 4 tag\r\n"); |
AppNearMe | 2:1f0b5d04ed11 | 44 | } |
AppNearMe | 2:1f0b5d04ed11 | 45 | else |
AppNearMe | 2:1f0b5d04ed11 | 46 | { |
AppNearMe | 2:1f0b5d04ed11 | 47 | continue; |
AppNearMe | 2:1f0b5d04ed11 | 48 | } |
AppNearMe | 2:1f0b5d04ed11 | 49 | |
AppNearMe | 2:1f0b5d04ed11 | 50 | bool ndefRead = false; |
AppNearMe | 2:1f0b5d04ed11 | 51 | while( nfc.connected() || nfc.polling() ) |
AppNearMe | 2:1f0b5d04ed11 | 52 | { |
AppNearMe | 2:1f0b5d04ed11 | 53 | if( !ndefRead && nfc.ndefPresent() ) |
AppNearMe | 2:1f0b5d04ed11 | 54 | { |
AppNearMe | 2:1f0b5d04ed11 | 55 | printf("Got message\r\n"); |
AppNearMe | 2:1f0b5d04ed11 | 56 | char buf[512]; |
AppNearMe | 2:1f0b5d04ed11 | 57 | if( nfc.readNdefUri(buf, sizeof(buf)) ) |
AppNearMe | 2:1f0b5d04ed11 | 58 | { |
AppNearMe | 2:1f0b5d04ed11 | 59 | printf("Got URI: %s\r\n", buf); |
AppNearMe | 2:1f0b5d04ed11 | 60 | } |
AppNearMe | 2:1f0b5d04ed11 | 61 | if( nfc.readNdefText(buf, sizeof(buf)) ) |
AppNearMe | 2:1f0b5d04ed11 | 62 | { |
AppNearMe | 2:1f0b5d04ed11 | 63 | printf("Got Text: %s\r\n", buf); |
AppNearMe | 2:1f0b5d04ed11 | 64 | } |
AppNearMe | 2:1f0b5d04ed11 | 65 | ndefRead = true; |
AppNearMe | 2:1f0b5d04ed11 | 66 | } |
AppNearMe | 2:1f0b5d04ed11 | 67 | } |
AppNearMe | 2:1f0b5d04ed11 | 68 | |
AppNearMe | 2:1f0b5d04ed11 | 69 | printf("Disconnected\r\n"); |
AppNearMe | 0:e6bbb5a92267 | 70 | } |
AppNearMe | 0:e6bbb5a92267 | 71 | } |