aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.c37
1 files changed, 30 insertions, 7 deletions
diff --git a/src/main.c b/src/main.c
index 38a4fd8..8fc4dba 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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());
}
}