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.

109 lines
3.2KB

  1. // Copyright 2013 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Calibration settings.
  28. #ifndef STREAMS_CV_SCALER_H_
  29. #define STREAMS_CV_SCALER_H_
  30. #include "stmlib/stmlib.h"
  31. #include "streams/cv_scaler.h"
  32. #include "streams/drivers/adc.h"
  33. namespace streams {
  34. const uint8_t kNumChannels = 2;
  35. struct CalibrationSettings {
  36. uint16_t excite_offset[kNumChannels];
  37. uint16_t audio_offset[kNumChannels];
  38. uint16_t dac_offset[kNumChannels];
  39. uint16_t padding[2];
  40. };
  41. class CvScaler {
  42. public:
  43. CvScaler() { }
  44. ~CvScaler() { }
  45. void Init(Adc* adc);
  46. void CaptureAdcOffsets();
  47. void SaveCalibrationData();
  48. bool can_calibrate() const;
  49. // This class owns the calibration data and is reponsible for removing
  50. // offsets.
  51. inline int16_t audio_sample(uint8_t channel) const {
  52. int32_t value = static_cast<int32_t>(
  53. calibration_settings_.audio_offset[channel]) - \
  54. adc_->cv(channel * 3 + 1);
  55. CLIP(value);
  56. return value;
  57. }
  58. inline int16_t excite_sample(uint8_t channel) const {
  59. int32_t value = static_cast<int32_t>(
  60. calibration_settings_.excite_offset[channel]) - \
  61. adc_->cv(channel * 3);
  62. CLIP(value);
  63. return value;
  64. }
  65. inline int32_t gain_sample(uint8_t channel) const {
  66. return -2 * adc_->cv(channel * 3 + 2);
  67. }
  68. inline int32_t raw_gain_sample(uint8_t channel) const {
  69. return adc_->cv(channel * 3 + 2);
  70. }
  71. inline void set_dac_offset(uint8_t channel, uint16_t offset) {
  72. calibration_settings_.dac_offset[channel] = offset;
  73. }
  74. inline uint16_t ScaleGain(uint8_t channel, uint16_t gain) const {
  75. int32_t g = static_cast<int32_t>(gain);
  76. g += static_cast<int32_t>(calibration_settings_.dac_offset[channel]);
  77. if (g > 65535) {
  78. g = 65535;
  79. }
  80. return static_cast<uint16_t>(g);
  81. }
  82. private:
  83. Adc* adc_;
  84. CalibrationSettings calibration_settings_;
  85. DISALLOW_COPY_AND_ASSIGN(CvScaler);
  86. };
  87. } // namespace streams
  88. #endif // STREAMS_CV_SCALER_H_