diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-02-10 12:12:22 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-02-10 12:12:22 +0100 |
commit | 51ef37f1463adc5aef108cff03ebfbad1390e394 (patch) | |
tree | e06b88d7d33e35741a14ad3e84aa08d5dea25d00 | |
parent | 20e188ab3c70f0f2f2502019f1f7cb3b05952321 (diff) |
-rw-r--r-- | src/main.c | 28 |
1 files changed, 12 insertions, 16 deletions
@@ -56,15 +56,9 @@ void shieldConfig() { GPIOB->PUPDR &= ~(0b11 << (PINOUT_BTN * 2)); GPIOB->PUPDR |= (0b01 << (PINOUT_BTN * 2)); - ADC1->SMPR &= ~(0x7); - // ADC1->SMPR |= (0x7); - // calirate ADC1 ADC1->CR |= ADC_CR_ADCAL; - while ((ADC1->CR & ADC_CR_ADCAL)); - - // enable ADC V_REFINT - ADC->CCR |= ADC_CCR_VREFEN; + while (ADC1->CR & ADC_CR_ADCAL); // enable ADC1 ADC1->CR |= ADC_CR_ADEN; @@ -101,10 +95,6 @@ unsigned int potRead() { return result; } -void delay(int amount) { - for (int i = 0; i < 500 * amount; i++) asm("nop"); -} - int mod(int a, int b) { int m = a % b; return m < 0 ? (b < 0) ? m - b : m + b : m; @@ -121,6 +111,7 @@ int main() { uint8_t reverse = 0; uint8_t led = 0; + unsigned long cycles = 0; button_state state = up_idle; while (1) { @@ -143,13 +134,18 @@ int main() { if (state == pressed) reverse ^= 1; - for(int j = 0; j < 4; j++) ledWrite(j, 0); + unsigned int delay_amount = 4e3 + 8e3 * potRead(); + if (cycles >= delay_amount) { + for(int j = 0; j < 4; j++) ledWrite(j, 0); + + // calculate next led index + led = mod(led + ((2 * reverse) - 1), 4); - // calculate next led index - led = mod(led + ((2 * reverse) - 1), 4); + ledWrite(led, 1); - ledWrite(led, 1); + cycles = 0; + } - delay(100 + 300 * potRead()); + cycles += 1; } } |