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.

255 lines
6.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. // User interface.
  28. #ifndef YARNS_UI_H_
  29. #define YARNS_UI_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/ui/event_queue.h"
  32. #include "yarns/drivers/channel_leds.h"
  33. #include "yarns/drivers/display.h"
  34. #include "yarns/drivers/encoder.h"
  35. #include "yarns/drivers/switches.h"
  36. #include "yarns/settings.h"
  37. #include "yarns/storage_manager.h"
  38. namespace yarns {
  39. const uint8_t kNumPrograms = 8;
  40. enum UiMode {
  41. UI_MODE_PARAMETER_SELECT,
  42. UI_MODE_PARAMETER_EDIT,
  43. UI_MODE_MAIN_MENU,
  44. UI_MODE_LOAD_SELECT_PROGRAM,
  45. UI_MODE_SAVE_SELECT_PROGRAM,
  46. UI_MODE_CALIBRATION_SELECT_VOICE,
  47. UI_MODE_CALIBRATION_SELECT_NOTE,
  48. UI_MODE_CALIBRATION_ADJUST_LEVEL,
  49. UI_MODE_SELECT_RECORDING_PART,
  50. UI_MODE_RECORDING,
  51. UI_MODE_OVERDUBBING,
  52. UI_MODE_PUSH_IT_SELECT_NOTE,
  53. UI_MODE_LEARNING,
  54. UI_MODE_FACTORY_TESTING,
  55. UI_MODE_SPLASH,
  56. UI_MODE_LAST
  57. };
  58. enum MainMenuEntry {
  59. MAIN_MENU_LOAD,
  60. MAIN_MENU_SAVE,
  61. MAIN_MENU_INIT,
  62. MAIN_MENU_LEARN,
  63. MAIN_MENU_DUMP,
  64. MAIN_MENU_CALIBRATE,
  65. MAIN_MENU_EXIT,
  66. MAIN_MENU_LAST
  67. };
  68. enum UiSwitch {
  69. UI_SWITCH_REC,
  70. UI_SWITCH_START_STOP,
  71. UI_SWITCH_TIE = UI_SWITCH_START_STOP,
  72. UI_SWITCH_TAP_TEMPO,
  73. UI_SWITCH_REST = UI_SWITCH_TAP_TEMPO,
  74. };
  75. enum UiFactoryTestingDisplay {
  76. UI_FACTORY_TESTING_DISPLAY_EMPTY,
  77. UI_FACTORY_TESTING_DISPLAY_NUMBER,
  78. UI_FACTORY_TESTING_DISPLAY_CLICK,
  79. UI_FACTORY_TESTING_DISPLAY_SW_1,
  80. UI_FACTORY_TESTING_DISPLAY_SW_2,
  81. UI_FACTORY_TESTING_DISPLAY_SW_3,
  82. };
  83. class Ui {
  84. public:
  85. typedef void (Ui::*CommandFn)();
  86. typedef void (Ui::*HandlerFn)(const stmlib::Event& event);
  87. typedef void (Ui::*PrintFn)();
  88. Ui() { }
  89. ~Ui() { }
  90. void Init();
  91. void Poll();
  92. void PollFast() {
  93. display_.RefreshFast();
  94. }
  95. void DoEvents();
  96. void FlushEvents();
  97. void Print(const char* text) {
  98. display_.Print(text, text);
  99. }
  100. void PrintDebugByte(uint8_t byte) {
  101. char buffer[3];
  102. char hexadecimal[] = ";";
  103. buffer[2] = '\0';
  104. buffer[0] = hexadecimal[byte >> 4];
  105. buffer[1] = hexadecimal[byte & 0xf];
  106. Print(buffer);
  107. }
  108. inline const Setting& setting() {
  109. return settings.setting(settings.menu()[setting_index_]);
  110. }
  111. inline bool calibrating() const {
  112. return mode_ == UI_MODE_CALIBRATION_SELECT_NOTE ||
  113. mode_ == UI_MODE_CALIBRATION_ADJUST_LEVEL;
  114. }
  115. inline bool factory_testing() const {
  116. return mode_ == UI_MODE_FACTORY_TESTING;
  117. }
  118. inline uint8_t calibration_voice() const { return calibration_voice_; }
  119. inline uint8_t calibration_note() const { return calibration_note_; }
  120. void StartFactoryTesting() {
  121. mode_ = UI_MODE_FACTORY_TESTING;
  122. }
  123. private:
  124. void RefreshDisplay();
  125. void TapTempo();
  126. inline Part* mutable_recording_part() {
  127. return multi.mutable_part(recording_part_);
  128. }
  129. inline const Part& recording_part() const {
  130. return multi.part(recording_part_);
  131. }
  132. // Generic Handler.
  133. void OnClick(const stmlib::Event& event);
  134. void OnLongClick(const stmlib::Event& e);
  135. void OnIncrement(const stmlib::Event& event);
  136. void OnSwitchPress(const stmlib::Event& event);
  137. void OnSwitchHeld(const stmlib::Event& event);
  138. // Specialized Handler.
  139. void OnClickMainMenu(const stmlib::Event& e);
  140. void OnClickLoadSave(const stmlib::Event& e);
  141. void OnClickCalibrationSelectVoice(const stmlib::Event& e);
  142. void OnClickCalibrationSelectNote(const stmlib::Event& e);
  143. void OnClickSelectRecordingPart(const stmlib::Event& e);
  144. void OnClickRecording(const stmlib::Event& e);
  145. void OnClickOverdubbing(const stmlib::Event& e);
  146. void OnClickLearning(const stmlib::Event& event);
  147. void OnClickFactoryTesting(const stmlib::Event& event);
  148. void OnIncrementParameterSelect(const stmlib::Event& e);
  149. void OnIncrementParameterEdit(const stmlib::Event& e);
  150. void OnIncrementCalibrationAdjustment(const stmlib::Event& e);
  151. void OnIncrementRecording(const stmlib::Event& e);
  152. void OnIncrementOverdubbing(const stmlib::Event& e);
  153. void OnIncrementPushItNote(const stmlib::Event& e);
  154. void OnIncrementFactoryTesting(const stmlib::Event& event);
  155. // Print functions.
  156. void PrintParameterName();
  157. void PrintParameterValue();
  158. void PrintMenuName();
  159. void PrintProgramNumber();
  160. void PrintCalibrationVoiceNumber();
  161. void PrintCalibrationNote();
  162. void PrintRecordingPart();
  163. void PrintRecordingStatus();
  164. void PrintPushItNote();
  165. void PrintLearning();
  166. void PrintFactoryTesting();
  167. void PrintVersionNumber();
  168. void DoInitCommand();
  169. void DoDumpCommand();
  170. void DoLearnCommand();
  171. struct Command {
  172. const char* name;
  173. UiMode next_mode;
  174. CommandFn function;
  175. };
  176. struct Mode {
  177. HandlerFn on_increment;
  178. HandlerFn on_click;
  179. PrintFn refresh_display;
  180. UiMode next_mode;
  181. int8_t* incremented_variable;
  182. int8_t min_value;
  183. int8_t max_value;
  184. };
  185. static const Command commands_[MAIN_MENU_LAST];
  186. static Mode modes_[UI_MODE_LAST];
  187. stmlib::EventQueue<32> queue_;
  188. ChannelLeds leds_;
  189. Display display_;
  190. Encoder encoder_;
  191. Switches switches_;
  192. char buffer_[32];
  193. bool long_press_event_sent_;
  194. uint32_t start_stop_press_time_;
  195. bool encoder_long_press_event_sent_;
  196. uint32_t encoder_press_time_;
  197. UiMode mode_;
  198. UiMode previous_mode_;
  199. int8_t setting_index_;
  200. int8_t command_index_;
  201. int8_t calibration_voice_;
  202. int8_t calibration_note_;
  203. int8_t program_index_;
  204. int8_t active_program_;
  205. int8_t recording_part_;
  206. bool push_it_;
  207. int16_t push_it_note_;
  208. UiFactoryTestingDisplay factory_testing_display_;
  209. int8_t factory_testing_number_;
  210. uint16_t factory_testing_leds_counter_;
  211. uint32_t tap_tempo_sum_;
  212. uint32_t tap_tempo_count_;
  213. uint32_t previous_tap_time_;
  214. DISALLOW_COPY_AND_ASSIGN(Ui);
  215. };
  216. } // namespace yarns
  217. #endif // YARNS_UI_H_