-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmds-cantemp.c
69 lines (49 loc) · 1.24 KB
/
cmds-cantemp.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <libmicro/can.h>
#include <libmicro/lap.h>
#include "cmds-cantemp.h"
#include <libmicro/debug.h>
void cmd_cantemp(int argc, char *argv[])
{
unsigned char addr;
can_message *msg;
msg = can_buffer_get();
can_message *result;
unsigned int mode = 0;
unsigned int scanned = 0;
int temperatur = 0;
unsigned char temp_msb = 0;
unsigned char temp_lsb = 0;
if (argc < 2 || argc > 3)
goto argerror;
scanned = sscanf(argv[1], "%x", (unsigned int*)&addr);
if (argc == 3)
scanned += sscanf(argv[2], "%x", (unsigned int*)&mode);
msg->addr_src = 0x00;
msg->addr_dst = addr;
msg->port_src = 0x00;
msg->port_dst = 0x10;
msg->dlc = 1;
msg->data[0] = 0;
can_transmit(msg);
for (;;)
{
result = can_get();
if ((result != NULL) && result->addr_src == addr && result->addr_dst == 0x00)
{
temp_msb = result->data[0];
temp_lsb = result->data[1];
temperatur = ((temp_msb << 4) + (temp_lsb >> 4));
if (temp_msb >= 0x80) //if sign bit is set, then temp is negative
temperatur -= 4096;
printf("Temp is %3.4f\n", (float) (temperatur * 0.0625));
return;
}
}
argerror:
debug(0, "cantemp <addr> [mode]");
}