A small library that's provide helpers for programmers
MySensor.cpp@3:9d3aebd62b82, 2015-04-02 (annotated)
- Committer:
- clemounet
- Date:
- Thu Apr 02 07:33:38 2015 +0000
- Revision:
- 3:9d3aebd62b82
- Parent:
- 2:6cc4c56940af
.add Callback
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
clemounet | 2:6cc4c56940af | 1 | |
clemounet | 1:ee7a5f05513d | 2 | |
clemounet | 1:ee7a5f05513d | 3 | #include "MySensor.h" |
clemounet | 1:ee7a5f05513d | 4 | |
clemounet | 2:6cc4c56940af | 5 | #define __DEBUG__ 0 |
clemounet | 2:6cc4c56940af | 6 | #ifndef __MODULE__ |
clemounet | 2:6cc4c56940af | 7 | #define __MODULE__ "MySensor.cpp" |
clemounet | 2:6cc4c56940af | 8 | #endif |
clemounet | 2:6cc4c56940af | 9 | #include "MyDebug.h" |
clemounet | 2:6cc4c56940af | 10 | |
clemounet | 2:6cc4c56940af | 11 | MySensor::MySensor(const char* sName, uint8_t type, uint32_t idle, uint32_t sz): |
clemounet | 2:6cc4c56940af | 12 | MyThread(sName,sz){ |
clemounet | 2:6cc4c56940af | 13 | // Each Sensor type have its ways to initialize the results structure |
clemounet | 2:6cc4c56940af | 14 | // After this constructor don't forget to call InitResults(), Becarefull the |
clemounet | 2:6cc4c56940af | 15 | // InitResultsStatic must be defined if you call InitResults with sz = -1 |
clemounet | 2:6cc4c56940af | 16 | sensorType = type; |
clemounet | 1:ee7a5f05513d | 17 | idleTime = idle; |
clemounet | 1:ee7a5f05513d | 18 | } |
clemounet | 1:ee7a5f05513d | 19 | |
clemounet | 2:6cc4c56940af | 20 | |
clemounet | 1:ee7a5f05513d | 21 | MySensor::~MySensor() { |
clemounet | 1:ee7a5f05513d | 22 | } |
clemounet | 1:ee7a5f05513d | 23 | |
clemounet | 1:ee7a5f05513d | 24 | const char *MySensor::GetSensorName() { |
clemounet | 1:ee7a5f05513d | 25 | return tName; |
clemounet | 1:ee7a5f05513d | 26 | } |
clemounet | 1:ee7a5f05513d | 27 | |
clemounet | 1:ee7a5f05513d | 28 | uint8_t MySensor::GetSensorType() { |
clemounet | 1:ee7a5f05513d | 29 | return sensorType; |
clemounet | 1:ee7a5f05513d | 30 | } |
clemounet | 1:ee7a5f05513d | 31 | |
clemounet | 1:ee7a5f05513d | 32 | void MySensor::SetIdleTime(uint32_t idle) { |
clemounet | 1:ee7a5f05513d | 33 | idleTime = idle; |
clemounet | 1:ee7a5f05513d | 34 | } |
clemounet | 1:ee7a5f05513d | 35 | |
clemounet | 1:ee7a5f05513d | 36 | void MySensor::Main() { |
clemounet | 1:ee7a5f05513d | 37 | while(running){ |
clemounet | 1:ee7a5f05513d | 38 | Loop(); |
clemounet | 2:6cc4c56940af | 39 | StoreLastImpact(); |
clemounet | 2:6cc4c56940af | 40 | Thread::wait(idleTime); |
clemounet | 1:ee7a5f05513d | 41 | } |
clemounet | 1:ee7a5f05513d | 42 | } |
clemounet | 1:ee7a5f05513d | 43 | |
clemounet | 2:6cc4c56940af | 44 | void MySensor::InitResults(int16_t size) { |
clemounet | 2:6cc4c56940af | 45 | if(size == -1) { |
clemounet | 2:6cc4c56940af | 46 | InitResultsStatic(); |
clemounet | 2:6cc4c56940af | 47 | } else { |
clemounet | 2:6cc4c56940af | 48 | results.dynamic = true; |
clemounet | 2:6cc4c56940af | 49 | results.start = (uint8_t*) malloc (sizeof(uint8_t)*size); |
clemounet | 2:6cc4c56940af | 50 | results.current = results.start; |
clemounet | 2:6cc4c56940af | 51 | results.max = size; |
clemounet | 2:6cc4c56940af | 52 | } |
clemounet | 2:6cc4c56940af | 53 | results.num = 0; |
clemounet | 2:6cc4c56940af | 54 | } |
clemounet | 2:6cc4c56940af | 55 | |
clemounet | 2:6cc4c56940af | 56 | void MySensor::ReleaseResults() { |
clemounet | 2:6cc4c56940af | 57 | if(results.dynamic && (results.start != NULL)) { |
clemounet | 2:6cc4c56940af | 58 | free(results.start); |
clemounet | 2:6cc4c56940af | 59 | results.start = NULL; |
clemounet | 2:6cc4c56940af | 60 | results.current = NULL; |
clemounet | 2:6cc4c56940af | 61 | results.max = 0; |
clemounet | 2:6cc4c56940af | 62 | } |
clemounet | 2:6cc4c56940af | 63 | } |
clemounet | 2:6cc4c56940af | 64 | |
clemounet | 2:6cc4c56940af | 65 | // Here we could imgine a mecanism that allow passing smaller buffer that the results data count |
clemounet | 2:6cc4c56940af | 66 | |
clemounet | 1:ee7a5f05513d | 67 | void MySensor::Capture(char *data, uint16_t *len) { |
clemounet | 3:9d3aebd62b82 | 68 | // Warning here, lock is not the lock we |
clemounet | 2:6cc4c56940af | 69 | if(results.start != NULL) { |
clemounet | 2:6cc4c56940af | 70 | if(resultsMutex.trylock()){ |
clemounet | 1:ee7a5f05513d | 71 | // Copy the results |
clemounet | 1:ee7a5f05513d | 72 | memcpy(data,results.start,results.num); |
clemounet | 1:ee7a5f05513d | 73 | *len = results.num; |
clemounet | 1:ee7a5f05513d | 74 | // Reset the results struct |
clemounet | 1:ee7a5f05513d | 75 | results.current = results.start; |
clemounet | 1:ee7a5f05513d | 76 | results.num = 0; |
clemounet | 2:6cc4c56940af | 77 | resultsMutex.unlock(); |
clemounet | 2:6cc4c56940af | 78 | return; |
clemounet | 1:ee7a5f05513d | 79 | } |
clemounet | 2:6cc4c56940af | 80 | } |
clemounet | 2:6cc4c56940af | 81 | len = 0; |
clemounet | 1:ee7a5f05513d | 82 | } |