Innomatix Support / InnomatixSupport
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example.cpp Source File

example.cpp

00001 /*******************************************************************
00002  *
00003  *  File: main.cpp
00004  *
00005  *  Description: Example of using Innomatix Coprocessor Support functionality
00006  *
00007  *  Copyright 2015 Innomatix, LLC., All Rights Reserved
00008  *
00009  *  THIS DOCUMENT AND ITS CONTENTS ARE INTELLECTUAL PROPERTY
00010  *  OF INNOMATIX, LLC.  ANY DUPLICATION IN PART OR WHOLE
00011  *  WITHOUT PRIOR WRITTEN CONSENT IS STRICTLY PROHIBITED.
00012  *
00013  *******************************************************************/
00014 #include "mbed.h"
00015 #include "Timer.h"
00016 #include "RemoteDataStoreAPI.h"
00017 
00018 // Name of the data bins we'll use
00019 char PCopVersBinName[] = "CoprocVersion";
00020 char PCopStatusBinName[] = "CoprocStatus";
00021 char VehiclePowerBinName[] = "VehiclePower";
00022 char PowerStatusBinName[] = "PowerStatus";
00023 
00024 // Data bin identifiers returned from GetDataBinInfo()
00025 unsigned int  VersBin = BinId_None;
00026 unsigned int  StatusBin = BinId_None;
00027 unsigned int  PowerValueBin = BinId_None;
00028 unsigned int  PowerStatusBin = BinId_None;
00029 
00030 
00031 /*************************************************************************/
00032 unsigned int GetDBinInfo( const char * name )
00033 {
00034     unsigned int id = BinId_None;
00035     RdsResults_e result = rdsNoConnection;
00036     BinInfoStruct_t info;
00037 
00038     printf( "RemoteDataStore getting info for databin %s...", name );
00039     fflush( NULL );
00040 
00041 
00042     result = GetDataBinInfo( name, &info );
00043     if( rdsSuccess == result )
00044     {
00045         // We only care about the databin ID for this example
00046         // Check RemoteDataStoreAPI.h for info on other fields
00047         id = info.BinId;
00048         printf( "SUCCESS, ID is %d\r\n", id );
00049     }else
00050     {
00051         printf( "FAILED (%d)\r\n", result );
00052     }
00053     return( id );
00054 }
00055 
00056 /*************************************************************************/
00057 RdsResults_e ExampleInit()
00058 {
00059     RdsResults_e result = rdsNoConnection;
00060 
00061     result = DataStoreInit( "usb", 0 );
00062     printf( "Innomatix RemoteDataStore API initialization: %s\r\n", (rdsSuccess == result ? "INITIALIZED" : "FAILED") );
00063 
00064     if( rdsSuccess == result )
00065     {
00066         VersBin = GetDBinInfo( PCopVersBinName );
00067         StatusBin = GetDBinInfo( PCopStatusBinName );
00068 
00069         PowerValueBin = GetDBinInfo( VehiclePowerBinName );
00070         PowerStatusBin = GetDBinInfo( PowerStatusBinName );
00071     }
00072     return( result );
00073 }
00074 
00075 /*************************************************************************/
00076 /*************************************************************************/
00077 int DoExample( char *zVersion )
00078 {
00079     RdsResults_e result = rdsNoConnection;
00080     int delay_msec = 10 * 1000;
00081 
00082     DigitalOut led1( LED1 );
00083 
00084     double PowerValue = 0.0;
00085     double PowerThreshold = 13.0;
00086     unsigned int Timestamp = 0;
00087     unsigned int PrevTimestamp = 0;
00088 
00089     // Init the Support Library subsystems we'll use for this example
00090     result = ExampleInit();
00091     if( rdsSuccess == result )
00092     {
00093         /*-----------------------------------------------*/
00094         // Update the DAP informational data bins
00095         PutString( StatusBin, "Starting PCop Example" );
00096         PutString( VersBin, zVersion );
00097 
00098         /*-----------------------------------------------*/
00099         // Use the Remote DataStore's special "debug" API
00100         // The DAP will update a pre-determined data bin with the string.
00101         // Typically the debug data bin is shown on the diagnostic display
00102         // and is included in the data stream to the host.
00103         PutDebug( "Entering example loop" );
00104 
00105         while( 1 )
00106         {
00107             // Get a value from the VehiclePower data bin
00108             result = GetDouble( PowerValueBin, &PowerValue, &Timestamp);
00109 
00110             // Check the timestamp to see if the value has been updated since
00111             // last time we checked.  Only test/publish if it has.
00112             if( Timestamp != PrevTimestamp )
00113             {
00114                 // Apply a threshold
00115                 int status = (PowerValue >= PowerThreshold ? 1 : 0);
00116 
00117                 // Publish the results back to the PowerOkay data bin
00118                 PutUnsignedInteger( PowerStatusBin, status );
00119                 led1 = status;
00120 
00121                 // Save the timestamp so we know if it is updated
00122                 PrevTimestamp = Timestamp;
00123             }
00124 
00125             // Wait for the next go-around
00126             wait_ms( delay_msec );
00127         }
00128 
00129 
00130 
00131         /*-----------------------------------------------*/
00132         DataStoreClose();
00133     }
00134     return( 0 );
00135 }
00136