You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.2KB

  1. // Copyright 2013 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. // Settings
  28. #ifndef PEAKS_UI_H_
  29. #define PEAKS_UI_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/ui/event_queue.h"
  32. #include "peaks/drivers/adc.h"
  33. #include "peaks/drivers/gate_input.h"
  34. #include "peaks/drivers/leds.h"
  35. #include "peaks/drivers/switches.h"
  36. #include "peaks/processors.h"
  37. namespace peaks {
  38. enum SwitchIndex {
  39. SWITCH_TWIN_MODE,
  40. SWITCH_FUNCTION,
  41. SWITCH_GATE_TRIG_1,
  42. SWITCH_GATE_TRIG_2
  43. };
  44. enum EditMode {
  45. EDIT_MODE_TWIN,
  46. EDIT_MODE_SPLIT,
  47. EDIT_MODE_FIRST,
  48. EDIT_MODE_SECOND,
  49. EDIT_MODE_LAST
  50. };
  51. enum Function {
  52. FUNCTION_ENVELOPE,
  53. FUNCTION_LFO,
  54. FUNCTION_TAP_LFO,
  55. FUNCTION_DRUM_GENERATOR,
  56. FUNCTION_MINI_SEQUENCER,
  57. FUNCTION_PULSE_SHAPER,
  58. FUNCTION_PULSE_RANDOMIZER,
  59. FUNCTION_FM_DRUM_GENERATOR,
  60. FUNCTION_LAST,
  61. FUNCTION_FIRST_ALTERNATE_FUNCTION = FUNCTION_MINI_SEQUENCER
  62. };
  63. struct Settings {
  64. uint8_t edit_mode;
  65. uint8_t function[2];
  66. uint8_t pot_value[8];
  67. bool snap_mode;
  68. };
  69. class Ui {
  70. public:
  71. Ui() { }
  72. ~Ui() { }
  73. void Init();
  74. void Poll();
  75. void PollPots();
  76. void DoEvents();
  77. void FlushEvents();
  78. void set_leds_brightness(int16_t channel_1, int16_t channel_2) {
  79. brightness_[0] = channel_1;
  80. brightness_[1] = channel_2;
  81. }
  82. inline uint8_t ReadPanelGateState() {
  83. uint8_t state = panel_gate_control_[0] ? INPUT_1_GATE : 0;
  84. state |= panel_gate_control_[1] ? INPUT_2_GATE : 0;
  85. if (!(panel_gate_state_ & INPUT_1_GATE) && (state & INPUT_1_GATE)) {
  86. state |= INPUT_1_RAISING;
  87. }
  88. if ((panel_gate_state_ & INPUT_1_GATE) && !(state & INPUT_1_GATE)) {
  89. state |= INPUT_1_FALLING;
  90. }
  91. if (!(panel_gate_state_ & INPUT_2_GATE) && (state & INPUT_2_GATE)) {
  92. state |= INPUT_2_RAISING;
  93. }
  94. if ((panel_gate_state_ & INPUT_2_GATE) && !(state & INPUT_2_GATE)) {
  95. state |= INPUT_2_FALLING;
  96. }
  97. panel_gate_state_ = state;
  98. return state;
  99. }
  100. private:
  101. inline Function function() const {
  102. return edit_mode_ == EDIT_MODE_SECOND ? function_[1] : function_[0];
  103. }
  104. void LockPots();
  105. void OnSwitchPressed(const stmlib::Event& e);
  106. void OnSwitchReleased(const stmlib::Event& e);
  107. void OnPotChanged(const stmlib::Event& e);
  108. void RefreshLeds();
  109. void ChangeControlMode();
  110. void SetFunction(uint8_t index, Function f);
  111. void SaveState();
  112. uint16_t adc_value_[kNumAdcChannels];
  113. uint16_t adc_threshold_[kNumAdcChannels];
  114. uint32_t press_time_[kNumSwitches];
  115. bool panel_gate_control_[2];
  116. static const ProcessorFunction function_table_[FUNCTION_LAST][2];
  117. stmlib::EventQueue<32> queue_;
  118. Leds leds_;
  119. Switches switches_;
  120. Adc adc_;
  121. EditMode edit_mode_;
  122. Function function_[2];
  123. Settings settings_;
  124. uint16_t version_token_;
  125. int16_t brightness_[2];
  126. uint8_t panel_gate_state_;
  127. uint8_t double_press_counter_;
  128. uint8_t pot_value_[8];
  129. bool snap_mode_;
  130. bool snapped_[kNumAdcChannels];
  131. DISALLOW_COPY_AND_ASSIGN(Ui);
  132. };
  133. } // namespace peaks
  134. #endif // PEAKS_UI_H_