/
nfc_unique_color_toggle.cpp
153 lines (134 loc) · 4.57 KB
/
nfc_unique_color_toggle.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
/////////////////////////
// INCLUDE STATEMENTS //
///////////////////////
// For console output
#include <iostream>
// For sleep
#include <chrono>
#include <thread>
// For string
#include <string>
// For set
#include <set>
// For using the Everloop
#include "matrix_hal/everloop.h"
#include "matrix_hal/everloop_image.h"
#include "matrix_hal/matrixio_bus.h"
// For using NFC
#include "matrix_nfc/nfc.h"
#include "matrix_nfc/nfc_data.h"
int main() {
////////////////////
// INITIAL SETUP //
//////////////////
// Setting up HAL bus
matrix_hal::MatrixIOBus bus;
if (!bus.Init()) return false;
// Setting up Everloop
matrix_hal::EverloopImage everloop_image(bus.MatrixLeds());
matrix_hal::Everloop everloop;
everloop.Setup(&bus);
// Setting up NFC
matrix_hal::NFC nfc;
matrix_hal::NFCData nfc_data;
/////////////////
// MAIN CODE //
///////////////
// Set used to check for uniqueness
std::set<std::string> UID_set;
// Strings to store the unique UID for three tags.
std::string green_UID = "";
std::string blue_UID = "";
std::string red_UID = "";
// Scan red tag, using a set to ensure that the UID has not been scanned
// before. Loop until a unique UID has been scanned.
std::cout << "Scan Red Tag" << std::endl;
while (true) {
nfc.Activate();
nfc.ReadInfo(&nfc_data.info);
nfc.Deactivate();
red_UID = nfc_data.info.UIDToHex();
// If the find function returns UID_set.end() then the UID was not
// previously read.
if (nfc_data.info.recently_updated &&
UID_set.find(red_UID) == UID_set.end())
break;
}
UID_set.insert(red_UID);
// Scan green tag, using a set to ensure that the UID has not been scanned
// before. Loop until a unique UID has been scanned.
std::cout << "Scan Green Tag" << std::endl;
while (true) {
nfc.Activate();
nfc.ReadInfo(&nfc_data.info);
nfc.Deactivate();
green_UID = nfc_data.info.UIDToHex();
// If the find function returns UID_set.end() then the UID was not
// previously read.
if (nfc_data.info.recently_updated &&
UID_set.find(green_UID) == UID_set.end())
break;
}
UID_set.insert(green_UID);
// Scan blue tag, using a set to ensure that the UID has not been scanned
// before. Loop until a unique UID has been scanned.
std::cout << "Scan Blue Tag" << std::endl;
while (true) {
nfc.Activate();
nfc.ReadInfo(&nfc_data.info);
nfc.Deactivate();
blue_UID = nfc_data.info.UIDToHex();
// If the find function returns UID_set.end() then the UID was not
// previously read.
if (nfc_data.info.recently_updated &&
UID_set.find(blue_UID) == UID_set.end())
break;
}
UID_set.insert(blue_UID);
std::cout << "\nScan specified tags to activate Everloop" << std::endl;
do {
// Scan NFC tag and read info into nfc_data.info
nfc.Activate();
nfc.ReadInfo(&nfc_data.info);
nfc.Deactivate();
// If tag was detected set everloop to the color that matches the
// scanned tag's UID, else set everloop to off.
if (nfc_data.info.recently_updated) {
std::string curr_UID = nfc_data.info.UIDToHex();
if (curr_UID == red_UID) {
for (matrix_hal::LedValue &led : everloop_image.leds) {
led.red = 50;
led.green = 0;
led.blue = 0;
led.white = 0;
}
} else if (curr_UID == green_UID) {
for (matrix_hal::LedValue &led : everloop_image.leds) {
led.red = 0;
led.green = 50;
led.blue = 0;
led.white = 0;
}
} else if (curr_UID == blue_UID) {
for (matrix_hal::LedValue &led : everloop_image.leds) {
led.red = 0;
led.green = 0;
led.blue = 50;
led.white = 0;
}
}
} else {
for (matrix_hal::LedValue &led : everloop_image.leds) {
led.red = 0;
led.green = 0;
led.blue = 0;
led.white = 0;
}
}
// Update the Everloop
everloop.Write(&everloop_image);
// Sleep for a reasonable amount of time
std::this_thread::sleep_for(std::chrono::microseconds(10000));
} while (true);
return 0;
}