update library.json, license, minor edits

This commit is contained in:
rob tillaart 2021-12-19 16:13:13 +01:00
parent 52a175706b
commit 5d65284e43
18 changed files with 126 additions and 146 deletions

View File

@ -1,7 +1,7 @@
//
// FILE: HX711.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.0
// VERSION: 0.3.1
// PURPOSE: Library for Loadcells for UNO
// URL: https://github.com/RobTillaart/HX711
//
@ -14,7 +14,7 @@
// 0.2.3 2021-05-26 add running_average() mode
// 0.3.0 2021-11-14 fix #11 shiftIn timing
// update build-CI, readme.md, badges
// 0.3.1 2021-12-19 update library.json, license, minor edits
#include "HX711.h"
@ -60,32 +60,33 @@ bool HX711::is_ready()
// from datasheet page 4
// When output data is not ready for retrieval,
// digital output pin DOUT is high. Serial clock
// input PD_SCK should be low. When DOUT goes
// to low, it indicates data is ready for retrieval.
float HX711::read()
// digital output pin DOUT is high.
// Serial clock input PD_SCK should be low.
// When DOUT goes to low, it indicates data is ready for retrieval.
float HX711::read()
{
// this waiting takes most time...
// this BLOCKING wait takes most time...
while (digitalRead(_dataPin) == HIGH) yield();
union
{
long value = 0;
uint8_t data[4];
} v;
// blocking part ...
noInterrupts();
// Pulse the clock pin 24 times to read the data.
// v.data[2] = shiftIn(_dataPin, _clockPin, MSBFIRST);
// v.data[1] = shiftIn(_dataPin, _clockPin, MSBFIRST);
// v.data[0] = shiftIn(_dataPin, _clockPin, MSBFIRST);
// Pulse the clock pin 24 times to read the data.
// v.data[2] = shiftIn(_dataPin, _clockPin, MSBFIRST);
// v.data[1] = shiftIn(_dataPin, _clockPin, MSBFIRST);
// v.data[0] = shiftIn(_dataPin, _clockPin, MSBFIRST);
v.data[2] = _shiftIn();
v.data[1] = _shiftIn();
v.data[0] = _shiftIn();
// TABLE 3 page 4 datasheet
// only default verified, so other values not supported yet
// TABLE 3 page 4 datasheet
// only default verified, so other values not supported yet
uint8_t m = 1; // default gain == 128
if (_gain == 64) m = 3;
if (_gain == 32) m = 2;
@ -98,8 +99,9 @@ float HX711::read()
}
interrupts();
// yield();
// SIGN extend
// SIGN extend
if (v.data[2] & 0x80) v.data[3] = 0xFF;
_lastRead = millis();
@ -113,15 +115,15 @@ void HX711::calibrate_scale(uint16_t weight, uint8_t times)
_scale = (1.0 * weight) / (read_average(times) - _offset);
}
// will be obsolete
// OBSOLETE 0.4.0 (LL is wrong)
void HX711::callibrate_scale(uint16_t weight, uint8_t times)
{
calibrate_scale(weight, times);
calibrate_scale(weight, times);
};
void HX711::wait_ready(uint32_t ms)
void HX711::wait_ready(uint32_t ms)
{
while (!is_ready())
{
@ -130,7 +132,7 @@ void HX711::wait_ready(uint32_t ms)
}
bool HX711::wait_ready_retry(uint8_t retries, uint32_t ms)
bool HX711::wait_ready_retry(uint8_t retries, uint32_t ms)
{
while (retries--)
{
@ -140,10 +142,11 @@ bool HX711::wait_ready_retry(uint8_t retries, uint32_t ms)
return false;
}
bool HX711::wait_ready_timeout(uint32_t timeout, uint32_t ms)
{
uint32_t start = millis();
while (millis() - start < timeout)
while (millis() - start < timeout)
{
if (is_ready()) return true;
delay(ms);
@ -152,11 +155,11 @@ bool HX711::wait_ready_timeout(uint32_t timeout, uint32_t ms)
}
float HX711::read_average(uint8_t times)
float HX711::read_average(uint8_t times)
{
if (times < 1) times = 1;
float sum = 0;
for (uint8_t i = 0; i < times; i++)
for (uint8_t i = 0; i < times; i++)
{
sum += read();
yield();
@ -165,12 +168,12 @@ float HX711::read_average(uint8_t times)
}
float HX711::read_median(uint8_t times)
float HX711::read_median(uint8_t times)
{
if (times > 15) times = 15;
if (times < 3) times = 3;
float s[15];
for (uint8_t i = 0; i < times; i++)
for (uint8_t i = 0; i < times; i++)
{
s[i] = read();
yield();
@ -181,12 +184,12 @@ float HX711::read_median(uint8_t times)
}
float HX711::read_medavg(uint8_t times)
float HX711::read_medavg(uint8_t times)
{
if (times > 15) times = 15;
if (times < 3) times = 3;
float s[15];
for (uint8_t i = 0; i < times; i++)
for (uint8_t i = 0; i < times; i++)
{
s[i] = read();
yield();
@ -206,13 +209,13 @@ float HX711::read_medavg(uint8_t times)
}
float HX711::read_runavg(uint8_t times, float alpha)
float HX711::read_runavg(uint8_t times, float alpha)
{
if (times < 1) times = 1;
if (alpha < 0) alpha = 0;
if (alpha > 1) alpha = 1;
float val = read();
for (uint8_t i = 1; i < times; i++)
for (uint8_t i = 1; i < times; i++)
{
val += alpha * (read() - val);
yield();
@ -225,11 +228,11 @@ void HX711::_insertSort(float * array, uint8_t size)
{
uint8_t t, z;
float temp;
for (t = 1; t < size; t++)
for (t = 1; t < size; t++)
{
z = t;
temp = array[z];
while( (z > 0) && (temp < array[z - 1] ))
while( (z > 0) && (temp < array[z - 1] ))
{
array[z] = array[z - 1];
z--;
@ -240,7 +243,7 @@ void HX711::_insertSort(float * array, uint8_t size)
}
float HX711::get_value(uint8_t times)
float HX711::get_value(uint8_t times)
{
float raw;
switch(_mode)
@ -270,14 +273,14 @@ float HX711::get_units(uint8_t times)
};
void HX711::power_down()
void HX711::power_down()
{
digitalWrite(_clockPin, LOW);
digitalWrite(_clockPin, HIGH);
}
void HX711::power_up()
void HX711::power_up()
{
digitalWrite(_clockPin, LOW);
}
@ -289,7 +292,7 @@ uint8_t HX711::_shiftIn()
{
uint8_t value = 0;
uint8_t mask = 0x80;
while(mask > 0)
while (mask > 0)
{
digitalWrite(_clockPin, HIGH);
delayMicroseconds(1); // T2 >= 0.2 us

View File

@ -2,8 +2,8 @@
//
// FILE: HX711.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.0
// PURPOSE: Library for Loadcells for UNO
// VERSION: 0.3.1
// PURPOSE: Library for Load cells for Arduino
// URL: https://github.com/RobTillaart/HX711
//
// HISTORY: see HX711.cpp
@ -15,16 +15,16 @@
#include "Arduino.h"
#define HX711_LIB_VERSION (F("0.3.0"))
#define HX711_LIB_VERSION (F("0.3.1"))
const uint8_t HX711_AVERAGE_MODE = 0x00;
// in median mode only between 3 and 15 samples are allowed.
// in median mode only between 3 and 15 samples are allowed.
const uint8_t HX711_MEDIAN_MODE = 0x01;
// medavg = average of the middle "half" of sorted elements
// in medavg mode only between 3 and 15 samples are allowed.
// medavg = average of the middle "half" of sorted elements
// in medavg mode only between 3 and 15 samples are allowed.
const uint8_t HX711_MEDAVG_MODE = 0x02;
// runavg = running average
// runavg = running average
const uint8_t HX711_RUNAVG_MODE = 0x03;
@ -34,97 +34,97 @@ public:
HX711();
~HX711();
// fixed gain 128 for now
// fixed gain 128 for now
void begin(uint8_t dataPin, uint8_t clockPin);
void reset();
// checks if load cell is ready to read.
// checks if load cell is ready to read.
bool is_ready();
// wait until ready,
// check every ms
// wait until ready,
// check every ms
void wait_ready(uint32_t ms = 0);
// max # retries
// max # retries
bool wait_ready_retry(uint8_t retries = 3, uint32_t ms = 0);
// max timeout
// max timeout
bool wait_ready_timeout(uint32_t timeout = 1000, uint32_t ms = 0);
// raw read
// raw read
float read();
// get average of multiple raw reads
// times = 1 or more
// get average of multiple raw reads
// times = 1 or more
float read_average(uint8_t times = 10);
// get median of multiple raw reads
// times = 3..15 - odd numbers preferred
float read_median(uint8_t times = 7);
// get median of multiple raw reads
// times = 3..15 - odd numbers preferred
float read_median(uint8_t times = 7);
// get average of "middle half" of multiple raw reads.
// times = 3..15 - odd numbers preferred
float read_medavg(uint8_t times = 7);
// get average of "middle half" of multiple raw reads.
// times = 3..15 - odd numbers preferred
float read_medavg(uint8_t times = 7);
// get running average over times measurements.
// the weight alpha can be set to any value between 0 and 1
// times = 1 or more.
float read_runavg(uint8_t times = 7, float alpha = 0.5);
// get running average over times measurements.
// the weight alpha can be set to any value between 0 and 1
// times = 1 or more.
float read_runavg(uint8_t times = 7, float alpha = 0.5);
// get set mode for get_value() and indirect get_units().
// in median and medavg mode only 3..15 samples are allowed.
// get set mode for get_value() and indirect get_units().
// in median and medavg mode only 3..15 samples are allowed.
void set_average_mode() { _mode = HX711_AVERAGE_MODE; };
void set_median_mode() { _mode = HX711_MEDIAN_MODE; };
void set_medavg_mode() { _mode = HX711_MEDAVG_MODE; };
// set_run_avg will use a default alpha of 0.5.
// set_run_avg will use a default alpha of 0.5.
void set_runavg_mode() { _mode = HX711_RUNAVG_MODE; };
uint8_t get_mode() { return _mode; };
// corrected for offset
// corrected for offset
float get_value(uint8_t times = 1);
// converted to proper units.
// converted to proper units.
float get_units(uint8_t times = 1);
// TARE
// call tare to calibrate zero
// TARE
// call tare to calibrate zero
void tare(uint8_t times = 10) { _offset = read_average(times); };
float get_tare() { return -_offset * _scale; };
bool tare_set() { return _offset != 0; };
// CORE "CONSTANTS" -> read datasheet
// GAIN values: 128, 64 32 [only 128 tested & verified]
// CORE "CONSTANTS" -> read datasheet
// GAIN values: 128, 64 32 [only 128 tested & verified]
void set_gain(uint8_t gain = 128) { _gain = gain; };
uint8_t get_gain() { return _gain; };
// SCALE > 0
// SCALE > 0
void set_scale(float scale = 1.0) { _scale = 1.0 / scale; };
float get_scale() { return 1.0 / _scale; };
// OFFSET > 0
// OFFSET > 0
void set_offset(long offset = 0) { _offset = offset; };
long get_offset() { return _offset; };
// CALIBRATION
// clear the scale
// call tare() to set the zero offset
// put a known weight on the scale
// call calibrate_scale(weight)
// scale is calculated.
// CALIBRATION
// clear the scale
// call tare() to set the zero offset
// put a known weight on the scale
// call calibrate_scale(weight)
// scale is calculated.
void calibrate_scale(uint16_t weight, uint8_t times = 10);
// obsolete typo but just do not want to break interface yet
void callibrate_scale(uint16_t weight, uint8_t times = 10);
// obsolete typo but just do not want to break interface yet
void callibrate_scale(uint16_t weight, uint8_t times = 10);
// POWER MANAGEMENT
// POWER MANAGEMENT
void power_down();
void power_up();
// TIME OF LAST READ
// TIME OF LAST READ
uint32_t last_read() { return _lastRead; };
// PRICING (idem calories?)
// PRICING (idem calories?)
float get_price(uint8_t times = 1) { return get_units(times) * _price; };
void set_unit_price(float price = 1.0) { _price = price; };
float get_unit_price() { return _price; };

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2019-2021 Rob Tillaart
Copyright (c) 2019-2022 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -206,6 +206,11 @@ See examples
- add examples
- test different load cells
- optimize the build-in **ShiftIn()** function to improve performance again.
- investigate read()
- investigate the need of yield after interrupts
- investigate blocking loop at begin of read()
-
#### the adding scale

View File

@ -1,16 +1,11 @@
//
// FILE: HX_kitchen_scale.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: HX711 demo
// URL: https://github.com/RobTillaart/HX711
//
// HISTORY:
// 0.1.0 2020-06-16 initial version
//
// to be tested
// to be tested
#include "HX711.h"
@ -58,7 +53,7 @@ void loop()
w2 = scale.get_units();
delay(100);
}
Serial.print("UNITS: ");
Serial.print(w1);
if (w1 == 0)

View File

@ -1,13 +1,8 @@
//
// FILE: HX_grocery_scale.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: HX711 demo
// URL: https://github.com/RobTillaart/HX711
//
// HISTORY:
// 0.1.0 2020-06-16 initial version
//
#include "HX711.h"
@ -30,11 +25,11 @@ void setup()
Serial.print("UNITS: ");
Serial.println(scale.get_units(10));
Serial.println("\nEmpty the scale, press a key to continue");
while(!Serial.available());
while(Serial.available()) Serial.read();
scale.tare();
Serial.print("UNITS: ");
Serial.println(scale.get_units(10));

View File

@ -1,13 +1,8 @@
//
// FILE: HX_kitchen_scale.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: HX711 demo
// URL: https://github.com/RobTillaart/HX711
//
// HISTORY:
// 0.1.0 2020-06-16 initial version
//
#include "HX711.h"
@ -30,11 +25,11 @@ void setup()
Serial.print("UNITS: ");
Serial.println(scale.get_units(10));
Serial.println("\nEmpty the scale, press a key to continue");
while(!Serial.available());
while(Serial.available()) Serial.read();
scale.tare();
Serial.print("UNITS: ");
Serial.println(scale.get_units(10));

View File

@ -1,13 +1,8 @@
//
// FILE: HX_performance.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: HX711 demo
// URL: https://github.com/RobTillaart/HX711
//
// HISTORY:
// 0.1.0 2020-06-15 initial version
//
#include "HX711.h"

View File

@ -1,13 +1,8 @@
//
// FILE: HX_performance2.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: HX711 demo
// URL: https://github.com/RobTillaart/HX711
//
// HISTORY:
// 0.1.0 2020-06-15 initial version
//
#include "HX711.h"

View File

@ -0,0 +1,8 @@
(dry run with no HW)
LIBRARY VERSION: 0.2.3
Counting get_units() calls for 1 minute...
calls per minute: 149202
calls per second: 2486.70

View File

@ -0,0 +1,8 @@
(dry run with no HW)
LIBRARY VERSION: 0.3.0
Counting get_units() calls for 1 minute...
calls per minute: 149202
calls per second: 2486.70

View File

@ -1,13 +1,8 @@
//
// FILE: HX_plotter.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: HX711 demo
// URL: https://github.com/RobTillaart/HX711
//
// HISTORY:
// 0.1.0 2020-06-15 initial version
//
#include "HX711.h"

View File

@ -1,13 +1,9 @@
//
// FILE: HX_read_median.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: HX711 demo
// URL: https://github.com/RobTillaart/HX711
//
// HISTORY:
// 0.1.0 2021-05-10 initial version
//
#include "HX711.h"

View File

@ -1,13 +1,8 @@
//
// FILE: HX_read_median_average.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: HX711 demo
// URL: https://github.com/RobTillaart/HX711
//
// HISTORY:
// 0.1.0 2021-05-13 initial version
//
#include "HX711.h"
@ -32,10 +27,10 @@ void setup()
scale.begin(dataPin, clockPin);
// TODO find a nice solution for this calibration..
// loadcell factor 20 KG
// load cell factor 20 KG
scale.set_scale(127.15);
// loadcell factor 5 KG
// load cell factor 5 KG
// scale.set_scale(420.0983);
// reset the scale to zero = 0
scale.tare();

View File

@ -1,13 +1,8 @@
//
// FILE: HX_set_mode.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: HX711 demo
// URL: https://github.com/RobTillaart/HX711
//
// HISTORY:
// 0.1.0 2021-05-13 initial version
//
#include "HX711.h"
@ -39,7 +34,7 @@ void setup()
// scale.set_scale(420.0983);
// reset the scale to zero = 0
scale.tare();
Serial.println();
scale.set_average_mode();

View File

@ -1,6 +1,6 @@
{
"name": "HX711",
"keywords": "load cell library kilo pound gram price ",
"keywords": "load cell,weight,kilo,pound,gram,price,lbs,ounce,pound",
"description": "Arduino library for HX711 loadcell amplifier",
"authors":
[
@ -15,8 +15,9 @@
"type": "git",
"url": "https://github.com/RobTillaart/HX711"
},
"version": "0.3.0",
"version": "0.3.1",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"
"platforms": "*",
"headers": "HX711.h"
}

View File

@ -1,5 +1,5 @@
name=HX711
version=0.3.0
version=0.3.1
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for HX711 loadcell amplifier

View File

@ -34,6 +34,7 @@ uint8_t clockPin = 7;
unittest_setup()
{
fprintf(stderr, "HX711_LIB_VERSION: %s\n", (char *) HX711_LIB_VERSION);
}
unittest_teardown()
@ -43,8 +44,6 @@ unittest_teardown()
unittest(test_constructor)
{
fprintf(stderr, "VERSION: %s\n", HX711_LIB_VERSION);
HX711 scale;
scale.begin(dataPin, clockPin);