Project Autus - Automated Plant Chamber

Dependencies:   TextLCD mbed

Fork of keypad_test by Plamen Totev

Autus

This is the codebase accompanying the project Autus.

Autus is an automated growth chamber for plants.

Features

Control Humidity inside chamber wrt to external humidity. Control Temperature inside chamber. ( Peltier Heaters/Coolers ) Water and shower plants. Control soil humidity. Monitor water tanks level (Load Cell) /media/uploads/umairaftab/frdm_-_new_page1.png

Code Base Features

Fixed timing and CRC for DHT-11 Sensor. Fixed OneWire bug for ds18b20

Cyclic Executive Scheduler with Priority. Async IPC framework for PC App over bluetooth

Fake RTC systick, I was having some trouble with the on board rtc.

/media/uploads/umairaftab/download.png

Committer:
umairaftab
Date:
Mon Apr 14 03:34:02 2014 +0000
Revision:
38:9f4107db1bff
Peltier Functions Updated; DS18B20 Multiple sensors added.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
umairaftab 38:9f4107db1bff 1 /*
umairaftab 38:9f4107db1bff 2 * OneWireCRC. This is a port to mbed of Jim Studt's Adruino One Wire
umairaftab 38:9f4107db1bff 3 * library. Please see additional copyrights below this one, including
umairaftab 38:9f4107db1bff 4 * references to other copyrights.
umairaftab 38:9f4107db1bff 5 *
umairaftab 38:9f4107db1bff 6 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
umairaftab 38:9f4107db1bff 7 *
umairaftab 38:9f4107db1bff 8 * This file is part of OneWireCRC.
umairaftab 38:9f4107db1bff 9 *
umairaftab 38:9f4107db1bff 10 * OneWireCRC is free software: you can redistribute it and/or modify
umairaftab 38:9f4107db1bff 11 * it under the terms of the GNU General Public License as published by
umairaftab 38:9f4107db1bff 12 * the Free Software Foundation, either version 3 of the License, or
umairaftab 38:9f4107db1bff 13 * (at your option) any later version.
umairaftab 38:9f4107db1bff 14 *
umairaftab 38:9f4107db1bff 15 * OneWireCRC is distributed in the hope that it will be useful,
umairaftab 38:9f4107db1bff 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
umairaftab 38:9f4107db1bff 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
umairaftab 38:9f4107db1bff 18 * GNU General Public License for more details.
umairaftab 38:9f4107db1bff 19 *
umairaftab 38:9f4107db1bff 20 * You should have received a copy of the GNU General Public License
umairaftab 38:9f4107db1bff 21 * along with OneWireCRC. If not, see <http://www.gnu.org/licenses/>.
umairaftab 38:9f4107db1bff 22 */
umairaftab 38:9f4107db1bff 23 /*
umairaftab 38:9f4107db1bff 24 Copyright (c) 2007, Jim Studt
umairaftab 38:9f4107db1bff 25
umairaftab 38:9f4107db1bff 26 Updated to work with arduino-0008 and to include skip() as of
umairaftab 38:9f4107db1bff 27 2007/07/06. --RJL20
umairaftab 38:9f4107db1bff 28
umairaftab 38:9f4107db1bff 29 Modified to calculate the 8-bit CRC directly, avoiding the need for
umairaftab 38:9f4107db1bff 30 the 256-byte lookup table to be loaded in RAM. Tested in arduino-0010
umairaftab 38:9f4107db1bff 31 -- Tom Pollard, Jan 23, 2008
umairaftab 38:9f4107db1bff 32
umairaftab 38:9f4107db1bff 33 Permission is hereby granted, free of charge, to any person obtaining
umairaftab 38:9f4107db1bff 34 a copy of this software and associated documentation files (the
umairaftab 38:9f4107db1bff 35 "Software"), to deal in the Software without restriction, including
umairaftab 38:9f4107db1bff 36 without limitation the rights to use, copy, modify, merge, publish,
umairaftab 38:9f4107db1bff 37 distribute, sublicense, and/or sell copies of the Software, and to
umairaftab 38:9f4107db1bff 38 permit persons to whom the Software is furnished to do so, subject to
umairaftab 38:9f4107db1bff 39 the following conditions:
umairaftab 38:9f4107db1bff 40
umairaftab 38:9f4107db1bff 41 The above copyright notice and this permission notice shall be
umairaftab 38:9f4107db1bff 42 included in all copies or substantial portions of the Software.
umairaftab 38:9f4107db1bff 43
umairaftab 38:9f4107db1bff 44 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
umairaftab 38:9f4107db1bff 45 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
umairaftab 38:9f4107db1bff 46 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
umairaftab 38:9f4107db1bff 47 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
umairaftab 38:9f4107db1bff 48 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
umairaftab 38:9f4107db1bff 49 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
umairaftab 38:9f4107db1bff 50 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
umairaftab 38:9f4107db1bff 51
umairaftab 38:9f4107db1bff 52 Much of the code was inspired by Derek Yerger's code, though I don't
umairaftab 38:9f4107db1bff 53 think much of that remains. In any event that was..
umairaftab 38:9f4107db1bff 54 (copyleft) 2006 by Derek Yerger - Free to distribute freely.
umairaftab 38:9f4107db1bff 55
umairaftab 38:9f4107db1bff 56 The CRC code was excerpted and inspired by the Dallas Semiconductor
umairaftab 38:9f4107db1bff 57 sample code bearing this copyright.
umairaftab 38:9f4107db1bff 58 //---------------------------------------------------------------------------
umairaftab 38:9f4107db1bff 59 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
umairaftab 38:9f4107db1bff 60 //
umairaftab 38:9f4107db1bff 61 // Permission is hereby granted, free of charge, to any person obtaining a
umairaftab 38:9f4107db1bff 62 // copy of this software and associated documentation files (the "Software"),
umairaftab 38:9f4107db1bff 63 // to deal in the Software without restriction, including without limitation
umairaftab 38:9f4107db1bff 64 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
umairaftab 38:9f4107db1bff 65 // and/or sell copies of the Software, and to permit persons to whom the
umairaftab 38:9f4107db1bff 66 // Software is furnished to do so, subject to the following conditions:
umairaftab 38:9f4107db1bff 67 //
umairaftab 38:9f4107db1bff 68 // The above copyright notice and this permission notice shall be included
umairaftab 38:9f4107db1bff 69 // in all copies or substantial portions of the Software.
umairaftab 38:9f4107db1bff 70 //
umairaftab 38:9f4107db1bff 71 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
umairaftab 38:9f4107db1bff 72 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
umairaftab 38:9f4107db1bff 73 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
umairaftab 38:9f4107db1bff 74 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
umairaftab 38:9f4107db1bff 75 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
umairaftab 38:9f4107db1bff 76 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
umairaftab 38:9f4107db1bff 77 // OTHER DEALINGS IN THE SOFTWARE.
umairaftab 38:9f4107db1bff 78 //
umairaftab 38:9f4107db1bff 79 // Except as contained in this notice, the name of Dallas Semiconductor
umairaftab 38:9f4107db1bff 80 // shall not be used except as stated in the Dallas Semiconductor
umairaftab 38:9f4107db1bff 81 // Branding Policy.
umairaftab 38:9f4107db1bff 82 //--------------------------------------------------------------------------
umairaftab 38:9f4107db1bff 83 */
umairaftab 38:9f4107db1bff 84
umairaftab 38:9f4107db1bff 85 #include "OneWireCRC.h"
umairaftab 38:9f4107db1bff 86 #include "OneWireDefs.h"
umairaftab 38:9f4107db1bff 87
umairaftab 38:9f4107db1bff 88 // recommended data sheet timings in micro seconds
umairaftab 38:9f4107db1bff 89 const int standardT[] = {6, 64, 60, 10, 9, 55, 0, 480, 70, 410};
umairaftab 38:9f4107db1bff 90 const int overdriveT[] = {1.5, 7.5, 7.5, 2.5, 0.75, 7, 2.5, 70, 8.5, 40};
umairaftab 38:9f4107db1bff 91
umairaftab 38:9f4107db1bff 92 OneWireCRC::OneWireCRC(PinName oneWire, eSpeed speed) : oneWirePort(oneWire)
umairaftab 38:9f4107db1bff 93 {
umairaftab 38:9f4107db1bff 94 if (STANDARD == speed) timing = standardT;
umairaftab 38:9f4107db1bff 95 else timing = overdriveT; // overdrive
umairaftab 38:9f4107db1bff 96
umairaftab 38:9f4107db1bff 97 resetSearch(); // reset address search state
umairaftab 38:9f4107db1bff 98 }
umairaftab 38:9f4107db1bff 99
umairaftab 38:9f4107db1bff 100 // Generate a 1-wire reset, return 1 if no presence detect was found,
umairaftab 38:9f4107db1bff 101 // return 0 otherwise.
umairaftab 38:9f4107db1bff 102 // (NOTE: does not handle alarm presence from DS2404/DS1994)
umairaftab 38:9f4107db1bff 103 int OneWireCRC::reset()
umairaftab 38:9f4107db1bff 104 {
umairaftab 38:9f4107db1bff 105
umairaftab 38:9f4107db1bff 106 BYTE result = 0; // sample presence pulse result
umairaftab 38:9f4107db1bff 107
umairaftab 38:9f4107db1bff 108 wait_us(timing[6]);
umairaftab 38:9f4107db1bff 109 oneWirePort.output();
umairaftab 38:9f4107db1bff 110 oneWirePort = 0;
umairaftab 38:9f4107db1bff 111 wait_us(timing[7]);
umairaftab 38:9f4107db1bff 112 oneWirePort.input();
umairaftab 38:9f4107db1bff 113 wait_us(timing[8]);
umairaftab 38:9f4107db1bff 114 result = !(oneWirePort & 0x01);
umairaftab 38:9f4107db1bff 115 wait_us(timing[9]);
umairaftab 38:9f4107db1bff 116
umairaftab 38:9f4107db1bff 117 return result;
umairaftab 38:9f4107db1bff 118 }
umairaftab 38:9f4107db1bff 119
umairaftab 38:9f4107db1bff 120 //
umairaftab 38:9f4107db1bff 121 // Write a bit. Port and bit is used to cut lookup time and provide
umairaftab 38:9f4107db1bff 122 // more certain timing.
umairaftab 38:9f4107db1bff 123 //
umairaftab 38:9f4107db1bff 124 void OneWireCRC::writeBit(int bit)
umairaftab 38:9f4107db1bff 125 {
umairaftab 38:9f4107db1bff 126 bit = bit & 0x01;
umairaftab 38:9f4107db1bff 127
umairaftab 38:9f4107db1bff 128 if (bit)
umairaftab 38:9f4107db1bff 129 {
umairaftab 38:9f4107db1bff 130 // Write '1' bit
umairaftab 38:9f4107db1bff 131 oneWirePort.output();
umairaftab 38:9f4107db1bff 132 oneWirePort = 0;
umairaftab 38:9f4107db1bff 133 wait_us(timing[0]);
umairaftab 38:9f4107db1bff 134 oneWirePort.input();
umairaftab 38:9f4107db1bff 135 wait_us(timing[1]);
umairaftab 38:9f4107db1bff 136 }
umairaftab 38:9f4107db1bff 137 else
umairaftab 38:9f4107db1bff 138 {
umairaftab 38:9f4107db1bff 139 // Write '0' bit
umairaftab 38:9f4107db1bff 140 oneWirePort.output();
umairaftab 38:9f4107db1bff 141 oneWirePort = 0;
umairaftab 38:9f4107db1bff 142 wait_us(timing[2]);
umairaftab 38:9f4107db1bff 143 oneWirePort.input();
umairaftab 38:9f4107db1bff 144 wait_us(timing[3]);
umairaftab 38:9f4107db1bff 145 }
umairaftab 38:9f4107db1bff 146 }
umairaftab 38:9f4107db1bff 147
umairaftab 38:9f4107db1bff 148 //
umairaftab 38:9f4107db1bff 149 // Read a bit. Port and bit is used to cut lookup time and provide
umairaftab 38:9f4107db1bff 150 // more certain timing.
umairaftab 38:9f4107db1bff 151 //
umairaftab 38:9f4107db1bff 152 int OneWireCRC::readBit()
umairaftab 38:9f4107db1bff 153 {
umairaftab 38:9f4107db1bff 154 BYTE result;
umairaftab 38:9f4107db1bff 155
umairaftab 38:9f4107db1bff 156 oneWirePort.output();
umairaftab 38:9f4107db1bff 157 oneWirePort = 0;
umairaftab 38:9f4107db1bff 158 wait_us(timing[0]);
umairaftab 38:9f4107db1bff 159 oneWirePort.input();
umairaftab 38:9f4107db1bff 160 wait_us(timing[4]);
umairaftab 38:9f4107db1bff 161 result = oneWirePort & 0x01;
umairaftab 38:9f4107db1bff 162 wait_us(timing[5]);
umairaftab 38:9f4107db1bff 163
umairaftab 38:9f4107db1bff 164 return result;
umairaftab 38:9f4107db1bff 165 }
umairaftab 38:9f4107db1bff 166
umairaftab 38:9f4107db1bff 167 //
umairaftab 38:9f4107db1bff 168 // Write a byte. The writing code uses the active drivers to raise the
umairaftab 38:9f4107db1bff 169 // pin high, if you need power after the write (e.g. DS18S20 in
umairaftab 38:9f4107db1bff 170 // parasite power mode) then set 'power' to 1, otherwise the pin will
umairaftab 38:9f4107db1bff 171 // go tri-state at the end of the write to avoid heating in a short or
umairaftab 38:9f4107db1bff 172 // other mishap.
umairaftab 38:9f4107db1bff 173 //
umairaftab 38:9f4107db1bff 174 void OneWireCRC::writeByte(int data)
umairaftab 38:9f4107db1bff 175 {
umairaftab 38:9f4107db1bff 176 // Loop to write each bit in the byte, LS-bit first
umairaftab 38:9f4107db1bff 177 for (int loop = 0; loop < 8; loop++)
umairaftab 38:9f4107db1bff 178 {
umairaftab 38:9f4107db1bff 179 writeBit(data & 0x01);
umairaftab 38:9f4107db1bff 180
umairaftab 38:9f4107db1bff 181 // shift the data byte for the next bit
umairaftab 38:9f4107db1bff 182 data >>= 1;
umairaftab 38:9f4107db1bff 183 }
umairaftab 38:9f4107db1bff 184 }
umairaftab 38:9f4107db1bff 185
umairaftab 38:9f4107db1bff 186 //
umairaftab 38:9f4107db1bff 187 // Read a byte
umairaftab 38:9f4107db1bff 188 //
umairaftab 38:9f4107db1bff 189 int OneWireCRC::readByte()
umairaftab 38:9f4107db1bff 190 {
umairaftab 38:9f4107db1bff 191 int result = 0;
umairaftab 38:9f4107db1bff 192
umairaftab 38:9f4107db1bff 193 for (int loop = 0; loop < 8; loop++)
umairaftab 38:9f4107db1bff 194 {
umairaftab 38:9f4107db1bff 195 // shift the result to get it ready for the next bit
umairaftab 38:9f4107db1bff 196 result >>= 1;
umairaftab 38:9f4107db1bff 197
umairaftab 38:9f4107db1bff 198 // if result is one, then set MS bit
umairaftab 38:9f4107db1bff 199 if (readBit()) result |= 0x80;
umairaftab 38:9f4107db1bff 200 }
umairaftab 38:9f4107db1bff 201
umairaftab 38:9f4107db1bff 202 return result;
umairaftab 38:9f4107db1bff 203 }
umairaftab 38:9f4107db1bff 204
umairaftab 38:9f4107db1bff 205 int OneWireCRC::touchByte(int data)
umairaftab 38:9f4107db1bff 206 {
umairaftab 38:9f4107db1bff 207 int result = 0;
umairaftab 38:9f4107db1bff 208
umairaftab 38:9f4107db1bff 209 for (int loop = 0; loop < 8; loop++)
umairaftab 38:9f4107db1bff 210 {
umairaftab 38:9f4107db1bff 211 // shift the result to get it ready for the next bit
umairaftab 38:9f4107db1bff 212 result >>= 1;
umairaftab 38:9f4107db1bff 213
umairaftab 38:9f4107db1bff 214 // If sending a '1' then read a bit else write a '0'
umairaftab 38:9f4107db1bff 215 if (data & 0x01)
umairaftab 38:9f4107db1bff 216 {
umairaftab 38:9f4107db1bff 217 if (readBit()) result |= 0x80;
umairaftab 38:9f4107db1bff 218 }
umairaftab 38:9f4107db1bff 219 else writeBit(0);
umairaftab 38:9f4107db1bff 220
umairaftab 38:9f4107db1bff 221 // shift the data byte for the next bit
umairaftab 38:9f4107db1bff 222 data >>= 1;
umairaftab 38:9f4107db1bff 223 }
umairaftab 38:9f4107db1bff 224
umairaftab 38:9f4107db1bff 225 return result;
umairaftab 38:9f4107db1bff 226 }
umairaftab 38:9f4107db1bff 227
umairaftab 38:9f4107db1bff 228 void OneWireCRC::block(BYTE* data, int data_len)
umairaftab 38:9f4107db1bff 229 {
umairaftab 38:9f4107db1bff 230 for (int loop = 0; loop < data_len; loop++)
umairaftab 38:9f4107db1bff 231 {
umairaftab 38:9f4107db1bff 232 data[loop] = touchByte(data[loop]);
umairaftab 38:9f4107db1bff 233 }
umairaftab 38:9f4107db1bff 234 }
umairaftab 38:9f4107db1bff 235
umairaftab 38:9f4107db1bff 236 int OneWireCRC::overdriveSkip(BYTE* data, int data_len)
umairaftab 38:9f4107db1bff 237 {
umairaftab 38:9f4107db1bff 238 // set the speed to 'standard'
umairaftab 38:9f4107db1bff 239 timing = standardT;
umairaftab 38:9f4107db1bff 240
umairaftab 38:9f4107db1bff 241 // reset all devices
umairaftab 38:9f4107db1bff 242 if (reset()) return 0; // if no devices found
umairaftab 38:9f4107db1bff 243
umairaftab 38:9f4107db1bff 244 // overdrive skip command
umairaftab 38:9f4107db1bff 245 writeByte(OVERDRIVE_SKIP);
umairaftab 38:9f4107db1bff 246
umairaftab 38:9f4107db1bff 247 // set the speed to 'overdrive'
umairaftab 38:9f4107db1bff 248 timing = overdriveT;
umairaftab 38:9f4107db1bff 249
umairaftab 38:9f4107db1bff 250 // do a 1-Wire reset in 'overdrive' and return presence result
umairaftab 38:9f4107db1bff 251 return reset();
umairaftab 38:9f4107db1bff 252 }
umairaftab 38:9f4107db1bff 253
umairaftab 38:9f4107db1bff 254 //
umairaftab 38:9f4107db1bff 255 // Do a ROM select
umairaftab 38:9f4107db1bff 256 //
umairaftab 38:9f4107db1bff 257 void OneWireCRC::matchROM(BYTE rom[8])
umairaftab 38:9f4107db1bff 258 {
umairaftab 38:9f4107db1bff 259 writeByte(MATCH_ROM); // Choose ROM
umairaftab 38:9f4107db1bff 260
umairaftab 38:9f4107db1bff 261 for(int i = 0; i < 8; i++) writeByte(rom[i]);
umairaftab 38:9f4107db1bff 262 }
umairaftab 38:9f4107db1bff 263
umairaftab 38:9f4107db1bff 264 //
umairaftab 38:9f4107db1bff 265 // Do a ROM skip
umairaftab 38:9f4107db1bff 266 //
umairaftab 38:9f4107db1bff 267 void OneWireCRC::skipROM()
umairaftab 38:9f4107db1bff 268 {
umairaftab 38:9f4107db1bff 269 writeByte(SKIP_ROM); // Skip ROM
umairaftab 38:9f4107db1bff 270 }
umairaftab 38:9f4107db1bff 271
umairaftab 38:9f4107db1bff 272 //
umairaftab 38:9f4107db1bff 273 // You need to use this function to start a search again from the beginning.
umairaftab 38:9f4107db1bff 274 // You do not need to do it for the first search, though you could.
umairaftab 38:9f4107db1bff 275 //
umairaftab 38:9f4107db1bff 276 void OneWireCRC::resetSearch()
umairaftab 38:9f4107db1bff 277 {
umairaftab 38:9f4107db1bff 278 searchJunction = -1;
umairaftab 38:9f4107db1bff 279 searchExhausted = false;
umairaftab 38:9f4107db1bff 280 for (int i = 0; i < 8; i++)
umairaftab 38:9f4107db1bff 281 {
umairaftab 38:9f4107db1bff 282 address[i] = 0;
umairaftab 38:9f4107db1bff 283 }
umairaftab 38:9f4107db1bff 284 }
umairaftab 38:9f4107db1bff 285
umairaftab 38:9f4107db1bff 286 //
umairaftab 38:9f4107db1bff 287 // Perform a search. If this function returns a '1' then it has
umairaftab 38:9f4107db1bff 288 // enumerated the next device and you may retrieve the ROM from the
umairaftab 38:9f4107db1bff 289 // OneWire::address variable. If there are no devices, no further
umairaftab 38:9f4107db1bff 290 // devices, or something horrible happens in the middle of the
umairaftab 38:9f4107db1bff 291 // enumeration then a 0 is returned. If a new device is found then
umairaftab 38:9f4107db1bff 292 // its address is copied to newAddr. Use OneWire::reset_search() to
umairaftab 38:9f4107db1bff 293 // start over.
umairaftab 38:9f4107db1bff 294 //
umairaftab 38:9f4107db1bff 295 BYTE OneWireCRC::search(BYTE* newAddr)
umairaftab 38:9f4107db1bff 296 {
umairaftab 38:9f4107db1bff 297 BYTE i;
umairaftab 38:9f4107db1bff 298 int lastJunction = -1;
umairaftab 38:9f4107db1bff 299 BYTE done = 1;
umairaftab 38:9f4107db1bff 300
umairaftab 38:9f4107db1bff 301 if (searchExhausted) return 0;
umairaftab 38:9f4107db1bff 302
umairaftab 38:9f4107db1bff 303 if (!reset()) return 0;
umairaftab 38:9f4107db1bff 304
umairaftab 38:9f4107db1bff 305 writeByte(SEARCH_ROM);
umairaftab 38:9f4107db1bff 306
umairaftab 38:9f4107db1bff 307 for(i = 0; i < 64; i++)
umairaftab 38:9f4107db1bff 308 {
umairaftab 38:9f4107db1bff 309 BYTE a = readBit( );
umairaftab 38:9f4107db1bff 310 BYTE nota = readBit( );
umairaftab 38:9f4107db1bff 311 BYTE ibyte = i/8;
umairaftab 38:9f4107db1bff 312 BYTE ibit = 1 << (i & 7);
umairaftab 38:9f4107db1bff 313
umairaftab 38:9f4107db1bff 314 // I don't think this should happen, this means nothing responded, but maybe if
umairaftab 38:9f4107db1bff 315 // something vanishes during the search it will come up.
umairaftab 38:9f4107db1bff 316 if (a && nota) return 0;
umairaftab 38:9f4107db1bff 317
umairaftab 38:9f4107db1bff 318 if (!a && !nota)
umairaftab 38:9f4107db1bff 319 {
umairaftab 38:9f4107db1bff 320 if (i == searchJunction)
umairaftab 38:9f4107db1bff 321 {
umairaftab 38:9f4107db1bff 322 // this is our time to decide differently, we went zero last time, go one.
umairaftab 38:9f4107db1bff 323 a = 1;
umairaftab 38:9f4107db1bff 324 searchJunction = lastJunction;
umairaftab 38:9f4107db1bff 325 }
umairaftab 38:9f4107db1bff 326 else if (i < searchJunction)
umairaftab 38:9f4107db1bff 327 {
umairaftab 38:9f4107db1bff 328 // take whatever we took last time, look in address
umairaftab 38:9f4107db1bff 329 if (address[ibyte] & ibit) a = 1;
umairaftab 38:9f4107db1bff 330 else
umairaftab 38:9f4107db1bff 331 {
umairaftab 38:9f4107db1bff 332 // Only 0s count as pending junctions, we've already exhasuted the 0 side of 1s
umairaftab 38:9f4107db1bff 333 a = 0;
umairaftab 38:9f4107db1bff 334 done = 0;
umairaftab 38:9f4107db1bff 335 lastJunction = i;
umairaftab 38:9f4107db1bff 336 }
umairaftab 38:9f4107db1bff 337 }
umairaftab 38:9f4107db1bff 338 else
umairaftab 38:9f4107db1bff 339 {
umairaftab 38:9f4107db1bff 340 // we are blazing new tree, take the 0
umairaftab 38:9f4107db1bff 341 a = 0;
umairaftab 38:9f4107db1bff 342 searchJunction = i;
umairaftab 38:9f4107db1bff 343 done = 0;
umairaftab 38:9f4107db1bff 344 }
umairaftab 38:9f4107db1bff 345 lastJunction = i;
umairaftab 38:9f4107db1bff 346 }
umairaftab 38:9f4107db1bff 347
umairaftab 38:9f4107db1bff 348 if (a) address[ibyte] |= ibit;
umairaftab 38:9f4107db1bff 349 else address[ibyte] &= ~ibit;
umairaftab 38:9f4107db1bff 350
umairaftab 38:9f4107db1bff 351 writeBit(a);
umairaftab 38:9f4107db1bff 352 }
umairaftab 38:9f4107db1bff 353
umairaftab 38:9f4107db1bff 354 if (done) searchExhausted = true;
umairaftab 38:9f4107db1bff 355
umairaftab 38:9f4107db1bff 356 for (i = 0; i < 8; i++) newAddr[i] = address[i];
umairaftab 38:9f4107db1bff 357
umairaftab 38:9f4107db1bff 358 return 1;
umairaftab 38:9f4107db1bff 359 }
umairaftab 38:9f4107db1bff 360
umairaftab 38:9f4107db1bff 361 // The 1-Wire CRC scheme is described in Maxim Application Note 27:
umairaftab 38:9f4107db1bff 362 // "Understanding and Using Cyclic Redundancy Checks with Maxim iButton Products"
umairaftab 38:9f4107db1bff 363 //
umairaftab 38:9f4107db1bff 364
umairaftab 38:9f4107db1bff 365 #if ONEWIRE_CRC8_TABLE
umairaftab 38:9f4107db1bff 366 // This table comes from Dallas sample code where it is freely reusable,
umairaftab 38:9f4107db1bff 367 // though Copyright (C) 2000 Dallas Semiconductor Corporation
umairaftab 38:9f4107db1bff 368 static BYTE dscrc_table[] =
umairaftab 38:9f4107db1bff 369 {
umairaftab 38:9f4107db1bff 370 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
umairaftab 38:9f4107db1bff 371 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
umairaftab 38:9f4107db1bff 372 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
umairaftab 38:9f4107db1bff 373 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
umairaftab 38:9f4107db1bff 374 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
umairaftab 38:9f4107db1bff 375 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
umairaftab 38:9f4107db1bff 376 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
umairaftab 38:9f4107db1bff 377 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
umairaftab 38:9f4107db1bff 378 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
umairaftab 38:9f4107db1bff 379 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
umairaftab 38:9f4107db1bff 380 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
umairaftab 38:9f4107db1bff 381 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
umairaftab 38:9f4107db1bff 382 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
umairaftab 38:9f4107db1bff 383 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
umairaftab 38:9f4107db1bff 384 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
umairaftab 38:9f4107db1bff 385 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
umairaftab 38:9f4107db1bff 386
umairaftab 38:9f4107db1bff 387 //
umairaftab 38:9f4107db1bff 388 // Compute a Dallas Semiconductor 8 bit CRC. These show up in the ROM
umairaftab 38:9f4107db1bff 389 // and the registers. (note: this might better be done without the
umairaftab 38:9f4107db1bff 390 // table, it would probably be smaller and certainly fast enough
umairaftab 38:9f4107db1bff 391 // compared to all those delayMicrosecond() calls. But I got
umairaftab 38:9f4107db1bff 392 // confused, so I use this table from the examples.)
umairaftab 38:9f4107db1bff 393 //
umairaftab 38:9f4107db1bff 394 BYTE OneWireCRC::crc8(BYTE* addr, BYTE len)
umairaftab 38:9f4107db1bff 395 {
umairaftab 38:9f4107db1bff 396 BYTE i;
umairaftab 38:9f4107db1bff 397 BYTE crc = 0;
umairaftab 38:9f4107db1bff 398
umairaftab 38:9f4107db1bff 399 for (i = 0; i < len; i++)
umairaftab 38:9f4107db1bff 400 {
umairaftab 38:9f4107db1bff 401 crc = dscrc_table[crc ^ addr[i] ];
umairaftab 38:9f4107db1bff 402 }
umairaftab 38:9f4107db1bff 403
umairaftab 38:9f4107db1bff 404 return crc;
umairaftab 38:9f4107db1bff 405 }
umairaftab 38:9f4107db1bff 406 #else
umairaftab 38:9f4107db1bff 407 //
umairaftab 38:9f4107db1bff 408 // Compute a Dallas Semiconductor 8 bit CRC directly.
umairaftab 38:9f4107db1bff 409 //
umairaftab 38:9f4107db1bff 410 BYTE OneWireCRC::crc8(BYTE* addr, BYTE len)
umairaftab 38:9f4107db1bff 411 {
umairaftab 38:9f4107db1bff 412 BYTE i, j;
umairaftab 38:9f4107db1bff 413 BYTE crc = 0;
umairaftab 38:9f4107db1bff 414
umairaftab 38:9f4107db1bff 415 for (i = 0; i < len; i++)
umairaftab 38:9f4107db1bff 416 {
umairaftab 38:9f4107db1bff 417 BYTE inbyte = addr[i];
umairaftab 38:9f4107db1bff 418 for (j = 0; j < 8; j++)
umairaftab 38:9f4107db1bff 419 {
umairaftab 38:9f4107db1bff 420 BYTE mix = (crc ^ inbyte) & 0x01;
umairaftab 38:9f4107db1bff 421 crc >>= 1;
umairaftab 38:9f4107db1bff 422 if (mix) crc ^= 0x8C;
umairaftab 38:9f4107db1bff 423 inbyte >>= 1;
umairaftab 38:9f4107db1bff 424 }
umairaftab 38:9f4107db1bff 425 }
umairaftab 38:9f4107db1bff 426
umairaftab 38:9f4107db1bff 427 return crc;
umairaftab 38:9f4107db1bff 428 }
umairaftab 38:9f4107db1bff 429 #endif
umairaftab 38:9f4107db1bff 430
umairaftab 38:9f4107db1bff 431 static short oddparity[16] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
umairaftab 38:9f4107db1bff 432
umairaftab 38:9f4107db1bff 433 //
umairaftab 38:9f4107db1bff 434 // Compute a Dallas Semiconductor 16 bit CRC. I have never seen one of
umairaftab 38:9f4107db1bff 435 // these, but here it is.
umairaftab 38:9f4107db1bff 436 //
umairaftab 38:9f4107db1bff 437 unsigned short OneWireCRC::crc16(unsigned short* data, unsigned short len)
umairaftab 38:9f4107db1bff 438 {
umairaftab 38:9f4107db1bff 439 unsigned short i;
umairaftab 38:9f4107db1bff 440 unsigned short crc = 0;
umairaftab 38:9f4107db1bff 441
umairaftab 38:9f4107db1bff 442 for ( i = 0; i < len; i++)
umairaftab 38:9f4107db1bff 443 {
umairaftab 38:9f4107db1bff 444 unsigned short cdata = data[len];
umairaftab 38:9f4107db1bff 445
umairaftab 38:9f4107db1bff 446 cdata = (cdata ^ (crc & 0xff)) & 0xff;
umairaftab 38:9f4107db1bff 447 crc >>= 8;
umairaftab 38:9f4107db1bff 448
umairaftab 38:9f4107db1bff 449 if (oddparity[cdata & 0xf] ^ oddparity[cdata >> 4]) crc ^= 0xc001;
umairaftab 38:9f4107db1bff 450
umairaftab 38:9f4107db1bff 451 cdata <<= 6;
umairaftab 38:9f4107db1bff 452 crc ^= cdata;
umairaftab 38:9f4107db1bff 453 cdata <<= 1;
umairaftab 38:9f4107db1bff 454 crc ^= cdata;
umairaftab 38:9f4107db1bff 455 }
umairaftab 38:9f4107db1bff 456
umairaftab 38:9f4107db1bff 457 return crc;
umairaftab 38:9f4107db1bff 458 }
umairaftab 38:9f4107db1bff 459