Dust sensor
Fork of DustSenzor by
DustSenzor.cpp@10:d846eb9ef8e6, 2017-06-28 (annotated)
- Committer:
- mpuric
- Date:
- Wed Jun 28 18:35:18 2017 +0000
- Revision:
- 10:d846eb9ef8e6
- Parent:
- 7:067de0268900
added api doc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mpuric | 5:fd42a3f8c7eb | 1 | #include "mbed.h" |
mpuric | 5:fd42a3f8c7eb | 2 | #include "DustSenzor.h" |
mpuric | 2:4c3bdadb6bc4 | 3 | |
mpuric | 2:4c3bdadb6bc4 | 4 | |
mpuric | 7:067de0268900 | 5 | DustSenzor :: DustSenzor ( PinName set, PinName uartTx, PinName uartRx ) : set( set ), uart( uartTx, uartRx ) { |
mpuric | 5:fd42a3f8c7eb | 6 | this -> set = 0; |
mpuric | 2:4c3bdadb6bc4 | 7 | uart.baud(9600); |
mpuric | 5:fd42a3f8c7eb | 8 | uart.format(8,Serial::None,1); |
mpuric | 6:f6a93c155525 | 9 | uart.attach(this, &DustSenzor::serialRead, Serial::RxIrq); |
mpuric | 5:fd42a3f8c7eb | 10 | br = 0; |
mpuric | 5:fd42a3f8c7eb | 11 | } |
mpuric | 5:fd42a3f8c7eb | 12 | |
mpuric | 5:fd42a3f8c7eb | 13 | |
mpuric | 5:fd42a3f8c7eb | 14 | void DustSenzor::start() { |
mpuric | 5:fd42a3f8c7eb | 15 | this -> set = 1; |
mpuric | 2:4c3bdadb6bc4 | 16 | |
mpuric | 6:f6a93c155525 | 17 | } |
mpuric | 5:fd42a3f8c7eb | 18 | |
mpuric | 6:f6a93c155525 | 19 | void DustSenzor::stop() { |
mpuric | 5:fd42a3f8c7eb | 20 | this -> set = 0; |
mpuric | 6:f6a93c155525 | 21 | } |
mpuric | 5:fd42a3f8c7eb | 22 | |
mpuric | 5:fd42a3f8c7eb | 23 | float * DustSenzor::read() { |
mpuric | 5:fd42a3f8c7eb | 24 | float* ptr = new float[6]; |
mpuric | 6:f6a93c155525 | 25 | for(int i = 0; i < 6; i++) |
mpuric | 5:fd42a3f8c7eb | 26 | ptr[i] = value[i]; |
mpuric | 5:fd42a3f8c7eb | 27 | return ptr; |
mpuric | 6:f6a93c155525 | 28 | } |
mpuric | 3:e382316d6d00 | 29 | |
mpuric | 7:067de0268900 | 30 | void DustSenzor::readBuffer() { |
mpuric | 7:067de0268900 | 31 | if(buff[0] == 0x42 ){ |
mpuric | 7:067de0268900 | 32 | if(buff[1] == 0x4d) { |
mpuric | 7:067de0268900 | 33 | save(buff); |
mpuric | 7:067de0268900 | 34 | br = 0; |
mpuric | 7:067de0268900 | 35 | } |
mpuric | 7:067de0268900 | 36 | } |
mpuric | 7:067de0268900 | 37 | } |
mpuric | 7:067de0268900 | 38 | |
mpuric | 7:067de0268900 | 39 | |
mpuric | 6:f6a93c155525 | 40 | void DustSenzor::serialRead() { |
mpuric | 0:513b42c738f3 | 41 | buff[br]=uart.getc(); |
mpuric | 2:4c3bdadb6bc4 | 42 | br++; |
mpuric | 6:f6a93c155525 | 43 | if(br == 32){ |
mpuric | 6:f6a93c155525 | 44 | readBuffer(); |
mpuric | 3:e382316d6d00 | 45 | } |
mpuric | 6:f6a93c155525 | 46 | } |
mpuric | 0:513b42c738f3 | 47 | |
mpuric | 0:513b42c738f3 | 48 | |
mpuric | 2:4c3bdadb6bc4 | 49 | |
mpuric | 6:f6a93c155525 | 50 | void DustSenzor::save( unsigned char *thebuff) { |
mpuric | 6:f6a93c155525 | 51 | int j = 16; |
mpuric | 6:f6a93c155525 | 52 | for(int i = 0; i < 6; i++) { |
mpuric | 6:f6a93c155525 | 53 | value[i] = thebuff[j+1]|thebuff[j]<<8; |
mpuric | 5:fd42a3f8c7eb | 54 | j += 2; |
mpuric | 5:fd42a3f8c7eb | 55 | } |
mpuric | 5:fd42a3f8c7eb | 56 | } |
mpuric | 6:f6a93c155525 | 57 | |
mpuric | 6:f6a93c155525 | 58 | /* |
mpuric | 6:f6a93c155525 | 59 | * thebuff[17]|thebuff[16]<<8; number of particles with diameter beyond 0.3 um in 0.1 L of air. |
mpuric | 6:f6a93c155525 | 60 | * thebuff[19]|thebuff[18]<<8; number of particles with diameter beyond 0.5 um in 0.1 L of air. |
mpuric | 6:f6a93c155525 | 61 | * thebuff[21]|thebuff[20]<<8; number of particles with diameter beyond 1.0 um in 0.1 L of air. |
mpuric | 6:f6a93c155525 | 62 | |
mpuric | 6:f6a93c155525 | 63 | * thebuff[23]|thebuff[22]<<8; number of particles with diameter beyond 2.5 um in 0.1 L of air. |
mpuric | 6:f6a93c155525 | 64 | * thebuff[25]|thebuff[24]<<8; number of particles with diameter beyond 5 um in 0.1 L of air. |
mpuric | 6:f6a93c155525 | 65 | * thebuff[27]|thebuff[26]<<8; number of particles with diameter beyond 10 um in 0.1 L of air. |
mpuric | 6:f6a93c155525 | 66 | */ |