to colorize a colorful pixel with a simple touch using nfc technology
Dependencies: Chainable_RGB_LED mbed
main.cpp
00001 00002 #include "mbed.h" 00003 #include "ChainableLED.h" 00004 #include "PN532_SPI.h" 00005 #include "emulatetag.h " 00006 #include "NdefMessage.h" 00007 00008 #include <string.h> 00009 00010 #define LOG(args...) 00011 00012 BusOut leds(LED1, LED2, LED3, LED4); 00013 00014 ChainableLED rgbled(P1_14, P1_13, 1); 00015 00016 SPI spi(P1_22, P1_21, P1_20); 00017 PN532_SPI interface(spi, P0_2); 00018 EmulateTag nfc(interface); 00019 00020 uint8_t ndefBuf[120]; 00021 NdefMessage message; 00022 int messageSize; 00023 00024 uint8_t uid[3] = { 0x12, 0x34, 0x56 }; 00025 00026 uint32_t getColor(uint8_t *buf) 00027 { 00028 uint32_t x = 0; 00029 for (uint8_t i = 0; i < 8; i++) { 00030 uint8_t c = *buf; 00031 if (c >= '0' && c <= '9') { 00032 x *= 16; 00033 x += c - '0'; 00034 } else if (c >= 'A' && c <= 'F') { 00035 x *= 16; 00036 x += (c - 'A') + 10; 00037 } else if (c >= 'a' && c <= 'f') { 00038 x *= 16; 00039 x += (c - 'a') + 10; 00040 } else 00041 break; 00042 00043 buf++; 00044 } 00045 00046 return x; 00047 } 00048 00049 void processNewNdef(uint8_t *buf, uint16_t length) 00050 { 00051 NdefMessage msg = NdefMessage(buf, length); 00052 00053 NdefRecord record = msg.getRecord(0); 00054 uint8_t recordbuf[32]; 00055 record.getType(recordbuf); 00056 if (!memcmp(recordbuf, "text/c", 6)) { 00057 uint8_t r, g, b; 00058 uint32_t color; 00059 record.getPayload(recordbuf); 00060 color = getColor(recordbuf); 00061 r = (color >> 16) & 0xFF; 00062 g = (color >> 8) & 0xFF; 00063 b = color & 0xFF; 00064 00065 rgbled.setColorRGB(0, r, g, b); 00066 leds = r; 00067 } 00068 } 00069 00070 int main() 00071 { 00072 LOG("------- Emulate Tag --------\n"); 00073 rgbled.setColorRGB(0, 0, 0, 0); 00074 00075 NdefRecord aarRecord = NdefRecord(); 00076 const uint8_t aarType[] = "android.com:pkg"; 00077 const uint8_t aarPayload[] = "com.seeedstudio.android.nfc.touchpixel"; 00078 00079 aarRecord.setTnf(TNF_EXTERNAL_TYPE); 00080 aarRecord.setType(aarType, sizeof(aarType) - 1); 00081 aarRecord.setPayload(aarPayload, sizeof(aarPayload) - 1); 00082 00083 message = NdefMessage(); 00084 message.addMimeMediaRecord("text/c", "FF000000"); 00085 message.addRecord(aarRecord); 00086 messageSize = message.getEncodedSize(); 00087 if (messageSize > sizeof(ndefBuf)) { 00088 while (1) { } 00089 } 00090 00091 LOG("Ndef encoded message size: %d\n", messageSize); 00092 00093 message.encode(ndefBuf); 00094 00095 // comment out this command for no ndef message 00096 nfc.setNdefFile(ndefBuf, messageSize); 00097 00098 // uid must be 3 bytes! 00099 nfc.setUid(uid); 00100 00101 nfc.init(); 00102 00103 nfc.attach(processNewNdef); 00104 00105 while (1) { 00106 //nfc.setNdefFile(ndefBuf, messageSize); 00107 nfc.emulate(); 00108 } 00109 }
Generated on Fri Jul 15 2022 06:47:11 by 1.7.2