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.

90 lines
3.0KB

  1. // Copyright 2014 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 "clouds/settings.h"
  29. #include "stmlib/system/storage.h"
  30. #include "clouds/dsp/granular_processor.h"
  31. namespace clouds {
  32. stmlib::Storage<1> storage;
  33. void Settings::Init() {
  34. freshly_baked_ = false;
  35. if (!storage.ParsimoniousLoad(&data_, &version_token_)) {
  36. data_.calibration_data.pitch_offset = 66.67f;
  37. data_.calibration_data.pitch_scale = -84.26f;
  38. for (size_t i = 0; i < ADC_CHANNEL_LAST; ++i) {
  39. data_.calibration_data.offset[i] = 0.505f;
  40. }
  41. data_.state.quality = 0;
  42. data_.state.blend_parameter = 0;
  43. data_.state.playback_mode = PLAYBACK_MODE_GRANULAR;
  44. data_.state.blend_value[0] = 255;
  45. data_.state.blend_value[1] = 128;
  46. data_.state.blend_value[2] = 0;
  47. data_.state.blend_value[3] = 0;
  48. freshly_baked_ = true;
  49. Save();
  50. }
  51. }
  52. void Settings::SaveSampleMemory(
  53. uint32_t index,
  54. PersistentBlock* blocks,
  55. size_t num_blocks) {
  56. uint32_t* data = mutable_sample_flash_data(index);
  57. // Unprotect flash and erase sector.
  58. FLASH_Unlock();
  59. FLASH_ClearFlag(
  60. FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
  61. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_PGSERR);
  62. FLASH_EraseSector(sample_flash_sector(index) * 8, VoltageRange_3);
  63. // Write all data blocks.
  64. for (size_t block = 0; block < num_blocks; ++block) {
  65. FLASH_ProgramWord((uint32_t)(data++), blocks[block].tag);
  66. FLASH_ProgramWord((uint32_t)(data++), blocks[block].size);
  67. size_t size = blocks[block].size;
  68. const uint32_t* words = (const uint32_t*)(blocks[block].data);
  69. while (size >= 4) {
  70. FLASH_ProgramWord((uint32_t)(data++), *words++);
  71. size -= 4;
  72. }
  73. }
  74. }
  75. void Settings::Save() {
  76. storage.ParsimoniousSave(data_, &version_token_);
  77. }
  78. } // namespace clouds