Skip to content

Commit

Permalink
Implement basic busy-wait waitReady method
Browse files Browse the repository at this point in the history
Fixes #40
  • Loading branch information
endail committed Dec 15, 2021
1 parent de87c5b commit 3c95edc
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ pi@raspberrypi:~/hx711 $ sudo bin/advancedhx711test 2 3 -377 -363712

- `bool isReady( )`. Returns true if the HX711 chip has data ready to be retrieved.

- `void waitReady( )`. When called, waits for the HX711 chip to have data ready, then returns.

- `void setStrictTiming( bool strict )`. The HX711 chip has specific timing requirements which if not adhered to may lead to corrupt data. If strict timing is enabled, an `IntegrityException` will be thrown when data integrity cannot be guaranteed. However, given the unreliability of timing on a non-realtime OS (such as Raspbian on a Raspberry Pi), this in itself is unreliable and therefore disabled by default. Use at your own risk.

- `bool isStrictTiming( )`. Returns true if strict timing is used.
Expand Down
1 change: 1 addition & 0 deletions include/HX711.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class HX711 {
void setConfig(const Channel c = Channel::A, const Gain g = Gain::GAIN_128);

bool isReady() const;
virtual void waitReady();
Value readValue();

void powerDown();
Expand Down
1 change: 1 addition & 0 deletions include/SimpleHX711.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SimpleHX711 : public AbstractScale, public HX711 {

virtual std::vector<Value> getValues(const std::chrono::nanoseconds timeout) override;
virtual std::vector<Value> getValues(const std::size_t samples) override;
//no override for waitReady; SimpleHX711 is based on busy-waiting

};
};
Expand Down
10 changes: 7 additions & 3 deletions src/HX711.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,8 @@ void HX711::setConfig(const Channel c, const Gain g) {
* A read must take place to set the gain at the
* hardware level. See datasheet pg. 4 "Serial
* Interface".
*
* TODO: this is an inefficient busy-wait. Solutions?
*/
while(!this->isReady());
this->waitReady();
this->readValue();

/**
Expand Down Expand Up @@ -309,6 +307,12 @@ bool HX711::isReady() const {

}

void HX711::waitReady() const {
while(!this->isReady()) {
//nop; busy-wait
}
}

Value HX711::readValue() {

std::int32_t v = 0;
Expand Down

0 comments on commit 3c95edc

Please sign in to comment.