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 // User interface.
28
29 #include "elements/ui.h"
30
31 #include <algorithm>
32
33 #include "stmlib/system/system_clock.h"
34
35 #include "elements/cv_scaler.h"
36 #include "elements/dsp/part.h"
37
38 namespace elements {
39
40 using namespace std;
41 using namespace stmlib;
42
Init(Part * part,CvScaler * cv_scaler)43 void Ui::Init(Part* part, CvScaler* cv_scaler) {
44 leds_.Init();
45 switch_.Init();
46 mode_ = UI_MODE_NORMAL;
47 part_ = part;
48 cv_scaler_ = cv_scaler;
49 part_->set_easter_egg(cv_scaler_->boot_in_easter_egg_mode());
50 }
51
Poll()52 void Ui::Poll() {
53 // 1kHz.
54 system_clock.Tick();
55 switch_.Debounce();
56 if (switch_.just_pressed()) {
57 queue_.AddEvent(CONTROL_SWITCH, 0, 0);
58 press_time_ = system_clock.milliseconds();
59 }
60
61 if (switch_.pressed() && \
62 press_time_ &&
63 (system_clock.milliseconds() - press_time_) >= 5000) {
64 if (cv_scaler_->ready_for_calibration()) {
65 queue_.AddEvent(CONTROL_SWITCH, 1, 0);
66 press_time_ = 0;
67 } else if (cv_scaler_->easter_egg()) {
68 queue_.AddEvent(CONTROL_SWITCH, 2, 0);
69 press_time_ = 0;
70 }
71 }
72
73 if (switch_.released() && press_time_) {
74 queue_.AddEvent(
75 CONTROL_SWITCH,
76 0,
77 system_clock.milliseconds() - press_time_ + 1);
78 }
79
80 bool blink = (system_clock.milliseconds() & 127) > 64;
81 switch (mode_) {
82 case UI_MODE_NORMAL:
83 leds_.set_gate(part_->gate());
84 leds_.set_exciter(
85 lut_db_led_brightness[int32_t(part_->exciter_level() * 512.0f)]);
86 leds_.set_resonator(
87 lut_db_led_brightness[int32_t(part_->resonator_level() * 512.0f)]);
88 break;
89
90 case UI_MODE_CALIBRATION_1:
91 leds_.set_gate(!blink);
92 leds_.set_exciter(blink ? 255 : 0);
93 leds_.set_resonator(0);
94 break;
95
96 case UI_MODE_CALIBRATION_2:
97 leds_.set_gate(!blink);
98 leds_.set_exciter(0);
99 leds_.set_resonator(blink ? 255 : 0);
100 break;
101
102 case UI_MODE_PANIC:
103 leds_.set_gate(blink);
104 leds_.set_exciter(blink ? 255 : 0);
105 leds_.set_resonator(blink ? 0 : 255);
106 break;
107 }
108
109 if (part_->bypass()) {
110 leds_.set_gate(true);
111 leds_.set_exciter(255);
112 leds_.set_resonator(255);
113 }
114
115 leds_.Write();
116 }
117
FlushEvents()118 void Ui::FlushEvents() {
119 queue_.Flush();
120 }
121
OnSwitchPressed(const Event & e)122 void Ui::OnSwitchPressed(const Event& e) {
123 switch (e.control_id) {
124 case 0:
125 if (mode_ == UI_MODE_CALIBRATION_1) {
126 CalibrateC1();
127 } else if (mode_ == UI_MODE_CALIBRATION_2) {
128 CalibrateC3();
129 } else {
130 gate_ = true;
131 }
132 break;
133
134 case 1:
135 mode_ = UI_MODE_CALIBRATION_1;
136 break;
137
138 case 2:
139 part_->set_easter_egg(!part_->easter_egg());
140 cv_scaler_->set_boot_in_easter_egg_mode(part_->easter_egg());
141 cv_scaler_->SaveCalibration();
142 gate_ = false;
143 break;
144
145 default:
146 break;
147 }
148 }
149
OnSwitchReleased(const Event & e)150 void Ui::OnSwitchReleased(const Event& e) {
151 gate_ = false;
152 }
153
CalibrateC1()154 void Ui::CalibrateC1() {
155 cv_scaler_->CalibrateC1();
156 cv_scaler_->CalibrateOffsets();
157 mode_ = UI_MODE_CALIBRATION_2;
158 }
159
CalibrateC3()160 void Ui::CalibrateC3() {
161 bool success = cv_scaler_->CalibrateC3();
162 if (success) {
163 cv_scaler_->SaveCalibration();
164 mode_ = UI_MODE_NORMAL;
165 } else {
166 mode_ = UI_MODE_PANIC;
167 }
168 }
169
DoEvents()170 void Ui::DoEvents() {
171 while (queue_.available()) {
172 Event e = queue_.PullEvent();
173 if (e.control_type == CONTROL_SWITCH) {
174 if (e.data == 0) {
175 OnSwitchPressed(e);
176 } else {
177 OnSwitchReleased(e);
178 }
179 }
180 }
181 if (queue_.idle_time() > 800 && mode_ == UI_MODE_PANIC) {
182 mode_ = UI_MODE_NORMAL;
183 }
184 if (queue_.idle_time() > 1000) {
185 queue_.Touch();
186 }
187 }
188
HandleFactoryTestingRequest(uint8_t command)189 uint8_t Ui::HandleFactoryTestingRequest(uint8_t command) {
190 uint8_t argument = command & 0x1f;
191 command = command >> 5;
192 uint8_t reply = 0;
193 switch (command) {
194 case FACTORY_TESTING_READ_POT:
195 reply = cv_scaler_->pot_value(argument);
196 break;
197
198 case FACTORY_TESTING_READ_CV:
199 reply = cv_scaler_->cv_value(argument);
200 break;
201
202 case FACTORY_TESTING_READ_GATE:
203 if (argument == 0x00) {
204 return gate_;
205 } else {
206 return cv_scaler_->gate();
207 }
208 break;
209
210 case FACTORY_TESTING_SET_BYPASS:
211 part_->set_bypass(argument);
212 break;
213
214 case FACTORY_TESTING_CALIBRATE:
215 if (argument == 0) {
216 mode_ = UI_MODE_CALIBRATION_1;
217 } else if (argument == 1) {
218 CalibrateC1();
219 } else {
220 CalibrateC3();
221
222 }
223 break;
224 }
225 return reply;
226 }
227
228 } // namespace elements
229