to colorize a colorful pixel with a simple touch using nfc technology
Dependencies: Chainable_RGB_LED mbed
use Arch, NFC Shield and Grove - Chainable RGB LED to DIY a touch pixel. Then use an Android with NFC support to colorize it.
The project is on https://github.com/Seeed-Studio/TouchPixel
main.cpp@0:88960f3eeb2c, 2013-12-27 (annotated)
- Committer:
- yihui
- Date:
- Fri Dec 27 01:46:32 2013 +0000
- Revision:
- 0:88960f3eeb2c
initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:88960f3eeb2c | 1 | |
yihui | 0:88960f3eeb2c | 2 | #include "mbed.h" |
yihui | 0:88960f3eeb2c | 3 | #include "ChainableLED.h" |
yihui | 0:88960f3eeb2c | 4 | #include "PN532_SPI.h" |
yihui | 0:88960f3eeb2c | 5 | #include "emulatetag.h" |
yihui | 0:88960f3eeb2c | 6 | #include "NdefMessage.h" |
yihui | 0:88960f3eeb2c | 7 | |
yihui | 0:88960f3eeb2c | 8 | #include <string.h> |
yihui | 0:88960f3eeb2c | 9 | |
yihui | 0:88960f3eeb2c | 10 | #define LOG(args...) |
yihui | 0:88960f3eeb2c | 11 | |
yihui | 0:88960f3eeb2c | 12 | BusOut leds(LED1, LED2, LED3, LED4); |
yihui | 0:88960f3eeb2c | 13 | |
yihui | 0:88960f3eeb2c | 14 | ChainableLED rgbled(P1_14, P1_13, 1); |
yihui | 0:88960f3eeb2c | 15 | |
yihui | 0:88960f3eeb2c | 16 | SPI spi(P1_22, P1_21, P1_20); |
yihui | 0:88960f3eeb2c | 17 | PN532_SPI interface(spi, P0_2); |
yihui | 0:88960f3eeb2c | 18 | EmulateTag nfc(interface); |
yihui | 0:88960f3eeb2c | 19 | |
yihui | 0:88960f3eeb2c | 20 | uint8_t ndefBuf[120]; |
yihui | 0:88960f3eeb2c | 21 | NdefMessage message; |
yihui | 0:88960f3eeb2c | 22 | int messageSize; |
yihui | 0:88960f3eeb2c | 23 | |
yihui | 0:88960f3eeb2c | 24 | uint8_t uid[3] = { 0x12, 0x34, 0x56 }; |
yihui | 0:88960f3eeb2c | 25 | |
yihui | 0:88960f3eeb2c | 26 | uint32_t getColor(uint8_t *buf) |
yihui | 0:88960f3eeb2c | 27 | { |
yihui | 0:88960f3eeb2c | 28 | uint32_t x = 0; |
yihui | 0:88960f3eeb2c | 29 | for (uint8_t i = 0; i < 8; i++) { |
yihui | 0:88960f3eeb2c | 30 | uint8_t c = *buf; |
yihui | 0:88960f3eeb2c | 31 | if (c >= '0' && c <= '9') { |
yihui | 0:88960f3eeb2c | 32 | x *= 16; |
yihui | 0:88960f3eeb2c | 33 | x += c - '0'; |
yihui | 0:88960f3eeb2c | 34 | } else if (c >= 'A' && c <= 'F') { |
yihui | 0:88960f3eeb2c | 35 | x *= 16; |
yihui | 0:88960f3eeb2c | 36 | x += (c - 'A') + 10; |
yihui | 0:88960f3eeb2c | 37 | } else if (c >= 'a' && c <= 'f') { |
yihui | 0:88960f3eeb2c | 38 | x *= 16; |
yihui | 0:88960f3eeb2c | 39 | x += (c - 'a') + 10; |
yihui | 0:88960f3eeb2c | 40 | } else |
yihui | 0:88960f3eeb2c | 41 | break; |
yihui | 0:88960f3eeb2c | 42 | |
yihui | 0:88960f3eeb2c | 43 | buf++; |
yihui | 0:88960f3eeb2c | 44 | } |
yihui | 0:88960f3eeb2c | 45 | |
yihui | 0:88960f3eeb2c | 46 | return x; |
yihui | 0:88960f3eeb2c | 47 | } |
yihui | 0:88960f3eeb2c | 48 | |
yihui | 0:88960f3eeb2c | 49 | void processNewNdef(uint8_t *buf, uint16_t length) |
yihui | 0:88960f3eeb2c | 50 | { |
yihui | 0:88960f3eeb2c | 51 | NdefMessage msg = NdefMessage(buf, length); |
yihui | 0:88960f3eeb2c | 52 | |
yihui | 0:88960f3eeb2c | 53 | NdefRecord record = msg.getRecord(0); |
yihui | 0:88960f3eeb2c | 54 | uint8_t recordbuf[32]; |
yihui | 0:88960f3eeb2c | 55 | record.getType(recordbuf); |
yihui | 0:88960f3eeb2c | 56 | if (!memcmp(recordbuf, "text/c", 6)) { |
yihui | 0:88960f3eeb2c | 57 | uint8_t r, g, b; |
yihui | 0:88960f3eeb2c | 58 | uint32_t color; |
yihui | 0:88960f3eeb2c | 59 | record.getPayload(recordbuf); |
yihui | 0:88960f3eeb2c | 60 | color = getColor(recordbuf); |
yihui | 0:88960f3eeb2c | 61 | r = (color >> 16) & 0xFF; |
yihui | 0:88960f3eeb2c | 62 | g = (color >> 8) & 0xFF; |
yihui | 0:88960f3eeb2c | 63 | b = color & 0xFF; |
yihui | 0:88960f3eeb2c | 64 | |
yihui | 0:88960f3eeb2c | 65 | rgbled.setColorRGB(0, r, g, b); |
yihui | 0:88960f3eeb2c | 66 | leds = r; |
yihui | 0:88960f3eeb2c | 67 | } |
yihui | 0:88960f3eeb2c | 68 | } |
yihui | 0:88960f3eeb2c | 69 | |
yihui | 0:88960f3eeb2c | 70 | int main() |
yihui | 0:88960f3eeb2c | 71 | { |
yihui | 0:88960f3eeb2c | 72 | LOG("------- Emulate Tag --------\n"); |
yihui | 0:88960f3eeb2c | 73 | rgbled.setColorRGB(0, 0, 0, 0); |
yihui | 0:88960f3eeb2c | 74 | |
yihui | 0:88960f3eeb2c | 75 | NdefRecord aarRecord = NdefRecord(); |
yihui | 0:88960f3eeb2c | 76 | const uint8_t aarType[] = "android.com:pkg"; |
yihui | 0:88960f3eeb2c | 77 | const uint8_t aarPayload[] = "com.seeedstudio.android.nfc.touchpixel"; |
yihui | 0:88960f3eeb2c | 78 | |
yihui | 0:88960f3eeb2c | 79 | aarRecord.setTnf(TNF_EXTERNAL_TYPE); |
yihui | 0:88960f3eeb2c | 80 | aarRecord.setType(aarType, sizeof(aarType) - 1); |
yihui | 0:88960f3eeb2c | 81 | aarRecord.setPayload(aarPayload, sizeof(aarPayload) - 1); |
yihui | 0:88960f3eeb2c | 82 | |
yihui | 0:88960f3eeb2c | 83 | message = NdefMessage(); |
yihui | 0:88960f3eeb2c | 84 | message.addMimeMediaRecord("text/c", "FF000000"); |
yihui | 0:88960f3eeb2c | 85 | message.addRecord(aarRecord); |
yihui | 0:88960f3eeb2c | 86 | messageSize = message.getEncodedSize(); |
yihui | 0:88960f3eeb2c | 87 | if (messageSize > sizeof(ndefBuf)) { |
yihui | 0:88960f3eeb2c | 88 | while (1) { } |
yihui | 0:88960f3eeb2c | 89 | } |
yihui | 0:88960f3eeb2c | 90 | |
yihui | 0:88960f3eeb2c | 91 | LOG("Ndef encoded message size: %d\n", messageSize); |
yihui | 0:88960f3eeb2c | 92 | |
yihui | 0:88960f3eeb2c | 93 | message.encode(ndefBuf); |
yihui | 0:88960f3eeb2c | 94 | |
yihui | 0:88960f3eeb2c | 95 | // comment out this command for no ndef message |
yihui | 0:88960f3eeb2c | 96 | nfc.setNdefFile(ndefBuf, messageSize); |
yihui | 0:88960f3eeb2c | 97 | |
yihui | 0:88960f3eeb2c | 98 | // uid must be 3 bytes! |
yihui | 0:88960f3eeb2c | 99 | nfc.setUid(uid); |
yihui | 0:88960f3eeb2c | 100 | |
yihui | 0:88960f3eeb2c | 101 | nfc.init(); |
yihui | 0:88960f3eeb2c | 102 | |
yihui | 0:88960f3eeb2c | 103 | nfc.attach(processNewNdef); |
yihui | 0:88960f3eeb2c | 104 | |
yihui | 0:88960f3eeb2c | 105 | while (1) { |
yihui | 0:88960f3eeb2c | 106 | //nfc.setNdefFile(ndefBuf, messageSize); |
yihui | 0:88960f3eeb2c | 107 | nfc.emulate(); |
yihui | 0:88960f3eeb2c | 108 | } |
yihui | 0:88960f3eeb2c | 109 | } |