Maxim Integrated / Mbed 2 deprecated MAXREFDES132_OneWire_Demo

Dependencies:   OneWire Terminal mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**********************************************************************
00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 **********************************************************************/
00032 
00033 
00034 #include "mbed.h"
00035 #include "OneWire.h"
00036 #include "Terminal.h"
00037 
00038 using namespace OneWire;
00039 using namespace RomCommands;
00040 
00041 void print_rom_id(Terminal & term, const RomId & romId);
00042 void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector);
00043 
00044 int main(void)
00045 {
00046     Terminal term(USBTX, USBRX);
00047     char user_entry = 'y';
00048         
00049     I2C i2cBus(D14, D15);
00050     i2cBus.frequency(400000);
00051     DS2484 owm(i2cBus);
00052     term.printf("\nStarting I2C to 1-wire Demo\n");
00053     
00054     OneWireMaster::CmdResult result = owm.OWInitMaster();
00055     
00056     if(result == OneWireMaster::Success)
00057     {
00058         //Rom Iterator for slave devices.  
00059         //Encapsulates owm, ROM cntl fxs, and RomId of device into one object 
00060         //that slave member fxs use to implement ROM cntl fxs so that you don't have too.
00061         //You just use the features of the slave device
00062         MultidropRomIterator selector(owm);
00063         
00064         //Search state var for main loop
00065         SearchState searchState;
00066         
00067         while(user_entry != 'n')
00068         {
00069             result = owm.OWReset();
00070             if(result == OneWireMaster::Success)
00071             {
00072                 term.printf("\nOWReset success, starting search\n");
00073                 
00074                 result = OWFirst(owm, searchState);
00075                 if(result == OneWireMaster::Success)
00076                 {
00077                     do
00078                     {
00079                         //print current devices rom id
00080                         print_rom_id(term, searchState.romId);
00081                         
00082                         
00083                         //This could be a switch statement based on familyCodes 
00084                         //if your app has more than one device on the bus
00085                         if(searchState.romId.familyCode() == 0x10)
00086                         {
00087                             DS1920Menu(term, searchState.romId, selector);
00088                         }
00089                         
00090                         //find the next device if any
00091                         result = OWNext(owm, searchState);
00092                     }
00093                     while(result == OneWireMaster::Success);
00094                 }
00095                 else
00096                 {
00097                     term.printf("\nSearch failed\n");
00098                     term.printf("\nError code = %d\n", result);
00099                 }
00100             }
00101             else 
00102             {
00103                 term.printf("\nFailed to find any 1-wire devices on bus\n");
00104                 term.printf("\nError code = %d\n", result);
00105             }
00106             
00107             user_entry = term.get_char("\nEnter 'y' to continue, or 'n' to quit: ", 'n', 'y');
00108             term.cls();
00109             term.home();
00110         }
00111     }
00112     else
00113     {
00114         term.printf("\nFailed to initialize the 1-wire Master\n");
00115         term.printf("\nError code = %d\n", result);
00116     }
00117     
00118     term.printf("\nEnding Program\n");
00119     
00120     return 0;
00121 }
00122 //***** end main ******************************************************
00123 
00124 
00125 //*********************************************************************
00126 void print_rom_id(Terminal & term, const RomId & romId)
00127 {
00128     //print the rom number
00129     term.printf("\r\n");
00130     for(int idx = 7; idx >= 0; idx--) {
00131         term.printf("0x%02x ", romId.buffer[idx]);
00132     }
00133     term.printf("\r\n");
00134 }
00135 
00136 
00137 //*********************************************************************
00138 void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector)
00139 {
00140     char userEntry = '0';
00141     uint8_t th, tl, idx;
00142     uint8_t scratchPadBuff[8];
00143     float temperature;
00144     
00145     DS1920 tempIbutton(selector);
00146     DS1920::CmdResult result = DS1920::OpFailure;
00147     tempIbutton.setRomId(romId);
00148     
00149     while(userEntry != '7')
00150     {
00151         term.printf("\nDS1920 Menu Options\n");
00152         term.printf("\n%t1. Write ScratchPad");
00153         term.printf("\n%t2. Read Scratchpad");
00154         term.printf("\n%t3. Copy Scratchpad");
00155         term.printf("\n%t4. Convert Temperature");
00156         term.printf("\n%t5. Recall EEPROM");
00157         term.printf("\n%t6. Clear Screen");
00158         term.printf("\n%t7. Quit");
00159         
00160         userEntry = term.get_char("\nPlease select an option above: ", '1', '7');
00161         
00162         switch(userEntry)
00163         {
00164             case '1':
00165                 th = term.get_int32("\nPlease enter upper temperature threshold, integer values only: ", 0, 100);
00166                 tl = term.get_int32("\nPlease enter lower temperature threshold, integer values only: ", -55, 0);
00167                 
00168                 result = tempIbutton.writeScratchPad(th, tl);
00169                 if(result == DS1920::Success)
00170                 {
00171                     term.printf("\nWrite Scratchpad Success\n");
00172                 }
00173                 else
00174                 {
00175                     term.printf("\nWrite Scratchpad Fail\n");
00176                 }
00177                 
00178             break;
00179             
00180             case '2':
00181             
00182                 result = tempIbutton.readScratchPad(scratchPadBuff);
00183                 if(result == DS1920::Success)
00184                 {
00185                     term.printf("\nRead Scratchpad Success\n");
00186                     term.printf("\nScratchpad = ");
00187                     for(idx = 0; idx < 8; idx++)
00188                     {
00189                         if(scratchPadBuff[idx] < 16)
00190                         {
00191                             term.printf("0x0%x ", scratchPadBuff[idx]);
00192                         }
00193                         else
00194                         {
00195                             term.printf("0x%2x ", scratchPadBuff[idx]);
00196                         }
00197                     }
00198                     term.printf("\n");
00199                     
00200                 }
00201                 else
00202                 {
00203                     term.printf("\nRead Scratchpad Fail\n");
00204                 }
00205                 
00206             break;
00207             
00208             case '3':
00209                 
00210                 result = tempIbutton.copyScratchPad();
00211                 if(result == DS1920::Success)
00212                 {
00213                     term.printf("\nCopy Scratchpad Success\n");
00214                 }
00215                 else
00216                 {
00217                     term.printf("\nCopy Scratchpad Fail\n");
00218                 }
00219                 
00220             break;
00221             
00222             case '4':
00223                 
00224                 result = tempIbutton.convertTemperature(temperature);
00225                 if(result == DS1920::Success)
00226                 {
00227                     term.printf("\nConvert Temperature Success\n");
00228                     term.printf("\nTemperature = %.1f", temperature);
00229                 }
00230                 else
00231                 {
00232                     term.printf("\nConvert Temperature Fail\n");
00233                 }
00234                 
00235             break;
00236             
00237             case '5':
00238                 
00239                 result = tempIbutton.recallEEPROM();
00240                 if(result == DS1920::Success)
00241                 {
00242                     term.printf("\nRecall EEPROM Success\n");
00243                 }
00244                 else
00245                 {
00246                     term.printf("\nRecall EEPROM Fail\n");
00247                 }
00248                 
00249             break;
00250             
00251             case '6':
00252                 term.cls();
00253                 term.home();
00254             break;
00255             
00256             case '7':
00257                 term.printf("\nLeaving DS1920 Menu Options\n");
00258             break;
00259             
00260             default:
00261                 mbed_die();
00262             break;
00263         }
00264     }
00265 }