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.

103 lines
3.7KB

  1. // Copyright 2015 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. // CV reader.
  28. #include "marbles/cv_reader.h"
  29. #include <algorithm>
  30. #include "stmlib/dsp/dsp.h"
  31. namespace marbles {
  32. using namespace std;
  33. using namespace stmlib;
  34. /* static */
  35. const CvReaderChannel::Settings CvReader::channel_settings_[] = {
  36. // cv_lp | pot_scale | pot_offset | pot_lp | min | max | hysteresis
  37. // ADC_CHANNEL_DEJA_VU_AMOUNT,
  38. { 0.05f, 1.0f, 0.0f, 0.01f, 0.0f, 1.0f, 0.00f },
  39. // ADC_CHANNEL_X_SPREAD_2 / ADC_CHANNEL_DEJA_VU_LENGTH,
  40. { 0.05f, 1.0f, 0.0f, 0.01f, 0.0f, 1.0f, 0.00f },
  41. // ADC_CHANNEL_T_RATE,
  42. { 0.2f, 120.0f, -60.0f, 0.01f, -120.0f, 120.0f, 0.001f },
  43. // ADC_CHANNEL_T_BIAS,
  44. { 0.05f, 1.05f, -0.025f, 0.01f, 0.0f, 1.0f, 0.00f },
  45. // ADC_CHANNEL_T_JITTER,
  46. { 0.05f, 1.0f, 0.0f, 0.01f, 0.0f, 1.0f, 0.00f },
  47. // ADC_CHANNEL_X_SPREAD,
  48. { 0.1f, 1.0f, 0.0f, 0.01f, 0.0f, 1.0f, 0.01f },
  49. // ADC_CHANNEL_X_BIAS,
  50. { 0.1f, 1.0f, 0.0f, 0.01f, 0.0f, 1.0f, 0.02f },
  51. // ADC_CHANNEL_X_STEPS,
  52. { 0.05f, 1.0f, 0.0f, 0.01f, 0.0f, 1.0f, 0.02f },
  53. };
  54. void CvReader::Init(CalibrationData* calibration_data) {
  55. calibration_data_ = calibration_data;
  56. adc_.Init(false);
  57. for (int i = 0; i < ADC_CHANNEL_LAST; ++i) {
  58. channel_[i].Init(
  59. &calibration_data_->adc_scale[i],
  60. &calibration_data_->adc_offset[i],
  61. channel_settings_[i]);
  62. }
  63. fill(&attenuverter_[0], &attenuverter_[ADC_CHANNEL_LAST], 1.0f);
  64. // Set virtual attenuverter to 12 o'clock to ignore the non-existing
  65. // CV input for DEJA VU length.
  66. attenuverter_[ADC_CHANNEL_DEJA_VU_LENGTH] = 0.5f;
  67. // Set virtual attenuverter to a little more than 100% to
  68. // compensate for op-amp clipping and get full parameter swing.
  69. attenuverter_[ADC_CHANNEL_DEJA_VU_AMOUNT] = 1.01f;
  70. attenuverter_[ADC_CHANNEL_T_BIAS] = 1.01f;
  71. attenuverter_[ADC_CHANNEL_T_JITTER] = 1.01f;
  72. attenuverter_[ADC_CHANNEL_X_SPREAD] = 1.01f;
  73. attenuverter_[ADC_CHANNEL_X_BIAS] = 1.01f;
  74. attenuverter_[ADC_CHANNEL_X_STEPS] = 1.01f;
  75. }
  76. void CvReader::Copy(uint16_t* output) {
  77. const uint16_t* adc_values = adc_.values();
  78. copy(&adc_values[0], &adc_values[ADC_CHANNEL_LAST * 2], output);
  79. adc_.Convert();
  80. }
  81. void CvReader::Process(const uint16_t* raw_values, float* output) {
  82. for (int i = 0; i < ADC_CHANNEL_LAST; ++i) {
  83. output[i] = channel_[i].Process(
  84. static_cast<float>(raw_values[ADC_GROUP_POT + i]) / 65536.0f,
  85. static_cast<float>(raw_values[ADC_GROUP_CV + i]) / 65536.0f,
  86. attenuverter_[i]);
  87. }
  88. }
  89. } // namespace marbles