| 1 | /** |
| 2 | Copyright (c) 2011, 2012, 2013 Research In Motion Limited. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | **/ |
| 16 | #include <Wire.h> |
| 17 | #include <SPI.h> |
| 18 | #include <Adafruit_PN532.h> |
| 19 | |
| 20 | // If using the breakout with SPI, define the pins for SPI communication. |
| 21 | #define PN532_SCK (2) |
| 22 | #define PN532_MOSI (3) |
| 23 | #define PN532_SS (4) |
| 24 | #define PN532_MISO (5) |
| 25 | |
| 26 | // If using the breakout or shield with I2C, define just the pins connected |
| 27 | // to the IRQ and reset lines. Use the values below (2, 3) for the shield! |
| 28 | #define PN532_IRQ (2) |
| 29 | #define PN532_RESET (3) // Not connected by default on the NFC Shield |
| 30 | |
| 31 | // Uncomment just _one_ line below depending on how your breakout or shield |
| 32 | // is connected to the Arduino: |
| 33 | |
| 34 | // Use this line for a breakout with a SPI connection: |
| 35 | Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS); |
| 36 | |
| 37 | // Use this line for a breakout with a hardware SPI connection. Note that |
| 38 | // the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's |
| 39 | // hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are |
| 40 | // SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin. |
| 41 | //Adafruit_PN532 nfc(PN532_SS); |
| 42 | |
| 43 | // Or use this line for a breakout or shield with an I2C connection: |
| 44 | //Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); |
| 45 | |
| 46 | // PWM LED will be on the following PINs. |
| 47 | #define R_PIN (9) |
| 48 | #define G_PIN (10) |
| 49 | #define B_PIN (11) |
| 50 | |
| 51 | // Initial values of RGB. |
| 52 | uint8_t r = 0x00; |
| 53 | uint8_t g = 0x00; |
| 54 | uint8_t b = 0x7f; |
| 55 | |
| 56 | /** |
| 57 | * Write the current color to the output pins. |
| 58 | */ |
| 59 | void showColor() { |
| 60 | analogWrite(R_PIN,r); |
| 61 | analogWrite(G_PIN,g); |
| 62 | analogWrite(B_PIN,b); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void setup() { |
| 67 | Serial.begin(115200); |
| 68 | while (!Serial) delay(10); // for Leonardo/Micro/Zero |
| 69 | |
| 70 | pinMode(R_PIN,OUTPUT); |
| 71 | pinMode(G_PIN,OUTPUT); |
| 72 | pinMode(B_PIN,OUTPUT); |
| 73 | showColor(); |
| 74 | |
| 75 | nfc.begin(); |
| 76 | |
| 77 | uint32_t versiondata = nfc.getFirmwareVersion(); |
| 78 | if (! versiondata) { |
| 79 | Serial.println("Did not find the shield - locking up"); |
| 80 | while (true) { |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Serial.print("Found chip PN5"); |
| 85 | Serial.println((versiondata>>24) & 0xFF, HEX); |
| 86 | Serial.print("Firmware ver. "); |
| 87 | Serial.print((versiondata>>16) & 0xFF, DEC); |
| 88 | Serial.print('.'); |
| 89 | Serial.println((versiondata>>8) & 0xFF, DEC); |
| 90 | |
| 91 | nfc.begin(); |
| 92 | } |
| 93 | |
| 94 | uint8_t message[4]; |
| 95 | |
| 96 | void loop(void) { |
| 97 | uint8_t i; |
| 98 | |
| 99 | // Serial.println("Listening..."); |
| 100 | if (nfc.inListPassiveTarget()) { |
| 101 | // Serial.println("Something's there..."); |
| 102 | while(true) { |
| 103 | message[0] = 1; |
| 104 | message[1] = r; |
| 105 | message[2] = g; |
| 106 | message[3] = b; |
| 107 | uint8_t responseLength = sizeof(message); |
| 108 | if (nfc.inDataExchange(message,sizeof(message),message,&responseLength)) { |
| 109 | uint8_t command = message[0]; |
| 110 | switch(command) { |
| 111 | case 0: |
| 112 | // NOP |
| 113 | break; |
| 114 | case 1: |
| 115 | if (responseLength==4) { |
| 116 | r = message[1]; |
| 117 | g = message[2]; |
| 118 | b = message[3]; |
| 119 | showColor(); |
| 120 | //Serial.print("Read a color: "); |
| 121 | //for (i=0; i<3; ++i) { |
| 122 | // Serial.print(message[i+1],HEX); |
| 123 | // Serial.print(' '); |
| 124 | //} |
| 125 | //Serial.println(); |
| 126 | } |
| 127 | else { |
| 128 | //Serial.println("Doesn't seem to be a color..."); |
| 129 | } |
| 130 | break; |
| 131 | default: |
| 132 | //Serial.print("Unknown command "); |
| 133 | //Serial.println(message[0]); |
| 134 | ; |
| 135 | } |
| 136 | delay(10); |
| 137 | } |
| 138 | else { |
| 139 | //Serial.println("It's gone..."); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | else { |
| 145 | //Serial.print("Trying again..."); |
| 146 | } |
| 147 | } |
| 148 | |