Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

measurement/Acceleration.cpp

Committer:
xinlei
Date:
2015-05-08
Revision:
100:dbcd3bc51758
Child:
114:c54e9731b9de

File content as of revision 100:dbcd3bc51758:

#include <stdio.h>
#include "SmartRestConf.h"
#include "Acceleration.h"
#include "logging.h"

// Percentage cut-off for avoiding sending similar sensor reading.
#define THRESHOLD_PERCENT_ACCE 0.1
// Timeout for forcing a sending even if readings are similar [seconds].
#define TIME_LIMIT_ACCE 900

size_t Acceleration::read(char *buf, size_t maxLen, char *status, size_t num)
{
        static const char *fmt = "106,%ld,%f,%f,%f\r\n";
        if (!deviceReady)
                return 0;
        float data[3] = { 0.0, 0.0, 0.0 };
        sensor.readData(data);

        if ((0.15 > abs(data[0]) || abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ACCE) &&
            (0.10 > abs(data[1]) || abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ACCE)) {
                time_t t_interval = time(NULL) - t_start;
                if (t_interval < TIME_LIMIT_ACCE) {
                        return 0;
                } else {
                        aDebug("Acc: Timeout at %d s.\n", t_interval);
                }
        }

        size_t l = snprintf(buf, maxLen, fmt, deviceID, data[0], data[1], data[2]);
        if (status) {
                snprintf(status, num, "Send Acc %.1f,%.1f,%.1f",
                         data[0], data[1], data[2]);
        }
        oldValues[0] = data[0];
        oldValues[1] = data[1];
        oldValues[2] = data[2];
        t_start = time(NULL);
        return l;
}