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