Grove soundsensor lib
soundsensor.cpp@1:29daa2bdd0c6, 2019-01-17 (annotated)
- Committer:
- math991e
- Date:
- Thu Jan 17 08:10:53 2019 +0000
- Revision:
- 1:29daa2bdd0c6
- Parent:
- 0:18d442efc99a
- Child:
- 3:372e67c123c3
Bug fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
math991e | 0:18d442efc99a | 1 | #include "soundsensor.h" |
math991e | 0:18d442efc99a | 2 | |
math991e | 0:18d442efc99a | 3 | soundsensor::soundsensor(PinName pin){ |
math991e | 1:29daa2bdd0c6 | 4 | soundsensor::setPin(pin); |
math991e | 0:18d442efc99a | 5 | } |
math991e | 0:18d442efc99a | 6 | |
math991e | 0:18d442efc99a | 7 | void soundsensor::setPin(PinName pin){ |
math991e | 0:18d442efc99a | 8 | this->_pin=pin; |
math991e | 0:18d442efc99a | 9 | } |
math991e | 0:18d442efc99a | 10 | |
math991e | 0:18d442efc99a | 11 | PinName soundsensor::getPin(){ |
math991e | 0:18d442efc99a | 12 | return this->_pin; |
math991e | 0:18d442efc99a | 13 | } |
math991e | 0:18d442efc99a | 14 | |
math991e | 0:18d442efc99a | 15 | float soundsensor::convertToDb(float value){ |
math991e | 0:18d442efc99a | 16 | return 16.801 * log (value/65535) + 9.872; |
math991e | 0:18d442efc99a | 17 | } |
math991e | 0:18d442efc99a | 18 | |
math991e | 0:18d442efc99a | 19 | float soundsensor::revertFromDb(float value){ |
math991e | 0:18d442efc99a | 20 | /** |
math991e | 0:18d442efc99a | 21 | *CODE NEEDS TO BE IMPLEMENTED |
math991e | 0:18d442efc99a | 22 | **/ |
math991e | 1:29daa2bdd0c6 | 23 | return 0; |
math991e | 0:18d442efc99a | 24 | } |
math991e | 0:18d442efc99a | 25 | |
math991e | 0:18d442efc99a | 26 | float soundsensor::listen(bool toDb){ |
math991e | 1:29daa2bdd0c6 | 27 | AnalogIn sensor(this->getPin()); |
math991e | 0:18d442efc99a | 28 | float value = sensor.read_u16(); |
math991e | 0:18d442efc99a | 29 | if(toDb){ |
math991e | 0:18d442efc99a | 30 | return this->convertToDb(value); |
math991e | 0:18d442efc99a | 31 | }else{ |
math991e | 0:18d442efc99a | 32 | return value; |
math991e | 0:18d442efc99a | 33 | } |
math991e | 0:18d442efc99a | 34 | } |