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.

247 lines
6.5KB

  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. #include "marbles/settings.h"
  29. #include <algorithm>
  30. #include "stmlib/system/storage.h"
  31. namespace marbles {
  32. using namespace std;
  33. const Scale preset_scales[6] = {
  34. // C major
  35. {
  36. 1.0f,
  37. 12,
  38. {
  39. { 0.0000f, 255 }, // C
  40. { 0.0833f, 16 }, // C#
  41. { 0.1667f, 96 }, // D
  42. { 0.2500f, 24 }, // D#
  43. { 0.3333f, 128 }, // E
  44. { 0.4167f, 64 }, // F
  45. { 0.5000f, 8 }, // F#
  46. { 0.5833f, 192 }, // G
  47. { 0.6667f, 16 }, // G#
  48. { 0.7500f, 96 }, // A
  49. { 0.8333f, 24 }, // A#
  50. { 0.9167f, 128 }, // B
  51. }
  52. },
  53. // C minor
  54. {
  55. 1.0f,
  56. 12,
  57. {
  58. { 0.0000f, 255 }, // C
  59. { 0.0833f, 16 }, // C#
  60. { 0.1667f, 96 }, // D
  61. { 0.2500f, 128 }, // Eb
  62. { 0.3333f, 8 }, // E
  63. { 0.4167f, 64 }, // F
  64. { 0.5000f, 4 }, // F#
  65. { 0.5833f, 192 }, // G
  66. { 0.6667f, 96 }, // G#
  67. { 0.7500f, 16 }, // A
  68. { 0.8333f, 128 }, // Bb
  69. { 0.9167f, 16 }, // B
  70. }
  71. },
  72. // Pentatonic
  73. {
  74. 1.0f,
  75. 12,
  76. {
  77. { 0.0000f, 255 }, // C
  78. { 0.0833f, 4 }, // C#
  79. { 0.1667f, 96 }, // D
  80. { 0.2500f, 4 }, // Eb
  81. { 0.3333f, 4 }, // E
  82. { 0.4167f, 140 }, // F
  83. { 0.5000f, 4 }, // F#
  84. { 0.5833f, 192 }, // G
  85. { 0.6667f, 4 }, // G#
  86. { 0.7500f, 96 }, // A
  87. { 0.8333f, 4 }, // Bb
  88. { 0.9167f, 4 }, // B
  89. }
  90. },
  91. // Pelog
  92. {
  93. 1.0f,
  94. 7,
  95. {
  96. { 0.0000f, 255 }, // C
  97. { 0.1275f, 128 }, // Db+
  98. { 0.2625f, 32 }, // Eb-
  99. { 0.4600f, 8 }, // F#-
  100. { 0.5883f, 192 }, // G
  101. { 0.7067f, 64 }, // Ab
  102. { 0.8817f, 16 }, // Bb+
  103. }
  104. },
  105. // Raag Bhairav That
  106. {
  107. 1.0f,
  108. 12,
  109. {
  110. { 0.0000f, 255 }, // ** Sa
  111. { 0.0752f, 128 }, // ** Komal Re
  112. { 0.1699f, 4 }, // Re
  113. { 0.2630f, 4 }, // Komal Ga
  114. { 0.3219f, 128 }, // ** Ga
  115. { 0.4150f, 64 }, // ** Ma
  116. { 0.4918f, 4 }, // Tivre Ma
  117. { 0.5850f, 192 }, // ** Pa
  118. { 0.6601f, 64 }, // ** Komal Dha
  119. { 0.7549f, 4 }, // Dha
  120. { 0.8479f, 4 }, // Komal Ni
  121. { 0.9069f, 64 }, // ** Ni
  122. }
  123. },
  124. // Raag Shri
  125. {
  126. 1.0f,
  127. 12,
  128. {
  129. { 0.0000f, 255 }, // ** Sa
  130. { 0.0752f, 4 }, // Komal Re
  131. { 0.1699f, 128 }, // ** Re
  132. { 0.2630f, 64 }, // ** Komal Ga
  133. { 0.3219f, 4 }, // Ga
  134. { 0.4150f, 128 }, // ** Ma
  135. { 0.4918f, 4 }, // Tivre Ma
  136. { 0.5850f, 192 }, // ** Pa
  137. { 0.6601f, 4 }, // Komal Dha
  138. { 0.7549f, 64 }, // ** Dha
  139. { 0.8479f, 128 }, // ** Komal Ni
  140. { 0.9069f, 4 }, // Ni
  141. }
  142. },
  143. };
  144. #define FIX_OUTLIER(destination, expected_value) if (fabsf(destination / expected_value - 1.0f) > 0.1f) { destination = expected_value; }
  145. void Settings::ResetScale(int i) {
  146. persistent_data_.scale[i] = preset_scales[i];
  147. }
  148. void Settings::Init() {
  149. freshly_baked_ = false;
  150. // Set default values for all calibration and state settings.
  151. // This settings will be written to flash memory the first time the module
  152. // is powered on, or if corrupted data is found in the flash sector,
  153. // following a major firmware upgrade.
  154. CalibrationData& c = persistent_data_.calibration_data;
  155. fill(&c.adc_scale[0], &c.adc_scale[ADC_CHANNEL_LAST], -2.0f);
  156. fill(&c.adc_offset[0], &c.adc_offset[ADC_CHANNEL_LAST], +1.0f);
  157. fill(&c.dac_scale[0], &c.dac_scale[DAC_CHANNEL_LAST], -6212.8f);
  158. fill(&c.dac_offset[0], &c.dac_offset[DAC_CHANNEL_LAST], 32768.0f);
  159. c.adc_offset[ADC_CHANNEL_T_RATE] = 60.0f;
  160. c.adc_scale[ADC_CHANNEL_T_RATE] = -120.0f;
  161. for (size_t i = 0; i < kNumScales; ++i) {
  162. ResetScale(i);
  163. }
  164. state_.t_deja_vu = 0;
  165. state_.t_model = 0;
  166. state_.t_range = 1;
  167. state_.t_pulse_width_mean = 128;
  168. state_.t_pulse_width_std = 0;
  169. state_.x_deja_vu = 0;
  170. state_.x_control_mode = 0;
  171. state_.x_register_mode = 0;
  172. state_.x_range = 2;
  173. state_.x_scale = 0;
  174. state_.y_spread = 128;
  175. state_.y_bias = 128;
  176. state_.y_steps = 0;
  177. state_.y_divider = 128;
  178. state_.y_range = 2;
  179. state_.color_blind = 0;
  180. freshly_baked_ = !chunk_storage_.Init(&persistent_data_, &state_);
  181. if (!freshly_baked_) {
  182. CONSTRAIN(state_.t_model, 0, 5);
  183. CONSTRAIN(state_.t_range, 0, 2);
  184. CONSTRAIN(state_.x_control_mode, 0, 2);
  185. CONSTRAIN(state_.x_range, 0, 2);
  186. CONSTRAIN(state_.x_scale, 0, 5);
  187. CONSTRAIN(state_.y_range, 0, 2);
  188. CalibrationData& c = persistent_data_.calibration_data;
  189. for (size_t i = 0; i < ADC_CHANNEL_LAST; ++i) {
  190. if (i == ADC_CHANNEL_T_RATE) {
  191. FIX_OUTLIER(c.adc_scale[i], -120.0f);
  192. FIX_OUTLIER(c.adc_offset[i], 60.0f);
  193. } else {
  194. FIX_OUTLIER(c.adc_scale[i], -2.0f);
  195. FIX_OUTLIER(c.adc_offset[i], +1.0f);
  196. }
  197. }
  198. for (size_t i = 0; i < DAC_CHANNEL_LAST; ++i) {
  199. FIX_OUTLIER(c.dac_scale[i], -6212.8f);
  200. FIX_OUTLIER(c.dac_offset[i], 32768.0f);
  201. }
  202. }
  203. }
  204. void Settings::SavePersistentData() {
  205. chunk_storage_.SavePersistentData();
  206. }
  207. void Settings::SaveState() {
  208. chunk_storage_.SaveState();
  209. }
  210. /* static */
  211. void Settings::ProgramOptionBytes() {
  212. FLASH_Unlock();
  213. FLASH_OB_Unlock();
  214. FLASH_OB_BORConfig(OB_BOR_OFF);
  215. FLASH_OB_Launch();
  216. FLASH_OB_Lock();
  217. }
  218. } // namespace marbles