Justin Jordan / Mbed 2 deprecated MAXREFDES130_131_Demo

Dependencies:   MAX11300 MAX4822 OWGridEye OneWire ds3231 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /******************************************************************************
00002 * MIT License
00003 *
00004 * Copyright (c) 2017 Justin J. Jordan
00005 *
00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
00007 * of this software and associated documentation files (the "Software"), to deal
00008 * in the Software without restriction, including without limitation the rights
00009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010 * copies of the Software, and to permit persons to whom the Software is
00011 * furnished to do so, subject to the following conditions:
00012 
00013 * The above copyright notice and this permission notice shall be included in all
00014 * copies or substantial portions of the Software.
00015 
00016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00022 * SOFTWARE.
00023 ******************************************************************************/
00024 
00025 #include "mbed.h"
00026 #include "OneWire.h"
00027 #include "OWGridEye.h"
00028 #include "MAX4822.h"
00029 #include "ds3231.h"
00030 
00031 using namespace OneWire;
00032 using namespace RomCommands;
00033 
00034 volatile uint8_t seconds = 0;
00035 void incrementSeconds()
00036 {
00037     seconds++;
00038 }
00039 
00040 
00041 
00042 int main()
00043 {
00044     //Get I2C bus instance
00045     I2C i2c(D14, D15);
00046     i2c.frequency(400000);
00047     
00048     //Get SPI bus instance
00049     SPI spi_bus(D11, D12, D13);
00050     //Get MAX4822 instance
00051     MAX4822 rly_drvr(spi_bus, D10);
00052     //Set/Reset all pins
00053     DigitalOut rly_drvr_set(D7, 1);
00054     DigitalOut rly_drvr_reset(D6, 1);
00055     
00056     rly_drvr.reset_all_relays(rly_drvr_reset);
00057     
00058     //Get 1-Wire Master instance, OWM
00059     DS2484 owm(i2c);
00060     OneWireMaster::CmdResult owm_result = owm.OWInitMaster();
00061     while(owm_result != OneWireMaster::Success)
00062     {
00063         printf("Failed to init OWM...\r\n");
00064         owm_result = owm.OWInitMaster();
00065     }
00066     printf("OWM Initialized!!!\r\n");
00067     
00068     //Struct for encapsulating search algorithm vars/state
00069     SearchState searchState;
00070     
00071     //Encapsulate the OWM in a Bus Iterator
00072     MultidropRomIterator selector(owm);
00073     //Give the iterator to the sensor.
00074     //Now we can work directly with the sensor instead of through the OWM
00075     OWGridEye sensor(selector);
00076     
00077     //Search bus, expecting three devices; 
00078     //DS2431 on RD130, DS2413 and DS28E17 on RD131
00079     owm_result = OWFirst(owm, searchState);
00080     if(owm_result == OneWireMaster::Success)
00081     {
00082         do
00083         {
00084             if(searchState.romId.familyCode() == OWGridEye::DS2413_FAMILY_CODE)
00085             {
00086                 sensor.setOWSwitchRomId(searchState.romId);
00087                 sensor.connectGridEye();
00088             }
00089             
00090             if(searchState.romId.familyCode() == OWGridEye::DS28E17_FAMILY_CODE)
00091             {
00092                 sensor.setI2CBridgeRomId(searchState.romId);
00093                 sensor.disconnectGridEye();
00094             }
00095             
00096             owm_result = OWNext(owm, searchState);
00097         }
00098         while(owm_result == OneWireMaster::Success);
00099     }
00100     else
00101     {
00102         printf("Failed to find any 1-Wire devices...\r\n");
00103     }
00104     
00105     const uint8_t NUM_PIXELS = 64;
00106     const float ON_TIME = (1.0F * 60.0F);  //Replace first literal with number of minutes
00107     
00108     int16_t dataBuffer[NUM_PIXELS];
00109     uint8_t idx(0), referenceCount(0);
00110     float sumTemp, avgTemp, referenceTemp;
00111     
00112     Ticker secondTimer;
00113     secondTimer.attach(&incrementSeconds, 1);
00114     
00115     do
00116     {
00117         if(seconds == 1)
00118         {
00119             seconds = 0;
00120             sensor.connectGridEye();
00121             
00122             wait(0.1); //Let sensor power up
00123             sensor.gridEyeGetFrameTemperature(dataBuffer);
00124             sensor.disconnectGridEye();
00125             
00126             sumTemp = 0.0F;
00127             for(idx = 0; idx < NUM_PIXELS; idx++)
00128             {
00129                 sumTemp += fAMG_PUB_CMN_ConvStoF(dataBuffer[idx]);
00130             }
00131             avgTemp = (sumTemp / (1.0F * NUM_PIXELS));
00132             printf("Average Temperature = %4.1f\r\n", avgTemp);
00133             
00134             referenceTemp += avgTemp;
00135             referenceCount++;
00136         }
00137     }
00138     while(referenceCount < 15);
00139     referenceTemp = ((referenceTemp/15.0F) + 1.0F);
00140     printf("Reference Temperature = %4.1f\r\n", referenceTemp);
00141     
00142     Timer onTimer;
00143     onTimer.start();
00144     
00145     while(1)
00146     {
00147         if(seconds == 1)
00148         {
00149             seconds = 0;
00150             sensor.connectGridEye();
00151             
00152             wait(0.1); //Let sensor power up
00153             sensor.gridEyeGetFrameTemperature(dataBuffer);
00154             sensor.disconnectGridEye();
00155             
00156             sumTemp = 0.0F;
00157             for(idx = 0; idx < NUM_PIXELS; idx++)
00158             {
00159                 sumTemp += fAMG_PUB_CMN_ConvStoF(dataBuffer[idx]);
00160             }
00161             avgTemp = (sumTemp / (1.0F * NUM_PIXELS));
00162             printf("Average Temperature = %4.1f\r\n", avgTemp);
00163             printf("Seconds since last reset = %0.1f\r\n\r\n", onTimer.read());
00164             
00165             //If average temperature is greater than reference, turn on relays.
00166             //Assumes object detected
00167             if(avgTemp > referenceTemp)
00168             {
00169                 onTimer.reset();
00170                 rly_drvr.set_all_relays(rly_drvr_set);
00171                 
00172             }
00173         }
00174         
00175         //If average is less than reference for ON_TIME, shut relays off
00176         if(onTimer.read() > ON_TIME)
00177         {
00178             rly_drvr.reset_all_relays(rly_drvr_reset);
00179         }
00180     }
00181 }