Last active
December 17, 2015 16:58
-
-
Save mattyclarkson/5642131 to your computer and use it in GitHub Desktop.
A test to set some thresholds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"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}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See it live here:
http://okta.ideone.com/nJiyFG