Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Fri May 08 12:19:57 2015 +0000
Revision:
100:dbcd3bc51758
Child:
112:3872569be2af
v2.1rc3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xinlei 100:dbcd3bc51758 1 #include <stdio.h>
xinlei 100:dbcd3bc51758 2 #include "Potentiometer.h"
xinlei 100:dbcd3bc51758 3 #include "SmartRestConf.h"
xinlei 100:dbcd3bc51758 4 #include "logging.h"
xinlei 100:dbcd3bc51758 5
xinlei 100:dbcd3bc51758 6 // Cutoff for avoiding sending similar sensor data.
xinlei 100:dbcd3bc51758 7 #define THRESHOLD_PERCENT_ANA 0.02
xinlei 100:dbcd3bc51758 8 // Timeout for forcing a sending even if readings are similar [seconds]
xinlei 100:dbcd3bc51758 9 #define TIME_LIMIT_ANA 900
xinlei 100:dbcd3bc51758 10
xinlei 100:dbcd3bc51758 11 AnalogIn analog1(A0);
xinlei 100:dbcd3bc51758 12 AnalogIn analog2(A1);
xinlei 100:dbcd3bc51758 13
xinlei 100:dbcd3bc51758 14 size_t Potentiometer::read(char *buf, size_t maxLen, char *status, size_t num)
xinlei 100:dbcd3bc51758 15 {
xinlei 100:dbcd3bc51758 16 static const char *fmt = "107,%ld,%f,%f\r\n";
xinlei 100:dbcd3bc51758 17 float data[2] = {(float)analog1*100, (float)analog2*100};
xinlei 100:dbcd3bc51758 18
xinlei 100:dbcd3bc51758 19 if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ANA &&
xinlei 100:dbcd3bc51758 20 abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ANA) {
xinlei 100:dbcd3bc51758 21 time_t t_interval = time(NULL) - t_start;
xinlei 100:dbcd3bc51758 22 if (t_interval < TIME_LIMIT_ANA) {
xinlei 100:dbcd3bc51758 23 return 0;
xinlei 100:dbcd3bc51758 24 } else {
xinlei 100:dbcd3bc51758 25 aDebug("Poti: Timeout at %d s.\n", t_interval);
xinlei 100:dbcd3bc51758 26 }
xinlei 100:dbcd3bc51758 27 }
xinlei 100:dbcd3bc51758 28
xinlei 100:dbcd3bc51758 29 size_t l = snprintf(buf, maxLen, fmt, deviceID, data[0], data[1]);
xinlei 100:dbcd3bc51758 30 if (status) {
xinlei 100:dbcd3bc51758 31 snprintf(status, num, "Send Poti %.1f,%.1f", data[0], data[1]);
xinlei 100:dbcd3bc51758 32 }
xinlei 100:dbcd3bc51758 33 oldValues[0] = data[0];
xinlei 100:dbcd3bc51758 34 oldValues[1] = data[1];
xinlei 100:dbcd3bc51758 35 t_start = time(NULL);
xinlei 100:dbcd3bc51758 36 return l;
xinlei 100:dbcd3bc51758 37 }