RFID code for SLVM

Dependencies:   ID12RFID mbed

Revision:
0:23e85e8989fc
diff -r 000000000000 -r 23e85e8989fc main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 03 22:39:53 2014 +0000
@@ -0,0 +1,89 @@
+#include "mbed.h"
+#include "ID12RFID.h"
+
+ID12RFID rfid(p27); // uart rx
+Serial pc(USBTX,USBRX);
+
+DigitalOut interrupt(p23);
+DigitalOut MSB(p21);
+DigitalOut LSB(p22);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+
+float speedModifiers[] = {
+    1.0, 0.7, 0.5, 0.3
+};
+
+int speedTags[] = {
+    5452129,
+    5449063,
+    36902518
+};
+
+volatile int tag = 0;
+volatile bool tagRead = false;
+
+int intCorrection;
+
+void readCard();
+void checkTag();
+
+void readCard() {
+    // First, disable interrupts to allow critical sections to run through
+    NVIC_DisableIRQ(UART2_IRQn);
+    //led3 = !led3;
+    if(rfid.readable()) {
+        tag = rfid.read();
+        tagRead = true;
+        bool match = false;
+        int length = (sizeof(speedTags)/sizeof(speedTags[0]) < sizeof(speedModifiers)/sizeof(speedModifiers[0])) ? (sizeof(speedTags)/sizeof(speedTags[0])) : (sizeof(speedModifiers)/(sizeof(speedModifiers[0])));
+        for (int i=0; i<length; i++) {
+            if (speedTags[i] == tag) {
+                printf("RFID Tag number : %d\nSpeed now at %d\n", tag, ((int)(speedModifiers[i]*100)));
+                match = true;
+                intCorrection++;
+            }
+        }
+    
+        // For some reason, this function is entered twice each time an RFID tag is read.
+        // This bit here is to detect that case and just ignore it, while still catching
+        // other cases and displaying a warning
+        if (!match and intCorrection == 0) {
+            printf("[ WARNING ] Tag number %d not recognized\n", tag);
+        }
+        else if (!match == 1) {
+            intCorrection = 0;
+        }
+    }
+    
+    NVIC_EnableIRQ(UART2_IRQn);
+}
+
+void checkTag() {
+}
+
+int main() {
+    interrupt=0;
+    rfid.setInterrupt(&readCard);
+    printf("Entering main loop\n");
+    while(1) {
+        if (tagRead) {
+            checkTag(); 
+        if(tag==5452129 || tag==5449063) LSB=1;
+        else LSB=0;
+        if(tag==5449063 || tag==36902518) MSB=1;
+        else MSB=0;
+        interrupt=1;
+        led1=MSB;
+        led2=LSB;
+        led3=interrupt;
+        wait(.5);
+        interrupt=0;  
+        led3=interrupt;      
+        tagRead = false;}
+        //led1 = 1; wait(0.5);
+        //led1 = 0; wait(0.5);
+        wait(.1);
+    }
+}