-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathOrganelle.cpp
More file actions
1262 lines (1052 loc) · 36.6 KB
/
Organelle.cpp
File metadata and controls
1262 lines (1052 loc) · 36.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Organelle.h"
#include <osc/OscOutboundPacketStream.h>
//#include <cmath>
#include <algorithm>
#include "../../m_pd.h"
static const unsigned SCREEN_WIDTH = 21;
static const unsigned PAGE_SWITCH_TIMEOUT = 50;
static const unsigned MODULE_SWITCH_TIMEOUT = 50;
//static const int PAGE_EXIT_TIMEOUT = 5;
static const unsigned MENU_TIMEOUT = 350;
const int8_t PATCH_SCREEN = 3;
static const float MAX_POT_VALUE = 1023.0F;
char Organelle::screenBuf_[Organelle::OUTPUT_BUFFER_SIZE];
static const char *OSC_MOTHER_HOST = "127.0.0.1";
static const unsigned OSC_MOTHER_PORT = 4001;
static const unsigned MOTHER_WRITE_POLL_WAIT_TIMEOUT = 1000;
static const unsigned ORGANELLE_NUM_TEXTLINES = 4;
static const unsigned ORGANELLE_NUM_PARAMS = 4;
enum OrganelleModes {
OM_PARAMETER,
OM_MAINMENU,
OM_PRESETMENU,
OM_MODULEMENU,
OM_MODULESELECTMENU
};
class OBaseMode : public DeviceMode {
public:
OBaseMode(Organelle &p) : parent_(p), popupTime_(-1) { ; }
bool init() override { return true; }
void poll() override;
void changePot(unsigned, float) override { ; }
void changeEncoder(unsigned, float) override { ; }
void encoderButton(unsigned, bool) override { ; }
void keyPress(unsigned, unsigned) override { ; }
void selectPage(unsigned) override { ; }
void rack(Kontrol::ChangeSource, const Kontrol::Rack &) override { ; }
void module(Kontrol::ChangeSource, const Kontrol::Rack &, const Kontrol::Module &) override { ; }
void page(Kontrol::ChangeSource, const Kontrol::Rack &, const Kontrol::Module &,
const Kontrol::Page &) override { ; }
void param(Kontrol::ChangeSource, const Kontrol::Rack &, const Kontrol::Module &,
const Kontrol::Parameter &) override { ; }
void changed(Kontrol::ChangeSource, const Kontrol::Rack &, const Kontrol::Module &,
const Kontrol::Parameter &) override { ; }
void resource(Kontrol::ChangeSource, const Kontrol::Rack &, const std::string &,
const std::string &) override { ; };
void deleteRack(Kontrol::ChangeSource, const Kontrol::Rack &) override { ; }
void displayPopup(const std::string &text, unsigned time, bool dblline);
protected:
Organelle &parent_;
std::shared_ptr<Kontrol::KontrolModel> model() { return parent_.model(); }
int popupTime_;
};
struct Pots {
enum {
K_UNLOCKED,
K_GT,
K_LT,
K_LOCKED
} locked_[ORGANELLE_NUM_PARAMS];
float rawValue[ORGANELLE_NUM_PARAMS];
};
class OParamMode : public OBaseMode {
public:
OParamMode(Organelle &p) : OBaseMode(p), pageIdx_(-1) { ; }
bool init() override;
void poll() override;
void activate() override;
void changePot(unsigned pot, float value) override;
void changeEncoder(unsigned encoder, float value) override;
void encoderButton(unsigned encoder, bool value) override;
void keyPress(unsigned, unsigned) override;
void selectPage(unsigned) override;
void module(Kontrol::ChangeSource source, const Kontrol::Rack &rack, const Kontrol::Module &module) override;
void changed(Kontrol::ChangeSource, const Kontrol::Rack &, const Kontrol::Module &,
const Kontrol::Parameter &) override;
void page(Kontrol::ChangeSource source, const Kontrol::Rack &rack, const Kontrol::Module &module,
const Kontrol::Page &page) override;
void loadModule(Kontrol::ChangeSource, const Kontrol::Rack &, const Kontrol::EntityId &,
const std::string &) override;
void activeModule(Kontrol::ChangeSource, const Kontrol::Rack &, const Kontrol::Module &) override;
private:
void setCurrentPage(unsigned pageIdx, bool UI);
void display();
void activateShortcut(unsigned key);
std::shared_ptr<Pots> pots_;
std::string moduleType_;
int pageIdx_ = -1;
Kontrol::EntityId pageId_;
bool encoderAction_ = false;
bool encoderDown_ = false;
};
class OMenuMode : public OBaseMode {
public:
OMenuMode(Organelle &p) : OBaseMode(p), cur_(0), top_(0) { ; }
virtual unsigned getSize() = 0;
virtual std::string getItemText(unsigned idx) = 0;
virtual void clicked(unsigned idx) = 0;
bool init() override { return true; }
void poll() override;
void activate() override;
void changeEncoder(unsigned encoder, float value) override;
void encoderButton(unsigned encoder, bool value) override;
protected:
void display();
void displayItem(unsigned idx);
unsigned cur_;
unsigned top_;
bool clickedDown_;
};
class OFixedMenuMode : public OMenuMode {
public:
OFixedMenuMode(Organelle &p) : OMenuMode(p) { ; }
unsigned getSize() override { return static_cast<unsigned int>(items_.size()); };
std::string getItemText(unsigned i) override { return items_[i]; }
protected:
std::vector<std::string> items_;
};
class OMainMenu : public OMenuMode {
public:
OMainMenu(Organelle &p) : OMenuMode(p) { ; }
bool init() override;
unsigned getSize() override;
std::string getItemText(unsigned idx) override;
void clicked(unsigned idx) override;
};
class OPresetMenu : public OMenuMode {
public:
OPresetMenu(Organelle &p) : OMenuMode(p) { ; }
bool init() override;
void activate() override;
unsigned getSize() override;
std::string getItemText(unsigned idx) override;
void clicked(unsigned idx) override;
private:
std::vector<std::string> presets_;
};
class OModuleMenu : public OFixedMenuMode {
public:
OModuleMenu(Organelle &p) : OFixedMenuMode(p) { ; }
void activate() override;
void clicked(unsigned idx) override;
private:
void populateMenu(const std::string& catSel);
std::string cat_;
};
class OModuleSelectMenu : public OFixedMenuMode {
public:
OModuleSelectMenu(Organelle &p) : OFixedMenuMode(p) { ; }
void activate() override;
void clicked(unsigned idx) override;
};
void OBaseMode::displayPopup(const std::string &text, unsigned time, bool dblline) {
popupTime_ = time;
parent_.displayPopup(text, dblline);
}
void OBaseMode::poll() {
if (popupTime_ < 0) return;
popupTime_--;
}
bool OParamMode::init() {
OBaseMode::init();
pots_ = std::make_shared<Pots>();
for (int i = 0; i < ORGANELLE_NUM_PARAMS; i++) {
pots_->rawValue[i] = std::numeric_limits<float>::max();
pots_->locked_[i] = Pots::K_LOCKED;
}
return true;
}
void OParamMode::display() {
parent_.clearDisplay();
auto rack = parent_.model()->getRack(parent_.currentRack());
auto module = parent_.model()->getModule(rack, parent_.currentModule());
auto page = parent_.model()->getPage(module, pageId_);
// auto pages = parent_.model()->getPages(module);
auto params = parent_.model()->getParams(module, page);
unsigned int j = 0;
for (const auto ¶m : params) {
if (param != nullptr) {
parent_.displayParamLine(j + 1, *param);
}
j++;
if (j == ORGANELLE_NUM_PARAMS) break;
}
parent_.flipDisplay();
}
void OParamMode::activate() {
auto module = model()->getModule(model()->getRack(parent_.currentRack()), parent_.currentModule());
if (module != nullptr) {
auto page = parent_.model()->getPage(module, pageId_);
parent_.sendPdMessage("activePage", module->id(), (page == nullptr ? "none" : page->id()));
}
display();
}
void OParamMode::poll() {
OBaseMode::poll();
// release pop, redraw display
if (popupTime_ == 0) {
display();
// cancel timing
popupTime_ = -1;
}
}
void OParamMode::changePot(unsigned pot, float rawvalue) {
OBaseMode::changePot(pot, rawvalue);
try {
auto rack = parent_.model()->getRack(parent_.currentRack());
auto module = parent_.model()->getModule(rack, parent_.currentModule());
auto page = parent_.model()->getPage(module, pageId_);
auto pages = parent_.model()->getPages(module);
if (pages.size() == 0 || (page && page->isCustomPage())) {
// a page with no parameters is a custom page
// and recieves pot events
char msg[7];
sprintf(msg, "knob%d",pot+1);
pots_->locked_[pot] = Pots::K_UNLOCKED;
if(pots_->rawValue[pot]!=rawvalue) {
float value = rawvalue / MAX_POT_VALUE;
parent_.sendPdModuleMessage(msg, module->id(), ( page == nullptr ? "none" : page->id() ) , value);
}
pots_->rawValue[pot] = rawvalue;
return;
}
// auto pages = parent_.model()->getPages(module);
auto params = parent_.model()->getParams(module, page);
if (pot >= params.size()) return;
auto ¶m = params[pot];
auto paramId = param->id();
Kontrol::ParamValue calc;
if (rawvalue != std::numeric_limits<float>::max()) {
float value = rawvalue / MAX_POT_VALUE;
calc = param->calcFloat(value);
if (rawvalue != pots_->rawValue[pot] && parent_.instantParamSetting()) {
pots_->locked_[pot] = Pots::K_UNLOCKED;
}
//std::cerr << "changePot " << pot << " " << value << " cv " << calc.floatValue() << " pv " << param->current().floatValue() << std::endl;
}
pots_->rawValue[pot] = rawvalue;
if (pots_->locked_[pot] != Pots::K_UNLOCKED && parent_.instantParamSetting() == false) {
//if pot is locked, determined if we can unlock it
if (calc == param->current()) {
pots_->locked_[pot] = Pots::K_UNLOCKED;
//std::cerr << "unlock condition met == " << pot << std::endl;
} else if (pots_->locked_[pot] == Pots::K_GT) {
if (calc > param->current()) {
pots_->locked_[pot] = Pots::K_UNLOCKED;
//std::cerr << "unlock condition met gt " << pot << std::endl;
}
} else if (pots_->locked_[pot] == Pots::K_LT) {
if (calc < param->current()) {
pots_->locked_[pot] = Pots::K_UNLOCKED;
//std::cerr << "unlock condition met lt " << pot << std::endl;
}
} else if (pots_->locked_[pot] == Pots::K_LOCKED) {
//std::cerr << "pot locked " << pot << " pv " << param->current().floatValue() << " cv " << calc.floatValue() << std::endl;
// initial locked, determine unlock condition
if (calc == param->current()) {
// pot value at current value, unlock it
pots_->locked_[pot] = Pots::K_UNLOCKED;
//std::cerr << "set unlock condition == " << pot << std::endl;
} else if (rawvalue == std::numeric_limits<float>::max()) {
// stay locked , we need a real value ;)
// init state
//std::cerr << "cannot set unlock condition " << pot << std::endl;
} else if (calc > param->current()) {
// pot starts greater than param, so wait for it to go less than
pots_->locked_[pot] = Pots::K_LT;
//std::cerr << "set unlock condition lt " << pot << std::endl;
} else {
// pot starts less than param, so wait for it to go greater than
pots_->locked_[pot] = Pots::K_GT;
//std::cerr << "set unlock condition gt " << pot << std::endl;
}
}
}
if (pots_->locked_[pot] == Pots::K_UNLOCKED) {
model()->changeParam(Kontrol::CS_LOCAL, parent_.currentRack(), parent_.currentModule(), paramId, calc);
}
} catch (std::out_of_range) {
return;
}
}
void OParamMode::setCurrentPage(unsigned pageIdx, bool UI) {
auto module = model()->getModule(model()->getRack(parent_.currentRack()), parent_.currentModule());
if (module == nullptr) return;
try {
// auto rack = parent_.model()->getRack(parent_.currentRack());
// auto module = parent_.model()->getModule(rack, parent_.currentModule());
auto page = parent_.model()->getPage(module, pageId_);
auto pages = parent_.model()->getPages(module);
// auto params = parent_.model()->getParams(module,page);
if (pageIdx_ != pageIdx) {
if (pageIdx < pages.size()) {
pageIdx_ = pageIdx;
try {
page = pages[pageIdx_];
pageId_ = page->id();
display();
} catch (std::out_of_range) { ;
}
} else {
// if no pages, or page selected is out of range, display blank
parent_.clearDisplay();
}
}
if (UI) {
displayPopup(page->displayName(), PAGE_SWITCH_TIMEOUT, false);
parent_.flipDisplay();
}
parent_.sendPdMessage("activePage", module->id(), (page == nullptr ? "none" : page->id()));
for (unsigned int i = 0; i < ORGANELLE_NUM_PARAMS; i++) {
pots_->locked_[i] = Pots::K_LOCKED;
changePot(i, pots_->rawValue[i]);
}
} catch (std::out_of_range) { ;
}
}
void OParamMode::selectPage(unsigned page) {
setCurrentPage(page,true);
}
void OParamMode::changeEncoder(unsigned enc, float value) {
OBaseMode::changeEncoder(enc, value);
auto rack = parent_.model()->getRack(parent_.currentRack());
auto module = parent_.model()->getModule(rack, parent_.currentModule());
// auto page = parent_.model()->getPage(module,pageId_);
auto pages = parent_.model()->getPages(module);
// auto params = parent_.model()->getParams(module,page);
if (pages.size()<2) {
// if single page send encoder messages to modules
parent_.sendPdModuleMessage("enc", module->id(), value);
return;
}
if (pageIdx_ < 0) {
setCurrentPage(0, false);
return;
}
auto pagenum = (unsigned) pageIdx_;
if (value > 0) {
// clockwise
pagenum++;
pagenum = std::min(pagenum, (unsigned) pages.size() - 1);
} else {
// anti clockwise
if (pagenum > 0) pagenum--;
}
if (pagenum != pageIdx_) {
setCurrentPage(pagenum, true);
}
}
void OParamMode::encoderButton(unsigned enc, bool value) {
OBaseMode::encoderButton(enc, value);
if(parent_.enableMenu()) {
if (encoderAction_ && !value) {
parent_.changeMode(OM_MAINMENU);
}
} else {
auto rack = parent_.model()->getRack(parent_.currentRack());
auto module = parent_.model()->getModule(rack, parent_.currentModule());
parent_.sendPdModuleMessage("encbut", module->id(), value);
}
encoderDown_ = value;
encoderAction_ = value;
}
void OParamMode::keyPress(unsigned key, unsigned value) {
if (value == 0 && encoderDown_) {
activateShortcut(key);
encoderAction_ = false;
}
}
void OParamMode::activateShortcut(unsigned key) {
if (key == 0) {
if(parent_.enableMenu()) {
// normal op = select menu
encoderDown_ = false;
encoderAction_ = false;
parent_.changeMode(OM_MODULESELECTMENU);
return;
} else {
//TODO backcompat mode
//this is not good, as it will obliterate screen
//and also will mean encoder cannot be used again
//but it necessary otherwise you cannot change module
//(also changing module means we need to have the menu enabled again)
// re-enable main menu
encoderDown_ = false;
encoderAction_ = false;
parent_.enableMenu(true);
parent_.changeMode(OM_MAINMENU);
}
}
if (key > 0) {
unsigned moduleIdx = key - 1;
auto rack = parent_.model()->getRack(parent_.currentRack());
auto modules = parent_.getModules(rack);
if (moduleIdx < modules.size()) {
auto module = modules[moduleIdx];
auto moduleId = module->id();
if (parent_.currentModule() != moduleId) {
// re-enable main menu
parent_.enableMenu(true);
parent_.currentModule(moduleId);
displayPopup(module->id() + ":" + module->displayName(), MODULE_SWITCH_TIMEOUT, true);
parent_.flipDisplay();
}
}
}
}
void OParamMode::activeModule(Kontrol::ChangeSource, const Kontrol::Rack &rack, const Kontrol::Module &) {
if (rack.id() == parent_.currentRack()) {
pageIdx_ = -1;
setCurrentPage(0, false);
parent_.flipDisplay();
}
}
void OParamMode::changed(Kontrol::ChangeSource src, const Kontrol::Rack &rack, const Kontrol::Module &module,
const Kontrol::Parameter ¶m) {
OBaseMode::changed(src, rack, module, param);
if (popupTime_ > 0) return;
if (rack.id() != parent_.currentRack() || module.id() != parent_.currentModule()) return;
auto prack = parent_.model()->getRack(parent_.currentRack());
auto pmodule = parent_.model()->getModule(prack, parent_.currentModule());
auto page = parent_.model()->getPage(pmodule, pageId_);
// auto pages = parent_.model()->getPages(pmodule);
auto params = parent_.model()->getParams(pmodule, page);
auto sz = static_cast<unsigned int>(params.size());
sz = sz < ORGANELLE_NUM_PARAMS ? sz : ORGANELLE_NUM_PARAMS;
for (unsigned int i = 0; i < sz; i++) {
try {
auto &p = params.at(i);
if (p->id() == param.id()) {
p->change(param.current(), src == Kontrol::CS_PRESET);
parent_.displayParamLine(i + 1, param);
if (src != Kontrol::CS_LOCAL) {
//std::cerr << "locking " << param.id() << " src " << src << std::endl;
pots_->locked_[i] = Pots::K_LOCKED;
changePot(i, pots_->rawValue[i]);
}
parent_.flipDisplay();
return;
}
} catch (std::out_of_range) {
return;
}
} // for
}
void OParamMode::module(Kontrol::ChangeSource source, const Kontrol::Rack &rack, const Kontrol::Module &module) {
OBaseMode::module(source, rack, module);
if (moduleType_ != module.type()) {
pageIdx_ = -1;
}
moduleType_ = module.type();
}
void OParamMode::page(Kontrol::ChangeSource source, const Kontrol::Rack &rack, const Kontrol::Module &module,
const Kontrol::Page &page) {
OBaseMode::page(source, rack, module, page);
if (pageIdx_ < 0) setCurrentPage(0, false);
}
void OParamMode::loadModule(Kontrol::ChangeSource source, const Kontrol::Rack &rack,
const Kontrol::EntityId &moduleId, const std::string &modType) {
OBaseMode::loadModule(source, rack, moduleId, modType);
if (parent_.currentModule() == moduleId) {
if (moduleType_ != modType) {
pageIdx_ = -1;
moduleType_ = modType;
}
}
}
void OMenuMode::activate() {
parent_.sendPdMessage("activePage", "none","none");
display();
popupTime_ = MENU_TIMEOUT;
clickedDown_ = false;
}
void OMenuMode::poll() {
OBaseMode::poll();
if (popupTime_ == 0) {
parent_.changeMode(OM_PARAMETER);
popupTime_ = -1;
}
}
void OMenuMode::display() {
parent_.clearDisplay();
for (unsigned i = top_; i < top_ + ORGANELLE_NUM_TEXTLINES; i++) {
displayItem(i);
}
}
void OMenuMode::displayItem(unsigned i) {
if (i < getSize()) {
std::string item = getItemText(i);
unsigned line = i - top_ + 1;
parent_.displayLine(line, item.c_str());
if (i == cur_) {
parent_.invertLine(line);
}
}
parent_.flipDisplay();
}
void OMenuMode::changeEncoder(unsigned, float value) {
unsigned cur = cur_;
if (value > 0) {
// clockwise
cur++;
cur = std::min(cur, getSize() - 1);
} else {
// anti clockwise
if (cur > 0) cur--;
}
if (cur != cur_) {
unsigned int line = 0;
if (cur < top_) {
top_ = cur;
cur_ = cur;
display();
} else if (cur >= top_ + ORGANELLE_NUM_TEXTLINES) {
top_ = cur - (ORGANELLE_NUM_TEXTLINES - 1);
cur_ = cur;
display();
} else {
line = cur_ - top_ + 1;
if (line <= ORGANELLE_NUM_TEXTLINES) parent_.invertLine(line);
cur_ = cur;
line = cur_ - top_ + 1;
if (line <= ORGANELLE_NUM_TEXTLINES) parent_.invertLine(line);
parent_.flipDisplay();
}
}
popupTime_ = MENU_TIMEOUT;
}
void OMenuMode::encoderButton(unsigned, bool value) {
if (clickedDown_ && value < 1.0) {
clicked(cur_);
}
clickedDown_ = value;
}
/// main menu
enum MainMenuItms {
MMI_MODULE,
MMI_PRESET,
MMI_MIDILEARN,
MMI_MODLEARN,
MMI_SAVE,
MMI_HOME,
MMI_SIZE
};
bool OMainMenu::init() {
return true;
}
unsigned OMainMenu::getSize() {
return (unsigned) MMI_SIZE;
}
std::string OMainMenu::getItemText(unsigned idx) {
switch (idx) {
case MMI_MODULE: {
auto rack = model()->getRack(parent_.currentRack());
auto module = model()->getModule(rack, parent_.currentModule());
if (module == nullptr)
return parent_.currentModule();
else
return parent_.currentModule() + ":" + module->displayName();
}
case MMI_PRESET: {
auto rack = model()->getRack(parent_.currentRack());
if (rack != nullptr) {
return rack->currentPreset();
}
return "No Preset";
}
case MMI_SAVE:
return "Save";
case MMI_MIDILEARN: {
if (parent_.midiLearn()) {
return "Midi Learn [X]";
}
return "Midi Learn [ ]";
}
case MMI_MODLEARN: {
if (parent_.modulationLearn()) {
return "Mod Learn [X]";
}
return "Mod Learn [ ]";
}
case MMI_HOME:
return "Home";
default:
break;
}
return "";
}
void OMainMenu::clicked(unsigned idx) {
switch (idx) {
case MMI_MODULE: {
parent_.changeMode(OM_MODULEMENU);
break;
}
case MMI_PRESET: {
parent_.changeMode(OM_PRESETMENU);
break;
}
case MMI_MIDILEARN: {
parent_.midiLearn(!parent_.midiLearn());
displayItem(MMI_MIDILEARN);
displayItem(MMI_MODLEARN);
parent_.flipDisplay();
// parent_.changeMode(OM_PARAMETER);
break;
}
case MMI_MODLEARN: {
parent_.modulationLearn(!parent_.modulationLearn());
displayItem(MMI_MIDILEARN);
displayItem(MMI_MODLEARN);
parent_.flipDisplay();
// parent_.changeMode(OM_PARAMETER);
break;
}
case MMI_SAVE: {
auto rack = model()->getRack(parent_.currentRack());
if (rack != nullptr) {
rack->saveSettings();
}
parent_.changeMode(OM_PARAMETER);
break;
}
case MMI_HOME: {
parent_.changeMode(OM_PARAMETER);
parent_.sendGoHome();
// parent_.sendPdMessage("goHome", 1.0);
break;
}
default:
break;
}
}
// preset menu
enum PresetMenuItms {
PMI_SAVE,
PMI_NEW,
PMI_SEP,
PMI_LAST
};
bool OPresetMenu::init() {
auto rack = model()->getRack(parent_.currentRack());
if (rack != nullptr) {
presets_ = rack->getPresetList();
} else {
presets_.clear();
}
return true;
}
void OPresetMenu::activate() {
auto rack = model()->getRack(parent_.currentRack());
if (rack != nullptr) {
presets_ = rack->getPresetList();
} else {
presets_.clear();
}
OMenuMode::activate();
}
unsigned OPresetMenu::getSize() {
return static_cast<unsigned int>((unsigned) PMI_LAST + presets_.size());
}
std::string OPresetMenu::getItemText(unsigned idx) {
switch (idx) {
case PMI_SAVE:
return "Save Preset";
case PMI_NEW:
return "New Preset";
case PMI_SEP:
return "--------------------";
default:
return presets_[idx - PMI_LAST];
}
}
void OPresetMenu::clicked(unsigned idx) {
switch (idx) {
case PMI_SAVE: {
auto rack = model()->getRack(parent_.currentRack());
if (rack != nullptr) {
rack->savePreset(rack->currentPreset());
}
parent_.changeMode(OM_PARAMETER);
break;
}
case PMI_NEW: {
auto rack = model()->getRack(parent_.currentRack());
if (rack != nullptr) {
std::string newPreset = "new-" + std::to_string(presets_.size());
rack->savePreset(newPreset);
}
parent_.changeMode(OM_MAINMENU);
break;
}
case PMI_SEP: {
break;
}
default: {
auto rack = model()->getRack(parent_.currentRack());
if (rack != nullptr) {
std::string newPreset = presets_[idx - PMI_LAST];
parent_.changeMode(OM_PARAMETER);
rack->loadPreset(newPreset);
}
break;
}
}
}
void OModuleMenu::populateMenu(const std::string& catSel) {
auto rack = model()->getRack(parent_.currentRack());
auto module = model()->getModule(rack, parent_.currentModule());
if (module == nullptr) return;
unsigned idx = 0;
auto res = rack->getResources("module");
items_.clear();
cur_ = 0;
top_ = 0;
std::set<std::string> cats;
unsigned catlen = cat_.length();
if(catlen) {
items_.push_back("..");
idx++;
}
for (const auto &modtype : res) {
if(cat_.length()) {
size_t pos=modtype.find(cat_);
if(pos==0) {
std::string mod=modtype.substr(catlen,modtype.length()-catlen);
items_.push_back(mod);
if (module->type() == modtype ) {
cur_ = idx;
top_ = idx;
}
idx++;
} // else filtered
} else {
// top level, so get categories
size_t pos=modtype.find("/");
if(pos==std::string::npos) {
items_.push_back(modtype);
if (modtype == module->type()) {
cur_ = idx;
top_ = idx;
}
idx++;
} else {
cats.insert(modtype.substr(0,pos+1));
}
}
}
size_t pos =std::string::npos;
std::string modcat;
pos = module->type().find("/");
if(pos!=std::string::npos) {
modcat=module->type().substr(0,pos+1);
}
for(auto s: cats) {
items_.push_back(s);
if (catSel.length() && s == catSel) {
cur_ = idx;
top_ = idx;
}
idx++;
}
}
void OModuleMenu::activate() {
auto rack = model()->getRack(parent_.currentRack());
auto module = model()->getModule(rack, parent_.currentModule());
if (module == nullptr) return;
unsigned idx = 0;
auto res = rack->getResources("module");
cat_="";
size_t pos =std::string::npos;
pos = module->type().find("/");
if(pos!=std::string::npos) {
cat_=module->type().substr(0,pos+1);
}
populateMenu(cat_);
OFixedMenuMode::activate();
}
void OModuleMenu::clicked(unsigned idx) {
if (idx < getSize()) {
auto modtype = items_[idx];
if (modtype == "..") {
std::string oldcat = cat_;
cat_ = "";
populateMenu(oldcat);
display();
return;
} else {
if(cat_.length()) {
// module dir
Kontrol::EntityId modType = cat_ + modtype;
auto rack = model()->getRack(parent_.currentRack());
auto module = model()->getModule(rack, parent_.currentModule());
if (modType != module->type()) {
//FIXME, workaround since changing the module needs to tell params to change page
parent_.changeMode(OM_PARAMETER);
model()->loadModule(Kontrol::CS_LOCAL, rack->id(), module->id(), modType);
}
} else {
cat_ = modtype;
populateMenu("");
display();
return;
}
}
}
parent_.changeMode(OM_PARAMETER);
}
void OModuleSelectMenu::activate() {
auto rack = model()->getRack(parent_.currentRack());
auto cmodule = model()->getModule(rack, parent_.currentModule());
if (cmodule == nullptr) return;
unsigned idx = 0;
items_.clear();
auto modules = parent_.getModules(rack);
for (const auto &module : modules) {
std::string desc = module->id() + ":" + module->displayName();
items_.push_back(desc);
if (module->id() == cmodule->id()) {
cur_ = idx;
top_ = idx;
}
idx++;
}
OFixedMenuMode::activate();
}
void OModuleSelectMenu::clicked(unsigned idx) {
parent_.changeMode(OM_PARAMETER);
if (idx < getSize()) {
unsigned moduleIdx = idx;
auto rack = parent_.model()->getRack(parent_.currentRack());
auto modules = parent_.getModules(rack);