Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Mon Jun 01 11:24:17 2015 +0000
Revision:
124:979ab0d075de
Parent:
123:311fa85af2b3
Child:
129:dc9e37d4bc05
Poti: sample interval extended to 10 ms.; Polling: pointer null checking in handshake

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xinlei 100:dbcd3bc51758 1 #include <stdio.h>
xinlei 112:3872569be2af 2 #include "rtos.h"
xinlei 100:dbcd3bc51758 3 #include "Potentiometer.h"
xinlei 100:dbcd3bc51758 4 #include "SmartRestConf.h"
xinlei 100:dbcd3bc51758 5 #include "logging.h"
xinlei 100:dbcd3bc51758 6
xinlei 100:dbcd3bc51758 7 // Cutoff for avoiding sending similar sensor data.
xinlei 100:dbcd3bc51758 8 #define THRESHOLD_PERCENT_ANA 0.02
xinlei 100:dbcd3bc51758 9 // Timeout for forcing a sending even if readings are similar [seconds]
xinlei 100:dbcd3bc51758 10 #define TIME_LIMIT_ANA 900
xinlei 100:dbcd3bc51758 11
xinlei 100:dbcd3bc51758 12 AnalogIn analog1(A0);
xinlei 100:dbcd3bc51758 13 AnalogIn analog2(A1);
xinlei 100:dbcd3bc51758 14
xinlei 114:c54e9731b9de 15 static inline bool valueChanged(float *V0, float *V1)
xinlei 114:c54e9731b9de 16 {
xinlei 114:c54e9731b9de 17 if (abs(V0[0]-V1[0]) <= abs(V0[0])*THRESHOLD_PERCENT_ANA &&
xinlei 114:c54e9731b9de 18 abs(V0[1]-V1[1]) <= abs(V0[1])*THRESHOLD_PERCENT_ANA)
xinlei 114:c54e9731b9de 19 return false;
xinlei 114:c54e9731b9de 20 else
xinlei 114:c54e9731b9de 21 return true;
xinlei 114:c54e9731b9de 22 }
xinlei 114:c54e9731b9de 23
xinlei 100:dbcd3bc51758 24 size_t Potentiometer::read(char *buf, size_t maxLen, char *status, size_t num)
xinlei 100:dbcd3bc51758 25 {
xinlei 100:dbcd3bc51758 26 static const char *fmt = "107,%ld,%f,%f\r\n";
xinlei 116:5de54f09f754 27 float data[2] = {0, 0};
xinlei 116:5de54f09f754 28 float min[2] = {100, 100};
xinlei 116:5de54f09f754 29 float max[2] = {0, 0};
xinlei 116:5de54f09f754 30 // multiple sampling for data smoothing
xinlei 123:311fa85af2b3 31 const unsigned short N = 20;
xinlei 116:5de54f09f754 32 for (unsigned short i = 0; i < N; ++i) {
xinlei 116:5de54f09f754 33 float d0 = (float)analog1*100;
xinlei 116:5de54f09f754 34 float d1 = (float)analog2*100;
xinlei 116:5de54f09f754 35 data[0] += d0;
xinlei 116:5de54f09f754 36 data[1] += d1;
xinlei 116:5de54f09f754 37 min[0] = min[0] <= d0 ? min[0] : d0;
xinlei 116:5de54f09f754 38 min[1] = min[1] <= d1 ? min[1] : d1;
xinlei 116:5de54f09f754 39 max[0] = max[0] >= d0 ? max[0] : d0;
xinlei 116:5de54f09f754 40 max[1] = max[1] >= d1 ? max[1] : d1;
xinlei 124:979ab0d075de 41 Thread::wait(10);
xinlei 116:5de54f09f754 42 }
xinlei 116:5de54f09f754 43 data[0] = (data[0]-min[0]-max[0]) / (N-2);
xinlei 116:5de54f09f754 44 data[1] = (data[1]-min[1]-max[1]) / (N-2);
xinlei 116:5de54f09f754 45
xinlei 123:311fa85af2b3 46 float t_interval = timer.read();
xinlei 123:311fa85af2b3 47 if (!valueChanged(oldValues, data) && t_interval < TIME_LIMIT_ANA) {
xinlei 123:311fa85af2b3 48 return 0;
xinlei 100:dbcd3bc51758 49 }
xinlei 100:dbcd3bc51758 50
xinlei 123:311fa85af2b3 51 if (t_interval >= TIME_LIMIT_ANA)
xinlei 123:311fa85af2b3 52 aInfo("[%f]: %f, %f (%f, %f)\n", t_interval, data[0], data[1], oldValues[0], oldValues[1]);
xinlei 100:dbcd3bc51758 53 size_t l = snprintf(buf, maxLen, fmt, deviceID, data[0], data[1]);
xinlei 100:dbcd3bc51758 54 if (status) {
xinlei 100:dbcd3bc51758 55 snprintf(status, num, "Send Poti %.1f,%.1f", data[0], data[1]);
xinlei 100:dbcd3bc51758 56 }
xinlei 100:dbcd3bc51758 57 oldValues[0] = data[0];
xinlei 100:dbcd3bc51758 58 oldValues[1] = data[1];
xinlei 123:311fa85af2b3 59 timer.reset();
xinlei 100:dbcd3bc51758 60 return l;
xinlei 100:dbcd3bc51758 61 }