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.

71 lines
2.3KB

  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. // CV scaling functions.
  28. #include "tides/cv_scaler.h"
  29. #include "stmlib/system/storage.h"
  30. namespace tides {
  31. using namespace stmlib;
  32. /* static */
  33. const CvScaler::CalibrationData CvScaler::init_calibration_data_ = {
  34. 65535, // 64576
  35. 20653, // 20030
  36. -9387, // -9364
  37. 33100, // 33221
  38. -12673 // -12641
  39. };
  40. Storage<0x8020000, 1> storage;
  41. void CvScaler::Init() {
  42. if (!storage.ParsimoniousLoad(&calibration_data_, &version_token_)) {
  43. calibration_data_ = init_calibration_data_;
  44. }
  45. }
  46. void CvScaler::SaveCalibrationData() {
  47. storage.ParsimoniousSave(calibration_data_, &version_token_);
  48. }
  49. void CvScaler::Calibrate() {
  50. calibration_data_.v_oct_offset = (v_oct_c2_ + v_oct_) >> 1;
  51. int32_t delta = v_oct_ - v_oct_c2_;
  52. if (delta != 0) {
  53. calibration_data_.v_oct_scale = (1 << 15) * (24 << 7) / delta;
  54. }
  55. calibration_data_.fm_offset = fm_;
  56. calibration_data_.level_offset = level_raw_;
  57. calibration_data_.fm_scale = calibration_data_.v_oct_scale * 27 / 20;
  58. SaveCalibrationData();
  59. }
  60. } // namespace tides