forked from TeamAntumbra/libantumbra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.c
78 lines (68 loc) · 2.49 KB
/
temp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* Copyright (c) 2015 Antumbra
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "libantumbra.h"
static uint32_t unpacku32(const uint8_t *buf)
{
return (uint32_t)buf[0] << 24 | (uint32_t)buf[1] << 16 | (uint32_t)buf[2] << 8 | buf[3];
}
static void packu32(uint32_t n, uint8_t *buf)
{
buf[0] = n >> 24;
buf[1] = n >> 16 & 0xff;
buf[2] = n >> 8 & 0xff;
buf[3] = n & 0xff;
}
AnError AnTemp_ReadRaw_S(AnCtx *ctx, AnDevice *dev, uint32_t *rawout)
{
uint8_t inbuf[4];
AnError err = AnCmd_Invoke_S(ctx, dev, AnTemp_API, AnTemp_CMD_READRAW,
NULL, 0, inbuf, sizeof inbuf);
if (err)
return err;
*rawout = unpacku32(inbuf);
return AnError_SUCCESS;
}
AnError AnTemp_ReadTemp_S(AnCtx *ctx, AnDevice *dev, uint32_t *tempout)
{
uint8_t inbuf[4];
AnError err = AnCmd_Invoke_S(ctx, dev, AnTemp_API, AnTemp_CMD_READTEMP,
NULL, 0, inbuf, sizeof inbuf);
if (err)
return err;
*tempout = unpacku32(inbuf);
return AnError_SUCCESS;
}
AnError AnTemp_ReadCal_S(AnCtx *ctx, AnDevice *dev, AnTempCal *calout)
{
uint8_t inbuf[16];
AnError err = AnCmd_Invoke_S(ctx, dev, AnTemp_API, AnTemp_CMD_READCAL,
NULL, 0, inbuf, sizeof inbuf);
if (err)
return err;
*calout = (AnTempCal){
.a_sensor = unpacku32(inbuf),
.a_temp = unpacku32(inbuf + 4),
.b_sensor = unpacku32(inbuf + 8),
.b_temp = unpacku32(inbuf + 12),
};
return AnError_SUCCESS;
}
AnError AnTemp_WriteCal_S(AnCtx *ctx, AnDevice *dev, const AnTempCal *calin)
{
uint8_t outbuf[16];
packu32(calin->a_sensor, outbuf);
packu32(calin->a_temp, outbuf + 4);
packu32(calin->b_sensor, outbuf + 8);
packu32(calin->b_temp, outbuf + 12);
return AnCmd_Invoke_S(ctx, dev, AnTemp_API, AnTemp_CMD_WRITECAL,
outbuf, sizeof outbuf, NULL, 0);
}