Browse Source

refactor

develop
Robin Thoni 7 years ago
parent
commit
e5d3e718c4
1 changed files with 46 additions and 32 deletions
  1. 46
    32
      main.ino

+ 46
- 32
main.ino View File

3
 #include <SoftwareSerial.h>
3
 #include <SoftwareSerial.h>
4
 #include <SCLib.h>
4
 #include <SCLib.h>
5
 
5
 
6
-#define ledPin 13
6
+#define STATUS_LED 13
7
 
7
 
8
 #define READER_VCC
8
 #define READER_VCC
9
 #define READER_RST 4
9
 #define READER_RST 4
20
 #define CARD_IO    10
20
 #define CARD_IO    10
21
 #define CARD_PRE   12
21
 #define CARD_PRE   12
22
 
22
 
23
-SmartCardReader cardReader(CARD_IO, CARD_RST, CARD_VCC, CARD_PRE, CARD_CLK, true);
24
-uint8_t atr[MAX_ATR_BYTES];
25
-int atr_size;
23
+SmartCardReader cardReader(CARD_IO, CARD_RST, CARD_VCC, CARD_PRE, CARD_CLK, false);
24
+uint8_t readerAtr[MAX_ATR_BYTES];
25
+int readerAtrSize = 0;
26
 
26
 
27
 bool readerReset = false;
27
 bool readerReset = false;
28
 int baudRate = -1;
28
 int baudRate = -1;
43
 
43
 
44
 #endif
44
 #endif
45
 
45
 
46
-void onReaderReset()
47
-{
48
-    readerReset = true;
49
-}
50
-
51
-void computeEtu()
46
+void emulatorComputeEtu()
52
 {
47
 {
53
     baudRate = 9600;
48
     baudRate = 9600;
54
     etu = 1000000 / baudRate;
49
     etu = 1000000 / baudRate;
55
 }
50
 }
56
 
51
 
57
-void sendAtrByte(char byte)
52
+void emulatorSendAtrByte(char byte)
58
 {
53
 {
59
     unsigned long nextBitTime = micros() + (2 * etu);
54
     unsigned long nextBitTime = micros() + (2 * etu);
60
     digitalWrite(READER_IO, HIGH);
55
     digitalWrite(READER_IO, HIGH);
89
     }
84
     }
90
 }
85
 }
91
 
86
 
92
-void sendAtr(const uint8_t* atr, size_t size)
87
+void emulatorSendAtr(const uint8_t* atr, size_t size)
93
 {
88
 {
94
     pinMode(READER_IO, OUTPUT);
89
     pinMode(READER_IO, OUTPUT);
95
     for (int i = 0; i < size; ++i)
90
     for (int i = 0; i < size; ++i)
96
     {
91
     {
97
-        sendAtrByte(atr[i]);
92
+        emulatorSendAtrByte(atr[i]);
98
         delayMicroseconds(4*etu);
93
         delayMicroseconds(4*etu);
99
     }
94
     }
100
 }
95
 }
101
 
96
 
97
+void readerResetAtr()
98
+{
99
+    for (size_t i = 0; i < sizeof(readerAtr); ++i)
100
+    {
101
+        readerAtr[i] = 0;
102
+    }
103
+    readerAtrSize = 0;
104
+}
105
+
102
 void setup() {
106
 void setup() {
103
     Serial.begin(9600);
107
     Serial.begin(9600);
104
     Serial.println("Initializing...");
108
     Serial.println("Initializing...");
108
     pinMode(READER_IO, OUTPUT);
112
     pinMode(READER_IO, OUTPUT);
109
     digitalWrite(READER_IO, HIGH);
113
     digitalWrite(READER_IO, HIGH);
110
 
114
 
111
-    pinMode(ledPin, OUTPUT);
112
-    digitalWrite (ledPin, LOW);
115
+    pinMode(STATUS_LED, OUTPUT);
116
+    digitalWrite(STATUS_LED, LOW);
113
     Serial.println("Initialized");
117
     Serial.println("Initialized");
114
 
118
 
115
-
116
-    atr_size = cardReader.activate(atr, sizeof(atr));
117
-    cardReader.deactivate();
119
+    readerResetAtr();
118
 }
120
 }
119
 
121
 
120
 void loop() {
122
 void loop() {
121
 
123
 
122
-    if (digitalRead(READER_RST))
124
+    if (readerAtrSize == 0)
123
     {
125
     {
124
-        if (!readerReset)
126
+        if (cardReader.cardInserted())
125
         {
127
         {
126
-//            atr_size = cardReader.activate(atr, sizeof(atr));
127
-
128
-//            uint8_t atr[] = {0x3B, 0x65, 0x00, 0x00, 0x20, 0x63, 0xCB, 0x6C, 0x80};
129
-//            int atr_size = sizeof(atr);
130
-            computeEtu();
131
-            sendAtr(atr, atr_size);
132
-            readerReset = true;
133
-            digitalWrite(ledPin, !digitalRead(ledPin));
134
-
135
-//            cardReader.deactivate();
128
+            readerAtrSize = cardReader.activate(readerAtr, sizeof(readerAtr));
129
+            cardReader.dumpHEX(readerAtr, readerAtrSize);
130
+            cardReader.deactivate();
131
+            digitalWrite(STATUS_LED, HIGH);
136
         }
132
         }
137
     }
133
     }
138
     else
134
     else
139
     {
135
     {
140
-        if (readerReset) {
141
-            readerReset = false;
136
+        if (!cardReader.cardInserted())
137
+        {
138
+            readerResetAtr();
139
+            digitalWrite(STATUS_LED, LOW);
140
+        }
141
+        else if (digitalRead(READER_RST))
142
+        {
143
+            if (!readerReset)
144
+            {
145
+                emulatorComputeEtu();
146
+                emulatorSendAtr(readerAtr, readerAtrSize);
147
+                readerReset = true;
148
+            }
149
+        }
150
+        else
151
+        {
152
+            if (readerReset)
153
+            {
154
+                readerReset = false;
155
+            }
142
         }
156
         }
143
     }
157
     }
144
 }
158
 }

Loading…
Cancel
Save