PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

control_cs42448.h 3.0KB

3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef control_cs42448_h_
  27. #define control_cs42448_h_
  28. #include "AudioControl.h"
  29. #include <math.h>
  30. class AudioControlCS42448 : public AudioControl
  31. {
  32. public:
  33. AudioControlCS42448(void) : i2c_addr(0x48), muted(true) { }
  34. void setAddress(uint8_t addr) {
  35. i2c_addr = 0x48 | (addr & 3);
  36. }
  37. bool enable(void);
  38. bool disable(void) {
  39. return false;
  40. }
  41. bool volume(float level) {
  42. return volumeInteger(volumebyte(level));
  43. }
  44. bool inputLevel(float level) {
  45. return inputLevelInteger(inputlevelbyte(level));
  46. }
  47. bool inputSelect(int n) {
  48. return (n == 0) ? true : false;
  49. }
  50. bool volume(int channel, float level) {
  51. if (channel < 1 || channel > 8) return false;
  52. return volumeInteger(channel, volumebyte(level));
  53. }
  54. bool inputLevel(int channel, float level) {
  55. if (channel < 1 || channel > 6) return false;
  56. return inputLevelInteger(channel, inputlevelbyte(level));
  57. }
  58. private:
  59. bool volumeInteger(uint32_t n);
  60. bool volumeInteger(int channel, uint32_t n);
  61. bool inputLevelInteger(int32_t n);
  62. bool inputLevelInteger(int chnnel, int32_t n);
  63. // convert level to volume byte, section 6.9.1, page 50
  64. uint32_t volumebyte(float level) {
  65. if (level >= 1.0) return 0;
  66. if (level <= 0.0000003981) return 128;
  67. return roundf(log10f(level) * -20.0);
  68. }
  69. // convert level to input gain, section 6.11.1, page 51
  70. int32_t inputlevelbyte(float level) {
  71. if (level > 15.8489) return 48;
  72. if (level < 0.00063095734) return -128;
  73. return roundf(log10f(level) * 40.0);
  74. }
  75. bool write(uint32_t address, uint32_t data);
  76. bool write(uint32_t address, const void *data, uint32_t len);
  77. uint8_t i2c_addr;
  78. bool muted;
  79. };
  80. #endif