diff --git a/README.md b/README.md index 2ce203d..fc925fb 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ An Arduino library handling iButton identification tags. -## 👍 Supported Devices +## 👍 Supported devices | Model | Reading ID-code | Writing ID-code | |:-----------------------------------------|:---------------:|:---------------:| @@ -38,14 +38,14 @@ _If you don't see the Install All button mentioned in step 5, you have an older 3. Select the downloaded ZIP file 4. Repeat steps above to install required _OneWire_ library; in step 1 download ZIP file from [OneWire releases](https://github.com/PaulStoffregen/OneWire/releases/latest) -## 🚶 Basic Usage +## 🚶 Basic usage -### Hardware Setup +### Hardware setup - Connect data line of iButton probe to an Arduino digital pin, this is the 1-Wire data line. - Connect a 2200 Ω to 4700 Ω pull-up resistor between the 1-Wire data line and Arduino 5V pin. - Connect ground line of iButton probe to an Arduino ground (GND) pin. -### Code Example +### Code example ```cpp #include // Include the library @@ -64,7 +64,7 @@ _If you don't see the Install All button mentioned in step 5, you have an older Serial.println( "Reading... " ); int8_t status = ibutton.readCode( code ); // Try to read ID-code - if ( status > 0 ) { // Code read succesfully + if ( status > 0 ) { // Code read successfully Serial.print( "iButton code read: " ); ibutton.printCode( code ); // ID-code is in variable _code_ Serial.println(); @@ -72,30 +72,35 @@ _If you don't see the Install All button mentioned in step 5, you have an older } ``` -## 🏃 Advanced Features +More [examples](https://vdwulp.github.io/iButtonTag/examples.html) are available after installation in the Arduino IDE via _File-menu > Examples > iButtonTag_. + +## 🏃 Advanced features - Writing new identification code to (re)writable iButton tag models - Reading multiple iButton probes on the same 1-Wire data line -## 🛠️ Hardware Notes +## 🛠️ Hardware notes ### iButton identification tags +- iButton tags are used for many purposes, including as key to lock of office, hotel sauna or cabinet, also to (de)activate alarm systems and log attendance/time. - Many iButton tags have their identification code engraved on them in hexadecimal: in the center the _reversed_ tag specific 6-byte code, above it on the right side 1-byte family code and on the left side 1-byte checksum. When read by this library the order is: family code - tag specific code - checksum. - Official iButton identification tags all have a unique identification code that _cannot_ be changed. However, this library supports writing a new code to multiple types of (re)writable _compatible_ tags. - DS1990 (not DS1990A or DS1990R) can't be used with multiple identification tags on a single data line and requires special handling. For details see comments on function [readCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#readCode) in [reference documentation](https://vdwulp.github.io/iButtonTag/REFERENCE.html). -### Pull-up resistor -- The official recommendation is to use a 4700 Ω pull-up resistor between the 1-Wire data line and Arduino 5V pin when _reading_ from an iButton tag. -- Writing a new code to (re)writable iButton tags may require _more power_ for a successful and persistent result. To get more power to the tag, a 2200 Ω pull-up resistor between the 1-Wire data line and Arduino 5V pin has been tested to be a good value. - ### iButton probes -- iButton probes come in many forms, some have LED-indicator(s) with one or two colors, see [some examples](https://vdwulp.github.io/iButtonTag/probes.html#examples). +- [iButton probes](https://vdwulp.github.io/iButtonTag/probes.html) come in many forms, some have LED-indicator(s) with one or two colors, see [some examples](https://vdwulp.github.io/iButtonTag/probes.html#examples). - No probe? No problem... see instructions in [probe documentation](https://vdwulp.github.io/iButtonTag/probes.html#noprobe). -## 📓 Documentation & References +### Pull-up resistor +- The official recommendation is to use a 4700 Ω pull-up resistor between the 1-Wire data line and Arduino 5V pin when _reading_ from an iButton tag. +- _Writing_ a new code to (re)writable iButton tags may require _more power_ for a successful and persistent result. To get more power to the tag, a 2200 Ω pull-up resistor between the 1-Wire data line and Arduino 5V pin has been tested to be a good value. + +## 🔗 Quick links +- [General information](https://vdwulp.github.io/iButtonTag/) (this file) +- [Reference documentation](https://vdwulp.github.io/iButtonTag/REFERENCE.html) +- [GitHub repository](https://github.com/vdwulp/iButtonTag) +- [Download latest release](https://github.com/vdwulp/iButtonTag/releases/latest) -### Documentation -- Complete iButtonTag library [reference documentation](https://vdwulp.github.io/iButtonTag/REFERENCE.html) with detailed information on all types, constants and functions. -- iButtonTag library [GitHub repository](https://github.com/vdwulp/iButtonTag). +## 📓 Documentation ### Articles - [What is an iButton Device?](https://www.analog.com/media/en/technical-documentation/tech-articles/what-is-an-ibutton-device.pdf) @@ -113,4 +118,4 @@ _If you don't see the Install All button mentioned in step 5, you have an older ## 🗒️ License MIT License, full text available in [LICENSE](https://github.com/vdwulp/iButtonTag/blob/main/LICENSE) file. -Copyright (c) 2025 SA van der Wulp +Copyright (c) 2025 SA van der Wulpcounter diff --git a/doc/REFERENCE.md b/doc/REFERENCE.md index f298ccd..53ffe75 100644 --- a/doc/REFERENCE.md +++ b/doc/REFERENCE.md @@ -1,8 +1,13 @@ # [📚 Arduino iButtonTag Library](https://vdwulp.github.io/iButtonTag/) +## 🔗 Quick links +- [General information](https://vdwulp.github.io/iButtonTag/) +- [Reference documentation](https://vdwulp.github.io/iButtonTag/REFERENCE.html) (this file) +- [GitHub repository](https://github.com/vdwulp/iButtonTag) +- [Download latest release](https://github.com/vdwulp/iButtonTag/releases/latest) + ## Reference documentation - This iButtonTag library reference documentation describes all available types, constants and functions. -- For more general information visit [Arduino iButtonTag Library](https://vdwulp.github.io/iButtonTag/). - All current _constants_ are used to indicate iButton (re)writable tag types and their valid value range. These are used in functions related to _writing_ identification codes. - Apart from the _constructor_, available functions can be arranged in three groups: - Reading identification code(s): [readCode](#readCode), [readCodes](#readCodes), [nextCode](#nextCode) @@ -45,79 +50,94 @@ Indicates the maximum value of an iButton tag type constant. Can be used to dete ## Functions -### iButtonTag( uint8_t pin ) + +### Constructor iButtonTag Constructs an iButtonTag object linked to the supplied pin. **Arguments** + | type | name | description | |:-----|:-----|:------------| | uint8_t | pin | Arduino pin number this iButtonTag object should be linked to. | -### int8_t readCode( iButtonCode code, bool old ) -Reads one single iButton identifying code from the OneWire. +### Function readCode +Reads one single [iButtonCode](#iButtonCode) from the data line. + +When multiple iButtons are connected to the data line this function will return an invalid reading because of a checksum failure (return value -2) caused by colliding responses. If there is _any_ possibility multiple iButtons are connected, use [readCodes](#readCodes) instead. -When multiple iButtons are connected to the OneWire this function will return an invalid reading because of a checksum failure (return value -2) caused by colliding responses. If there is _any_ possibility multiple iButtons are connected, use [readCodes](#readCodes) instead. +DS1990 iButton tags can't be used with multiple tags on a single data line and require special handling. This function facilitates compatibility with the DS1990 iButton tags when argument _old_ is set to true. However, this reduces compatibility with other iButton tags: DS1990A, DS1990R and TM1990A will still be handled correctly (they offer backwards compatibility), but other OneWire devices (including iButtons) won't and may even show unexpected behaviour. -DS1990 iButton tags can't be used with multiple tags on a single OneWire data line and require special handling. This function facilitates compatibility with the DS1990 iButton Tags when argument _old_ is set to true. However, this reduces compatibility with other iButton tags: DS1990A, DS1990R and TM1990A will still be handled correctly (they offer backwards compatibility), but other OneWire devices (including iButtons) won't and may even show unexpected behaviour. +Examples [Simple](https://vdwulp.github.io/iButtonTag/examples.html#Simple) and [Single](https://vdwulp.github.io/iButtonTag/examples.html#Single) show how to use this function. **Arguments** + | type | name | description | |:-----|:-----|:------------| -| [iButtonCode](#iButtonCode) | code | Variable to store code read from the OneWire. | +| [iButtonCode](#iButtonCode) | code | Variable to store code read from the data line. | | bool | old | Setting to _true_ enables compatibility with DS1990 iButton tags. Default value is _false_. | -**Returns** +**Returns _type int8_t_** + | value | description | |:-----:|:------------| -| 1 | Next iButton read succesfully, code array filled with identifying code | +| 1 | Next iButton read successfully, code array filled with identifying code | | 0 | No more iButtons detected, code array is unchanged | | -1 | Invalid iButton code read, checksum failed, code array with invalid bytes | | -2 | Invalid iButton code read, all zeros, code array with invalid bytes | -### int8_t readCodes() -Starts the search for multiple iButton identifying codes on the OneWire. +### Function readCodes +Starts the search for multiple [iButtonCode](#iButtonCode)'s on the data line. + +Resets the domain to search for [iButtonCode](#iButtonCode)'s. This function is needed to start searching for codes _again_. It's not really needed the first time, though it's good practice to always use it before enumerating codes with the [nextCode](#nextCode) function. -Resets the domain to search for iButton identifying codes. This function is needed to start searching for codes _again_. It's not really needed the first time, though it's good practice to always use it before enumerating codes with the [nextCode](#nextCode) function. +Example [Multiple](https://vdwulp.github.io/iButtonTag/examples.html#Multiple) shows how to use this function in combination with the [nextCode](#nextCode) function. + +**Returns _type int8_t_** -**Returns** | value | description | |:-----:|:------------| | 1 | At least one iButton detected, enumerate with [nextCode](#nextCode) function | 0 | No iButton detected -### int8_t nextCode( iButtonCode code ) -Continues the search for multiple iButton identifying codes on the OneWire. +### Function nextCode +Continues the search for multiple [iButtonCode](#iButtonCode)'s on the data line. + +Start the search for multiple iButton identyfying codes with the [readCodes](#readCodes) function. The is this function to enumerate all [iButtonCode](#iButtonCode)'s on the data line. -Start the search for multiple iButton identyfying codes with the [readCodes](#readCodes) function. The is this function to enumerate all iButton identifying codes on the OneWire. +A return value 0 means searching finished successfully, but there are no more iButtons on the data line. Negative return values indicate a problem during the search (mostly due to movement of the iButton on the reader), but additional calls to the function _may_ yield new iButton identifying codes. However, the overall result will be unreliable. -A return value 0 means searching finished succesfully, but there are no more iButtons on the OneWire. Negative return values indicate a problem during the search (mostly due to movement of the iButton on the reader), but additional calls to the function _may_ yield new iButton identifying codes. However, the overall result will be unreliable. +Example [Multiple](https://vdwulp.github.io/iButtonTag/examples.html#Multiple) shows how to use this function in combination with the [readCodes](#readCodes) function. **Arguments** + | type | name | description | |:-----|:-----|:------------| -| [iButtonCode](#iButtonCode) | code | Variable to store code read from the OneWire. | +| [iButtonCode](#iButtonCode) | code | Variable to store code read from the data line. | + +**Returns _type int8_t_** -**Returns** | value | description | |:-----:|:------------| -| 1 | Next iButton read succesfully, code array filled with identifying code | +| 1 | Next iButton read successfully, code array filled with identifying code | | 0 | No more iButtons detected, code array is unchanged | | -1 | Invalid iButton code read, checksum failed, code array with invalid bytes | | -2 | Invalid iButton code read, all zeros, code array with invalid bytes | -### static int8_t testCode( iButtonCode code ) +### Static function testCode Tests [iButtonCode](#iButtonCode) for validity. **Arguments** + | type | name | description | |:-----|:-----|:------------| | [iButtonCode](#iButtonCode) | code | Code to be tested. | -**Returns** +**Returns _type int8_t_** + | value | description | |:-----:|:------------| | 1 | iButton code valid | @@ -125,44 +145,52 @@ Tests [iButtonCode](#iButtonCode) for validity. | -2 | iButton code invalid, all zeros | -### static bool equalCode( iButtonCode a, iButtonCode b ) +### Static function equalCode Tests if two [iButtonCode](#iButtonCode)'s are equal. +Example [Match](https://vdwulp.github.io/iButtonTag/examples.html#Match) shows how to use this function. + **Arguments** + | type | name | description | |:-----|:-----|:------------| | [iButtonCode](#iButtonCode) | a | First code to be tested. | | [iButtonCode](#iButtonCode) | b | Second code to be tested. | -**Returns** +**Returns _type bool_** + | value | description | |:-----:|:------------| | true | The two [iButtonCode](#iButtonCode)'s are equal | | false | The two [iButtonCode](#iButtonCode)'s are _not_ equal | -### static void printCode( iButtonCode code, bool reverse ) +### Static function printCode Prints [iButtonCode](#iButtonCode) to Serial as hexadecimal byte values. Serial must be initialised in the main code first. By default the bytes are printed as received from the iButton (reverse = false). The order can be reversed (reverse = true) to match the sequence fysically engraved on many iButtons. **Arguments** + | type | name | description | |:-----|:-----|:------------| | [iButtonCode](#iButtonCode) | code | The code to be printed. | | bool | reverse | Setting to _true_ will reverse the printed code. Default value is _false_. | -### static void updateChecksum( iButtonCode code ) -Updates checksum of [iButtonCode](#iButtonCode) to a correct value. +### Static function updateChecksum +Updates checksum of [iButtonCode](#iButtonCode) to the correct value. + +Example [WriteCode](https://vdwulp.github.io/iButtonTag/examples.html#WriteCode) shows how to use this function. **Arguments** + | type | name | description | |:-----|:-----|:------------| | [iButtonCode](#iButtonCode) | code | The code to be updated. | -### int8_t detectWritableType() +### Function detectWritableType Detects type of (re)writable iButton tag. Performs multiple tests to check for known reponses of (re)writable iButton tag types. If a detectable (re)writable type is found, the return value indicates the specific model. All supported (re)writable types are defined as iButton (re)writable tag type constants, like [IBUTTON_RW1990V1](#IBUTTON_RW1990V1), [IBUTTON_RW1990V2](#IBUTTON_RW1990V2), [IBUTTON_RW2004](#IBUTTON_RW2004) or [IBUTTON_TM01](#IBUTTON_TM01). @@ -172,7 +200,8 @@ A return value of iButton (re)writable type constant [IBUTTON_UNKNOWN](#IBUTTON_ - iButton tag _is not_ of a (re)writable type supported by this library. However, it _is_ readable. - iButton tag _is_ of a (re)writable type supported by this library. However, this specific type _is not_ detectable, like types indicated by [IBUTTON_TM01](#IBUTTON_TM01). -**Returns** +**Returns _type int8_t_** + | value | description | |:-----:|:------------| | \>0 | iButton writable type found as indicated by type constant | @@ -180,24 +209,28 @@ A return value of iButton (re)writable type constant [IBUTTON_UNKNOWN](#IBUTTON_ | -1 | No iButton detected | -### int8_t writeCode( iButtonCode code, int8_t type, bool check ) -Writes a new iButton identifying code to a (re)writable tag. +### Function writeCode +Writes a new [iButtonCode](#iButtonCode) to a (re)writable tag. Strong recommendations, please read carefully: - It is recommended to have _only one_ iButton probe/tag connected to the data line when writing. Some iButton (re)writable tag types will allow multiple tags to be written at the same time, but it may lead to failure. - It is _also_ recommended to only supply the new code as an argument. The function will try to detect a (re)writable iButton tag type. If it fails, you should seriously check if the tag really is a (re)writable tag of a type supported by this library. As iButton tag type TM01 is non-detectable, this type will never be detected. In this case supply iButton (re)writable tag type constant [IBUTTON_TM01](#IBUTTON_TM01). To override auto-detection in other cases, you can also supply an iButton (re)writable tag type constant. - It is _also_ recommended to have checking _on_ like the default, making sure as much checks as possible are done before actually writing a new code to the iButton tag. As some writeble tags are just _write once_ there is a risk in trying to write without appropriate checking. -This function supports writing a new iButton identification code to tag models RW1990, RW1990.1, ТM08, ТM08v2 (type [IBUTTON_RW1990V1](#IBUTTON_RW1990V1)), RW1990v2, RW1990.2 (type [IBUTTON_RW1990V2](#IBUTTON_RW1990V2)), RW2004, TM2004 (type [IBUTTON_RW2004](#IBUTTON_RW2004)), TM01 and TM01C (type [IBUTTON_TM01](#IBUTTON_TM01)). +This function supports writing a new [iButtonCode](#iButtonCode) to tag models RW1990, RW1990.1, ТM08, ТM08v2 (type [IBUTTON_RW1990V1](#IBUTTON_RW1990V1)), RW1990v2, RW1990.2 (type [IBUTTON_RW1990V2](#IBUTTON_RW1990V2)), RW2004, TM2004 (type [IBUTTON_RW2004](#IBUTTON_RW2004)), TM01 and TM01C (type [IBUTTON_TM01](#IBUTTON_TM01)). + +Example [WriteCode](https://vdwulp.github.io/iButtonTag/examples.html#WriteCode) shows how to use this function. **Arguments** + | type | name | description | |:-----|:-----|:------------| | [iButtonCode](#iButtonCode) | code | Code to be written. | | int8_t | type | iButton (re)writable tag type, use library constants. Default value is [IBUTTON_UNKNOWN](#IBUTTON_UNKNOWN). | | bool | check | Setting to _false_ disables most checking done before trying to write. Default value is _true_. | -**Returns grouped** +**Returns _type int8_t_ - grouped values** + | value | description | |:-----:|:------------| | 1 | Writing procedure finished successfully | @@ -206,15 +239,17 @@ This function supports writing a new iButton identification code to tag models R | -11 to -19 | Problem related to supplied type | | -21 to -29 | Failure of actual writing | -**Returns specific** +**Returns _type int8_t_ - specific values** + | value | description | |:-----:|:------------| | 1 | Writing procedure finished successfully | 0 | No iButton detected at some time during procedure -| -1 | iButton code invalid, checksum failed +| -1 | iButton code invalid, checksum failed, update with [updateChecksum](#updateChecksum) | -2 | iButton code invalid, all zeros | -11 | iButton writable type invalid, supplied value out of range | -12 | iButton writable type not detectable, supply specific type constant | -13 | iButton writable type incorrect, unexpected response while testing | -21 | Writing code failed, code read after writing procedure is not equal | -22 | Writing code failed, unexpected response while writing +counter diff --git a/doc/examples.md b/doc/examples.md new file mode 100644 index 0000000..15a0e14 --- /dev/null +++ b/doc/examples.md @@ -0,0 +1,51 @@ +# [📚 Arduino iButtonTag Library](https://vdwulp.github.io/iButtonTag/) + +## 🔗 Quick links +- [General information](https://vdwulp.github.io/iButtonTag/) +- [Reference documentation](https://vdwulp.github.io/iButtonTag/REFERENCE.html) +- [GitHub repository](https://github.com/vdwulp/iButtonTag) +- [Download latest release](https://github.com/vdwulp/iButtonTag/releases/latest) + +## Examples +- After installation in the Arduino IDE examples are available via _File-menu > Examples > iButtonTag_. +- A short description of all available examples follows below. + + +### Simple +[source code](https://github.com/vdwulp/iButtonTag/blob/main/examples/Simple/Simple.ino) + +Simple example showing basic usage of the library to read an identification code from an iButton tag. + +Uses functions [readCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#readCode) and [printCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#printCode). + + +### Single +[source code](https://github.com/vdwulp/iButtonTag/blob/main/examples/Single/Single.ino) + +Example showing usage of the library to read an identification code from an iButton tag and handle all possible status codes. + +Uses functions [readCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#readCode) and [printCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#printCode). + + +### Multiple +[source code](https://github.com/vdwulp/iButtonTag/blob/main/examples/Multiple/Multiple.ino) + +Example showing usage of the library to read identification codes from multiple iButton tags connected to the same data line. + +Uses functions [readCodes](https://vdwulp.github.io/iButtonTag/REFERENCE.html#readCodes), [nextCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#nextCode) and [printCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#printCode). + + +### Match +[source code](https://github.com/vdwulp/iButtonTag/blob/main/examples/Match/Match.ino) + +Example showing usage of the library to read an identification code from an iButton tag and check if it matches a predefined code. + +Uses functions [readCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#readCode), [equalCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#equalCode) and [printCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#printCode). + + +### WriteCode +[source code](https://github.com/vdwulp/iButtonTag/blob/main/examples/WriteCode/WriteCode.ino) + +Example showing usage of the library to _write_ an identification code to a (re)writable iButton tag and check if writing succeeded. + +Uses functions [readCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#readCode), [equalCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#equalCode), [printCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#printCode), [updateChecksum](https://vdwulp.github.io/iButtonTag/REFERENCE.html#updateChecksum) and [writeCode](https://vdwulp.github.io/iButtonTag/REFERENCE.html#writeCode). diff --git a/doc/probes.md b/doc/probes.md index 4d7fc22..a975fa5 100644 --- a/doc/probes.md +++ b/doc/probes.md @@ -1,4 +1,10 @@ -# [📚 Arduino iButtonTag Library](https://github.com/vdwulp/iButtonTag) +# [📚 Arduino iButtonTag Library](https://vdwulp.github.io/iButtonTag/) + +## 🔗 Quick links +- [General information](https://vdwulp.github.io/iButtonTag/) +- [Reference documentation](https://vdwulp.github.io/iButtonTag/REFERENCE.html) +- [GitHub repository](https://github.com/vdwulp/iButtonTag) +- [Download latest release](https://github.com/vdwulp/iButtonTag/releases/latest) ## iButton probes - iButton probes come in many forms, some have LED-indicator(s) with one or two colors. diff --git a/examples/Match/Match.ino b/examples/Match/Match.ino index f112f91..1fac82c 100644 --- a/examples/Match/Match.ino +++ b/examples/Match/Match.ino @@ -1,6 +1,7 @@ -// SA van der Wulp | March 26, 2025 +// SA van der Wulp | April 14, 2025 // Copyright (c) 2025 | MIT License -// https://github.com/vdwulp/iButtonTag +// https://vdwulp.github.io/iButtonTag + // Include the library #include @@ -15,9 +16,11 @@ iButtonTag ibutton( PIN_PROBE ); * The setup function. */ void setup( void ) { + // Start serial port Serial.begin( 9600 ); Serial.println( "iButtonTag Library Demo" ); + } /* @@ -28,6 +31,7 @@ void loop(void) // Variable to store identification code iButtonCode code; + // Pre-defined code to match, change to the code _you_ want to match! iButtonCode matchcode = { 0x01, 0x5F, 0x94, 0xC5, 0x01, 0x00, 0x00, 0x8C }; @@ -35,7 +39,7 @@ void loop(void) Serial.println( "Reading... " ); int8_t status = ibutton.readCode( code ); - if ( status > 0 ) { // iButton code read succesfully + if ( status > 0 ) { // iButton code read successfully Serial.print( "iButton code read: " ); ibutton.printCode( code ); // Variable _code_ contains the ID-code diff --git a/examples/Multiple/Multiple.ino b/examples/Multiple/Multiple.ino index 962c0f5..3455c76 100644 --- a/examples/Multiple/Multiple.ino +++ b/examples/Multiple/Multiple.ino @@ -1,6 +1,7 @@ -// SA van der Wulp | March 26, 2025 +// SA van der Wulp | April 14, 2025 // Copyright (c) 2025 | MIT License -// https://github.com/vdwulp/iButtonTag +// https://vdwulp.github.io/iButtonTag + // Include the library #include @@ -15,9 +16,11 @@ iButtonTag ibutton( PIN_PROBE ); * The setup function. */ void setup( void ) { + // Start serial port Serial.begin( 9600 ); Serial.println( "iButtonTag Library Demo" ); + } /* @@ -30,6 +33,7 @@ void setup( void ) { */ void loop(void) { + // Variable to store identification code iButtonCode code; @@ -46,11 +50,11 @@ void loop(void) // Keep trying to get next code until status indicates there are no more codes while ( status = ibutton.nextCode( code ) != 0 ) { - // Variable /status/ will now indicate the succes + // Variable _status_ will now indicate success/failure switch( status ) { - case 1: // Succes - Serial.print( "iButton code read succesfully: " ); + case 1: // Success + Serial.print( "iButton code read successfully: " ); ibutton.printCode( code ); // Variable _code_ contains the ID-code Serial.println(); break; diff --git a/examples/Simple/Simple.ino b/examples/Simple/Simple.ino index 4a2b86f..e7b35d7 100644 --- a/examples/Simple/Simple.ino +++ b/examples/Simple/Simple.ino @@ -1,6 +1,7 @@ -// SA van der Wulp | March 26, 2025 +// SA van der Wulp | April 14, 2025 // Copyright (c) 2025 | MIT License -// https://github.com/vdwulp/iButtonTag +// https://vdwulp.github.io/iButtonTag + #include // Include the library @@ -9,19 +10,23 @@ iButtonTag ibutton( PIN_PROBE ); // Setup iButtonTag on the pin void setup(void) { + Serial.begin( 9600 ); Serial.println( "iButtonTag Library Demo" ); + } void loop(void) { + iButtonCode code; // Variable to store ID-code Serial.println( "Reading... " ); int8_t status = ibutton.readCode( code ); // Try to read ID-code - if ( status > 0 ) { // Code read succesfully + if ( status > 0 ) { // Code read successfully Serial.print( "iButton code read: " ); ibutton.printCode( code ); // ID-code is in variable _code_ Serial.println(); } + } diff --git a/examples/Single/Single.ino b/examples/Single/Single.ino index 5636ae6..d050010 100644 --- a/examples/Single/Single.ino +++ b/examples/Single/Single.ino @@ -1,6 +1,7 @@ -// SA van der Wulp | April 4, 2025 +// SA van der Wulp | April 14, 2025 // Copyright (c) 2025 | MIT License -// https://github.com/vdwulp/iButtonTag +// https://vdwulp.github.io/iButtonTag + // Include the library #include @@ -15,9 +16,11 @@ iButtonTag ibutton( PIN_PROBE ); * The setup function. */ void setup( void ) { + // Start serial port Serial.begin( 9600 ); Serial.println( "iButtonTag Library Demo" ); + } /* @@ -25,6 +28,7 @@ void setup( void ) { */ void loop(void) { + // Variable to store identification code iButtonCode code; @@ -32,11 +36,11 @@ void loop(void) Serial.print( "Reading... " ); int8_t status = ibutton.readCode( code ); - // Variable _status_ will now indicate the succes + // Variable _status_ will now indicate success/failure switch( status ) { - case 1: // Succes - Serial.print( "iButton code read succesfully: " ); + case 1: // Success + Serial.print( "iButton code read successfully: " ); ibutton.printCode( code ); // Variable _code_ contains the ID-code Serial.println(); break; diff --git a/examples/WriteCode/WriteCode.ino b/examples/WriteCode/WriteCode.ino index b9b1601..661909e 100644 --- a/examples/WriteCode/WriteCode.ino +++ b/examples/WriteCode/WriteCode.ino @@ -1,6 +1,7 @@ -// SA van der Wulp | April 4, 2025 +// SA van der Wulp | April 14, 2025 // Copyright (c) 2025 | MIT License -// https://github.com/vdwulp/iButtonTag +// https://vdwulp.github.io/iButtonTag + #include // Include the library @@ -64,9 +65,9 @@ void setup(void) { // // int8_t status = ibutton.writeCode( newcode, IBUTTON_TM01 ); - // Evaluate succes/failure based on returned status + // Evaluate success/failure based on returned status Serial.print( "Writing procedure finished " ); - if ( status == 1 ) Serial.print( "succesfully"); + if ( status == 1 ) Serial.print( "successfully"); else Serial.print( "with an error" ) ; Serial.print( " (status " ); Serial.print( status ); @@ -78,7 +79,7 @@ void setup(void) { ibutton.printCode( currentcode ); Serial.println(); - // Evaluate succes/failure based on iButtoncode read from tag + // Evaluate success/failure based on iButtoncode read from tag Serial.print( "Current code on tag *is" ); if ( !ibutton.equalCode( oldcode, currentcode ) ) Serial.print( " not" ); Serial.println( "* the same as before writing" ); @@ -105,7 +106,7 @@ iButtonTag Library Demo Old code on iButton tag : 01 0B 15 1F 29 33 3D E8 New code for iButton tag : 01 23 45 67 89 AB CD E9 -Writing procedure finished succesfully (1) +Writing procedure finished successfully (1) Current code on iButton tag : 01 23 45 67 89 AB CD E9 Current code on tag *is not* the same as before writing Current code on tag *is* the same as new code supplied diff --git a/library.json b/library.json index f47ace1..6e4cccb 100644 --- a/library.json +++ b/library.json @@ -1,16 +1,16 @@ { "name": "iButtonTag", - "version": "1.1.0", + "version": "1.1.1", "description": "Handle iButton identification tags such as DS1990A, TM1990A, RW1990, RW1990.2, RW2004, TM2004, TM01 and many others. Supports writing a new identification code to (re)writable iButton tags and reading of multiple iButton probes on the same data line.", - "keywords": "iButton, tag, identification, id, code, probe, onewire, 1-wire, bus, write, rewrite, writable, rewritable, DS1990, DS1990A, DS1990R, TM1990A, RW1990, RW1990.1, RW1990v1, TM08, TM08v2, RW1990.2, RW1990v2, RW2004, TM2004, TM01, TM01C", - "homepage": "https://github.com/vdwulp/iButtonTag", + "keywords": "iButton, tag, key, identification, id, probe, write, rewrite, writable, rewritable, DS1990, RW1990, RW1990.2, RW2004, TM01, TM08, TM1990, TM2004", + "homepage": "https://vdwulp.github.io/iButtonTag/", "repository": { "type": "git", "url": "https://github.com/vdwulp/iButtonTag.git" }, "authors": { "name": "SA van der Wulp", - "url": "https://github.com/vdwulp/iButtonTag", + "url": "https://vdwulp.github.io", "maintainer": true }, "license": "MIT", diff --git a/library.properties b/library.properties index 802e29d..4632dd5 100644 --- a/library.properties +++ b/library.properties @@ -1,10 +1,10 @@ name=iButtonTag -version=1.1.0 +version=1.1.1 author=SA van der Wulp maintainer=SA van der Wulp sentence=Handle iButton identification tags such as DS1990A, TM1990A, RW1990, RW1990.2, RW2004, TM2004, TM01 and many others. paragraph=Supports writing a new identification code to (re)writable iButton tags and reading of multiple iButton probes on the same data line. category=Other -url=https://github.com/vdwulp/iButtonTag +url=https://vdwulp.github.io/iButtonTag/ architectures=* depends=OneWire diff --git a/src/iButtonTag.cpp b/src/iButtonTag.cpp index 70316a5..d8fcb40 100644 --- a/src/iButtonTag.cpp +++ b/src/iButtonTag.cpp @@ -1,42 +1,42 @@ -// SA van der Wulp | April 3, 2025 +// SA van der Wulp | April 14, 2025 // Copyright (c) 2025 | MIT License -// https://github.com/vdwulp/iButtonTag +// https://vdwulp.github.io/iButtonTag + + +/* + * Reference documentation available in doc-folder of library. Only short + * descriptions in this source file. Full documentation can be viewed online + * via: https://vdwulp.github.io/iButtonTag/REFERENCE.html + */ + #include "iButtonTag.h" -// Constructs an iButtonTag object linked to the supplied pin. + +// PUBLIC FUNCTIONS + +/* + * Constructs an iButtonTag object linked to the supplied pin. + * + * See https://vdwulp.github.io/iButtonTag/REFERENCE.html#constructor + */ iButtonTag::iButtonTag( uint8_t pin ) { _wire = new OneWire( pin ); } -// Reads one single iButton identifying code from the OneWire. -// -// When multiple iButtons are connected to the OneWire this function will return -// an invalid reading because of a CRC8 failure (return value -2) caused by -// colliding responses. If there is _any_ possibility multiple iButtons are -// connected, use _getCodes_ instead. -// -// DS1990 iButton tags can't be used with multiple tags on a single 1-Wire bus -// and require special handling. This function facilitates compatibility with -// the DS1990 iButton Tags when argument _old_ is set to true. However, this -// reduces compatibility with other iButton tags: DS1990A, DS1990R and TM1990A -// will still be handled correctly (they offer backwards compatibility), but -// other 1-Wire devices (including iButtons) won't and may even show unexpected -// behaviour. -// -// Return values: -// 1 - Next iButton read succesfully, code array filled with identifying code -// 0 - No more iButtons detected, code array is unchanged -// -1 - Invalid iButton code read, CRC8 failed, code array with invalid bytes -// -2 - Invalid iButton code read, all zeros, code array with invalid bytes +/* + * Reads one single iButtonCode from the data line. + * + * See https://vdwulp.github.io/iButtonTag/REFERENCE.html#readCode + */ int8_t iButtonTag::readCode( uint8_t* code, bool old /* = false */ ) { - // RESET the OneWire + // RESET the data line // - Connected devices will assert presence with a pulse // - Returns 1 if at least one device is present, 0 otherwise // - Exit with status 0 when no device asserted presence if ( _wire -> reset() == 0 ) return 0; - // Issue READ ROM command to the OneWire + // Issue READ ROM command to the data line // - 0x33 is protocol standard // - 0x0F for compatibility with DS1990 _wire -> write( old ? 0x0F : 0x33 ); @@ -47,48 +47,31 @@ int8_t iButtonTag::readCode( uint8_t* code, bool old /* = false */ ) { return testCode( code ); } -// Starts the search for multiple iButton identifying codes on the OneWire. -// -// Resets the domain to search for iButton identifying codes. This function is -// needed to start searching for codes _again_. It's not really needed the first -// time, though it's good practice to always use it before enumerating codes -// with the nextCode function. -// -// Return values: -// 1 - At least one iButton detected, enumerate with nextCode function -// 0 - No iButton detected +/* + * Starts the search for multiple iButtonCode's on the data line. + * + * See https://vdwulp.github.io/iButtonTag/REFERENCE.html#readCodes + */ int8_t iButtonTag::readCodes() { - // RESET the OneWire + // RESET the data line // - Connected devices will assert presence with a pulse // - Returns 1 if at least one device is present, 0 otherwise // - Exit with status 0 when no device asserted presence if ( _wire -> reset() == 0 ) return 0; - // Reset search domain on OneWire + // Reset search domain on data line _wire -> reset_search(); return 1; } -// Continues the search for multiple iButton identifying codes on the OneWire. -// -// Start the search for multiple iButton identyfying codes with the readCodes -// function. The is this function to enumerate all iButton identifying codes on -// the OneWire. -// -// A return value 0 means searching finished succesfully, but there are no more -// iButtons on the OneWire. Negative return values indicate a problem during the -// search (mostly due to movement of the iButton on the reader), but additional -// calls to the function _may_ yield new iButton identifying codes. However, the -// overall result will be unreliable. -// -// Return values: -// 1 - Next iButton read succesfully, code array filled with identifying code -// 0 - No more iButtons detected, code array is unchanged -// -1 - Invalid iButton code read, CRC8 failed, code array with invalid bytes -// -2 - Invalid iButton code read, all zeros, code array with invalid bytes +/* + * Continues the search for multiple iButtonCode's on the data line. + * + * See https://vdwulp.github.io/iButtonTag/REFERENCE.html#nextCode + */ int8_t iButtonTag::nextCode( uint8_t* code ) { - // Search for the next identification code on OneWire + // Search for the next iButtonCode on data line // - Returns 1 when a code is found, 0 when there are no more iButtons // - Exit with status 0 when no more iButtons are detected if ( _wire -> search( code ) == 0 ) return 0; @@ -97,12 +80,11 @@ int8_t iButtonTag::nextCode( uint8_t* code ) { return testCode( code ); } -// Tests iButtonCode for validity. -// -// Return values: -// 1 - iButton code valid -// -1 - iButton code invalid, CRC8 failed -// -2 - iButton code invalid, all zeros +/* + * Tests iButtonCode for validity. + * + * See https://vdwulp.github.io/iButtonTag/REFERENCE.html#testCode + */ int8_t iButtonTag::testCode( const uint8_t* code ) { // Check if last byte of code is CRC8 of first 7 bytes // - Exit with status -1 when CRC8 fails @@ -113,10 +95,10 @@ int8_t iButtonTag::testCode( const uint8_t* code ) { // it doesn't even say values 0xFF (observed when no iButton is present) or // 0x00 (observed when reader is shorted) are invalid. In theory these values // are possible, as long as an iButton asserts presence in response to the - // OneWire RESET command. + // RESET command. // However, sliding around an iButton on a reader may lead to a code of 8 0x00 - // bytes being 'succesfully' read (how? apparently reset is succesfull, then a - // short occurs?). Then, the CRC8 is also correct (0x00), so this case needs + // bytes being 'successfully' read (how? apparently reset is successfull, then + // a short occurs?). Then, the CRC8 is also correct (0x00), so this case needs // an extra check to raise an error status. // Because it _is_ theoretically possible to have a FAMILY CODE of 0x00, we // need to check all code positions and thus only exclude one specific case. @@ -132,22 +114,21 @@ int8_t iButtonTag::testCode( const uint8_t* code ) { return 1; } -// Tests if two iButtonCode's are equal. -// -// Return values: -// true - The two iButtonCode's are equal -// false - The two iButtonCode's are *NOT* equal +/* + * Tests if two iButtonCode's are equal. + * + * See https://vdwulp.github.io/iButtonTag/REFERENCE.html#equalCode + */ bool iButtonTag::equalCode( const uint8_t* a, const uint8_t* b ) { for( uint8_t i = 0; i < 8; i++ ) if ( a[i] != b[i] ) return false; return true; } -// Prints iButtonCode to Serial as hexadecimal byte values. -// -// Serial must be initialised in the main code first. By default the bytes are -// printed as received from the iButton (reverse = false). The order can be -// reversed (reverse = true) to match the sequence fysically engraved on many -// iButtons. +/* + * Prints iButtonCode to Serial as hexadecimal byte values. + * + * See https://vdwulp.github.io/iButtonTag/REFERENCE.html#printCode + */ void iButtonTag::printCode( const uint8_t* code, bool reverse /* = false */ ) { for( uint8_t i = 0; i < 8; i++ ) { uint8_t j = reverse ? 7 - i : i; @@ -157,31 +138,20 @@ void iButtonTag::printCode( const uint8_t* code, bool reverse /* = false */ ) { } } -// Updates checksum of iButtonCode to a correct value. +/* + * Updates checksum of iButtonCode to the correct value. + * + * See https://vdwulp.github.io/iButtonTag/REFERENCE.html#updateChecksum + */ void iButtonTag::updateChecksum( uint8_t* code ) { code[7] = calculateChecksum( code ); } -// Detects type of (re)writable iButton tag. -// -// Performs multiple tests to check for known reponses of (re)writable iButton -// tag types. If a detectable (re)writable type is found, the return value -// indicates the specific model. All supported (re)writable types are defined -// as _iButton (re)writable tag type_ constants, like IBUTTON_RW1990V1, -// IBUTTON_RW1990V2, IBUTTON_RW2004 or IBUTTON_TM01. -// -// A return value of _iButton (re)writable type_ constant IBUTTON_UNKNOWN may -// indicate one of the following: -// - iButton tag _is not_ a (re)writable tag. However, it _is_ readable. -// - iButton tag _is not_ of a (re)writable type supported by this library. -// However, it _is_ readable. -// - iButton tag _is_ of a (re)writable type supported by this library. However, -// this specific type _is not_ detectable, like IBUTTON_TM01. -// -// Return values: -// >0 - iButton writable type found as indicated by type constant -// 0 - iButton writable type unknown, no detectable writable type found -// -1 - No iButton detected +/* + * Detects type of (re)writable iButton tag. + * + * See https://vdwulp.github.io/iButtonTag/REFERENCE.html#detectWritableType + */ int8_t iButtonTag::detectWritableType() { int8_t t = isWritableTypeRW1990V1(); if ( t == 1 ) return IBUTTON_RW1990V1; // Detected @@ -198,46 +168,11 @@ int8_t iButtonTag::detectWritableType() { return IBUTTON_UNKNOWN; } -// Writes a new iButton identifying code to a (re)writable tag. -// -// Strong recommendations, please read carefully: -// - It is recommended to have _only one_ iButton probe/tag connected to the -// data line when writing. Some iButton (re)writable tag types will allow -// multiple tags to be written at the same time, but it may lead to failure. -// - It is _also_ recommended to only supply the new code as an argument. The -// function will try to detect a (re)writable iButton tag type. If it fails, -// you should seriously check if the tag really is a (re)writable tag of a -// type supported by this library. As iButton tag type TM01 is non-detectable, -// this type will never be detected. In this case supply iButton (re)writable -// tag type constant IBUTTON_TM01. To override auto-detection in other cases, -// you can also supply an iButton (re)writable tag type constant. -// - It is _also_ recommended to have checking _on_ like the default, making -// sure as much checks as possible are done before actually writing a new code -// to the iButton tag. As some writeble tags are just _write once_ there is a -// risk in trying to write without appropriate checking. -// -// This function supports writing a new iButton identification code to tag -// models RW1990, RW1990.1, ТM08, ТM08v2 (type IBUTTON_RW1990V1), RW1990v2, -// RW1990.2 (type IBUTTON_RW1990V2), RW2004, TM2004 (type IBUTTON_RW2004), TM01 -// and TM01C (type IBUTTON_TM01). -// -// Return values, grouped: -// 1 - Writing procedure finished successfully -// 0 - No iButton detected at some time during procedure -// -1 to -9 - Problem related to supplied code -// -11 to -19 - Problem related to supplied type -// -21 to -29 - Failure of actual writing -// -// Return values, specific: -// 1 - Writing procedure finished successfully -// 0 - No iButton detected at some time during procedure -// -1 - iButton code invalid, CRC8 failed -// -2 - iButton code invalid, all zeros -// -11 - iButton writable type invalid, supplied value out of range -// -12 - iButton writable type not detectable, supply specific type constant -// -13 - iButton writable type incorrect, unexpected response while testing -// -21 - Writing code failed, code read after writing procedure is not equal -// -22 - Writing code failed, unexpected response while writing +/* + * Writes a new iButtonCode to a (re)writable tag. + * + * See https://vdwulp.github.io/iButtonTag/REFERENCE.html#writeCode + */ int8_t iButtonTag::writeCode( const uint8_t* code, int8_t type /* = IBUTTON_UNKNOWN */, bool check /* = true */ ) { @@ -297,23 +232,30 @@ int8_t iButtonTag::writeCode( const uint8_t* code, break; } - // Writing procedure finished, check success by reading code - iButtonCode result; - readCode( result ); - if ( !equalCode( result, code ) ) return -21; + // Writing procedure finished, check success if checking is on + if ( check ) { + iButtonCode result; + readCode( result ); + if ( !equalCode( result, code ) ) return -21; + } return 1; } -// Performs test to determine if iButton tag is of (re)writable type RW1990v1. -// -// This type includes iButton tag models RW1990, RW1990.1, ТM08 and ТM08v2. -// -// Return values: -// 1 - iButton _is_ of (re)writable type RW1990v1 -// 0 - iButton _is not_ of (re)writable type RW1990v1 -// -1 - No iButton detected during test + +// PRIVATE FUNCTIONS + +/* + * Performs test to determine if iButton tag is of (re)writable type RW1990v1. + * + * This type includes iButton tag models RW1990, RW1990.1, ТM08 and ТM08v2. + * + * Return values: + * 1 - iButton _is_ of (re)writable type RW1990v1 + * 0 - iButton _is not_ of (re)writable type RW1990v1 + * -1 - No iButton detected during test + */ int8_t iButtonTag::isWritableTypeRW1990V1() { // Write flag value 1 (writing disabled) if ( _wire -> reset() == 0 ) return -1; @@ -329,14 +271,16 @@ int8_t iButtonTag::isWritableTypeRW1990V1() { return 0; } -// Performs test to determine if iButton tag is of (re)writable type RW1990v2. -// -// This type includes iButton tag models RW1990v2 and RW1990.2. -// -// Return values: -// 1 - iButton _is_ of (re)writable type RW1990v2 -// 0 - iButton _is not_ of (re)writable type RW1990v2 -// -1 - No iButton detected during test +/* + * Performs test to determine if iButton tag is of (re)writable type RW1990v2. + * + * This type includes iButton tag models RW1990v2 and RW1990.2. + * + * Return values: + * 1 - iButton _is_ of (re)writable type RW1990v2 + * 0 - iButton _is not_ of (re)writable type RW1990v2 + * -1 - No iButton detected during test + */ int8_t iButtonTag::isWritableTypeRW1990V2() { // Write flag value 1 (writing enabled) if ( _wire -> reset() == 0 ) return -1; @@ -359,14 +303,16 @@ int8_t iButtonTag::isWritableTypeRW1990V2() { return 0; } -// Performs test to determine if iButton tag is of (re)writable type RW2004. -// -// This type includes iButton tag models RW2004 and TM2004. -// -// Return values: -// 1 - iButton _is_ of (re)writable type RW2004 -// 0 - iButton _is not_ of (re)writable type RW2004 -// -1 - No iButton detected during test +/* + * Performs test to determine if iButton tag is of (re)writable type RW2004. + * + * This type includes iButton tag models RW2004 and TM2004. + * + * Return values: + * 1 - iButton _is_ of (re)writable type RW2004 + * 0 - iButton _is not_ of (re)writable type RW2004 + * -1 - No iButton detected during test + */ int8_t iButtonTag::isWritableTypeRW2004() { // Send command 0xAA to read status register starting address 0x00 0x00. The // response of RW2004/TM2004 is CRC8 of those three bytes, followed by one @@ -392,20 +338,22 @@ int8_t iButtonTag::isWritableTypeRW2004() { return result; } -// Writes new iButtonCode to (re)writable types RW1990V1, RW1990V2 and TM01 -// without checks. -// -// These types include iButton tag models RW1990, RW1990.1, ТM08, ТM08v2 (type -// IBUTTON_RW1990V1), RW1990v2, RW1990.2 (type IBUTTON_RW1990V2), TM01 and TM01C -// (type IBUTTON_TM01). -// -// When using this function, apart from the new iButtonCode, the byte-code for -// the type-specific write-enable command needs to be supplied. For RW1990V1 all -// written bits need to be inverted. - -// Return values: -// 1 - Writing procedure finished successfully -// 0 - No iButton detected at some time during procedure +/* + * Writes new iButtonCode to (re)writable types RW1990V1, RW1990V2 and TM01 + * without checks. + * + * These types include iButton tag models RW1990, RW1990.1, ТM08, ТM08v2 (type + * IBUTTON_RW1990V1), RW1990v2, RW1990.2 (type IBUTTON_RW1990V2), TM01 and TM01C + * (type IBUTTON_TM01). + * + * When using this function, apart from the new iButtonCode, the byte-code for + * the type-specific write-enable command needs to be supplied. For RW1990V1 all + * written bits need to be inverted. + * + * Return values: + * 1 - Writing procedure finished successfully + * 0 - No iButton detected at some time during procedure + */ int8_t iButtonTag::writeCodeCommon( const uint8_t* code, uint8_t enablecommand, bool invert /* = false */ ) { // Set flag value to [writing enabled] @@ -424,14 +372,16 @@ int8_t iButtonTag::writeCodeCommon( const uint8_t* code, uint8_t enablecommand, return 1; } -// Writes new iButtonCode to (re)writable type RW2004 without checks. -// -// This type includes iButton tag models RW2004 and TM2004. -// -// Return values: -// 1 - Writing procedure finished successfully -// 0 - No iButton detected at some time during procedure -// -22 - Writing code failed, unexpected response while writing +/* + * Writes new iButtonCode to (re)writable type RW2004 without checks. + * + * This type includes iButton tag models RW2004 and TM2004. + * + * Return values: + * 1 - Writing procedure finished successfully + * 0 - No iButton detected at some time during procedure + * -22 - Writing code failed, unexpected response while writing + */ int8_t iButtonTag::writeCodeRW2004( const uint8_t* code ) { // Send command 0x3C to start writing at address 0x00 0x00. Then the code is // written byte-by-byte: write byte > read value > send program pulse > read @@ -462,12 +412,14 @@ int8_t iButtonTag::writeCodeRW2004( const uint8_t* code ) { return 1; } -// Writes a byte to the OneWire with a delay after each bit. -// -// By default the byte is written _as is_, if indicated all bits are inverted. -// -// The OneWire data line stays _high_ after this function. Calling procedures -// should perform other actions on the OneWire or _depower_. +/* + * Writes a byte to the data line with a delay after each bit. + * + * By default the byte is written _as is_, if indicated all bits are inverted. + * + * The data line stays _high_ after this function. Calling procedures should + * perform other actions on the data line or _depower_. + */ void iButtonTag::writeByteDelayed( uint8_t b, bool invert /* = false */ ) { // Invert byte if specified if ( invert ) b = ~b; @@ -477,10 +429,12 @@ void iButtonTag::writeByteDelayed( uint8_t b, bool invert /* = false */ ) { } } -// Writes a bit to the OneWire with a delay after it. -// -// The OneWire data line stays _high_ after this function. Calling procedures -// should perform other actions on the OneWire or _depower_. +/* + * Writes a bit to the data line with a delay after it. + * + * The data line stays _high_ after this function. Calling procedures should + * perform other actions on the data line or _depower_. + */ void iButtonTag::writeBitDelayed( uint8_t b ) { // b can only be 0 or 1 !! // Write as always _wire -> write_bit( b ); @@ -488,9 +442,11 @@ void iButtonTag::writeBitDelayed( uint8_t b ) { // b can only be 0 or 1 !! delay( 10 ); } -// Calculates checksum of iButtonCode. -// -// Returns correct checksum value +/* + * Calculates checksum of iButtonCode. + * + * Returns correct checksum value + */ uint8_t iButtonTag::calculateChecksum( const uint8_t* code ) { return OneWire::crc8( code, 7 ); } diff --git a/src/iButtonTag.h b/src/iButtonTag.h index e9a202c..075b844 100644 --- a/src/iButtonTag.h +++ b/src/iButtonTag.h @@ -1,6 +1,7 @@ -// SA van der Wulp | April 3, 2025 +// SA van der Wulp | April 14, 2025 // Copyright (c) 2025 | MIT License -// https://github.com/vdwulp/iButtonTag +// https://vdwulp.github.io/iButtonTag + #ifndef iButtonTag_h #define iButtonTag_h @@ -20,6 +21,7 @@ // Type definition typedef uint8_t iButtonCode[8]; +// Class definition class iButtonTag { public: diff --git a/test/TEST_iButtonTag.cpp b/test/TEST_iButtonTag.cpp index 488041e..9b50317 100644 --- a/test/TEST_iButtonTag.cpp +++ b/test/TEST_iButtonTag.cpp @@ -1,6 +1,7 @@ -// SA van der Wulp | April 2, 2025 +// SA van der Wulp | April 14, 2025 // Copyright (c) 2025 | MIT License -// https://github.com/vdwulp/iButtonTag +// https://vdwulp.github.io/iButtonTag + #include #include @@ -23,11 +24,6 @@ unittest( iButtonTag_basics ) { iButtonTag ibutton( 2 ); // Function readCode - // Return values: - // 1 - Next iButton read succesfully, code array filled with identifying code - // 0 - No more iButtons detected, code array is unchanged - // -1 - Invalid iButton code read, CRC8 failed, code array with invalid bytes - // -2 - Invalid iButton code read, all zeros, code array with invalid bytes assertEqual( 0, ibutton.readCode( code ) ); // No iButton detected assertTrue( ibutton.equalCode( code, codezero ) ); @@ -38,34 +34,19 @@ unittest( iButtonTag_basics ) { assertTrue( ibutton.equalCode( code, codezero ) ); // Function readCodes - // Return values: - // 1 - At least one iButton detected, enumerate with nextCode function - // 0 - No iButton detected assertEqual( 0, ibutton.readCodes() ); // No iButton detected // Function nextCode - // Return values: - // 1 - Next iButton read succesfully, code array filled with identifying code - // 0 - No more iButtons detected, code array is unchanged - // -1 - Invalid iButton code read, CRC8 failed, code array with invalid bytes - // -2 - Invalid iButton code read, all zeros, code array with invalid bytes assertEqual( 0, ibutton.nextCode( code ) ); // No iButton detected assertTrue( ibutton.equalCode( code, codezero ) ); // Function testCode - // Return values: - // 1 - iButton code valid - // -1 - iButton code invalid, CRC8 failed - // -2 - iButton code invalid, all zeros assertEqual( -2, ibutton.testCode( code ) ); // Invalid code, all zeros assertEqual( -2, ibutton.testCode( codezero ) ); // Invalid code, all zeros assertEqual( -1, ibutton.testCode( codecrcfail ) ); // Invalid code, CRC failed assertEqual( 1, ibutton.testCode( codecrc ) ); // Valid code // Function equalCode - // Return values: - // true - The two iButtonCode's are equal - // false - The two iButtonCode's are *NOT* equal assertFalse( ibutton.equalCode( codezero, codecrc ) ); assertFalse( ibutton.equalCode( codezero, codecrcfail ) ); assertFalse( ibutton.equalCode( codecrc, codecrcfail ) ); @@ -102,23 +83,9 @@ unittest( iButtonTag_basics ) { assertEqual( "47 3D 33 29 1F 15 0B 01", state -> serialPort[0].dataOut ); // Function detectWritableType - // Return values: - // >0 - iButton writable type found as indicated by type constant - // 0 - iButton writable type unknown, no detectable writable type found - // -1 - No iButton detected assertEqual( -1, ibutton.detectWritableType() ); // Function writeCode - // Return values: - // 1 - Writing procedure finished successfully - // 0 - No iButton detected at some time during procedure - // -1 - iButton code invalid, CRC8 failed - // -2 - iButton code invalid, all zeros - // -11 - iButton writable type invalid, supplied value out of range - // -12 - iButton writable type not detectable, supply specific type constant - // -13 - iButton writable type incorrect, unexpected response while testing - // -21 - Writing code failed, code read after writing procedure is not equal - // -22 - Writing code failed, unexpected response while writing assertEqual( 0, ibutton.writeCode( codecrc ) ); assertEqual( -1, ibutton.writeCode( codecrcfail ) ); assertEqual( -2, ibutton.writeCode( codezero ) );