Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Onewire by
Diff: Onewire.cpp
- Revision:
- 2:b7ee49dbd7ef
- Parent:
- 1:8e9464e05ddf
- Child:
- 3:712bf8967b68
diff -r 8e9464e05ddf -r b7ee49dbd7ef Onewire.cpp
--- a/Onewire.cpp Sat Feb 21 17:16:16 2015 +0000
+++ b/Onewire.cpp Sat Feb 21 17:26:40 2015 +0000
@@ -1,18 +1,23 @@
#include "Onewire.h"
-Onewire::Onewire(PinName oneBus):oneBus_(oneBus){
-
+Onewire::Onewire(PinName oneBus):oneBus_(oneBus)
+{
}
-void Onewire::writeBit(int bit) {
+
+void Onewire::writeBit(int bit)
+{
bit = bit & 0x01;
- if (bit) {
+ if (bit)
+ {
// Write '1' bit
oneBus_.output();
oneBus_ = 0;
wait_us(5);
oneBus_.input();
wait_us(60);
- } else {
+ }
+ else
+ {
// Write '0' bit
oneBus_.output();
oneBus_ = 0;
@@ -22,7 +27,8 @@
}
}
-int Onewire::readBit() {
+int Onewire::readBit()
+{
char result;
oneBus_.output();
@@ -33,22 +39,25 @@
result = oneBus_.read();
wait_us(55);
return result;
-
}
-int Onewire::init() {
+int Onewire::init()
+{
oneBus_.output();
oneBus_ = 0;
wait_us(480);
oneBus_.input();
wait_us(60);
- if (oneBus_.read() == 0) {
+ if (oneBus_.read() == 0)
+ {
wait(0.0001);
return 1;
}
return 0;
}
-int Onewire::readByte() {
+
+int Onewire::readByte()
+{
int result = 0;
for (int loop = 0; loop < 8; loop++) {
@@ -61,7 +70,9 @@
}
return result;
}
-void Onewire::writeByte(char data) {
+
+void Onewire::writeByte(char data)
+{
// Loop to write each bit in the byte, LS-bit first
for (int loop = 0; loop < 8; loop++) {
writeBit(data & 0x01);
@@ -70,7 +81,9 @@
data >>= 1;
}
}
-unsigned char Onewire::CRC(unsigned char* addr, unsigned char len) {
+
+unsigned char Onewire::CRC(unsigned char* addr, unsigned char len)
+{
unsigned char i, j;
unsigned char crc = 0;
@@ -83,7 +96,6 @@
inbyte >>= 1;
}
}
-
return crc;
}
@@ -93,16 +105,15 @@
//
void Onewire::reset_search()
{
- // reset the search state
- _search_LastDiscrepancy = 0;
- _search_LastDeviceFlag = false;
- _search_LastFamilyDiscrepancy = 0;
- for(int i = 7; ; i--) {
- _search_ROM_NO[i] = 0;
- if ( i == 0) break;
- }
+ // reset the search state
+ _search_LastDiscrepancy = 0;
+ _search_LastDeviceFlag = false;
+ _search_LastFamilyDiscrepancy = 0;
+ for(int i = 7; i >= 0; i--)
+ _search_ROM_NO[i] = 0;
}
+// Search Copied from Arduino code
//
// Perform a search. If this function returns a '1' then it has
// enumerated the next device and you may retrieve the ROM from the
@@ -121,18 +132,12 @@
//
uint8_t Onewire::search(uint8_t *newAddr)
{
- uint8_t id_bit_number;
- uint8_t last_zero, rom_byte_number, search_result;
- uint8_t id_bit, cmp_id_bit;
-
- unsigned char rom_byte_mask, search_direction;
+ uint8_t id_bit_number = 1;
+ uint8_t last_zero = 0, rom_byte_number = 0, search_result = 0;
+ uint8_t id_bit = 0, cmp_id_bit = 0;
// initialize for search
- id_bit_number = 1;
- last_zero = 0;
- rom_byte_number = 0;
- rom_byte_mask = 1;
- search_result = 0;
+ unsigned char rom_byte_mask = 1, search_direction = 0;
// if the last call was not the last one
if (!_search_LastDeviceFlag)
