A small library that's provide helpers for programmers
MySensor.cpp@1:ee7a5f05513d, 2015-02-17 (annotated)
- Committer:
- clemounet
- Date:
- Tue Feb 17 11:55:34 2015 +0000
- Revision:
- 1:ee7a5f05513d
- Child:
- 2:6cc4c56940af
more Things
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
clemounet | 1:ee7a5f05513d | 1 | |
clemounet | 1:ee7a5f05513d | 2 | #include "MySensor.h" |
clemounet | 1:ee7a5f05513d | 3 | |
clemounet | 1:ee7a5f05513d | 4 | MySensor::MySensor(const char* sName, uint8_t t, uint32_t idle): MyThread(sName) { |
clemounet | 1:ee7a5f05513d | 5 | resultsMutex = new Mutex(); |
clemounet | 1:ee7a5f05513d | 6 | sensorType = t; |
clemounet | 1:ee7a5f05513d | 7 | idleTime = idle; |
clemounet | 1:ee7a5f05513d | 8 | } |
clemounet | 1:ee7a5f05513d | 9 | |
clemounet | 1:ee7a5f05513d | 10 | MySensor::~MySensor() { |
clemounet | 1:ee7a5f05513d | 11 | delete(resultsMutex); |
clemounet | 1:ee7a5f05513d | 12 | } |
clemounet | 1:ee7a5f05513d | 13 | |
clemounet | 1:ee7a5f05513d | 14 | const char *MySensor::GetSensorName() { |
clemounet | 1:ee7a5f05513d | 15 | return tName; |
clemounet | 1:ee7a5f05513d | 16 | } |
clemounet | 1:ee7a5f05513d | 17 | |
clemounet | 1:ee7a5f05513d | 18 | uint8_t MySensor::GetSensorType() { |
clemounet | 1:ee7a5f05513d | 19 | return sensorType; |
clemounet | 1:ee7a5f05513d | 20 | } |
clemounet | 1:ee7a5f05513d | 21 | |
clemounet | 1:ee7a5f05513d | 22 | void MySensor::SetIdleTime(uint32_t idle) { |
clemounet | 1:ee7a5f05513d | 23 | idleTime = idle; |
clemounet | 1:ee7a5f05513d | 24 | } |
clemounet | 1:ee7a5f05513d | 25 | |
clemounet | 1:ee7a5f05513d | 26 | void MySensor::Main() { |
clemounet | 1:ee7a5f05513d | 27 | while(running){ |
clemounet | 1:ee7a5f05513d | 28 | Loop(); |
clemounet | 1:ee7a5f05513d | 29 | Wait(idleTime); |
clemounet | 1:ee7a5f05513d | 30 | } |
clemounet | 1:ee7a5f05513d | 31 | } |
clemounet | 1:ee7a5f05513d | 32 | |
clemounet | 1:ee7a5f05513d | 33 | void MySensor::Capture(char *data, uint16_t *len) { |
clemounet | 1:ee7a5f05513d | 34 | if(resultsMutex) { |
clemounet | 1:ee7a5f05513d | 35 | // Warning here, lock have no timeout it could leads to deadlocks |
clemounet | 1:ee7a5f05513d | 36 | if(results.start) { |
clemounet | 1:ee7a5f05513d | 37 | resultsMutex->lock(); |
clemounet | 1:ee7a5f05513d | 38 | // Copy the results |
clemounet | 1:ee7a5f05513d | 39 | memcpy(data,results.start,results.num); |
clemounet | 1:ee7a5f05513d | 40 | *len = results.num; |
clemounet | 1:ee7a5f05513d | 41 | // Reset the results struct |
clemounet | 1:ee7a5f05513d | 42 | results.current = results.start; |
clemounet | 1:ee7a5f05513d | 43 | results.num = 0; |
clemounet | 1:ee7a5f05513d | 44 | resultsMutex->unlock(); |
clemounet | 1:ee7a5f05513d | 45 | } |
clemounet | 1:ee7a5f05513d | 46 | } else |
clemounet | 1:ee7a5f05513d | 47 | *len = 0; |
clemounet | 1:ee7a5f05513d | 48 | } |