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.

190 lines
5.1KB

  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 "frames/drivers/dac.h"
  27. #include "frames/drivers/system.h"
  28. #include "frames/drivers/trigger_output.h"
  29. #include "frames/keyframer.h"
  30. #include "frames/poly_lfo.h"
  31. #include "frames/ui.h"
  32. using namespace frames;
  33. using namespace stmlib;
  34. Dac dac;
  35. Keyframer keyframer;
  36. PolyLfo poly_lfo;
  37. System sys;
  38. TriggerOutput trigger_output;
  39. Ui ui;
  40. // Default interrupt handlers.
  41. extern "C" {
  42. void HardFault_Handler() { while (1); }
  43. void MemManage_Handler() { while (1); }
  44. void BusFault_Handler() { while (1); }
  45. void UsageFault_Handler() { while (1); }
  46. void NMI_Handler() { }
  47. void SVC_Handler() { }
  48. void DebugMon_Handler() { }
  49. void PendSV_Handler() { }
  50. }
  51. extern "C" {
  52. void SysTick_Handler() {
  53. system_clock.Tick(); // Tick global ms counter.
  54. ui.Poll();
  55. }
  56. volatile uint16_t refresh = 0;
  57. static uint16_t factory_testing_timer = 0;
  58. static int16_t previous_position = -2;
  59. static int16_t previous_nearest_keyframe = -2;
  60. static uint16_t pulse_counter;
  61. static bool can_fire_trigger = false;
  62. static const uint16_t kPulseDuration = 128;
  63. void TIM1_UP_IRQHandler(void) {
  64. if (TIM_GetITStatus(TIM1, TIM_IT_Update) == RESET) {
  65. return;
  66. }
  67. TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
  68. dac.Update();
  69. if (dac.ready()) {
  70. ++refresh;
  71. }
  72. int16_t position = keyframer.position();
  73. if (previous_position != position) {
  74. previous_position = position;
  75. if (can_fire_trigger) {
  76. pulse_counter = kPulseDuration;
  77. trigger_output.High();
  78. can_fire_trigger = false;
  79. }
  80. }
  81. int16_t nearest_keyframe = keyframer.nearest_keyframe();
  82. if (previous_nearest_keyframe != nearest_keyframe) {
  83. previous_nearest_keyframe = nearest_keyframe;
  84. can_fire_trigger = true;
  85. }
  86. if (ui.mode() == UI_MODE_FACTORY_TESTING) {
  87. ++factory_testing_timer;
  88. if (factory_testing_timer == 1280) {
  89. pulse_counter = kPulseDuration;
  90. trigger_output.High();
  91. factory_testing_timer = 0;
  92. }
  93. }
  94. if (pulse_counter) {
  95. --pulse_counter;
  96. if (!pulse_counter) {
  97. trigger_output.Low();
  98. }
  99. }
  100. }
  101. }
  102. void Init() {
  103. sys.Init(F_CPU / 128000 - 1, true);
  104. dac.Init();
  105. trigger_output.Init();
  106. trigger_output.Low();
  107. keyframer.Init();
  108. poly_lfo.Init();
  109. ui.Init(&keyframer, &poly_lfo);
  110. sys.StartTimers();
  111. }
  112. int main(void) {
  113. Init();
  114. while (ui.mode() == UI_MODE_SPLASH) {
  115. ui.DoEvents();
  116. }
  117. ui.TryCalibration();
  118. bool trigger_detector_armed = false;
  119. uint8_t sequencer_step = 0;
  120. int32_t dc_offset_frame_modulation = keyframer.dc_offset_frame_modulation();
  121. while (1) {
  122. ui.DoEvents();
  123. if (refresh) {
  124. --refresh;
  125. int32_t frame = ui.frame();
  126. int32_t frame_modulation = \
  127. (ui.frame_modulation() - dc_offset_frame_modulation) << 1;
  128. frame += frame_modulation;
  129. if (ui.poly_lfo_mode()) {
  130. poly_lfo.Render(frame);
  131. dac.Write(0, poly_lfo.dac_code(0));
  132. dac.Write(1, poly_lfo.dac_code(1));
  133. dac.Write(2, poly_lfo.dac_code(2));
  134. dac.Write(3, poly_lfo.dac_code(3));
  135. } else {
  136. if (frame < 0) {
  137. frame = 0;
  138. } else if (frame > 65535) {
  139. frame = 65535;
  140. }
  141. if (ui.sequencer_mode()) {
  142. // Detect a trigger on the FRAME input.
  143. if (frame_modulation < 21845) {
  144. trigger_detector_armed = true;
  145. }
  146. if (frame_modulation > 43690 && trigger_detector_armed) {
  147. trigger_detector_armed = false;
  148. ++sequencer_step;
  149. }
  150. if (sequencer_step >= keyframer.num_keyframes()) {
  151. sequencer_step = 0;
  152. }
  153. frame = keyframer.keyframe(sequencer_step).timestamp;
  154. }
  155. keyframer.Evaluate(frame);
  156. dac.Write(0, keyframer.dac_code(0));
  157. dac.Write(1, keyframer.dac_code(1));
  158. dac.Write(2, keyframer.dac_code(2));
  159. dac.Write(3, keyframer.dac_code(3));
  160. }
  161. }
  162. }
  163. }