diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-02-08 19:56:32 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-02-08 19:56:32 +0100 |
commit | 20e188ab3c70f0f2f2502019f1f7cb3b05952321 (patch) | |
tree | eaa2604baad45eb448f27d6d2e141f694c844f2b | |
parent | f875ab2a70afe023c8457cdd8814e146814d2b7e (diff) |
practicum done
-rw-r--r-- | src/main.c | 37 |
1 files changed, 30 insertions, 7 deletions
@@ -56,6 +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)); @@ -83,12 +86,11 @@ void ledWrite(int num, int on) { * value 0 indicates that the button is currently not pressed. */ int buttonRead() { - return (GPIOB->IDR & (1 << PINOUT_BTN)) > 0; + return (GPIOB->IDR & (1 << PINOUT_BTN)) == 0; } /* * Read potmeter value as analog value between 0 and 2^12 - 1 - * TODO: increase measurement sensitivity */ unsigned int potRead() { ADC1->CHSELR = (1 << PINOUT_POT); @@ -108,25 +110,46 @@ int mod(int a, int b) { return m < 0 ? (b < 0) ? m - b : m + b : m; } +typedef enum { + up_idle = 0, + pressed = 1, + down_idle = 2 +} button_state; + int main() { shieldConfig(); uint8_t reverse = 0; uint8_t led = 0; + button_state state = up_idle; while (1) { - for(int j = 0; j < 4; j++) ledWrite(j, 0); - ledWrite(potRead(), 1); + uint8_t button_now = buttonRead(); + + switch(state) { + case up_idle: { + state = button_now ? pressed : up_idle; + break; + } + case pressed: { + state = button_now ? down_idle : up_idle; + break; + } + case down_idle: { + state = button_now ? down_idle : up_idle; + break; + } + } - continue; + if (state == pressed) reverse ^= 1; - reverse = buttonRead(); for(int j = 0; j < 4; j++) ledWrite(j, 0); + // calculate next led index led = mod(led + ((2 * reverse) - 1), 4); ledWrite(led, 1); - // ledWrite(3, (ADC1->ISR & ADC_ISR_ADRDY) == 0 ? 0 : 1); + delay(100 + 300 * potRead()); } } |