This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Amiibo.cpp
executable file
·168 lines (137 loc) · 3.55 KB
/
Amiibo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Amiibo.h"
#include "Adafruit_PN532.h"
Amiibo::Amiibo(Adafruit_PN532 nfc) : nfc(nfc)
{
this->amiiboDataCache = new uint8_t[AMIIBO_PAGE_COUNT*4];
this->amiiboDataCacheStatus = new bool[AMIIBO_PAGE_COUNT];
memset(this->amiiboDataCache, 0, AMIIBO_PAGE_COUNT*4);
memset(this->amiiboDataCacheStatus, false, AMIIBO_PAGE_COUNT);
}
Amiibo::~Amiibo()
{
delete [] this->amiiboDataCache;
delete [] this->amiiboDataCacheStatus;
}
void Amiibo::clearCache()
{
memset(this->amiiboDataCacheStatus, false, AMIIBO_PAGE_COUNT);
}
void Amiibo::begin()
{
this->nfc.begin();
uint32_t versiondata = this->nfc.getFirmwareVersion();
if (!versiondata) {
Serial.print("Didn't find PN53x board");
while (1);
}
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
this->nfc.SAMConfig();
}
bool Amiibo::read()
{
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
uint8_t uidLength;
success = this->nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (!success) {
return false;
}
// Serial.println("Found an ISO14443A card");
// Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
// Serial.print(" UID Value: ");
// for (uint8_t i = 0; i < uidLength; i++) {
// if (uid[i] <= 0x0F)
// Serial.print(F("0"));
// Serial.print(uid[i], HEX);
// }
// Serial.println();
if (uidLength != 7) {
return false;
}
if (this->getByteAt(22, 3) != 0x02) {
return false;
}
return true;
}
bool Amiibo::readFull()
{
bool success = true;
for (uint8_t i = 0; i < AMIIBO_PAGE_COUNT; i++)
{
if (!this->readPage(i)) {
success = false;
Serial.print("Cannot read page "); Serial.println(i);
}
}
return success;
}
void Amiibo::printCache()
{
for (uint8_t i = 0; i < AMIIBO_PAGE_COUNT; i++) {
switch (i) {
case 0:
Serial.println(F("-------- Header data --------"));
break;
case 4:
Serial.println(F("-------- User page START --------"));
break;
case 129+1:
Serial.println(F("-------- User page END --------"));
break;
case 131:
Serial.println(F("-------- Cfg page START --------"));
break;
case 132+1:
Serial.println(F("-------- Cfg page END --------"));
break;
}
Serial.print(this->amiiboDataCacheStatus[i] ? F("+") : F("-"));
Serial.print(" PAGE ");
if (i < 10)
{
Serial.print("0");
Serial.print(i);
}
else
{
Serial.print(i);
}
Serial.print(": ");
nfc.PrintHexChar(this->amiiboDataCache+(i*4), 4);
}
}
bool Amiibo::readPage(uint8_t page)
{
if (this->amiiboDataCacheStatus[page]) {
return true;
}
if (this->nfc.ntag2xx_ReadPage(page, this->amiiboDataCache + (page * 4))) {
this->amiiboDataCacheStatus[page] = true;
return true;
}
return false;
}
uint8_t Amiibo::getByteAt(uint8_t page, uint8_t pos)
{
if (!this->readPage(page)) {
return -1;
}
return this->amiiboDataCache[page*4+pos];
}
bool Amiibo::readInfo(Amiibo_Info *info)
{
if (!(this->readPage(21) && this->readPage(22))) {
return false;
}
memcpy(info, this->amiiboDataCache+84, 8);
// reversing endianness on amiibo id
info->amiibo_id = (info->amiibo_id << 8) | ((info->amiibo_id >> 8) & 0x00ff);
return info->static_byte == 0x02;
}