Skip to content

Instantly share code, notes, and snippets.

@mattyclarkson
Last active December 17, 2015 16:58
Show Gist options
  • Save mattyclarkson/5642131 to your computer and use it in GitHub Desktop.
Save mattyclarkson/5642131 to your computer and use it in GitHub Desktop.
A test to set some thresholds
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
struct threshold_t {
uint8_t upper;
uint8_t lower;
};
struct threshold_abc_t {
struct threshold_t a;
struct threshold_t b;
struct threshold_t c;
};
static void print(const struct threshold_abc_t * const thresholds) {
printf("{\"a\": {\"lower\": %u, \"upper\": %u},\n"
" \"b\": {\"lower\": %u, \"upper\": %u},\n"
" \"c\": {\"lower\": %u, \"upper\": %u}}\n",
thresholds->a.lower, thresholds->a.upper,
thresholds->b.lower, thresholds->b.upper,
thresholds->c.lower, thresholds->c.upper);
}
static void set_thresholds_0(struct threshold_t * const threshold,
const uint8_t lower, const uint8_t upper) {
threshold->lower = lower;
threshold->upper = upper;
}
enum threshold {
THRESHOLD_A = offsetof(struct threshold_abc_t, a),
THRESHOLD_B = offsetof(struct threshold_abc_t, b),
THRESHOLD_C = offsetof(struct threshold_abc_t, c),
};
static void set_thresholds_1(struct threshold_abc_t * const thresholds,
const enum threshold selection, const uint8_t lower, const uint8_t upper) {
struct threshold_t * const threshold =
(struct threshold_t * const)(&((uint8_t * const)thresholds)[selection]);
threshold->lower = lower;
threshold->upper = upper;
}
int main() {
struct threshold_abc_t thresholds = {
.a = {
.lower = 5,
.upper = 15,
},
.b = {
.lower = 0,
.upper = 20,
},
.c = {
.lower = 0,
.upper = 30,
}
};
print(&thresholds);
set_thresholds_0(&thresholds.a, 2, 50);
print(&thresholds);
set_thresholds_1(&thresholds, THRESHOLD_C, 69, 99);
print(&thresholds);
return 0;
}
{"a": {"lower": 5, "upper": 15},
"b": {"lower": 0, "upper": 20},
"c": {"lower": 0, "upper": 30}}
{"a": {"lower": 2, "upper": 50},
"b": {"lower": 0, "upper": 20},
"c": {"lower": 0, "upper": 30}}
{"a": {"lower": 2, "upper": 50},
"b": {"lower": 0, "upper": 20},
"c": {"lower": 69, "upper": 99}}
@mattyclarkson
Copy link
Author

See it live here:

http://okta.ideone.com/nJiyFG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment