Library to manage Inovafitness SDS021 particles sensor

Dependents:   LoRaWAN-demo-72-bootcamp SDS021Test

Committer:
abouillot
Date:
Mon Jan 30 21:48:36 2017 +0000
Revision:
3:fd34f67797d9
Parent:
2:49822e10d111
Fixed usage bug, where the third parameter in not speed but buffer size

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abouillot 0:0c1a6a29b8c4 1 /*!
abouillot 0:0c1a6a29b8c4 2 * Sds021.cpp
abouillot 0:0c1a6a29b8c4 3 *
abouillot 0:0c1a6a29b8c4 4 * Control the inovafitness particles sensor SDS021
abouillot 0:0c1a6a29b8c4 5 *
abouillot 0:0c1a6a29b8c4 6 * Copyright (c) 2017 - Alexandre Bouillot github.com/abouillot
abouillot 0:0c1a6a29b8c4 7 *
abouillot 0:0c1a6a29b8c4 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
abouillot 0:0c1a6a29b8c4 9 * of this software and associated documnetation files (the "Software"), to deal
abouillot 0:0c1a6a29b8c4 10 * in the Software without restriction, including without limitation the rights
abouillot 0:0c1a6a29b8c4 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
abouillot 0:0c1a6a29b8c4 12 * copies of the Software, and to permit persons to whom the Software is
abouillot 0:0c1a6a29b8c4 13 * furished to do so, subject to the following conditions:
abouillot 0:0c1a6a29b8c4 14 *
abouillot 0:0c1a6a29b8c4 15 * The above copyright notice and this permission notice shall be included in
abouillot 0:0c1a6a29b8c4 16 * all copies or substantial portions of the Software.
abouillot 0:0c1a6a29b8c4 17 *
abouillot 0:0c1a6a29b8c4 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
abouillot 0:0c1a6a29b8c4 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
abouillot 0:0c1a6a29b8c4 20 * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
abouillot 0:0c1a6a29b8c4 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
abouillot 0:0c1a6a29b8c4 22 * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
abouillot 0:0c1a6a29b8c4 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
abouillot 0:0c1a6a29b8c4 24 * THE SOFTWARE.
abouillot 0:0c1a6a29b8c4 25 */
abouillot 0:0c1a6a29b8c4 26
abouillot 0:0c1a6a29b8c4 27 #ifndef __SDS021_H__
abouillot 0:0c1a6a29b8c4 28 #define __SDS021_H__
abouillot 0:0c1a6a29b8c4 29
abouillot 0:0c1a6a29b8c4 30 #include "mbed.h"
abouillot 0:0c1a6a29b8c4 31 #include "BufferedSerial.h"
abouillot 0:0c1a6a29b8c4 32
abouillot 0:0c1a6a29b8c4 33
abouillot 1:fb3d61ffd503 34 class SDS021
abouillot 0:0c1a6a29b8c4 35 {
abouillot 0:0c1a6a29b8c4 36 public:
abouillot 0:0c1a6a29b8c4 37 /* public structures and constants */
abouillot 0:0c1a6a29b8c4 38 typedef struct version {
abouillot 0:0c1a6a29b8c4 39 char year;
abouillot 0:0c1a6a29b8c4 40 char month;
abouillot 0:0c1a6a29b8c4 41 char day;
abouillot 0:0c1a6a29b8c4 42 } version_t;
abouillot 0:0c1a6a29b8c4 43
abouillot 0:0c1a6a29b8c4 44 public:
abouillot 0:0c1a6a29b8c4 45
abouillot 3:fd34f67797d9 46 SDS021(PinName tx, PinName rx) : _sds021(tx, rx), _PM2_5(0), _PM10(0), _mode(false), _state(true), _interval(0), _id(0xffff) {
abouillot 0:0c1a6a29b8c4 47 _firmware.year = 0;
abouillot 0:0c1a6a29b8c4 48 }
abouillot 0:0c1a6a29b8c4 49 bool update();
abouillot 0:0c1a6a29b8c4 50 int getId() {
abouillot 0:0c1a6a29b8c4 51 return _id;
abouillot 0:0c1a6a29b8c4 52 };
abouillot 0:0c1a6a29b8c4 53 void setId(int new_id);
abouillot 0:0c1a6a29b8c4 54 bool getPassiveMode() {
abouillot 0:0c1a6a29b8c4 55 return _mode;
abouillot 0:0c1a6a29b8c4 56 };
abouillot 0:0c1a6a29b8c4 57 void setPassiveMode(bool passive);
abouillot 0:0c1a6a29b8c4 58 void readData();
abouillot 0:0c1a6a29b8c4 59 uint8_t getInterval() {
abouillot 0:0c1a6a29b8c4 60 return _interval;
abouillot 0:0c1a6a29b8c4 61 };
abouillot 0:0c1a6a29b8c4 62 void setInterval(uint8_t minutes);
abouillot 0:0c1a6a29b8c4 63 bool getAwake() {
abouillot 0:0c1a6a29b8c4 64 return _state;
abouillot 0:0c1a6a29b8c4 65 };
abouillot 0:0c1a6a29b8c4 66 void setAwake(bool working);
abouillot 0:0c1a6a29b8c4 67 version_t getVersion();
abouillot 0:0c1a6a29b8c4 68
abouillot 0:0c1a6a29b8c4 69 float PM2_5() {
abouillot 0:0c1a6a29b8c4 70 return _PM2_5;
abouillot 0:0c1a6a29b8c4 71 };
abouillot 0:0c1a6a29b8c4 72 float PM10() {
abouillot 0:0c1a6a29b8c4 73 return _PM10;
abouillot 0:0c1a6a29b8c4 74 };
abouillot 0:0c1a6a29b8c4 75 bool mode() {
abouillot 0:0c1a6a29b8c4 76 return _mode;
abouillot 0:0c1a6a29b8c4 77 };
abouillot 0:0c1a6a29b8c4 78 bool state() {
abouillot 0:0c1a6a29b8c4 79 return _state;
abouillot 0:0c1a6a29b8c4 80 };
abouillot 0:0c1a6a29b8c4 81 char interval() {
abouillot 0:0c1a6a29b8c4 82 return _interval;
abouillot 0:0c1a6a29b8c4 83 };
abouillot 0:0c1a6a29b8c4 84 version_t firmware() {
abouillot 0:0c1a6a29b8c4 85 return _firmware;
abouillot 0:0c1a6a29b8c4 86 };
abouillot 0:0c1a6a29b8c4 87 int id() {
abouillot 0:0c1a6a29b8c4 88 return _id;
abouillot 0:0c1a6a29b8c4 89 };
abouillot 0:0c1a6a29b8c4 90
abouillot 0:0c1a6a29b8c4 91 private:
abouillot 0:0c1a6a29b8c4 92 /* constants and structures types */
abouillot 0:0c1a6a29b8c4 93 static const int kOutputLength = 19;
abouillot 0:0c1a6a29b8c4 94 static const int kInputLength = 10;
abouillot 0:0c1a6a29b8c4 95
abouillot 0:0c1a6a29b8c4 96 static const char head = 0xaa;
abouillot 0:0c1a6a29b8c4 97 static const char tail = 0xab;
abouillot 0:0c1a6a29b8c4 98
abouillot 0:0c1a6a29b8c4 99 // commands
abouillot 0:0c1a6a29b8c4 100 enum command_t {
abouillot 0:0c1a6a29b8c4 101 commandCmd = 0xB4,
abouillot 0:0c1a6a29b8c4 102 dataCmd = 0xC0,
abouillot 0:0c1a6a29b8c4 103 replyCmd = 0xC5
abouillot 0:0c1a6a29b8c4 104 };
abouillot 0:0c1a6a29b8c4 105
abouillot 0:0c1a6a29b8c4 106 // actions
abouillot 0:0c1a6a29b8c4 107 enum action_t {
abouillot 0:0c1a6a29b8c4 108 modeAct = 0x2,
abouillot 0:0c1a6a29b8c4 109 queryAct = 0x4,
abouillot 0:0c1a6a29b8c4 110 idAct = 0x5,
abouillot 0:0c1a6a29b8c4 111 stateAct = 0x6,
abouillot 0:0c1a6a29b8c4 112 versionAct = 0x7,
abouillot 0:0c1a6a29b8c4 113 intervalAct = 0x8
abouillot 0:0c1a6a29b8c4 114 };
abouillot 0:0c1a6a29b8c4 115
abouillot 0:0c1a6a29b8c4 116 private:
abouillot 0:0c1a6a29b8c4 117 /* lacal variables */
abouillot 0:0c1a6a29b8c4 118 char outMessage[kOutputLength];
abouillot 0:0c1a6a29b8c4 119 char inMessage[kInputLength];
abouillot 0:0c1a6a29b8c4 120
abouillot 1:fb3d61ffd503 121 char* prepareMessage(SDS021::action_t action, bool set = 0, int address = 0xFFFF, int value = 0);
abouillot 0:0c1a6a29b8c4 122 char calcCheckSum(char* buffer, int start_idx, int stop_idx);
abouillot 0:0c1a6a29b8c4 123 bool checkSum(char* buffer, int start_idx = 2, int stop_idx = 8);
abouillot 0:0c1a6a29b8c4 124 void writeMessage(char* buffer);
abouillot 0:0c1a6a29b8c4 125
abouillot 0:0c1a6a29b8c4 126 BufferedSerial _sds021;
abouillot 0:0c1a6a29b8c4 127 float _PM2_5;
abouillot 0:0c1a6a29b8c4 128 float _PM10;
abouillot 0:0c1a6a29b8c4 129 bool _mode;
abouillot 0:0c1a6a29b8c4 130 bool _state;
abouillot 0:0c1a6a29b8c4 131 char _interval;
abouillot 0:0c1a6a29b8c4 132 version_t _firmware;
abouillot 0:0c1a6a29b8c4 133 int _id;
abouillot 0:0c1a6a29b8c4 134 };
abouillot 0:0c1a6a29b8c4 135
abouillot 0:0c1a6a29b8c4 136 #endif // __SDS021_H__