1 // Copyright 2015 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 // User interface.
28 
29 #ifndef MARBLES_UI_H_
30 #define MARBLES_UI_H_
31 
32 #include "stmlib/stmlib.h"
33 
34 #include "stmlib/ui/event_queue.h"
35 
36 #include "marbles/drivers/adc.h"
37 #include "marbles/drivers/leds.h"
38 #include "marbles/drivers/switches.h"
39 #include "marbles/settings.h"
40 
41 namespace marbles {
42 
43 class ClockInputs;
44 class CvReader;
45 class ScaleRecorder;
46 
47 enum UiMode {
48   UI_MODE_NORMAL,
49   UI_MODE_SELECT_SCALE,
50   UI_MODE_RECORD_SCALE,
51   UI_MODE_CALIBRATION_1,
52   UI_MODE_CALIBRATION_2,
53   UI_MODE_CALIBRATION_3,
54   UI_MODE_CALIBRATION_4,
55   UI_MODE_PANIC,
56 };
57 
58 enum FactoryTestingCommand {
59   FACTORY_TESTING_READ_POT,
60   FACTORY_TESTING_READ_CV,
61   FACTORY_TESTING_READ_GATE,
62   FACTORY_TESTING_GENERATE_TEST_SIGNALS,
63   FACTORY_TESTING_CALIBRATE,
64   FACTORY_TESTING_READ_NORMALIZATION,
65   FACTORY_TESTING_FORCE_DAC_CODE,
66   FACTORY_TESTING_WRITE_CALIBRATION_DATA_NIBBLE,
67 };
68 
69 struct AlternateKnobMapping {
70   Switch unlock_switch;
71   uint8_t* destination;
72 };
73 
74 class Ui {
75  public:
Ui()76   Ui() { }
~Ui()77   ~Ui() { }
78 
79   void Init(
80       Settings* settings,
81       CvReader* cv_reader,
82       ScaleRecorder* scale_recorder,
83       ClockInputs* clock_inputs);
84   void Poll();
85   void DoEvents();
86   void FlushEvents();
87   uint8_t HandleFactoryTestingRequest(uint8_t command);
88 
recording_scale()89   bool recording_scale() const {
90     return mode_ == UI_MODE_RECORD_SCALE;
91   }
92 
output_test_mode()93   bool output_test_mode() const {
94     return output_test_mode_;
95   }
96 
output_test_forced_dac_code(int i)97   uint16_t output_test_forced_dac_code(int i) const {
98     return output_test_forced_dac_code_[i];
99   }
100 
set_deja_vu_lock(bool deja_vu_lock)101   void set_deja_vu_lock(bool deja_vu_lock) {
102     deja_vu_lock_ = deja_vu_lock;
103   }
104 
105  private:
106   void UpdateLEDs();
107   void OnSwitchPressed(const stmlib::Event& e);
108   void OnSwitchReleased(const stmlib::Event& e);
109   void NextCalibrationStep();
110   void SaveState();
111   void UpdateHiddenParameters();
112   void TerminateScaleRecording();
113   static LedColor MakeColor(uint8_t value, bool color_blind);
114   static LedColor DejaVuColor(DejaVuState state, bool lock);
115 
116   stmlib::EventQueue<16> queue_;
117 
118   Leds leds_;
119   Switches switches_;
120   uint32_t press_time_[SWITCH_LAST];
121   bool ignore_release_[SWITCH_LAST];
122 
123   UiMode mode_;
124 
125   Settings* settings_;
126   CvReader* cv_reader_;
127   ScaleRecorder* scale_recorder_;
128   ClockInputs* clock_inputs_;
129 
130   static const LedColor palette_[4];
131   static AlternateKnobMapping alternate_knob_mappings_[ADC_CHANNEL_LAST];
132 
133   float pot_value_[ADC_CHANNEL_LAST];
134 
135   bool setting_modification_flag_;
136   bool deja_vu_lock_;
137 
138   bool output_test_mode_;
139   uint16_t output_test_forced_dac_code_[4];
140   uint32_t calibration_data_;
141 
142   DISALLOW_COPY_AND_ASSIGN(Ui);
143 };
144 
145 }  // namespace marbles
146 
147 #endif  // MARBLES_UI_H_
148