To use the node in your project you should have the fred13632587/stm32avr-hx711 library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.
C++ implementation
struct State {
};
bool flag = 0;
{{ GENERATED_CODE }}
//#include <Arduino.h>
//#include <HX711.h>
// TEENSYDUINO has a port of Dean Camera's ATOMIC_BLOCK macros for AVR to ARM Cortex M3.
#define HAS_ATOMIC_BLOCK (defined(ARDUINO_ARCH_AVR) || defined(TEENSYDUINO))
// Whether we are running on either the ESP8266 or the ESP32.
#define ARCH_ESPRESSIF (defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32))
// Whether we are actually running on FreeRTOS.
#define IS_FREE_RTOS defined(ARDUINO_ARCH_ESP32)
// Define macro designating whether we're running on a reasonable
// fast CPU and so should slow down sampling from GPIO.
#define FAST_CPU \
( \
ARCH_ESPRESSIF || \
defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD) || \
defined(ARDUINO_ARCH_STM32) || defined(TEENSYDUINO) \
)
#if HAS_ATOMIC_BLOCK
// Acquire AVR-specific ATOMIC_BLOCK(ATOMIC_RESTORESTATE) macro.
#include <util/atomic.h>
#endif
#if FAST_CPU
// Make shiftIn() be aware of clockspeed for
// faster CPUs like ESP32, Teensy 3.x and friends.
// See also:
// - https://github.com/bogde/HX711/issues/75
// - https://github.com/arduino/Arduino/issues/6561
// - https://community.hiveeyes.org/t/using-bogdans-canonical-hx711-library-on-the-esp32/539
uint8_t shiftInSlow(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
uint8_t value = 0;
uint8_t i;
for(i = 0; i < 8; ++i) {
digitalWrite(clockPin, HIGH);
delayMicroseconds(1);
if(bitOrder == LSBFIRST)
value |= digitalRead(dataPin) << i;
else
value |= digitalRead(dataPin) << (7 - i);
digitalWrite(clockPin, LOW);
delayMicroseconds(1);
}
return value;
}
#define SHIFTIN_WITH_SPEED_SUPPORT(data,clock,order) shiftInSlow(data,clock,order)
#else
#define SHIFTIN_WITH_SPEED_SUPPORT(data,clock,order) shiftIn(data,clock,order)
#endif
////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
bool is_ready(uint8_t DOUT) {
// const uint8_t DOUT = getValue<input_dout>(ctx);
return digitalRead(DOUT) == LOW;
}
///////////////////////////////////////////////////////////
void wait_ready(unsigned long delay_ms , uint8_t DOUT) {
while (!is_ready( DOUT)) {
// Probably will do no harm on AVR but will feed the Watchdog Timer (WDT) on ESP.
// https://github.com/bogde/HX711/issues/73
delay(delay_ms);
}
}
//////////////////////////////////////////////////////////
long read(unsigned long delay_ms , uint8_t DOUT , uint8_t PD_SCK , uint8_t GAIN ) {
/* //////// global variable ///////////////////
const uint8_t PD_SCK = getValue<input_pd_sck>(ctx);
const uint8_t DOUT = getValue<input_dout>(ctx);
const uint8_t GAIN = getValue<input_gain>(ctx);
const uint8_t OFFSET = getValue<input_offset>(ctx);
const uint8_t SCALE = getValue<input_scale>(ctx);
////////////////////////////////////////////
const uint8_t time = getValue<input_TIME>(ctx);
if ( (!isValidDigitalPort(PD_SCK)) || (!isValidDigitalPort(DOUT)) )
{
emitValue<output_ERR>(ctx, 1);
return;
} */
// Wait for the chip to become ready.
wait_ready( delay_ms , DOUT);
// Define structures for reading data into.
unsigned long value = 0;
uint8_t data[3] = { 0 };
uint8_t filler = 0x00;
#if HAS_ATOMIC_BLOCK
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
#elif IS_FREE_RTOS
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
portENTER_CRITICAL(&mux);
#else
// Disable interrupts.
noInterrupts();
#endif
// Pulse the clock pin 24 times to read the data.
data[2] = SHIFTIN_WITH_SPEED_SUPPORT(DOUT, PD_SCK, MSBFIRST);
data[1] = SHIFTIN_WITH_SPEED_SUPPORT(DOUT, PD_SCK, MSBFIRST);
data[0] = SHIFTIN_WITH_SPEED_SUPPORT(DOUT, PD_SCK, MSBFIRST);
// Set the channel and the gain factor for the next reading using the clock pin.
for (unsigned int i = 0; i < GAIN; i++) {
digitalWrite(PD_SCK, HIGH);
#if ARCH_ESPRESSIF
delayMicroseconds(1);
#endif
digitalWrite(PD_SCK, LOW);
#if ARCH_ESPRESSIF
delayMicroseconds(1);
#endif
}
#if IS_FREE_RTOS
// End of critical section.
portEXIT_CRITICAL(&mux);
#elif HAS_ATOMIC_BLOCK
}
#else
// Enable interrupts again.
interrupts();
#endif
// Replicate the most significant bit to pad out a 32-bit signed integer
if (data[2] & 0x80) {
filler = 0xFF;
} else {
filler = 0x00;
}
// Construct a 32-bit signed integer
value = ( static_cast<unsigned long>(filler) << 24
| static_cast<unsigned long>(data[2]) << 16
| static_cast<unsigned long>(data[1]) << 8
| static_cast<unsigned long>(data[0]) );
return static_cast<long>(value);
}
//////////////////////////////////////
long read_average(uint8_t time , unsigned long delay_ms , uint8_t DOUT , uint8_t PD_SCK , uint8_t GAIN) {
long sum = 0;
for (byte i = 0; i < time; i++) {
sum += read( delay_ms , DOUT , PD_SCK , GAIN);
// Probably will do no harm on AVR but will feed the Watchdog Timer (WDT) on ESP.
// https://github.com/bogde/HX711/issues/73
delay(0);
}
return sum / time;
}
////////////////////////////////////
double get_value(uint8_t time , unsigned long delay_ms , uint8_t DOUT , uint8_t PD_SCK , uint8_t GAIN , uint8_t OFFSET) {
//////// global variable ///////////////////
//const uint8_t OFFSET = getValue<input_offset>(ctx);
return read_average( time , delay_ms , DOUT , PD_SCK , GAIN ) - OFFSET;
}
///////////////////////////////////
float get_units(uint8_t time , unsigned long delay_ms , uint8_t DOUT , uint8_t PD_SCK , uint8_t GAIN , uint8_t OFFSET , uint8_t SCALE) {
//const uint8_t SCALE = getValue<input_scale>(ctx);
return get_value( time , delay_ms , DOUT , PD_SCK , GAIN , OFFSET) / SCALE;
}
void evaluate(Context ctx) {
//auto inValue = getValue<input_IN>(ctx);
//emitValue<output_OUT>(ctx, inValue);
// State beg
if (!isInputDirty<input_UPD>(ctx))
return;
//////// global variable ///////////////////
const uint8_t PD_SCK = getValue<input_pd_sck>(ctx);
const uint8_t DOUT = getValue<input_dout>(ctx);
uint8_t GAIN = getValue<input_gain>(ctx);
const uint8_t OFFSET = getValue<input_offset>(ctx);
const uint8_t SCALE = getValue<input_scale>(ctx);
////////////////////////////////////////////
const uint8_t time = getValue<input_TIME>(ctx);
if ( (!isValidDigitalPort(PD_SCK)) || (!isValidDigitalPort(DOUT)) )
{
emitValue<output_ERR>(ctx, 1);
return;
}
if (GAIN >3) GAIN=3;
else if (GAIN<1) GAIN=1;
emitValue<output_OUT>(ctx, get_units(time , 0, DOUT , PD_SCK , GAIN , OFFSET , SCALE));
emitValue<output_END_PROCESS_1>(ctx, 1);
emitValue<output_END_PROCESS_2>(ctx, 1);
}