-
Notifications
You must be signed in to change notification settings - Fork 20
/
moat_temp.c
133 lines (114 loc) · 2.61 KB
/
moat_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright © 2014-2015, Matthias Urlichs <[email protected]>
*
* 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 (included; see the file LICENSE)
* for more details.
*/
/* This code implements reading temp pins via 1wire.
*/
#include <string.h>
#include "moat_internal.h"
#include "dev_data.h"
#include "debug.h"
#include "onewire.h"
#include "temp.h"
#ifdef N_TEMP
uint8_t read_temp_len(uint8_t chan)
{
if(chan)
return 7;
else
return N_TEMP*2;
}
void read_temp(uint8_t chan, uint8_t *buf)
{
temp_t *tempp;
if (chan) { // one input: send flags, mark as scanned
if (chan > N_TEMP)
next_idle('p');
tempp = &temps[chan-1];
*buf++ = tempp->flags;
*buf++ = tempp->value>>8;
*buf++ = tempp->value&0xFF;
*buf++ = tempp->lower>>8;
*buf++ = tempp->lower&0xFF;
*buf++ = tempp->upper>>8;
*buf++ = tempp->upper&0xFF;
} else { // all inputs: send values
uint8_t i;
tempp = temps;
for(i=0;i<N_TEMP;i++) {
*buf++ = tempp->value>>8;
*buf++ = tempp->value&0xFF;
tempp++;
}
}
}
void read_temp_done(uint8_t chan) {
temp_t *tempp;
if(!chan) return;
tempp = &temps[chan-1];
tempp->flags &=~ (TEMP_IS_ALERT_L|TEMP_IS_ALERT_H);
}
void write_temp_check(uint8_t chan, uint8_t *buf, uint8_t len)
{
if (chan == 0 || chan > N_TEMP)
next_idle('p');
if (len != 2 && len != 4)
next_idle('a');
}
void write_temp(uint8_t chan, uint8_t *buf, uint8_t len)
{
uint8_t x;
uint16_t lower,upper;
temp_t *tempp = &temps[chan-1];
if (len == 2) {
x = *buf++;
lower = (x<<8)|x;
x = *buf++;
upper = (x<<8)|x;
} else {
lower = (*buf++)<<8;
lower |= *buf++;
upper = (*buf++)<<8;
upper |= *buf++;
}
tempp->lower = lower;
tempp->upper = upper;
tempp->flags &=~ (TEMP_IS_ALERT_L|TEMP_IS_ALERT_H);
}
#ifdef CONDITIONAL_SEARCH
char alert_temp_check(void)
{
return (temp_changed_cache*2 +7)>>3;
}
void alert_temp_fill(uint8_t *buf)
{
uint8_t i;
temp_t *t = temps;
uint8_t m=1;
DBG_X(temp_changed_cache);
memset(buf,0,(N_TEMP*2 +7)>>3);
for(i=0;i < N_TEMP; i++,t++) {
if (!m) {
m = 1;
buf++;
}
if (t->flags & TEMP_IS_ALERT_L)
*buf |= m;
m <<= 1;
if (t->flags & TEMP_IS_ALERT_H)
*buf |= m;
m <<= 1;
}
}
#endif // conditional
#endif