Skip to content

Add NFC examples #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ jobs:
mbed config root .
mbed update
mbed add https://github.com/c1728p9/AudioPlayer
mbed add https://github.com/ARMmbed/mbed-nfc-m24sr
mbed test --compile -m K64F -t GCC_ARM -n sinppet-* --app-config=sinppet/TESTS/test.json -v
3 changes: 3 additions & 0 deletions APIs_NFC/MessageBuilder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## MessageBuilder Example

This MessageBuilder example shows how to build an NDEF message into a user-provided buffer. `URI`, `Text` and `Mime` types can be serialized in the builder with the help of the member function `append_as_record`.
30 changes: 30 additions & 0 deletions APIs_NFC/MessageBuilder/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "nfc/ndef/MessageBuilder.h"
#include "nfc/ndef/common/URI.h"
#include "nfc/ndef/common/Text.h"
#include "nfc/ndef/common/util.h"

using mbed::nfc::ndef::MessageBuilder;
using mbed::nfc::ndef::common::Text;
using mbed::nfc::ndef::common::URI;
using mbed::nfc::ndef::common::span_from_cstr;

int main()
{
uint8_t message_data[128];
const Span<uint8_t, 128> buffer(message_data, sizeof(message_data));

MessageBuilder builder(buffer);

URI uri(URI::HTTPS_WWW, span_from_cstr("mbed.com"));
Text text(Text::UTF8, span_from_cstr("en-US"), span_from_cstr("Mbed website"));

uri.append_as_record(builder);
text.append_as_record(builder, /* last record */ true);

printf("Message size: %d bytes\r\n", builder.get_message().size());
}
3 changes: 3 additions & 0 deletions APIs_NFC/NFCController/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# NFCController example

This example shows how to use an NFC controller.
21 changes: 21 additions & 0 deletions APIs_NFC/NFCController/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "stdint.h"
#include "NFC.h"
#include "events/EventQueue.h"
#include "nfc/controllers/PN512Driver.h"
#include "nfc/controllers/PN512SPITransportDriver.h"

static uint8_t ndef_buffer[1024] = {0};

int main()
{
mbed::nfc::PN512SPITransportDriver pn512_transport(D11, D12, D13, D10, A1, A0);
mbed::nfc::PN512Driver pn512_driver(&pn512_transport);
events::EventQueue queue;
mbed::nfc::NFCController nfc(&pn512_driver, &queue, ndef_buffer);

// ...
}
3 changes: 3 additions & 0 deletions APIs_NFC/NFC_EEPROM/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# NFCEEPROM example

This example shows how to use an NFCEEPROM class.
21 changes: 21 additions & 0 deletions APIs_NFC/NFC_EEPROM/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "stdint.h"

#include "NFC.h"
#include "events/EventQueue.h"
#include "m24sr_driver.h"

static uint8_t ndef_buffer[1024] = {0};

int main()
{
mbed::nfc::vendor::ST::M24srDriver m24sr_driver(D14, D15);
events::EventQueue queue;
mbed::nfc::NFCEEPROM nfc(&m24sr_driver, &queue, ndef_buffer);

// ...
nfc.write_ndef_message();
}
3 changes: 3 additions & 0 deletions APIs_NFC/SimpleMessageParser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## SimpleMessageParser Example

This SimpleMessageParser example shows how to parse NDEF messages.
93 changes: 93 additions & 0 deletions APIs_NFC/SimpleMessageParser/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "nfc/ndef/MessageBuilder.h"
#include "nfc/ndef/common/URI.h"
#include "nfc/ndef/common/Text.h"
#include "nfc/ndef/common/util.h"
#include "nfc/ndef/common/SimpleMessageParser.h"

using mbed::nfc::ndef::Record;
using mbed::nfc::ndef::RecordType;
using mbed::nfc::ndef::RecordID;
using mbed::nfc::ndef::MessageParser;
using mbed::nfc::ndef::common::Text;
using mbed::nfc::ndef::common::URI;
using mbed::nfc::ndef::common::Mime;
using mbed::nfc::ndef::MessageBuilder;
using mbed::nfc::ndef::common::span_from_cstr;
using mbed::nfc::ndef::common::SimpleMessageParser;

int main()
{
uint8_t message_data[512];
const Span<uint8_t, 512> buffer(message_data, sizeof(message_data));

MessageBuilder builder(buffer);

Text text(Text::UTF8, span_from_cstr("en-US"), span_from_cstr("Mbed website"));
URI uri(URI::HTTPS_WWW, span_from_cstr("mbed.com"));

uri.append_as_record(builder);
text.append_as_record(builder, /* last record */ true);

SimpleMessageParser parser;
struct : SimpleMessageParser::Delegate {
virtual void on_parsing_started()
{
printf("parsing started\r\n");
}

virtual void on_text_parsed(const Text &text, const RecordID &)
{
printf("Text record parsed.\r\n");
printf(
"\tlanguage: %.*s\r\n",
text.get_language_code().size(), text.get_language_code().data()
);
printf("\ttext: %.*s\r\n", text.get_text().size(), text.get_text().data());
}

virtual void on_uri_parsed(const URI &uri, const RecordID &)
{
printf("URI parsed.\r\n");
printf("\tid: %d\r\n", uri.get_id());
printf("\tvalue: %.*s\r\n", uri.get_uri_field().size(), uri.get_uri_field().data());
}

virtual void on_mime_parsed(const Mime &mime, const RecordID &)
{
printf("Mime object parsed.\r\n");
printf("\ttype: %.*s\r\n", mime.get_mime_type().size(), mime.get_mime_type().data());
printf("\tcontent size: %d\r\n", mime.get_mime_content().size());
}

virtual void on_unknown_record_parsed(const Record &record)
{
printf("Unknown record parsed.\r\n");
printf(
"\ttype: %d, type_value: %.*s\r\n",
record.type.tnf, record.type.value.size(), record.type.value.data()
);
printf(
"\tpayload size: %d, payload: %.*s\r\n",
record.payload.size(), record.payload.size(), record.payload.data()
);
}

virtual void on_parsing_terminated()
{
printf("parsing terminated\r\n");
}

virtual void on_parsing_error(MessageParser::error_t error)
{
printf("Parsing error: %d\r\n", error);
}
} delegate;

parser.set_delegate(&delegate);
parser.parse(buffer);
}