25 #include "hardware/gpio.h"
26 #include "hardware/pio.h"
27 #include "hardware/timer.h"
28 #include "pico/platform.h"
29 #include "pico/mutex.h"
30 #include "pico/time.h"
31 #include "../include/hx711.h"
32 #include "../include/util.h"
57 assert(config != NULL);
59 assert(config->
pio != NULL);
69 #ifndef HX711_NO_MUTEX
70 mutex_init(&hx->
_mut);
95 gpio_set_input_enabled(
168 pio_sm_drain_tx_fifo(
282 rawVal = pio_sm_get_blocking(
295 const uint timeout) {
300 bool success =
false;
301 const absolute_time_t endTime = make_timeout_time_us(timeout);
304 assert(!is_nil_time(endTime));
307 while(!time_reached(endTime)) {
324 int32_t*
const val) {
350 #ifndef HX711_NO_MUTEX
351 mutex_is_initialized(&hx->
_mut) &&
362 return util_int32_t_in_range(
369 return util_uint32_t_in_range(
376 return util_int_in_range(
383 return util_int_in_range(
435 pio_sm_drain_tx_fifo(
507 const uint32_t clockPulses =
519 uint32_t*
const val) {
void hx711_set_gain(hx711_t *const hx, const hx711_gain_t gain)
Sets the HX711 gain.
bool hx711_is_max_saturated(const int32_t val)
Returns true if the HX711 is saturated at its maximum level.
bool hx711_get_value_noblock(hx711_t *const hx, int32_t *const val)
Obtains a value from the HX711. Returns immediately if no value is available.
const unsigned short HX711_SETTLING_TIMES[]
void hx711_init(hx711_t *const hx, const hx711_config_t *const config)
int32_t hx711_get_value(hx711_t *const hx)
Obtains a value from the HX711. Blocks until a value is available.
bool hx711__is_initd(hx711_t *const hx)
Check whether the hx struct has been initalised.
void hx711_close(hx711_t *const hx)
Stop communication with the HX711.
unsigned short hx711_get_settling_time(const hx711_rate_t rate)
Returns the number of milliseconds to wait according to the given HX711 sample rate to allow readings...
bool hx711_is_min_saturated(const int32_t val)
Returns true if the HX711 is saturated at its minimum level.
bool hx711__try_get_value(PIO const pio, const uint sm, uint32_t *const val)
Attempts to obtain a value from the PIO RX FIFO if one is available.
bool hx711_is_rate_valid(const hx711_rate_t r)
Check whether the given rate is within the range of the predefined rates.
bool hx711_is_gain_valid(const hx711_gain_t g)
Check whether the given gain is within the range of the predefined gains.
unsigned char hx711_get_rate_sps(const hx711_rate_t rate)
Returns the numeric sample rate of the given rate.
const unsigned char HX711_SAMPLE_RATES[]
void hx711_wait_settle(const hx711_rate_t rate)
Convenience function for sleeping for the appropriate amount of time according to the given sample ra...
bool hx711_is_value_valid(const int32_t v)
Check whether the given value is valid for a HX711 implementation.
void hx711_power_down(hx711_t *const hx)
int32_t hx711_get_twos_comp(const uint32_t raw)
Convert a raw value from the HX711 to a 32-bit signed int.
void hx711_wait_power_down()
Convenience function for sleeping for the appropriate amount of time to allow the HX711 to power down...
bool hx711_is_pio_gain_valid(const uint32_t g)
Check whether a given value is permitted to be transmitted to a PIO State Machine to set a HX711's ga...
const unsigned char HX711_CLOCK_PULSES[]
bool hx711_get_value_timeout(hx711_t *const hx, int32_t *const val, const uint timeout)
Obtains a value from the HX711. Blocks until a value is available or the timeout is reached.
uint32_t hx711_gain_to_pio_gain(const hx711_gain_t gain)
Convert a hx711_gain_t to a numeric value appropriate for a PIO State Machine.
bool hx711__is_state_machine_enabled(hx711_t *const hx)
Check whether the hx struct's state machines are running.
void hx711_power_up(hx711_t *const hx, const hx711_gain_t gain)
unsigned char hx711_get_clock_pulses(const hx711_gain_t gain)
Returns the clock pulse count for a given gain value.
#define HX711_MUTEX_BLOCK(mut,...)
#define HX711_PIO_MAX_GAIN
#define HX711_PIO_MIN_GAIN
#define HX711_POWER_DOWN_TIMEOUT
hx711_pio_init_t pio_init
const pio_program_t * reader_prog
hx711_program_init_t reader_prog_init
const pio_program_t * _reader_prog
pio_sm_config _reader_prog_default_config
void util_gpio_set_output(const uint gpio)
Initialises and sets GPIO pin to output.
bool util_pio_sm_is_enabled(PIO const pio, const uint sm)
Check whether a given state machine is enabled.
bool util_pio_sm_try_get(PIO const pio, const uint sm, uint32_t *const word, const uint threshold)
Attempts to get a word from the state machine's RX FIFO if more than threshold words are in the buffe...