diff --git a/lib/stepped.c b/lib/stepped.c new file mode 100644 index 0000000..35c0ac2 --- /dev/null +++ b/lib/stepped.c @@ -0,0 +1,69 @@ +#include "stepped.h" + +int steps = 12; +int current_step = 0; +int reset = 0; +int twoup = 0; +int down = 0; + +int vsteps[12] = {0}; + +void stepped_init(void){ + current_step = 0; + reset = 0; + twoup = 0; + down = 0; + + float total_real_volts = (2.5f * 120.f / 24.9f); + float offset_real_volts = (2.5f * 120.f / 49.9f); + + float one_volt = (float)(0xfff) / total_real_volts; // ~340 lsbs per volt + float half_range = offset_real_volts * one_volt; + + for(int i=0; i<6; i++){ + vsteps[i] = (int)(half_range - one_volt * i); + } + for(int i=6; i<12; i++){ + vsteps[i] = (int)(half_range - one_volt * (i-12)); + } +} + +void stepped(float _steps, int _reset, int _twoup, int _down){ + // perform reset first so we can wrap & then advance on simultaneous triggers + if(_reset != reset){ + reset = _reset; + if(reset == 1){ + current_step = 0; + } + } + + // adjust playhead + if(_twoup != twoup){ + twoup = _twoup; + if(twoup == 1){ + current_step += 2; + } + } + if(_down != down){ + down = _down; + if(down == 1){ + current_step -= 1; + } + } + + // wrap to steps + // don't use modulo to correctly handle negative values + // could just add steps, and then modulo for simpler solution + steps = 1 + (int)(_steps*11.999f); // [1,12) + while(current_step < 0){ + current_step += steps; + } + while(current_step >= steps){ + current_step -= steps; + } +} + +int stepped_get(void){ + // perform scaling + return vsteps[current_step]; +} diff --git a/lib/stepped.h b/lib/stepped.h new file mode 100644 index 0000000..8e6988b --- /dev/null +++ b/lib/stepped.h @@ -0,0 +1,7 @@ +#pragma once + +void stepped_init(void); + +void stepped(float _steps, int _reset, int _twoup, int _down); + +int stepped_get(void); diff --git a/ll/din.c b/ll/din.c index ba15847..92c8043 100644 --- a/ll/din.c +++ b/ll/din.c @@ -15,6 +15,7 @@ static const Pin ds[3] = static GPIO_InitTypeDef g; +// TODO use EXTI instead of manual scanning void din_init(void){ __HAL_RCC_GPIOB_CLK_ENABLE(); diff --git a/main.c b/main.c index 4d12a24..9b0e824 100755 --- a/main.c +++ b/main.c @@ -19,14 +19,15 @@ #include "ll/adc.h" #include "ll/dac108.h" #include "ll/adda.h" +#include "lib/stepped.h" /* -density, C4, 2_in14 -steps, A5, 2_in5 -rotate, A6, 2_in6 fold, A7, 1_in7 id, B0, 1_in8 offset, B1, 1_in9 +density, C4, 2_in14 +steps, A5, 2_in5 +rotate, A6, 2_in6 MOSI, A0, SAI2_SD_B, AF10 SCK, A2, SAI2_SCK_B, AF8 @@ -45,7 +46,7 @@ int main(void){ status_led_fast(LED_SLOW); // slow blink until USB connection goes live status_led_set(1); // set status to ON to show sign of life straight away - printf("\n\nhi from crow!\n"); + printf("\n\nhi from parafocus!\n"); lights_init(); lights_all(0); @@ -67,6 +68,8 @@ int main(void){ // ii_init( II_CROW ); // Random_Init(); + stepped_init(); + DAC_Init(32, 16); // 32 samples per block, 16 channels DAC_Start(); @@ -103,6 +106,12 @@ int main(void){ ADDA_set_val(i, ADC_get(i)); } } + + stepped(1.f, din_get(DIN_RESET), din_get(DIN_2UP), din_get(DIN_DOWN)); + ADDA_set_val(0, stepped_get()); + ADDA_set_val(1, 0x0); // set fine tune to zero position + // NOTE: chan 0 is main stepped cv, chan1 is fine tune + // lights_set(0, din_get(DIN_RESET)); // lights_set(1, din_get(DIN_DOWN)); // lights_set(2, din_get(DIN_2UP));