A small library that's provide helpers for programmers
Diff: MySensor.cpp
- Revision:
- 2:6cc4c56940af
- Parent:
- 1:ee7a5f05513d
- Child:
- 3:9d3aebd62b82
diff -r ee7a5f05513d -r 6cc4c56940af MySensor.cpp --- a/MySensor.cpp Tue Feb 17 11:55:34 2015 +0000 +++ b/MySensor.cpp Fri Mar 27 16:48:12 2015 +0000 @@ -1,14 +1,24 @@ + #include "MySensor.h" -MySensor::MySensor(const char* sName, uint8_t t, uint32_t idle): MyThread(sName) { - resultsMutex = new Mutex(); - sensorType = t; +#define __DEBUG__ 0 +#ifndef __MODULE__ +#define __MODULE__ "MySensor.cpp" +#endif +#include "MyDebug.h" + +MySensor::MySensor(const char* sName, uint8_t type, uint32_t idle, uint32_t sz): + MyThread(sName,sz){ + // Each Sensor type have its ways to initialize the results structure + // After this constructor don't forget to call InitResults(), Becarefull the + // InitResultsStatic must be defined if you call InitResults with sz = -1 + sensorType = type; idleTime = idle; } + MySensor::~MySensor() { - delete(resultsMutex); } const char *MySensor::GetSensorName() { @@ -26,23 +36,47 @@ void MySensor::Main() { while(running){ Loop(); - Wait(idleTime); + StoreLastImpact(); + Thread::wait(idleTime); } } +void MySensor::InitResults(int16_t size) { + if(size == -1) { + InitResultsStatic(); + } else { + results.dynamic = true; + results.start = (uint8_t*) malloc (sizeof(uint8_t)*size); + results.current = results.start; + results.max = size; + } + results.num = 0; +} + +void MySensor::ReleaseResults() { + if(results.dynamic && (results.start != NULL)) { + free(results.start); + results.start = NULL; + results.current = NULL; + results.max = 0; + } +} + +// Here we could imgine a mecanism that allow passing smaller buffer that the results data count + void MySensor::Capture(char *data, uint16_t *len) { - if(resultsMutex) { - // Warning here, lock have no timeout it could leads to deadlocks - if(results.start) { - resultsMutex->lock(); + // Warning here, lock have no timeout it could leads to deadlocks + if(results.start != NULL) { + if(resultsMutex.trylock()){ // Copy the results memcpy(data,results.start,results.num); *len = results.num; // Reset the results struct results.current = results.start; results.num = 0; - resultsMutex->unlock(); + resultsMutex.unlock(); + return; } - } else - *len = 0; + } + len = 0; } \ No newline at end of file