Dreamforce 2014 Workshop RFID Case Generator - FRDM-K64F

Dependencies:   EndpointMain-rfid EthernetInterface StatusReporter BufferedSerial mbed-rtos mbed Logger C12832

Fork of df-2014-workshop-rfid-case-generator-ublox by Doug Anson

main.cpp

Committer:
ansond
Date:
2014-08-27
Revision:
8:fefcecb66463
Parent:
7:5d6e03557246
Child:
13:2561e3855a81

File content as of revision 8:fefcecb66463:

/* Copyright C2014 ARM, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files the "Software", to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
 // our definitions
 #include "Definitions.h"

 // RFID Reader - on P14 pin  
 #if ID12_ENABLE
    #include "ID12RFID.h"
    ID12RFID rfid_reader(p13,p14);
 #endif 

 // Salesforce Case Generator
 #include "SalesForceCaseGenerator.h"
 
 // our Logger
 #include "Logger.h"
 
 // our WidgetDB
 #include "WidgetDB.h"
 
 // our LCD Panel
 #include "C12832_lcd.h"
 C12832_LCD lcd;
 
 // our Serial port
 #include "Serial.h"
 Serial pc(USBTX, USBRX);
 
 // Ethernet
 #include "EthernetInterface.h"
 EthernetInterface ethernet;
 
 // HARD RESET
extern "C" void HardFault_Handler() { NVIC_SystemReset(); }

 // Main Entry...
 int main() {
    // create our object instances
    Logger logger(&pc,&lcd);
    
    // announce
    logger.log("DF 2014 RFID Reader\r\nVersion: %s",APP_VERSION);
    
    // initialize Ethernet
    logger.log("Initializing Ethernet...");
    ethernet.init();
    
    // get a DHCP address and bring the network interface up
    logger.log("Getting IP Address...");
    if (ethernet.connect() == 0) {
        // log our IP address (DHCP)
        logger.log("IP Address: %s",ethernet.getIPAddress());
        
        // create the WidgetDB
        WidgetDB db;
        
        // create the Salesforce Case Generator
        SalesForceCaseGenerator case_generator(&logger);
        
        // entering main loop
        logger.log("Entering Main Loop...\r\nScanning...");
        
        // Enter the main loop
        while(true) {
#if ID12_ENABLE
            // look for a readable RFID tag
            //logger.log("RFID: Scanning...");
            if(rfid_reader.readable()) {
                // capture the RFID id...
                logger.log("RFID: Found RFID.\r\nReading...");
                int rfid = rfid_reader.read();
                logger.log("RFID: ID %d found...\r\nProcessing...",rfid);
                
                // look it up in our WidgetDB... proceed only if we find something we know about...
                char *name = db.lookupWidgetName(rfid);
                if (name != NULL) {
                    // build out a simple subject for the case
                    char subject[MAX_NAME_LENGTH+1];
                    memset(subject,0,MAX_NAME_LENGTH+1);
                    sprintf(subject,"%s case update",name);
                    
                    // create and dispatch a case
                    bool success = case_generator.createCase(subject,db.lookupWidgetDescription(rfid));
                    if (success == true) {
                        logger.log("Case Generated!\r\nScanning...");
                    }
                    else {
                        logger.log("Case Generation FAILED\r\nScanning...");
                    }
                }
                else {
                    // unrecognized RFID
                    logger.log("RFID unk-ignored.\r\nScanning...");
                }
             }
#else 
             // simply create and dispatch a single case
             logger.log("Creating Case...");
             bool success = case_generator.createCase("sample mbed case","this is a sample mbed case");
             if (success == true) {
                 logger.log("Case Generated!");
                 logger.log("Exiting...");
                 exit(1);
             }
             else {
                 logger.log("Case Generation FAILED");
                 logger.log("Exiting...");
                 exit(1);
             }
#endif
          
             // wait a bit and look again
             wait_ms(WAIT_TIME_MS);
          }
     }
     else {
         logger.log("No Network... Exiting...");
         exit(1);
     }
     
     // disconnect
     logger.log("Disconnecting...");
     ethernet.disconnect();
     
     // Exit
     logger.log("Exiting...");
     exit(1);
  }