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 #ifndef CLOUDS_SETTINGS_H_
30 #define CLOUDS_SETTINGS_H_
31 
32 #include "stmlib/stmlib.h"
33 
34 #include "clouds/drivers/adc.h"
35 
36 namespace clouds {
37 
38 struct CalibrationData {
39   float pitch_offset;
40   float pitch_scale;
41   float offset[ADC_CHANNEL_LAST];
42 };
43 
44 struct State {
45   uint8_t quality;
46   uint8_t blend_parameter;
47   uint8_t playback_mode;
48   uint8_t blend_value[4];
49   uint8_t padding;
50 };
51 
52 struct SettingsData {
53   CalibrationData calibration_data; // 48 bytes
54   State state;  // 8 bytes
55   uint8_t padding[8];
56 };
57 
58 class PersistentBlock;
59 
60 class Settings {
61  public:
Settings()62   Settings() { }
~Settings()63   ~Settings() { }
64 
65   void Init();
66   void Save();
67 
sample_flash_data(uint32_t index)68   inline const uint32_t* sample_flash_data(uint32_t index) const {
69     return (const uint32_t*)(mutable_sample_flash_data(index));
70   }
71 
mutable_sample_flash_data(uint32_t index)72   inline uint32_t* mutable_sample_flash_data(uint32_t index) const {
73     return (uint32_t*)(0x08080000 + index * 0x0020000);
74   }
75 
sample_flash_sector(uint32_t index)76   inline uint32_t sample_flash_sector(uint32_t index) {
77     return index + 8;
78   }
79 
80   void SaveSampleMemory(
81       uint32_t index,
82       PersistentBlock* blocks,
83       size_t num_blocks);
84 
mutable_calibration_data()85   inline CalibrationData* mutable_calibration_data() {
86     return &data_.calibration_data;
87   }
88 
mutable_state()89   inline State* mutable_state() {
90     return &data_.state;
91   }
92 
state()93   inline const State& state() const {
94     return data_.state;
95   }
96 
97   // True when no calibration data has been found on flash sector 1, that is
98   // to say when the module has just been flashed.
freshly_baked()99   inline bool freshly_baked() const {
100     return freshly_baked_;
101   }
102 
103  private:
104   bool freshly_baked_;
105   SettingsData data_;
106   uint16_t version_token_;
107 
108   DISALLOW_COPY_AND_ASSIGN(Settings);
109 };
110 
111 }  // namespace clouds
112 
113 #endif  // CLOUDS_SETTINGS_H_
114