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.

157 lines
3.9KB

  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 CalibrationData;
  70. class Ui {
  71. public:
  72. Ui() { }
  73. ~Ui() { }
  74. void Init(CalibrationData* calibration_data);
  75. void Poll();
  76. void PollPots();
  77. void DoEvents();
  78. void FlushEvents();
  79. void set_led_brightness(int channel, int16_t value) {
  80. brightness_[channel] = value;
  81. }
  82. inline uint32_t ReadPanelGateState() {
  83. uint32_t state = 0;
  84. state |= panel_gate_control_[0] ? 1 : 0;
  85. state |= panel_gate_control_[1] ? 2 : 0;
  86. return state;
  87. }
  88. inline bool calibrating() const { return calibrating_; }
  89. private:
  90. inline Function function() const {
  91. return edit_mode_ == EDIT_MODE_SECOND ? function_[1] : function_[0];
  92. }
  93. void LockPots();
  94. void OnSwitchPressed(const stmlib::Event& e);
  95. void OnSwitchReleased(const stmlib::Event& e);
  96. void OnPotChanged(const stmlib::Event& e);
  97. void RefreshLeds();
  98. void ChangeControlMode();
  99. void SetFunction(uint8_t index, Function f);
  100. void SaveState();
  101. int32_t adc_lp_[kNumAdcChannels];
  102. int32_t adc_value_[kNumAdcChannels];
  103. int32_t adc_threshold_[kNumAdcChannels];
  104. uint32_t press_time_[kNumSwitches];
  105. bool panel_gate_control_[2];
  106. static const ProcessorFunction function_table_[FUNCTION_LAST][2];
  107. stmlib::EventQueue<32> queue_;
  108. Leds leds_;
  109. Switches switches_;
  110. Adc adc_;
  111. EditMode edit_mode_;
  112. Function function_[2];
  113. Settings settings_;
  114. uint16_t version_token_;
  115. int16_t brightness_[2];
  116. uint8_t panel_gate_state_;
  117. uint8_t double_press_counter_;
  118. uint8_t pot_value_[8];
  119. bool snap_mode_;
  120. bool snapped_[kNumAdcChannels];
  121. CalibrationData* calibration_data_;
  122. bool calibrating_;
  123. DISALLOW_COPY_AND_ASSIGN(Ui);
  124. };
  125. } // namespace peaks
  126. #endif // PEAKS_UI_H_