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
Speaker.cpp@20:e068469ffb89, 2018-04-20 (annotated)
- Committer:
- Nurchu
- Date:
- Fri Apr 20 18:39:20 2018 +0000
- Revision:
- 20:e068469ffb89
- Parent:
- 14:4637a9f02919
- Child:
- 33:5d86c111d9bc
Cleaned up some comments/code; Compiles now
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Nurchu | 14:4637a9f02919 | 1 | #include "Speaker.h" |
| Nurchu | 14:4637a9f02919 | 2 | #define UINT8_MAX 255 |
| Nurchu | 14:4637a9f02919 | 3 | |
| Nurchu | 14:4637a9f02919 | 4 | Speaker::Speaker(PinName pin) : _pin(pin), dc(0.67f / 3.3f) {} |
| Nurchu | 12:efcfe4c0d9f2 | 5 | |
| Nurchu | 14:4637a9f02919 | 6 | void Speaker::write(uint8_t value) { |
| Nurchu | 14:4637a9f02919 | 7 | const static float alpha = 0.99999f; |
| Nurchu | 14:4637a9f02919 | 8 | float sample = (float)value / UINT8_MAX; |
| Nurchu | 14:4637a9f02919 | 9 | |
| Nurchu | 14:4637a9f02919 | 10 | dc = alpha*dc + (1.0f - alpha)*sample; |
| Nurchu | 14:4637a9f02919 | 11 | |
| Nurchu | 14:4637a9f02919 | 12 | _pin = 0.5f + (sample - dc)* 33.0f; |
| Nurchu | 20:e068469ffb89 | 13 | } |
| Nurchu | 20:e068469ffb89 | 14 | |
| Nurchu | 20:e068469ffb89 | 15 | void Speaker::turnOff() { |
| Nurchu | 20:e068469ffb89 | 16 | _pin = 0.0f; |
| Nurchu | 14:4637a9f02919 | 17 | } |