pico-scale
scale.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2022 Daniel Robertson
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #ifndef SCALE_H_4B64A868_05B1_4F4C_99CF_E75ED9BAED50
24 #define SCALE_H_4B64A868_05B1_4F4C_99CF_E75ED9BAED50
25 
26 #include <stdbool.h>
27 #include <stddef.h>
28 #include <stdint.h>
29 #include "pico/time.h"
30 #include "mass.h"
31 #include "scale_adaptor.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 typedef enum {
41 
42 typedef enum {
46 
47 typedef struct {
50  size_t samples;
51  uint timeout; //us
52  int32_t* buffer; //read buffer
53  size_t bufflen; //read buffer length
55 
58  .read = read_type_median,
59  .samples = 3, //3 samples
60  .timeout = 1000000, //1 second
61  .buffer = NULL,
62  .bufflen = 0
63 };
64 
71  scale_options_t* const opt);
72 
73 typedef struct {
75  int32_t ref_unit;
76  int32_t offset;
78 } scale_t;
79 
89 void scale_init(
90  scale_t* const sc,
91  scale_adaptor_t* const adaptor,
92  const mass_unit_t unit,
93  const int32_t ref_unit,
94  const int32_t offset);
95 
106 bool scale_normalise(
107  const scale_t* const sc,
108  const double* const raw,
109  double* const normalised);
110 
122  scale_t* const sc,
123  int32_t* const arr,
124  const size_t len);
125 
139  scale_t* const sc,
140  int32_t* const arr,
141  const size_t arrlen,
142  size_t* const len,
143  const uint timeout);
144 
155 bool scale_read(
156  scale_t* const sc,
157  double* const val,
158  const scale_options_t* const opt);
159 
169 bool scale_zero(
170  scale_t* const sc,
171  const scale_options_t* const opt);
172 
183 bool scale_weight(
184  scale_t* const sc,
185  mass_t* const m,
186  const scale_options_t* const opt);
187 
188 #ifdef __cplusplus
189 }
190 #endif
191 
192 #endif
mass_unit_t
Definition: mass.h:64
static const scale_options_t SCALE_DEFAULT_OPTIONS
Definition: scale.h:56
bool scale_zero(scale_t *const sc, const scale_options_t *const opt)
Zeros the scale (tare) by adjusting its offset from 0 according to the given options....
Definition: scale.c:206
bool scale_weight(scale_t *const sc, mass_t *const m, const scale_options_t *const opt)
Obtains a weight from the scale according to the given options. Returns true if the operation succeed...
Definition: scale.c:231
strategy_type_t
Definition: scale.h:37
@ strategy_type_samples
Definition: scale.h:38
@ strategy_type_time
Definition: scale.h:39
bool scale_get_values_samples(scale_t *const sc, int32_t *const arr, const size_t len)
Fills arr with len number of samples from the scale. Returns true if the operation succeeded.
Definition: scale.c:76
read_type_t
Definition: scale.h:42
@ read_type_median
Definition: scale.h:43
@ read_type_average
Definition: scale.h:44
void scale_options_get_default(scale_options_t *const opt)
Fill options will default values.
Definition: scale.c:33
bool scale_read(scale_t *const sc, double *const val, const scale_options_t *const opt)
Obtains a value from the scale according to the given options. Returns true if the operation succeede...
Definition: scale.c:153
void scale_init(scale_t *const sc, scale_adaptor_t *const adaptor, const mass_unit_t unit, const int32_t ref_unit, const int32_t offset)
Initialise the scale with a hx711_t.
Definition: scale.c:39
bool scale_normalise(const scale_t *const sc, const double *const raw, double *const normalised)
Adjusts a raw value to a normalised value according to the scale's reference unit and offset....
Definition: scale.c:57
bool scale_get_values_timeout(scale_t *const sc, int32_t *const arr, const size_t arrlen, size_t *const len, const uint timeout)
Fills arr with as many number of samples as possible up to the timeout. Returns true if the operation...
Definition: scale.c:95
Definition: mass.h:93
int32_t * buffer
Definition: scale.h:52
strategy_type_t strat
Definition: scale.h:48
size_t samples
Definition: scale.h:50
read_type_t read
Definition: scale.h:49
size_t bufflen
Definition: scale.h:53
uint timeout
Definition: scale.h:51
Definition: scale.h:73
int32_t ref_unit
Definition: scale.h:75
scale_adaptor_t * _adaptor
Definition: scale.h:77
mass_unit_t unit
Definition: scale.h:74
int32_t offset
Definition: scale.h:76