Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
measurement/Potentiometer.cpp
- Committer:
- xinlei
- Date:
- 2015-05-15
- Revision:
- 113:3872569be2af
- Parent:
- 101:dbcd3bc51758
- Child:
- 114:80610be056e2
File content as of revision 113:3872569be2af:
#include <stdio.h>
#include "rtos.h"
#include "Potentiometer.h"
#include "SmartRestConf.h"
#include "logging.h"
// Cutoff for avoiding sending similar sensor data.
#define THRESHOLD_PERCENT_ANA 0.02
// Timeout for forcing a sending even if readings are similar [seconds]
#define TIME_LIMIT_ANA 900
AnalogIn analog1(A0);
AnalogIn analog2(A1);
size_t Potentiometer::read(char *buf, size_t maxLen, char *status, size_t num)
{
static const char *fmt = "107,%ld,%f,%f\r\n";
const unsigned short N = 10;
float data[2] = {0, 0};
float min[2] = {100, 100};
float max[2] = {0, 0};
// multiple sampling for data smoothing
for (unsigned short i = 0; i < N; ++i) {
float d0 = (float)analog1*100;
float d1 = (float)analog2*100;
data[0] += d0;
data[1] += d1;
min[0] = min[0] <= d0 ? min[0] : d0;
min[1] = min[1] <= d1 ? min[1] : d1;
max[0] = max[0] >= d0 ? max[0] : d0;
max[1] = max[1] >= d1 ? max[1] : d1;
Thread::wait(10);
}
data[0] = (data[0]-min[0]-max[0]) / (N-2);
data[1] = (data[1]-min[1]-max[1]) / (N-2);
if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ANA &&
abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ANA) {
time_t t_interval = time(NULL) - t_start;
if (t_interval < TIME_LIMIT_ANA) {
return 0;
} else {
aDebug("Poti: Timeout at %d s.\n", t_interval);
}
}
size_t l = snprintf(buf, maxLen, fmt, deviceID, data[0], data[1]);
if (status) {
snprintf(status, num, "Send Poti %.1f,%.1f", data[0], data[1]);
}
oldValues[0] = data[0];
oldValues[1] = data[1];
t_start = time(NULL);
return l;
}

Cumulocity