Uses an ID20 RFID Reader to create a tag list with a few functions that include: printing the current list, checking if a tag exists in the list, adding a tag to the list, and deleting a tag from the list

Dependencies:   ID12RFID TextLCD mbed

Code to create an RFID Tag List. Page is located at http://mbed.org/users/memsterjr09/notebook/rfid-tag-list-builder/

main.cpp

Committer:
memsterjr09
Date:
2013-10-16
Revision:
1:867cf1706b42
Parent:
0:fa3fe200c570

File content as of revision 1:867cf1706b42:

#include "mbed.h"
#include "ID12RFID.h"
#include "TextLCD.h"
#include "RFID_Functions.h"


DigitalIn show(p24); //PushButton to show list
DigitalIn look(p23); //PushButton to look if user is in list
DigitalIn add(p22); //PushButton to add user to list
DigitalIn del(p21); //PushButton to delete user from list
ID12RFID rfid(p14); // uart rx
Serial pc(USBTX, USBRX); //PC Terminal Display
TextLCD lcd(p15, p16, p17, p18, p19, p20); //LCD Screen Display
int state = 0; // Will determine what pushbutton is being pressed
LinkedList IDList = emptyList(); //initialize the IDList
LLNode *toAdd = IDList.head; //Create an Empty Tag Node
int ID = 0; // Initialize the ID number
int found; // Flag that will determine if an ID exists in the list already
int main()
{
    lcd.cls(); //Clear the Screen on Reset
    // Initialize the pullup resistors in pushbutton
    show.mode(PullUp);
    look.mode(PullUp);
    add.mode(PullUp);
    del.mode(PullUp);
    while(1) {
        lcd.cls();
        // Determine if any of the pushbuttons are being pressed, and update the state accordingly
        if(!show) state = 1;
        if(!look) state = 2;
        if(!add) state = 3;
        if(!del) state = 4;
        switch(state) {
            // Print the List
            case 1:
                lcd.printf("Printing List");
                wait(1);
                showList(IDList);
                lcd.cls();
                lcd.printf("List Printed");
                state = 0;
                wait(3);
                break;
            // Check to see if an ID exists in the list already
            case 2:
                int flag = false;
                lcd.printf("Look Up Tag\n");
                lcd.printf("Scan RFID Tag");
                // Wait for a Tag to be scanned
                while(!ID) {
                    if(rfid.readable()) {
                        ID = rfid.read();
                    }
                    wait(1);
                }
                lcd.cls();
                LLNode *node = IDList.head;
                // Traverse through the list.  Will figure out if the tag exists in the list already
                while(node->data != NULL) {
                    if(node->data == ID) {
                        lcd.printf("ID: %d\n", ID);
                        lcd.printf("Was Found");
                        ID = 0;
                        state = 0;
                        wait(3);
                        flag = true;
                        break;
                    }
                    if(node->next == NULL) break;
                    node = node->next;
                }
                // If the flag was set, then the tag was found in the list, so don't want to do the Not Found part
                if(flag) break;
                lcd.printf("ID: %d\n", ID);
                lcd.printf("Was Not Found");
                ID = 0;
                state = 0;
                wait(3);
                break;
            // Add a tag to the list
            case 3:
                lcd.printf("Add Tag\n");
                lcd.printf("Scan RFID Tag");
                // Wait or a tag scan
                while(!ID) {
                    if(rfid.readable()) {
                        ID = rfid.read();
                    }
                    wait(1);
                }
                lcd.cls();
                // Determine if the Tag already exists in the list
                found = lookUp(IDList, ID);
                // Tag previously exists
                if(found) {
                    lcd.printf("ID: %d\n", ID);
                    lcd.printf("Exists in List");
                    ID = 0;
                    state = 0;
                    wait(3);
                    break;
                // Tag doesn't exist
                } else {
                    addToList(&IDList, ID);
                    lcd.printf("ID: %d\n", ID);
                    lcd.printf("Added to List");
                    ID = 0;
                    state = 0;
                    wait(3);
                    break;
                }
            // Delete a tag from the list
            case 4:
                lcd.printf("Delete Tag\n");
                lcd.printf("Scan RFID Tag");
                // Wait for a tag scan
                while(!ID) {
                    if(rfid.readable()) {
                        ID = rfid.read();
                    }
                    wait(1);
                }
                lcd.cls();
                // Determine if the tag exists in the list
                found = lookUp(IDList, ID);
                // If it does, delete it from the list
                if(found) {
                    deleteFromList(&IDList, ID);
                    lcd.printf("ID: %d\n", ID);
                    lcd.printf("Deleted");
                    ID = 0;
                    state = 0;
                    wait(3);
                    break;
                // Otherwise, tell the user it doesn't exist
                } else {
                    lcd.printf("ID: %d\n", ID);
                    lcd.printf("Not in List");
                    ID = 0;
                    state = 0;
                    wait(3);
                    break;
                }
        }
    }
}

// Initializes an empty Tag list
LinkedList emptyList()
{
    LinkedList it = {NULL, NULL};
    addPhantom(&it);
    return it;
}

// Print the list via USB Serial port to the PC
void showList(LinkedList theList)
{
    LLNode *phere = theList.head;   /* start at the head */
    int n = 1;
    pc.printf("***********\n");  //start the list
    if(phere->data == 0) pc.printf("No Tags in List\n"); //List is empty
    else pc.printf("%d: %d\n", n, phere->data); //List isn't empty, print first tag
    n++;
    while(phere->next) {
        pc.printf("%d: %d\n", n, phere->next->data); // keep printing while there is still data
        phere = phere->next;        /* work forward via next ptrs */
        n++;
    }
    pc.printf("***********\n\n"); // end the list
}

// Add a tag to the list (added to the back of the list)
void addToList(LinkedList *pLL, int data)
{
    // If the list has no data, its the head of the list
    if(pLL->head->data == NULL) {
        pLL->head->data = data;
    // otherwise you have to add a new node to the tail of the list
    } else {
        // Initialize the node
        LLNode *toAdd = (LLNode *) malloc(sizeof(LLNode));
        // Add the data and fix all of the pointers
        toAdd->data = data;
        toAdd->prev = pLL->tail;
        toAdd->next = NULL;
        pLL->tail->next = toAdd;
        pLL->tail = toAdd;
    }
}

// Used to create the first empty node in the initial list
void addPhantom(LinkedList *pLL)
{
    /* create LLNode */
    LLNode *phere = (LLNode *) malloc(sizeof(LLNode));
    phere->data = NULL;
    phere->next = NULL;
    phere->prev = NULL;
    pLL->head = phere;
    pLL->tail = phere;
}

// delete a tag from the list
void deleteFromList(LinkedList *pll, int data)
{
    // Flags used to delete
    int flag = true;
    int first = true;
    // Get first node
    LLNode *node = pll->head;
    while(flag) {
        // If you find the tag
        if(node->data == data) {
            // If its the first node, then you just have to move the head pointer
            if(first) {
                pll->head = pll->head->next;
                flag = false;
            // if its the last tag added... just adjust the tail pointers
            } else {
                if(node->next == NULL) {
                    pll->tail = node->prev;
                    node->prev->next = NULL;
                    flag = false;
                //otherwise just skip the node in the list
                } else {
                    node->prev->next = node->next;
                    node->next->prev = node->prev;
                    flag = false;
                }
            }
        }
        // Traverse through the list via next pointers
        node = node->next;
        first = false;
    }
}

// Used to determine if a tag already exists in the list
int lookUp(LinkedList IDList, int ID)
{
    int found = false;
    LLNode *node = IDList.head;
    // While the data isn't null, check it
    while(node->data != NULL) {
        if(node->data == ID) {
            found = true;
            return found;
        }
        // if the next pointer is null, we're at the end of the list;
        if(node->next == NULL) return found;
        node = node->next;
    }
    return found;
}