-
Notifications
You must be signed in to change notification settings - Fork 3
/
magspoof.ino
184 lines (154 loc) · 3.91 KB
/
magspoof.ino
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copied from https://github.com/joshlf/magspoof.git
// and https://github.com/samyk/magspoof.git
// and port something to Arduino Nano
// - miaoski
void setPolarity(int polarity);
void playBit(int bit);
#define PIN_A 3 // left pin, to L293D pin 2
#define PIN_B 4 // right pin, to L293D pin 7
#define ENABLE_PIN 13
#define BUTTON_PIN 12
#define CLOCK_US 400
#define BETWEEN_ZERO 53 // 53 zeros between track1 & 2
int polarity = 0;
unsigned int curTrack = 0;
char revTrack[80];
const int sublen[] = { 32, 48, 48 };
const int bitlen[] = { 7, 5, 5 };
const char* tracks[] = {
"%B4503111122223333^MIAOSKI LIN ^2010200999909999999900990000000?", // Track 1; %BPAN^NAME^YYMMSSSCVV? + LRC
";4503111122223333=200120099999999?" // Track 2 ;PAN=YYMMSSSDIS? + LRC
};
void setup() {
pinMode(PIN_A, OUTPUT);
pinMode(PIN_B, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
storeRevTrack(2);
}
// Set the polarity of the elctromagnet.
void setPolarity(int polarity) {
if (polarity) {
digitalWrite(PIN_B, LOW);
digitalWrite(PIN_A, HIGH);
} else {
digitalWrite(PIN_A, LOW);
digitalWrite(PIN_B, HIGH);
}
}
void playBit(int bit) {
polarity ^= 1;
setPolarity(polarity);
delayMicroseconds(CLOCK_US);
if (bit == 1) {
polarity ^= 1;
setPolarity(polarity);
}
delayMicroseconds(CLOCK_US);
}
// when reversing
void reverseTrack(int track) {
int i = 0;
track--; // index 0
polarity = 0;
while(revTrack[i++] != '\0');
i--;
while(i--)
for (int j = bitlen[track]-1; j >= 0; j--)
playBit((revTrack[i] >> j) & 1);
}
// plays out a full track, calculating CRCs and LRC
void playTrack(int track)
{
int tmp, crc, lrc = 0;
track--; // index 0
polarity = 0;
// enable H-bridge and LED
digitalWrite(ENABLE_PIN, HIGH);
// First put out a bunch of leading zeros.
for(int i = 0; i < 25; i++)
playBit(0);
Serial.print("Sending: ");
for(int i = 0; tracks[track][i] != '\0'; i++) {
Serial.print(tracks[track][i]); // The delay that Track 1 needs.
crc = 1;
tmp = tracks[track][i] - sublen[track];
for(int j = 0; j < bitlen[track]-1; j++) {
crc ^= (tmp & 1);
lrc ^= (tmp & 1) << j;
playBit(tmp & 1);
tmp >>= 1;
}
playBit(crc);
}
Serial.println();
// finish calculating and send last "byte" (LRC)
tmp = lrc;
crc = 1;
for(int j = 0; j < bitlen[track]-1; j++) {
crc ^= tmp & 1;
playBit(tmp & 1);
tmp >>= 1;
}
playBit(crc);
// if track 1, play 2nd track in reverse (like swiping back?)
/*
if(track == 0) {
for(int i = 0; i < BETWEEN_ZERO; i++)
playBit(0);
reverseTrack(2);
}
*/
// finish with 0's
for (int i = 0; i < 25; i++)
playBit(0);
digitalWrite(PIN_A, LOW);
digitalWrite(PIN_B, LOW);
digitalWrite(ENABLE_PIN, LOW);
}
// stores track for reverse usage later
void storeRevTrack(int track) {
int i, tmp, crc, lrc = 0;
track--; // index 0
polarity = 0;
for (i = 0; tracks[track][i] != '\0'; i++) {
crc = 1;
tmp = tracks[track][i] - sublen[track];
for (int j = 0; j < bitlen[track]-1; j++) {
crc ^= tmp & 1;
lrc ^= (tmp & 1) << j;
tmp & 1 ?
(revTrack[i] |= 1 << j) :
(revTrack[i] &= ~(1 << j));
tmp >>= 1;
}
crc ?
(revTrack[i] |= 1 << 4) :
(revTrack[i] &= ~(1 << 4));
}
// finish calculating and send last "byte" (LRC)
tmp = lrc;
crc = 1;
for (int j = 0; j < bitlen[track]-1; j++) {
crc ^= tmp & 1;
tmp & 1 ?
(revTrack[i] |= 1 << j) :
(revTrack[i] &= ~(1 << j));
tmp >>= 1;
}
crc ?
(revTrack[i] |= 1 << 4) :
(revTrack[i] &= ~(1 << 4));
i++;
revTrack[i] = '\0';
}
void loop() {
memset(revTrack, 1, sizeof(revTrack));
while(digitalRead(BUTTON_PIN) == HIGH);
delay(50);
while(digitalRead(BUTTON_PIN) == HIGH);
Serial.print("Track ");
Serial.println(1 + (curTrack % 2));
playTrack(1 + (curTrack++ % 2));
delay(400);
}