version one
Fork of SHTx by
Diff: i2c.cpp
- Revision:
- 2:cd8e210049b9
- Parent:
- 1:8465801be23f
--- a/i2c.cpp Fri Nov 19 16:24:46 2010 +0000 +++ b/i2c.cpp Mon Mar 04 19:11:37 2013 +0000 @@ -27,127 +27,127 @@ #include "i2c.hpp" namespace SHTx { - I2C::I2C(PinName sda, PinName scl) : - scl_pin(scl), sda_pin(sda), frequency(10) { - this->sda_pin.output(); - this->scl_pin.output(); - } + I2C::I2C(PinName sda, PinName scl) : + scl_pin(scl), sda_pin(sda), frequency(10) { + this->sda_pin.output(); + this->scl_pin.output(); + } - void - I2C::setFrequency(uint32_t hz) { - this->frequency = (1000000 / hz); - } + void + I2C::setFrequency(uint32_t hz) { + this->frequency = (1000000 / hz); + } - void - I2C::start(void) { - this->output(); - this->sda(1); - this->scl(1); - this->sda(0); - this->scl(0); - this->scl(1); - this->sda(1); - this->scl(0); - } + void + I2C::start(void) { + this->output(); + this->sda(1); + this->scl(1); + this->sda(0); + this->scl(0); + this->scl(1); + this->sda(1); + this->scl(0); + } - void - I2C::stop(void) { - this->output(); - this->sda(0); - this->scl(1); - this->sda(1); - } + void + I2C::stop(void) { + this->output(); + this->sda(0); + this->scl(1); + this->sda(1); + } - bool - I2C::wait(void) { - bool ack = false; - - this->input(); - for (uint8_t i = 0; i < 500 && !ack; i++) { - wait_ms(1); - ack = !this->sda_pin; - } - - return ack; - } - - void - I2C::reset(void) { - this->output(); - for (uint8_t i = 9; i; i--) { - this->shift_out(1); - } - this->start(); - this->scl(1); - } + bool + I2C::wait(void) { + bool ack = false; + + this->input(); + for (uint8_t i = 0; i < 500 && !ack; i++) { + wait_ms(1); + ack = !this->sda_pin; + } + + return ack; + } + + void + I2C::reset(void) { + this->output(); + for (uint8_t i = 9; i; i--) { + this->shift_out(1); + } + this->start(); + this->scl(1); + } - bool - I2C::write(uint8_t data) { - bool ack; + bool + I2C::write(uint8_t data) { + bool ack; - this->output(); - for (uint8_t i = 8; i; i--) { - this->shift_out(data & 0x80); - data <<= 1; - } + this->output(); + for (uint8_t i = 8; i; i--) { + this->shift_out(data & 0x80); + data <<= 1; + } - this->input(); - ack = !this->shift_in(); + this->input(); + ack = !this->shift_in(); - return ack; - } + return ack; + } - uint8_t - I2C::read(bool ack) { - uint8_t data = 0; - - this->input(); - for (uint8_t i = 8; i; i--) { - data <<= 1; - data |= this->shift_in(); - } - - this->output(); - this->shift_out(!ack); - - return data; - } + uint8_t + I2C::read(bool ack) { + uint8_t data = 0; + + this->input(); + for (uint8_t i = 8; i; i--) { + data <<= 1; + data |= this->shift_in(); + } + + this->output(); + this->shift_out(!ack); + + return data; + } - void - I2C::output(void) { - this->sda_pin.output(); - } + void + I2C::output(void) { + this->sda_pin.output(); + } - void - I2C::input(void) { - this->sda_pin.input(); - } + void + I2C::input(void) { + this->sda_pin.input(); + } - void - I2C::sda(bool value) { - this->sda_pin = value; - wait_us(this->frequency); - } + void + I2C::sda(bool value) { + this->sda_pin = value; + wait_us(this->frequency); + } - void - I2C::scl(bool value) { - this->scl_pin = value; - wait_us(this->frequency); - } + void + I2C::scl(bool value) { + this->scl_pin = value; + wait_us(this->frequency); + } - void - I2C::shift_out(bool bit) { - this->sda(bit); - this->scl(1); - this->scl(0); - } + void + I2C::shift_out(bool bit) { + this->sda(bit); + this->scl(1); + this->scl(0); + } - bool - I2C::shift_in(void) { - wait_us(this->frequency); - this->scl(1); - bool bit = this->sda_pin; - this->scl(0); - return bit; - } + bool + I2C::shift_in(void) { + wait_us(this->frequency); + this->scl(1); + bool bit = this->sda_pin; + this->scl(0); + return bit; + } } \ No newline at end of file