-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_values.cpp
118 lines (97 loc) · 3.63 KB
/
node_values.cpp
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
#include <stdio.h>
#include <string.h>
#include "main.h"
extern const double TempCorrections[];
//=================================================================================
// ========= Sensor Data/value specific type dependent manipulation functions =====
//=================================================================================
void countDS1820Temp STORE_VALUE_PARAMS
{
int TReading = rawData[0] + (rawData[1] << 8);
uchar SignBit = ((TReading & 0x8000) >> 8); // test most sig bit
if (SignBit > 0) { // negative
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
double result;
result = (6.0 * TReading) + TReading / 4.0; // multiply by (100 * 0.0625) or 6.25
result = result / 100.0;
if (SignBit) { // If its negative
result = result * (-1.0);
}
result -= TempCorrections[ nodeP->node ];
nodeP->sensors[sensorNum]->float_val = result;
}
void countInternalProcTemp STORE_VALUE_PARAMS
{
int TReading = rawData[0];
nodeP->sensors[sensorNum]->float_val = (TReading - 128.0) / 1.07;
}
void decideOnOffValue STORE_VALUE_PARAMS
{
int TReading = rawData[0];
nodeP->sensors[sensorNum]->int_val = (TReading > 0) ? 1 : 0;
}
void countBatteryVoltOneCell STORE_VALUE_PARAMS
{
int val = rawData[0] + (rawData[1] << 8);
double volts = (5.0 / 1023.0) * val; //with 5V reference
nodeP->sensors[sensorNum]->float_val = volts;
}
void countBatteryVoltTwoCell STORE_VALUE_PARAMS
{
int val = rawData[0] + (rawData[1] << 8);
double volts = (5.0 / 1023.0) * val * 2; //cell voltage divisor on circuit is 2
nodeP->sensors[sensorNum]->float_val = volts;
}
void countLowPowerVcc STORE_VALUE_PARAMS
{
int TReading = rawData[2];
printf("Raw Vcc value: %d\n", TReading);
//smart hack - if the value is very low it probably means that it in fact
//overflowed 255 on the node during computation
//and also it means that measured voltage was over 5.0X Volts
//so we will smartly add those 255 to TReading
//normally the lower value the lower voltage and 1.1V reference means value 55
//so if it is lower than 50 we can assume overflow
if (TReading < 50) TReading += 256;
nodeP->low_power_voltage = TReading * 20;
}
void storePwmValsOneToFourBytes STORE_VALUE_PARAMS
{
//For PWM sensors, which return value as 1 byte for each channel (1-4 bytes total)
//The value does not need anyy conversion, so just copy proper number of received bytes
//into the pwm_vals struct in union structure
for (int i = 0; i < rawLen; i++)
{
nodeP->sensors[sensorNum]->pwm_vals.value[i] = rawData[i];
}
nodeP->sensors[sensorNum]->pwm_vals.num_channels = rawLen;
}
void getFloatValStr GET_VALUE_PARAMS
{
*strLen = asprintf( (char**)strBuf, "%.1f", nodeP->sensors[sensorNum]->float_val );
}
void getIntValStr GET_VALUE_PARAMS
{
*strLen = asprintf( (char**)strBuf, "%i", nodeP->sensors[sensorNum]->int_val );
}
void getUintValStr GET_VALUE_PARAMS
{
*strLen = asprintf( (char**)strBuf, "%d", nodeP->sensors[sensorNum]->uint_val );
}
void getUcharValStr GET_VALUE_PARAMS
{
*strLen = asprintf( (char**)strBuf, "%d", nodeP->sensors[sensorNum]->uchar_val );
}
void getPwmValStr GET_VALUE_PARAMS
{
asprintf((char**)strBuf, "%d,", nodeP->sensors[sensorNum]->pwm_vals.value[1]);
for (unsigned int i = 1; i < nodeP->sensors[sensorNum]->pwm_vals.num_channels; i++)
{
*strLen = asprintf((char**)strBuf, "%s,%d,", *strBuf, nodeP->sensors[sensorNum]->pwm_vals.value[i]);
}
}
void copySensorValueToLastValid(volatile NODE_VALUES_T* node, uchar sensorNum)
{
memcpy((void*)node->last_valid_values[sensorNum], (const void*)node->sensors[sensorNum], sizeof(SENSOR_VAL_T));
}