Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
measurement/Temperature.cpp@123:311fa85af2b3, 2015-05-29 (annotated)
- Committer:
- xinlei
- Date:
- Fri May 29 12:05:01 2015 +0000
- Revision:
- 123:311fa85af2b3
- Parent:
- 117:e831cdb799ab
- Child:
- 136:a52821cdb108
measurement: revert timing to use timer to avoid negative time after long run
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xinlei | 100:dbcd3bc51758 | 1 | #include <stdio.h> |
xinlei | 100:dbcd3bc51758 | 2 | #include "Temperature.h" |
xinlei | 100:dbcd3bc51758 | 3 | #include "SmartRestConf.h" |
xinlei | 100:dbcd3bc51758 | 4 | #include "logging.h" |
xinlei | 100:dbcd3bc51758 | 5 | |
xinlei | 100:dbcd3bc51758 | 6 | // Cut-off for avoiding sending similar sensor reading. |
xinlei | 100:dbcd3bc51758 | 7 | #define THRESHOLD_PERCENT_TEMP 0.02 |
xinlei | 100:dbcd3bc51758 | 8 | // Timeout for forcing a sending even if readings are similar [seconds]. |
xinlei | 100:dbcd3bc51758 | 9 | #define TIME_LIMIT_TEMP 900 |
xinlei | 100:dbcd3bc51758 | 10 | |
xinlei | 123:311fa85af2b3 | 11 | static inline bool valueChanged(float V0, float V1) |
xinlei | 123:311fa85af2b3 | 12 | { |
xinlei | 123:311fa85af2b3 | 13 | if (abs(V0-V1) <= abs(V0)*THRESHOLD_PERCENT_TEMP) |
xinlei | 123:311fa85af2b3 | 14 | return false; |
xinlei | 123:311fa85af2b3 | 15 | else |
xinlei | 123:311fa85af2b3 | 16 | return true; |
xinlei | 123:311fa85af2b3 | 17 | } |
xinlei | 123:311fa85af2b3 | 18 | |
xinlei | 100:dbcd3bc51758 | 19 | size_t Temperature::read(char *buf, size_t maxLen, char *status, size_t num) |
xinlei | 100:dbcd3bc51758 | 20 | { |
xinlei | 100:dbcd3bc51758 | 21 | static const char *fmt = "105,%ld,%f\r\n"; |
xinlei | 100:dbcd3bc51758 | 22 | if (!deviceReady) |
xinlei | 100:dbcd3bc51758 | 23 | return 0; |
xinlei | 100:dbcd3bc51758 | 24 | |
xinlei | 100:dbcd3bc51758 | 25 | float data = sensor.temp(); |
xinlei | 123:311fa85af2b3 | 26 | float t_interval = timer.read(); |
xinlei | 123:311fa85af2b3 | 27 | if (!valueChanged(oldValue, data) && t_interval < TIME_LIMIT_TEMP) { |
xinlei | 123:311fa85af2b3 | 28 | return 0; |
xinlei | 100:dbcd3bc51758 | 29 | } |
xinlei | 100:dbcd3bc51758 | 30 | size_t l = snprintf(buf, maxLen, fmt, deviceID, data); |
xinlei | 100:dbcd3bc51758 | 31 | if (status) { |
xinlei | 100:dbcd3bc51758 | 32 | snprintf(status, num, "Send Temp %.2f", data); |
xinlei | 100:dbcd3bc51758 | 33 | } |
xinlei | 100:dbcd3bc51758 | 34 | oldValue = data; |
xinlei | 123:311fa85af2b3 | 35 | timer.reset(); |
xinlei | 100:dbcd3bc51758 | 36 | return l; |
xinlei | 100:dbcd3bc51758 | 37 | } |