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@8:7af592c694b2, 2016-09-26 (annotated)
- Committer:
- j3
- Date:
- Mon Sep 26 21:39:50 2016 +0000
- Revision:
- 8:7af592c694b2
- Parent:
- 7:81d4e19b7b50
- Child:
- 10:a43e5b99d61f
Updated ow library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 0:11c9d61c8876 | 1 | /********************************************************************** |
j3 | 0:11c9d61c8876 | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 0:11c9d61c8876 | 3 | * |
j3 | 0:11c9d61c8876 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 0:11c9d61c8876 | 5 | * copy of this software and associated documentation files (the "Software"), |
j3 | 0:11c9d61c8876 | 6 | * to deal in the Software without restriction, including without limitation |
j3 | 0:11c9d61c8876 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 0:11c9d61c8876 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 0:11c9d61c8876 | 9 | * Software is furnished to do so, subject to the following conditions: |
j3 | 0:11c9d61c8876 | 10 | * |
j3 | 0:11c9d61c8876 | 11 | * The above copyright notice and this permission notice shall be included |
j3 | 0:11c9d61c8876 | 12 | * in all copies or substantial portions of the Software. |
j3 | 0:11c9d61c8876 | 13 | * |
j3 | 0:11c9d61c8876 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 0:11c9d61c8876 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 0:11c9d61c8876 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 0:11c9d61c8876 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 0:11c9d61c8876 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 0:11c9d61c8876 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 0:11c9d61c8876 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 0:11c9d61c8876 | 21 | * |
j3 | 0:11c9d61c8876 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 0:11c9d61c8876 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 0:11c9d61c8876 | 24 | * Products, Inc. Branding Policy. |
j3 | 0:11c9d61c8876 | 25 | * |
j3 | 0:11c9d61c8876 | 26 | * The mere transfer of this software does not imply any licenses |
j3 | 0:11c9d61c8876 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 0:11c9d61c8876 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 0:11c9d61c8876 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 0:11c9d61c8876 | 30 | * ownership rights. |
j3 | 0:11c9d61c8876 | 31 | **********************************************************************/ |
j3 | 0:11c9d61c8876 | 32 | |
j3 | 0:11c9d61c8876 | 33 | |
j3 | 0:11c9d61c8876 | 34 | #include "mbed.h" |
j3 | 4:9f799f2e157a | 35 | #include "OneWire.h" |
j3 | 0:11c9d61c8876 | 36 | #include "Terminal.h" |
j3 | 0:11c9d61c8876 | 37 | |
j3 | 0:11c9d61c8876 | 38 | using namespace OneWire; |
j3 | 1:c9d52433407f | 39 | using namespace RomCommands; |
j3 | 0:11c9d61c8876 | 40 | |
j3 | 0:11c9d61c8876 | 41 | void print_rom_id(Terminal & term, const RomId & romId); |
j3 | 0:11c9d61c8876 | 42 | void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector); |
j3 | 0:11c9d61c8876 | 43 | |
j3 | 0:11c9d61c8876 | 44 | int main(void) |
j3 | 0:11c9d61c8876 | 45 | { |
j3 | 0:11c9d61c8876 | 46 | Terminal term(USBTX, USBRX); |
j3 | 0:11c9d61c8876 | 47 | char user_entry = 'y'; |
j3 | 0:11c9d61c8876 | 48 | |
j3 | 8:7af592c694b2 | 49 | I2C i2cBus(D14, D15); |
j3 | 8:7af592c694b2 | 50 | i2cBus.frequency(400000); |
j3 | 8:7af592c694b2 | 51 | DS2484 owm(i2cBus); |
j3 | 8:7af592c694b2 | 52 | term.printf("\nStarting I2C to 1-wire Demo\n"); |
j3 | 0:11c9d61c8876 | 53 | |
j3 | 0:11c9d61c8876 | 54 | OneWireMaster::CmdResult result = owm.OWInitMaster(); |
j3 | 0:11c9d61c8876 | 55 | |
j3 | 0:11c9d61c8876 | 56 | if(result == OneWireMaster::Success) |
j3 | 0:11c9d61c8876 | 57 | { |
j3 | 0:11c9d61c8876 | 58 | //Rom Iterator for slave devices. |
j3 | 0:11c9d61c8876 | 59 | //Encapsulates owm, ROM cntl fxs, and RomId of device into one object |
j3 | 0:11c9d61c8876 | 60 | //that slave member fxs use to implement ROM cntl fxs so that you don't have too. |
j3 | 0:11c9d61c8876 | 61 | //You just use the features of the slave device |
j3 | 0:11c9d61c8876 | 62 | MultidropRomIterator selector(owm); |
j3 | 0:11c9d61c8876 | 63 | |
j3 | 0:11c9d61c8876 | 64 | //Search state var for main loop |
j3 | 1:c9d52433407f | 65 | SearchState searchState; |
j3 | 0:11c9d61c8876 | 66 | |
j3 | 0:11c9d61c8876 | 67 | while(user_entry != 'n') |
j3 | 0:11c9d61c8876 | 68 | { |
j3 | 0:11c9d61c8876 | 69 | result = owm.OWReset(); |
j3 | 0:11c9d61c8876 | 70 | if(result == OneWireMaster::Success) |
j3 | 0:11c9d61c8876 | 71 | { |
j3 | 0:11c9d61c8876 | 72 | term.printf("\nOWReset success, starting search\n"); |
j3 | 0:11c9d61c8876 | 73 | |
j3 | 1:c9d52433407f | 74 | result = OWFirst(owm, searchState); |
j3 | 0:11c9d61c8876 | 75 | if(result == OneWireMaster::Success) |
j3 | 0:11c9d61c8876 | 76 | { |
j3 | 0:11c9d61c8876 | 77 | do |
j3 | 0:11c9d61c8876 | 78 | { |
j3 | 0:11c9d61c8876 | 79 | //print current devices rom id |
j3 | 0:11c9d61c8876 | 80 | print_rom_id(term, searchState.romId); |
j3 | 0:11c9d61c8876 | 81 | |
j3 | 0:11c9d61c8876 | 82 | |
j3 | 0:11c9d61c8876 | 83 | //This could be a switch statement based on familyCodes |
j3 | 0:11c9d61c8876 | 84 | //if your app has more than one device on the bus |
j3 | 0:11c9d61c8876 | 85 | if(searchState.romId.familyCode() == 0x10) |
j3 | 0:11c9d61c8876 | 86 | { |
j3 | 0:11c9d61c8876 | 87 | DS1920Menu(term, searchState.romId, selector); |
j3 | 0:11c9d61c8876 | 88 | } |
j3 | 0:11c9d61c8876 | 89 | |
j3 | 0:11c9d61c8876 | 90 | //find the next device if any |
j3 | 1:c9d52433407f | 91 | result = OWNext(owm, searchState); |
j3 | 0:11c9d61c8876 | 92 | } |
j3 | 0:11c9d61c8876 | 93 | while(result == OneWireMaster::Success); |
j3 | 0:11c9d61c8876 | 94 | } |
j3 | 0:11c9d61c8876 | 95 | else |
j3 | 0:11c9d61c8876 | 96 | { |
j3 | 0:11c9d61c8876 | 97 | term.printf("\nSearch failed\n"); |
j3 | 0:11c9d61c8876 | 98 | term.printf("\nError code = %d\n", result); |
j3 | 0:11c9d61c8876 | 99 | } |
j3 | 0:11c9d61c8876 | 100 | } |
j3 | 0:11c9d61c8876 | 101 | else |
j3 | 0:11c9d61c8876 | 102 | { |
j3 | 0:11c9d61c8876 | 103 | term.printf("\nFailed to find any 1-wire devices on bus\n"); |
j3 | 0:11c9d61c8876 | 104 | term.printf("\nError code = %d\n", result); |
j3 | 0:11c9d61c8876 | 105 | } |
j3 | 0:11c9d61c8876 | 106 | |
j3 | 0:11c9d61c8876 | 107 | user_entry = term.get_char("\nEnter 'y' to continue, or 'n' to quit: ", 'n', 'y'); |
j3 | 0:11c9d61c8876 | 108 | term.cls(); |
j3 | 0:11c9d61c8876 | 109 | term.home(); |
j3 | 0:11c9d61c8876 | 110 | } |
j3 | 0:11c9d61c8876 | 111 | } |
j3 | 0:11c9d61c8876 | 112 | else |
j3 | 0:11c9d61c8876 | 113 | { |
j3 | 0:11c9d61c8876 | 114 | term.printf("\nFailed to initialize the 1-wire Master\n"); |
j3 | 0:11c9d61c8876 | 115 | term.printf("\nError code = %d\n", result); |
j3 | 0:11c9d61c8876 | 116 | } |
j3 | 0:11c9d61c8876 | 117 | |
j3 | 0:11c9d61c8876 | 118 | term.printf("\nEnding Program\n"); |
j3 | 0:11c9d61c8876 | 119 | |
j3 | 0:11c9d61c8876 | 120 | return 0; |
j3 | 0:11c9d61c8876 | 121 | } |
j3 | 0:11c9d61c8876 | 122 | //***** end main ****************************************************** |
j3 | 0:11c9d61c8876 | 123 | |
j3 | 0:11c9d61c8876 | 124 | |
j3 | 0:11c9d61c8876 | 125 | //********************************************************************* |
j3 | 0:11c9d61c8876 | 126 | void print_rom_id(Terminal & term, const RomId & romId) |
j3 | 0:11c9d61c8876 | 127 | { |
j3 | 0:11c9d61c8876 | 128 | //print the rom number |
j3 | 0:11c9d61c8876 | 129 | term.printf("\n"); |
j3 | 0:11c9d61c8876 | 130 | int8_t idx = (RomId::byteLen - 1); |
j3 | 0:11c9d61c8876 | 131 | do |
j3 | 0:11c9d61c8876 | 132 | { |
j3 | 0:11c9d61c8876 | 133 | if(romId[idx] < 16) |
j3 | 0:11c9d61c8876 | 134 | { |
j3 | 0:11c9d61c8876 | 135 | term.printf("0x0%x ", romId[idx--]); |
j3 | 0:11c9d61c8876 | 136 | } |
j3 | 0:11c9d61c8876 | 137 | else |
j3 | 0:11c9d61c8876 | 138 | { |
j3 | 0:11c9d61c8876 | 139 | term.printf("0x%2x ", romId[idx--]); |
j3 | 0:11c9d61c8876 | 140 | } |
j3 | 0:11c9d61c8876 | 141 | } |
j3 | 0:11c9d61c8876 | 142 | while(idx >= 0); |
j3 | 0:11c9d61c8876 | 143 | term.printf("\n"); |
j3 | 0:11c9d61c8876 | 144 | } |
j3 | 0:11c9d61c8876 | 145 | |
j3 | 0:11c9d61c8876 | 146 | |
j3 | 0:11c9d61c8876 | 147 | //********************************************************************* |
j3 | 0:11c9d61c8876 | 148 | void DS1920Menu(Terminal & term, const RomId & romId, MultidropRomIterator & selector) |
j3 | 0:11c9d61c8876 | 149 | { |
j3 | 0:11c9d61c8876 | 150 | char userEntry = '0'; |
j3 | 0:11c9d61c8876 | 151 | uint8_t th, tl, idx; |
j3 | 0:11c9d61c8876 | 152 | uint8_t scratchPadBuff[8]; |
j3 | 0:11c9d61c8876 | 153 | float temperature; |
j3 | 0:11c9d61c8876 | 154 | |
j3 | 0:11c9d61c8876 | 155 | DS1920 tempIbutton(selector); |
j3 | 0:11c9d61c8876 | 156 | DS1920::CmdResult result = DS1920::OpFailure; |
j3 | 1:c9d52433407f | 157 | tempIbutton.setRomId(romId); |
j3 | 0:11c9d61c8876 | 158 | |
j3 | 0:11c9d61c8876 | 159 | while(userEntry != '7') |
j3 | 0:11c9d61c8876 | 160 | { |
j3 | 0:11c9d61c8876 | 161 | term.printf("\nDS1920 Menu Options\n"); |
j3 | 0:11c9d61c8876 | 162 | term.printf("\n%t1. Write ScratchPad"); |
j3 | 0:11c9d61c8876 | 163 | term.printf("\n%t2. Read Scratchpad"); |
j3 | 0:11c9d61c8876 | 164 | term.printf("\n%t3. Copy Scratchpad"); |
j3 | 0:11c9d61c8876 | 165 | term.printf("\n%t4. Convert Temperature"); |
j3 | 0:11c9d61c8876 | 166 | term.printf("\n%t5. Recall EEPROM"); |
j3 | 0:11c9d61c8876 | 167 | term.printf("\n%t6. Clear Screen"); |
j3 | 0:11c9d61c8876 | 168 | term.printf("\n%t7. Quit"); |
j3 | 0:11c9d61c8876 | 169 | |
j3 | 0:11c9d61c8876 | 170 | userEntry = term.get_char("\nPlease select an option above: ", '1', '7'); |
j3 | 0:11c9d61c8876 | 171 | |
j3 | 0:11c9d61c8876 | 172 | switch(userEntry) |
j3 | 0:11c9d61c8876 | 173 | { |
j3 | 0:11c9d61c8876 | 174 | case '1': |
j3 | 0:11c9d61c8876 | 175 | th = term.get_int32("\nPlease enter upper temperature threshold, integer values only: ", 0, 100); |
j3 | 0:11c9d61c8876 | 176 | tl = term.get_int32("\nPlease enter lower temperature threshold, integer values only: ", -55, 0); |
j3 | 0:11c9d61c8876 | 177 | |
j3 | 0:11c9d61c8876 | 178 | result = tempIbutton.writeScratchPad(th, tl); |
j3 | 0:11c9d61c8876 | 179 | if(result == DS1920::Success) |
j3 | 0:11c9d61c8876 | 180 | { |
j3 | 0:11c9d61c8876 | 181 | term.printf("\nWrite Scratchpad Success\n"); |
j3 | 0:11c9d61c8876 | 182 | } |
j3 | 0:11c9d61c8876 | 183 | else |
j3 | 0:11c9d61c8876 | 184 | { |
j3 | 0:11c9d61c8876 | 185 | term.printf("\nWrite Scratchpad Fail\n"); |
j3 | 0:11c9d61c8876 | 186 | } |
j3 | 0:11c9d61c8876 | 187 | |
j3 | 0:11c9d61c8876 | 188 | break; |
j3 | 0:11c9d61c8876 | 189 | |
j3 | 0:11c9d61c8876 | 190 | case '2': |
j3 | 0:11c9d61c8876 | 191 | |
j3 | 0:11c9d61c8876 | 192 | result = tempIbutton.readScratchPad(scratchPadBuff); |
j3 | 0:11c9d61c8876 | 193 | if(result == DS1920::Success) |
j3 | 0:11c9d61c8876 | 194 | { |
j3 | 0:11c9d61c8876 | 195 | term.printf("\nRead Scratchpad Success\n"); |
j3 | 0:11c9d61c8876 | 196 | term.printf("\nScratchpad = "); |
j3 | 0:11c9d61c8876 | 197 | for(idx = 0; idx < 8; idx++) |
j3 | 0:11c9d61c8876 | 198 | { |
j3 | 0:11c9d61c8876 | 199 | if(scratchPadBuff[idx] < 16) |
j3 | 0:11c9d61c8876 | 200 | { |
j3 | 0:11c9d61c8876 | 201 | term.printf("0x0%x ", scratchPadBuff[idx]); |
j3 | 0:11c9d61c8876 | 202 | } |
j3 | 0:11c9d61c8876 | 203 | else |
j3 | 0:11c9d61c8876 | 204 | { |
j3 | 0:11c9d61c8876 | 205 | term.printf("0x%2x ", scratchPadBuff[idx]); |
j3 | 0:11c9d61c8876 | 206 | } |
j3 | 0:11c9d61c8876 | 207 | } |
j3 | 0:11c9d61c8876 | 208 | term.printf("\n"); |
j3 | 0:11c9d61c8876 | 209 | |
j3 | 0:11c9d61c8876 | 210 | } |
j3 | 0:11c9d61c8876 | 211 | else |
j3 | 0:11c9d61c8876 | 212 | { |
j3 | 0:11c9d61c8876 | 213 | term.printf("\nRead Scratchpad Fail\n"); |
j3 | 0:11c9d61c8876 | 214 | } |
j3 | 0:11c9d61c8876 | 215 | |
j3 | 0:11c9d61c8876 | 216 | break; |
j3 | 0:11c9d61c8876 | 217 | |
j3 | 0:11c9d61c8876 | 218 | case '3': |
j3 | 0:11c9d61c8876 | 219 | |
j3 | 0:11c9d61c8876 | 220 | result = tempIbutton.copyScratchPad(); |
j3 | 0:11c9d61c8876 | 221 | if(result == DS1920::Success) |
j3 | 0:11c9d61c8876 | 222 | { |
j3 | 0:11c9d61c8876 | 223 | term.printf("\nCopy Scratchpad Success\n"); |
j3 | 0:11c9d61c8876 | 224 | } |
j3 | 0:11c9d61c8876 | 225 | else |
j3 | 0:11c9d61c8876 | 226 | { |
j3 | 0:11c9d61c8876 | 227 | term.printf("\nCopy Scratchpad Fail\n"); |
j3 | 0:11c9d61c8876 | 228 | } |
j3 | 0:11c9d61c8876 | 229 | |
j3 | 0:11c9d61c8876 | 230 | break; |
j3 | 0:11c9d61c8876 | 231 | |
j3 | 0:11c9d61c8876 | 232 | case '4': |
j3 | 0:11c9d61c8876 | 233 | |
j3 | 0:11c9d61c8876 | 234 | result = tempIbutton.convertTemperature(temperature); |
j3 | 0:11c9d61c8876 | 235 | if(result == DS1920::Success) |
j3 | 0:11c9d61c8876 | 236 | { |
j3 | 0:11c9d61c8876 | 237 | term.printf("\nConvert Temperature Success\n"); |
j3 | 0:11c9d61c8876 | 238 | term.printf("\nTemperature = %.1f", temperature); |
j3 | 0:11c9d61c8876 | 239 | } |
j3 | 0:11c9d61c8876 | 240 | else |
j3 | 0:11c9d61c8876 | 241 | { |
j3 | 0:11c9d61c8876 | 242 | term.printf("\nConvert Temperature Fail\n"); |
j3 | 0:11c9d61c8876 | 243 | } |
j3 | 0:11c9d61c8876 | 244 | |
j3 | 0:11c9d61c8876 | 245 | break; |
j3 | 0:11c9d61c8876 | 246 | |
j3 | 0:11c9d61c8876 | 247 | case '5': |
j3 | 0:11c9d61c8876 | 248 | |
j3 | 0:11c9d61c8876 | 249 | result = tempIbutton.recallEEPROM(); |
j3 | 0:11c9d61c8876 | 250 | if(result == DS1920::Success) |
j3 | 0:11c9d61c8876 | 251 | { |
j3 | 0:11c9d61c8876 | 252 | term.printf("\nRecall EEPROM Success\n"); |
j3 | 0:11c9d61c8876 | 253 | } |
j3 | 0:11c9d61c8876 | 254 | else |
j3 | 0:11c9d61c8876 | 255 | { |
j3 | 0:11c9d61c8876 | 256 | term.printf("\nRecall EEPROM Fail\n"); |
j3 | 0:11c9d61c8876 | 257 | } |
j3 | 0:11c9d61c8876 | 258 | |
j3 | 0:11c9d61c8876 | 259 | break; |
j3 | 0:11c9d61c8876 | 260 | |
j3 | 0:11c9d61c8876 | 261 | case '6': |
j3 | 0:11c9d61c8876 | 262 | term.cls(); |
j3 | 0:11c9d61c8876 | 263 | term.home(); |
j3 | 0:11c9d61c8876 | 264 | break; |
j3 | 0:11c9d61c8876 | 265 | |
j3 | 0:11c9d61c8876 | 266 | case '7': |
j3 | 0:11c9d61c8876 | 267 | term.printf("\nLeaving DS1920 Menu Options\n"); |
j3 | 0:11c9d61c8876 | 268 | break; |
j3 | 0:11c9d61c8876 | 269 | |
j3 | 0:11c9d61c8876 | 270 | default: |
j3 | 0:11c9d61c8876 | 271 | mbed_die(); |
j3 | 0:11c9d61c8876 | 272 | break; |
j3 | 0:11c9d61c8876 | 273 | } |
j3 | 0:11c9d61c8876 | 274 | } |
j3 | 0:11c9d61c8876 | 275 | } |