Demo program for MAXREFDES132. Searches 1-Wire® bus and prints ROM IDs of devices found. If the DS1920 is found presents user with menu for interfacing with the DS1920 temperature iButton

Dependencies:   OneWire Terminal mbed

main.cpp

Committer:
andrewae
Date:
2017-07-10
Revision:
12:a649f4e955a4
Parent:
11:11a01dc7a2c9

File content as of revision 12:a649f4e955a4:

/**********************************************************************
* Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
*
* 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 MAXIM INTEGRATED 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.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
**********************************************************************/


#include "mbed.h"
#include "OneWire.h"
#include "Terminal.h"

using namespace OneWire;
using namespace RomCommands;

void print_rom_id(Terminal & term, const RomId & romId);
void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector);

int main(void)
{
    Terminal term(USBTX, USBRX);
    char user_entry = 'y';
        
    I2C i2cBus(D14, D15);
    i2cBus.frequency(400000);
    DS2484 owm(i2cBus);
    term.printf("\nStarting I2C to 1-wire Demo\n");
    
    OneWireMaster::CmdResult result = owm.OWInitMaster();
    
    if(result == OneWireMaster::Success)
    {
        //Rom Iterator for slave devices.  
        //Encapsulates owm, ROM cntl fxs, and RomId of device into one object 
        //that slave member fxs use to implement ROM cntl fxs so that you don't have too.
        //You just use the features of the slave device
        MultidropRomIterator selector(owm);
        
        //Search state var for main loop
        SearchState searchState;
        
        while(user_entry != 'n')
        {
            result = owm.OWReset();
            if(result == OneWireMaster::Success)
            {
                term.printf("\nOWReset success, starting search\n");
                
                result = OWFirst(owm, searchState);
                if(result == OneWireMaster::Success)
                {
                    do
                    {
                        //print current devices rom id
                        print_rom_id(term, searchState.romId);
                        
                        
                        //This could be a switch statement based on familyCodes 
                        //if your app has more than one device on the bus
                        if(searchState.romId.familyCode() == 0x10)
                        {
                            DS1920Menu(term, searchState.romId, selector);
                        }
                        
                        //find the next device if any
                        result = OWNext(owm, searchState);
                    }
                    while(result == OneWireMaster::Success);
                }
                else
                {
                    term.printf("\nSearch failed\n");
                    term.printf("\nError code = %d\n", result);
                }
            }
            else 
            {
                term.printf("\nFailed to find any 1-wire devices on bus\n");
                term.printf("\nError code = %d\n", result);
            }
            
            user_entry = term.get_char("\nEnter 'y' to continue, or 'n' to quit: ", 'n', 'y');
            term.cls();
            term.home();
        }
    }
    else
    {
        term.printf("\nFailed to initialize the 1-wire Master\n");
        term.printf("\nError code = %d\n", result);
    }
    
    term.printf("\nEnding Program\n");
    
    return 0;
}
//***** end main ******************************************************


//*********************************************************************
void print_rom_id(Terminal & term, const RomId & romId)
{
    //print the rom number
    term.printf("\r\n");
    for(int idx = 7; idx >= 0; idx--) {
        term.printf("0x%02x ", romId.buffer[idx]);
    }
    term.printf("\r\n");
}


//*********************************************************************
void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector)
{
    char userEntry = '0';
    uint8_t th, tl, idx;
    uint8_t scratchPadBuff[8];
    float temperature;
    
    DS1920 tempIbutton(selector);
    DS1920::CmdResult result = DS1920::OpFailure;
    tempIbutton.setRomId(romId);
    
    while(userEntry != '7')
    {
        term.printf("\nDS1920 Menu Options\n");
        term.printf("\n%t1. Write ScratchPad");
        term.printf("\n%t2. Read Scratchpad");
        term.printf("\n%t3. Copy Scratchpad");
        term.printf("\n%t4. Convert Temperature");
        term.printf("\n%t5. Recall EEPROM");
        term.printf("\n%t6. Clear Screen");
        term.printf("\n%t7. Quit");
        
        userEntry = term.get_char("\nPlease select an option above: ", '1', '7');
        
        switch(userEntry)
        {
            case '1':
                th = term.get_int32("\nPlease enter upper temperature threshold, integer values only: ", 0, 100);
                tl = term.get_int32("\nPlease enter lower temperature threshold, integer values only: ", -55, 0);
                
                result = tempIbutton.writeScratchPad(th, tl);
                if(result == DS1920::Success)
                {
                    term.printf("\nWrite Scratchpad Success\n");
                }
                else
                {
                    term.printf("\nWrite Scratchpad Fail\n");
                }
                
            break;
            
            case '2':
            
                result = tempIbutton.readScratchPad(scratchPadBuff);
                if(result == DS1920::Success)
                {
                    term.printf("\nRead Scratchpad Success\n");
                    term.printf("\nScratchpad = ");
                    for(idx = 0; idx < 8; idx++)
                    {
                        if(scratchPadBuff[idx] < 16)
                        {
                            term.printf("0x0%x ", scratchPadBuff[idx]);
                        }
                        else
                        {
                            term.printf("0x%2x ", scratchPadBuff[idx]);
                        }
                    }
                    term.printf("\n");
                    
                }
                else
                {
                    term.printf("\nRead Scratchpad Fail\n");
                }
                
            break;
            
            case '3':
                
                result = tempIbutton.copyScratchPad();
                if(result == DS1920::Success)
                {
                    term.printf("\nCopy Scratchpad Success\n");
                }
                else
                {
                    term.printf("\nCopy Scratchpad Fail\n");
                }
                
            break;
            
            case '4':
                
                result = tempIbutton.convertTemperature(temperature);
                if(result == DS1920::Success)
                {
                    term.printf("\nConvert Temperature Success\n");
                    term.printf("\nTemperature = %.1f", temperature);
                }
                else
                {
                    term.printf("\nConvert Temperature Fail\n");
                }
                
            break;
            
            case '5':
                
                result = tempIbutton.recallEEPROM();
                if(result == DS1920::Success)
                {
                    term.printf("\nRecall EEPROM Success\n");
                }
                else
                {
                    term.printf("\nRecall EEPROM Fail\n");
                }
                
            break;
            
            case '6':
                term.cls();
                term.home();
            break;
            
            case '7':
                term.printf("\nLeaving DS1920 Menu Options\n");
            break;
            
            default:
                mbed_die();
            break;
        }
    }
}