Thursday, November 14, 2013

Kick-off for the sensor library




This is my first attempt in writing a library for Arduino. The idea is to write some generic class for sensors. Specialized classes will implement each particular family of sensors. I would like also to start an independent sensor identification code. To uniquely identify each sensor component so that one can implement component comparisons and so on.


Here is an example of sensor (DHT11) which I would like to support. Here is the minimal code for the Sensor class.





 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/******************************************************
 SENSOR LIBRARY:
 
 Written by Enrico Formenti.  
 BSD license, check license.txt for more information
 All text above must be included in any redistribution.
 ******************************************************/


/*
 
 SENSOR.H: generic class for all sensors. Any other class should derive from this.
 
 */


#ifndef SENSOR_H
#define SENSOR_H
#if ARDUINO >= 100
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif


#include "Sensor_Constants.h"


class Sensor {
private:
uint16_t _type;           // type of sensor
uint32_t _idcode;         // id code, manifacturer code
uint16_t _errcode;        // error code
virtualvoid read(void);  // read data from sensor

public:
inline uint16_t getType() { return _type; }
inline uint32_t getIDcode() { return _idcode; }
inline uint16_t error() { return _errcode; }
virtualvoid begin(void); // setup sensor internals
};
#endif
The "Sensor_Constants.h" file contains constants for uniquely identifying the components and their types. The constants have been generated using a random number generator so to avoid collisions. Here is the current content of the file.




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 SENSOR LIBRARY:
 
 Written by Enrico Formenti.  
 BSD license, check license.txt for more information
 All text above must be included in any redistribution.
 ******************************************************/


#ifndef SENSOR_CONSTANTS_H
#define SENSOR_CONSTANTS_H


/* 
 SENSOR_CONSTANTS: this files contains all the constants definitions for sensor  type, sensors
 ids and errors. All numbers are randomly generated so to avoid numbering conflicts.
*/




// Constants for sensor ids
// NB: some have the same ID since they are equivalent


#define DHT11 0x644a2c7b
#define DHT21 0xe53fd0d1
#define DHT22 0x79aca58e
#define RHT03 0x79aca58e
#define LM34  0x06884901
#define LM35  0x446904b2
#define DS18B20 0xfb04f450


// Constants for type of sensors


#define ST_HUMIDITY 0x45dd
#define ST_TEMPERATURE_HUMITY 0xd130
#define ST_TEMPERATURE 0x44ce


// Constants for errors


#define NO_ERROR 0x0
#define INVALID_CRC 0xb9e5
#define NO_MORE_ADDRESSES 0x52aa
#define TIME_OUT 0xab70
#define READ_FAILURE 0xca07


#endif

No comments :

Post a Comment