Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed 4DGL-uLCD-SE mbed-rtos nRF24L01P
Microphone.cpp@33:5d86c111d9bc, 2018-04-25 (annotated)
- Committer:
- Nurchu
- Date:
- Wed Apr 25 15:39:03 2018 +0000
- Revision:
- 33:5d86c111d9bc
- Parent:
- 8:0222df74596e
Fixed a few bugs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nurchu | 2:dc046ff72566 | 1 | #include "Microphone.h" |
Nurchu | 7:0ac1f1ca8aa6 | 2 | #define UINT8_MAX 255 |
Nurchu | 2:dc046ff72566 | 3 | |
Nurchu | 6:7c72902a735f | 4 | Microphone::Microphone (PinName pin): |
Nurchu | 33:5d86c111d9bc | 5 | _pin(pin), dc(.67 / 3.3) |
Nurchu | 2:dc046ff72566 | 6 | {} |
Nurchu | 2:dc046ff72566 | 7 | |
Nurchu | 6:7c72902a735f | 8 | float Microphone::read() |
Nurchu | 2:dc046ff72566 | 9 | { |
Nurchu | 2:dc046ff72566 | 10 | return _pin.read(); |
Nurchu | 2:dc046ff72566 | 11 | } |
Nurchu | 2:dc046ff72566 | 12 | |
Nurchu | 6:7c72902a735f | 13 | inline Microphone::operator float () |
Nurchu | 2:dc046ff72566 | 14 | { |
Nurchu | 2:dc046ff72566 | 15 | return _pin.read(); |
Nurchu | 6:7c72902a735f | 16 | } |
Nurchu | 6:7c72902a735f | 17 | |
Nurchu | 6:7c72902a735f | 18 | uint8_t Microphone::getData() |
Nurchu | 6:7c72902a735f | 19 | { |
Nurchu | 7:0ac1f1ca8aa6 | 20 | // This can be better but this should work for now |
Nurchu | 33:5d86c111d9bc | 21 | const static float alpha = 0.99999f; |
Nurchu | 33:5d86c111d9bc | 22 | float sample = (float)read(); |
Nurchu | 33:5d86c111d9bc | 23 | |
Nurchu | 33:5d86c111d9bc | 24 | dc = alpha*dc + (1.0f - alpha)*sample; |
Nurchu | 33:5d86c111d9bc | 25 | |
Nurchu | 33:5d86c111d9bc | 26 | sample = 0.5f + (sample - dc)* 33.0f; |
Nurchu | 33:5d86c111d9bc | 27 | return sample * UINT8_MAX; |
Nurchu | 2:dc046ff72566 | 28 | } |