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.

339 lines
10KB

  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. #include <stm32f10x_conf.h>
  25. #include "stmlib/system/system_clock.h"
  26. #include "stmlib/utils/random.h"
  27. #include "frames/drivers/dac.h"
  28. #include "frames/drivers/system.h"
  29. #include "frames/drivers/trigger_output.h"
  30. #include "frames/keyframer.h"
  31. #include "frames/poly_lfo.h"
  32. #include "frames/euclidean.h"
  33. #include "frames/ui.h"
  34. using namespace frames;
  35. using namespace stmlib;
  36. Dac dac;
  37. Keyframer keyframer;
  38. PolyLfo poly_lfo;
  39. Euclidean euclidean[kNumChannels];
  40. System sys;
  41. TriggerOutput trigger_output;
  42. Ui ui;
  43. // Default interrupt handlers.
  44. extern "C" {
  45. void HardFault_Handler() { while (1); }
  46. void MemManage_Handler() { while (1); }
  47. void BusFault_Handler() { while (1); }
  48. void UsageFault_Handler() { while (1); }
  49. void NMI_Handler() { }
  50. void SVC_Handler() { }
  51. void DebugMon_Handler() { }
  52. void PendSV_Handler() { }
  53. }
  54. extern "C" {
  55. void SysTick_Handler() {
  56. system_clock.Tick(); // Tick global ms counter.
  57. ui.Poll();
  58. }
  59. volatile uint16_t refresh = 0;
  60. static uint16_t factory_testing_timer = 0;
  61. static int16_t previous_position = -2;
  62. static int16_t previous_nearest_keyframe = -2;
  63. static uint16_t pulse_counter;
  64. static bool can_fire_trigger = false;
  65. static const uint16_t kPulseDuration = 128;
  66. static uint16_t counter = 0;
  67. void TIM1_UP_IRQHandler(void) {
  68. if (TIM_GetITStatus(TIM1, TIM_IT_Update) == RESET) {
  69. return;
  70. }
  71. TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
  72. dac.Update();
  73. if (dac.ready()) {
  74. ++refresh;
  75. }
  76. if (ui.feature_mode() == Ui::FEAT_MODE_KEYFRAMER ||
  77. ui.feature_mode() == Ui::FEAT_MODE_KEYFRAME_LOOPER ) {
  78. int16_t position = keyframer.position();
  79. if (previous_position != position) {
  80. previous_position = position;
  81. if (can_fire_trigger) {
  82. pulse_counter = kPulseDuration;
  83. trigger_output.High();
  84. can_fire_trigger = false;
  85. }
  86. }
  87. }
  88. int16_t nearest_keyframe = keyframer.nearest_keyframe();
  89. if (previous_nearest_keyframe != nearest_keyframe) {
  90. previous_nearest_keyframe = nearest_keyframe;
  91. can_fire_trigger = true;
  92. }
  93. if (ui.mode() == UI_MODE_FACTORY_TESTING) {
  94. ++factory_testing_timer;
  95. if (factory_testing_timer == 1280) {
  96. pulse_counter = kPulseDuration;
  97. trigger_output.High();
  98. factory_testing_timer = 0;
  99. }
  100. }
  101. if (ui.feature_mode() != Ui::FEAT_MODE_POLY_LFO &&
  102. pulse_counter) {
  103. --pulse_counter;
  104. if (!pulse_counter) {
  105. trigger_output.Low();
  106. }
  107. }
  108. }
  109. }
  110. void Init() {
  111. sys.Init(F_CPU / 128000 - 1, true);
  112. dac.Init();
  113. trigger_output.Init();
  114. trigger_output.Low();
  115. keyframer.Init();
  116. poly_lfo.Init();
  117. for (int i=0; i<kNumChannels; i++)
  118. euclidean[i].Init();
  119. ui.Init(&keyframer, &poly_lfo, euclidean);
  120. sys.StartTimers();
  121. }
  122. inline uint16_t fold_add(uint16_t a, int16_t b) {
  123. if (a > 0 && b > 65535 - a) {
  124. return 65535 - a - b - 1;
  125. } else if (b < 0 && a < - b) {
  126. return 65535 - a - b + 1;
  127. } else {
  128. return a + b;
  129. }
  130. }
  131. int32_t lp_frame = 0;
  132. uint32_t phase = 0;
  133. int main(void) {
  134. Init();
  135. while (ui.mode() == UI_MODE_SPLASH) {
  136. ui.DoEvents();
  137. }
  138. ui.TryCalibration();
  139. bool trigger_detector_armed = false;
  140. int32_t dc_offset_frame_modulation = keyframer.dc_offset_frame_modulation();
  141. int32_t clock_counter = 0;
  142. while (1) {
  143. ui.DoEvents();
  144. if (refresh) {
  145. --refresh;
  146. int32_t frame = ui.frame();
  147. int32_t frame_modulation = \
  148. (ui.frame_modulation() - dc_offset_frame_modulation) << 1;
  149. frame += frame_modulation;
  150. if (ui.feature_mode() == Ui::FEAT_MODE_POLY_LFO) {
  151. poly_lfo.Render(frame);
  152. if (poly_lfo.level(0) > 128) {
  153. trigger_output.High();
  154. } else {
  155. trigger_output.Low();
  156. }
  157. dac.Write(0, poly_lfo.dac_code(0));
  158. dac.Write(1, poly_lfo.dac_code(1));
  159. dac.Write(2, poly_lfo.dac_code(2));
  160. dac.Write(3, poly_lfo.dac_code(3));
  161. } else if (ui.feature_mode() == Ui::FEAT_MODE_EUCLIDEAN) {
  162. // Render envelopes
  163. for (int i=0; i<kNumChannels; i++)
  164. euclidean[i].Render();
  165. // update big LED
  166. if (counter++ % 32 == 0)
  167. ui.rgb_led_.Dim(65535);
  168. // Detect a trigger on the FRAME input.
  169. if (frame_modulation < 21845) {
  170. trigger_detector_armed = true;
  171. }
  172. // on each clock:
  173. if (frame_modulation > 43690 && trigger_detector_armed) {
  174. trigger_detector_armed = false;
  175. clock_counter++;
  176. // step
  177. for (int i=0; i<kNumChannels; i++)
  178. euclidean[i].Step(clock_counter);
  179. ui.rgb_led_.set_color(255, 255, 255);
  180. // update gate output and LED
  181. bool gate = true;
  182. for (int i=0; i<kNumChannels; i++)
  183. gate ^= euclidean[i].gate();
  184. if (gate) {
  185. trigger_output.High();
  186. ui.keyframe_led_.High();
  187. } else {
  188. trigger_output.Low();
  189. ui.keyframe_led_.Low();
  190. }
  191. }
  192. dac.Write(0, euclidean[0].dac_code());
  193. dac.Write(1, euclidean[1].dac_code());
  194. dac.Write(2, euclidean[2].dac_code());
  195. dac.Write(3, euclidean[3].dac_code());
  196. } else {
  197. if (ui.feature_mode() == Ui::FEAT_MODE_SEQ_SHIFT_REGISTER ||
  198. ui.feature_mode() == Ui::FEAT_MODE_SEQ_STEP_EDIT ||
  199. ui.feature_mode() == Ui::FEAT_MODE_SEQ_MAIN) {
  200. // Detect a trigger on the FRAME input.
  201. if (frame_modulation < 21845) {
  202. trigger_detector_armed = true;
  203. }
  204. // on each clock
  205. if (frame_modulation > 43690 && trigger_detector_armed) {
  206. trigger_detector_armed = false;
  207. clock_counter++;
  208. if (ui.feature_mode() == Ui::FEAT_MODE_SEQ_SHIFT_REGISTER) {
  209. // action: shift
  210. if (ui.shift_divider > 0 &&
  211. clock_counter % ui.shift_divider == 0 &&
  212. static_cast<uint8_t>(Random::GetWord()) > ui.shift_random) {
  213. uint16_t temp = ui.shift_register[ui.active_registers-1];
  214. // shift all registers one place
  215. for (int i=ui.active_registers-1; i>0; i--)
  216. ui.shift_register[i] = ui.shift_register[i-1];
  217. // feed back last value into first, with random added
  218. if (static_cast<uint8_t>(Random::GetWord()) > ui.feedback_random) {
  219. ui.shift_register[0] = temp;
  220. } else {
  221. int16_t rnd = static_cast<int8_t>(Random::GetWord()) * ui.feedback_random;
  222. ui.shift_register[0] = fold_add(temp, rnd);
  223. }
  224. // trigger
  225. pulse_counter = kPulseDuration;
  226. trigger_output.High();
  227. }
  228. // action: step
  229. if (ui.step_divider > 0 &&
  230. clock_counter % ui.step_divider == 0 &&
  231. static_cast<uint8_t>(Random::GetWord()) > ui.step_random) {
  232. ui.shift_register[0] = keyframer.level(0);
  233. int32_t max_step = keyframer.num_keyframes();
  234. int8_t rnd = static_cast<int8_t>(Random::GetWord()) *
  235. ui.sequencer_random * max_step / 255 / 128 / 2;
  236. ui.sequencer_step = (ui.sequencer_step + 1 + rnd) % max_step;
  237. // trigger
  238. pulse_counter = kPulseDuration;
  239. trigger_output.High();
  240. }
  241. dac.Write(0, Keyframer::ConvertToDacCode(ui.shift_register[0], 0));
  242. dac.Write(1, Keyframer::ConvertToDacCode(ui.shift_register[1], 0));
  243. dac.Write(2, Keyframer::ConvertToDacCode(ui.shift_register[2], 0));
  244. dac.Write(3, Keyframer::ConvertToDacCode(ui.shift_register[3], 0));
  245. } else {
  246. // step
  247. int32_t max_step = ui.feature_mode() == Ui::FEAT_MODE_SEQ_STEP_EDIT ?
  248. (keyframer.num_keyframes() * ui.frame() / 65536) + 1 :
  249. keyframer.num_keyframes();
  250. ui.sequencer_step = (ui.sequencer_step + 1) % max_step;
  251. // output a trigger when sequence resets
  252. if (ui.sequencer_step == 0) {
  253. pulse_counter = kPulseDuration;
  254. trigger_output.High();
  255. }
  256. }
  257. }
  258. frame = keyframer.keyframe(ui.sequencer_step).timestamp;
  259. } else {
  260. lp_frame += (frame - lp_frame) >> 6;
  261. frame = lp_frame;
  262. }
  263. if (ui.feature_mode() == Ui::FEAT_MODE_KEYFRAME_LOOPER) {
  264. int32_t speed = frame_modulation; // -32768..32767
  265. int32_t frequency = speed > 0 ? speed : -speed;
  266. uint32_t phase_increment = PolyLfo::FrequencyToPhaseIncrement(frequency);
  267. if (speed > 0) {
  268. phase += phase_increment;
  269. } else {
  270. phase -= phase_increment;
  271. }
  272. }
  273. if (ui.feature_mode() == Ui::FEAT_MODE_KEYFRAME_LOOPER) {
  274. keyframer.Evaluate(phase >> 16);
  275. } else {
  276. keyframer.Evaluate(frame);
  277. }
  278. if (ui.feature_mode() != Ui::FEAT_MODE_SEQ_SHIFT_REGISTER) {
  279. // sequencer or keyframer mode
  280. dac.Write(0, keyframer.dac_code(0));
  281. dac.Write(1, keyframer.dac_code(1));
  282. dac.Write(2, keyframer.dac_code(2));
  283. dac.Write(3, keyframer.dac_code(3));
  284. }
  285. }
  286. }
  287. }
  288. }