This library is used for student's experiment. Chiba Institute of Technology
Dependencies: QEI SoftwarePWM
Fork of adrobo by
Sound.cpp@1:45772cb4e01c, 2020-04-20 (annotated)
- Committer:
- yasuohayashibara
- Date:
- Mon Apr 20 04:07:37 2020 +0000
- Revision:
- 1:45772cb4e01c
- Parent:
- 0:4508c5b68135
add required libraries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yasuohayashibara | 0:4508c5b68135 | 1 | #include "Sound.h" |
yasuohayashibara | 0:4508c5b68135 | 2 | |
yasuohayashibara | 0:4508c5b68135 | 3 | Sound::Sound(PinName pin) |
yasuohayashibara | 0:4508c5b68135 | 4 | : pwm(pin) |
yasuohayashibara | 0:4508c5b68135 | 5 | { |
yasuohayashibara | 0:4508c5b68135 | 6 | this->write(0); |
yasuohayashibara | 0:4508c5b68135 | 7 | pwm.Enable(0, 1000000); |
yasuohayashibara | 0:4508c5b68135 | 8 | } |
yasuohayashibara | 0:4508c5b68135 | 9 | |
yasuohayashibara | 0:4508c5b68135 | 10 | void Sound::write(float frequency) |
yasuohayashibara | 0:4508c5b68135 | 11 | { |
yasuohayashibara | 0:4508c5b68135 | 12 | frequency_ = frequency; |
yasuohayashibara | 0:4508c5b68135 | 13 | if (frequency_ != 0){ |
yasuohayashibara | 0:4508c5b68135 | 14 | int us = (int)(1000000.0f / frequency_); |
yasuohayashibara | 0:4508c5b68135 | 15 | pwm.Disable(); |
yasuohayashibara | 0:4508c5b68135 | 16 | pwm.Enable(us/2, us); |
yasuohayashibara | 0:4508c5b68135 | 17 | } else { |
yasuohayashibara | 0:4508c5b68135 | 18 | pwm.Disable(); |
yasuohayashibara | 0:4508c5b68135 | 19 | } |
yasuohayashibara | 0:4508c5b68135 | 20 | } |
yasuohayashibara | 0:4508c5b68135 | 21 | |
yasuohayashibara | 0:4508c5b68135 | 22 | float Sound::read() |
yasuohayashibara | 0:4508c5b68135 | 23 | { |
yasuohayashibara | 0:4508c5b68135 | 24 | return frequency_; |
yasuohayashibara | 0:4508c5b68135 | 25 | } |