1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#pragma once
14
15typedef int millicelcius_t;
16typedef unsigned int millikelvin_t;
17typedef int celcius_t;
18typedef unsigned int kelvin_t;
19
20/* The canonical temperature unit is millikelvin */
21typedef millikelvin_t temperature_t;
22
23/* Temperature Conversions */
24#define MILLIKELVIN_OFFSET 273150
25
26#define MILLIKELVIN_IN_KELVIN      1000
27#define MILLICELCIUS_IN_CELCIUS    1000
28
29/* Coverts from a millicelcius to millikelvin
30 * Behaviour when given a value less than -237150 (absolute zero)
31 * is undefined.
32 */
33static inline millikelvin_t millicelcius_to_millikelvin(millicelcius_t mc) {
34    return mc + MILLIKELVIN_OFFSET;
35}
36
37/* Converts from millikelvin to millicelcius */
38static inline millicelcius_t millikelvin_to_millicelcius(millikelvin_t mk) {
39    return mk - MILLIKELVIN_OFFSET;
40}
41
42/* Converts from celcius to kelvin */
43static inline kelvin_t celcius_to_kelvin(celcius_t c) {
44    return millicelcius_to_millikelvin(c * MILLICELCIUS_IN_CELCIUS) / MILLIKELVIN_IN_KELVIN;
45}
46
47/* Converts from kelvin to celcius */
48static inline celcius_t kelvin_to_celcius(kelvin_t k) {
49    return millikelvin_to_millicelcius(k * MILLIKELVIN_IN_KELVIN) / MILLICELCIUS_IN_CELCIUS;
50}
51