ST
/
mbed-os-example-nfc04a1
NFC EEPROM sample application
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2018-2018 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "events/EventQueue.h" 00018 00019 #include "nfc/ndef/MessageBuilder.h" 00020 #include "nfc/ndef/common/URI.h" 00021 #include "nfc/ndef/common/util.h" 00022 00023 #include "NFCEEPROM.h" 00024 00025 #include "EEPROMDriver.h" 00026 #include <cstdio> 00027 00028 using events::EventQueue; 00029 00030 using mbed::nfc::NFCEEPROM; 00031 using mbed::nfc::NFCEEPROMDriver; 00032 using mbed::Span; 00033 00034 using mbed::nfc::ndef::MessageBuilder; 00035 using mbed::nfc::ndef::common::URI; 00036 using mbed::nfc::ndef::common::span_from_cstr; 00037 00038 /* URL that will be written into the tag */ 00039 const char url_string[] = "st.com"; 00040 00041 class EEPROMExample : mbed::nfc::NFCEEPROM::Delegate 00042 { 00043 public: 00044 EEPROMExample(events::EventQueue& queue, NFCEEPROMDriver& eeprom_driver) : 00045 _ndef_buffer(), 00046 _eeprom(&eeprom_driver, &queue, _ndef_buffer), 00047 _queue(queue) 00048 { } 00049 00050 void run() 00051 { 00052 if (_eeprom.initialize() != NFC_OK) { 00053 printf("failed to initialise\r\n"); 00054 _queue.break_dispatch(); 00055 } 00056 00057 _eeprom.set_delegate(this); 00058 00059 _queue.call(&_eeprom, &NFCEEPROM::write_ndef_message); 00060 } 00061 00062 private: 00063 virtual void on_ndef_message_written(nfc_err_t result) { 00064 if (result == NFC_OK) { 00065 printf("message written successfully\r\n"); 00066 } else { 00067 printf("failed to write (error: %d)\r\n", result); 00068 } 00069 00070 _queue.call(&_eeprom, &NFCEEPROM::read_ndef_message); 00071 } 00072 00073 virtual void on_ndef_message_read(nfc_err_t result) { 00074 if (result == NFC_OK) { 00075 printf("message read successfully\r\n"); 00076 } else { 00077 printf("failed to read (error: %d)\r\n", result); 00078 } 00079 } 00080 00081 virtual void parse_ndef_message(const Span<const uint8_t> &buffer) { 00082 printf("Received an ndef message of size %d\r\n", buffer.size()); 00083 } 00084 00085 virtual size_t build_ndef_message(const Span<uint8_t> &buffer) { 00086 printf("Building an ndef message\r\n"); 00087 00088 /* create a message containing the URL */ 00089 00090 MessageBuilder builder(buffer); 00091 00092 /* URI expected a non-null terminated string so we use a helper function that casts 00093 * the pointer into a Span of size smaller by one */ 00094 URI uri(URI::HTTPS_WWW, span_from_cstr(url_string)); 00095 00096 uri.append_as_record(builder, true); 00097 00098 return builder.get_message().size(); 00099 } 00100 00101 private: 00102 uint8_t _ndef_buffer[1024]; 00103 NFCEEPROM _eeprom; 00104 EventQueue& _queue; 00105 }; 00106 00107 int main() 00108 { 00109 EventQueue queue; 00110 NFCEEPROMDriver& eeprom_driver = get_eeprom_driver(queue); 00111 00112 EEPROMExample example(queue, eeprom_driver); 00113 00114 example.run(); 00115 queue.dispatch_forever(); 00116 00117 return 0; 00118 } 00119
Generated on Tue Jul 12 2022 15:46:04 by 1.7.2