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.

153 lines
3.6KB

  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. // Settings storage.
  28. #ifndef MARBLES_SETTINGS_H_
  29. #define MARBLES_SETTINGS_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/dsp/dsp.h"
  32. #include "stmlib/system/storage.h"
  33. #include "marbles/drivers/adc.h"
  34. #include "marbles/drivers/dac.h"
  35. #include "marbles/random/quantizer.h"
  36. namespace marbles {
  37. struct CalibrationData {
  38. float adc_offset[ADC_CHANNEL_LAST];
  39. float adc_scale[ADC_CHANNEL_LAST];
  40. float dac_offset[DAC_CHANNEL_LAST];
  41. float dac_scale[DAC_CHANNEL_LAST];
  42. };
  43. const int kNumScales = 6;
  44. struct PersistentData {
  45. CalibrationData calibration_data;
  46. Scale scale[kNumScales];
  47. uint8_t padding[16];
  48. enum { tag = 0x494C4143 };
  49. };
  50. struct State {
  51. uint8_t t_deja_vu;
  52. uint8_t t_model;
  53. uint8_t t_range;
  54. uint8_t t_pulse_width_mean;
  55. uint8_t t_pulse_width_std;
  56. uint8_t x_deja_vu;
  57. uint8_t x_control_mode;
  58. uint8_t x_register_mode;
  59. uint8_t x_range;
  60. uint8_t x_scale;
  61. uint8_t y_spread;
  62. uint8_t y_bias;
  63. uint8_t y_steps;
  64. uint8_t y_divider;
  65. uint8_t y_range;
  66. uint8_t color_blind;
  67. uint8_t padding[8];
  68. enum { tag = 0x54415453 };
  69. };
  70. class Settings {
  71. public:
  72. Settings() { }
  73. ~Settings() { }
  74. void Init();
  75. void SavePersistentData();
  76. void SaveState();
  77. void ResetScale(int i);
  78. static void ProgramOptionBytes();
  79. inline const CalibrationData& calibration_data() {
  80. return persistent_data_.calibration_data;
  81. }
  82. inline CalibrationData* mutable_calibration_data() {
  83. return &persistent_data_.calibration_data;
  84. }
  85. inline const Scale& scale(int i) const {
  86. return persistent_data_.scale[i];
  87. }
  88. inline Scale* mutable_scale(int i) {
  89. return &persistent_data_.scale[i];
  90. }
  91. inline const State& state() const {
  92. return state_;
  93. }
  94. inline State* mutable_state() {
  95. return &state_;
  96. }
  97. inline const PersistentData& persistent_data() const {
  98. return persistent_data_;
  99. }
  100. inline bool freshly_baked() const {
  101. return freshly_baked_;
  102. }
  103. inline void set_dirty_scale_index(int i) {
  104. dirty_scale_index_ = i;
  105. }
  106. inline int dirty_scale_index() const {
  107. return dirty_scale_index_;
  108. }
  109. private:
  110. bool freshly_baked_;
  111. int dirty_scale_index_;
  112. PersistentData persistent_data_;
  113. State state_;
  114. stmlib::ChunkStorage<1, PersistentData, State> chunk_storage_;
  115. DISALLOW_COPY_AND_ASSIGN(Settings);
  116. };
  117. } // namespace marbles
  118. #endif // MARBLES_SETTINGS_H_