Library to communicate with Maxim OneWire protocol devices Modified timings and IRQ overrides

Dependents:   RdGasUseMonitor

Fork of Onewire by Simon Barker

Files at this revision

API Documentation at this revision

Comitter:
Bobty
Date:
Mon Oct 05 14:03:29 2015 +0000
Parent:
5:45b6a39002f1
Child:
7:0a87f8c2d9e6
Commit message:
Changed return code from search to be more meaningful about the reason for failure

Changed in this revision

Onewire.cpp Show annotated file Show diff for this revision Revisions of this file
Onewire.h Show annotated file Show diff for this revision Revisions of this file
--- a/Onewire.cpp	Mon Sep 28 10:31:58 2015 +0000
+++ b/Onewire.cpp	Mon Oct 05 14:03:29 2015 +0000
@@ -128,7 +128,12 @@
 
 // Search Copied from Arduino code
 //
-// Perform a search. If this function returns a '1' then it has
+// Perform a search.
+// Returns 
+// ONEWIRE_OK if a new address has been returned.
+// ONEWIRE_SEARCH_ALL_DONE = all devices found
+// ONEWIRE_SEARCH_INIT_FAIL = failed to init
+// ONEWIRE_SEARCH_NOT_FOUND = no devices found If this function returns a '1' then it has
 // enumerated the next device and you may retrieve the ROM from the
 // Onewire::address variable. If there are no devices, no further
 // devices, or something horrible happens in the middle of the
@@ -146,7 +151,8 @@
 uint8_t Onewire::search(uint8_t *newAddr)
 {
    uint8_t id_bit_number = 1;
-   uint8_t last_zero = 0, rom_byte_number = 0, search_result = 0;
+   uint8_t last_zero = 0, rom_byte_number = 0;
+   uint8_t search_result = ONEWIRE_SEARCH_ALL_DONE;
    uint8_t id_bit = 0, cmp_id_bit = 0;
 
    // initialize for search
@@ -162,7 +168,7 @@
          _search_LastDiscrepancy = 0;
          _search_LastDeviceFlag = false;
          _search_LastFamilyDiscrepancy = 0;
-         return false;
+         return ONEWIRE_SEARCH_INIT_FAIL;
       }
 
       // issue the search command
@@ -239,18 +245,20 @@
          if (_search_LastDiscrepancy == 0)
             _search_LastDeviceFlag = true;
 
-         search_result = true;
+         search_result = ONEWIRE_OK;
       }
    }
 
    // if no device found then reset counters so next 'search' will be like a first
-   if (!search_result || !_search_ROM_NO[0])
+   if (!_search_ROM_NO[0])
    {
       _search_LastDiscrepancy = 0;
       _search_LastDeviceFlag = false;
       _search_LastFamilyDiscrepancy = 0;
-      search_result = false;
+      search_result = ONEWIRE_SEARCH_NOT_FOUND;
    }
+   if (search_result != ONEWIRE_OK)
+        return search_result;
    for (int i = 0; i < ONEWIRE_ADDR_BYTES; i++)
        newAddr[i] = _search_ROM_NO[i];
    return search_result;
--- a/Onewire.h	Mon Sep 28 10:31:58 2015 +0000
+++ b/Onewire.h	Mon Oct 05 14:03:29 2015 +0000
@@ -5,9 +5,15 @@
 
 #define ONEWIRE_ADDR_BYTES 8
 
-class Onewire{
+const int ONEWIRE_OK = 0;
+const int ONEWIRE_SEARCH_ALL_DONE = 1;
+const int ONEWIRE_SEARCH_INIT_FAIL = 2;
+const int ONEWIRE_SEARCH_NOT_FOUND = 3;
 
-public:
+class Onewire
+{
+
+public:    
     Onewire(PinName oneBus);
     void writeBit(int bit);
     int readBit();
@@ -18,10 +24,13 @@
 
     // Clear the search state so that if will start from the beginning again.
     void reset_search();
-    // Look for the next device. Returns 1 if a new address has been
-    // returned. A zero might mean that the bus is shorted, there are
-    // no devices, or you have already retrieved all of them.  It
-    // might be a good idea to check the CRC to make sure you didn't
+    // Look for the next device.
+    // Returns 
+    // ONEWIRE_OK if a new address has been returned.
+    // ONEWIRE_SEARCH_ALL_DONE = all devices found
+    // ONEWIRE_SEARCH_INIT_FAIL = failed to init
+    // ONEWIRE_SEARCH_NOT_FOUND = no devices found
+    // It might be a good idea to check the CRC to make sure you didn't
     // get garbage.  The order is deterministic. You will always get
     // the same devices in the same order.
     uint8_t search(uint8_t *newAddr);