summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--table-fan/Makefile2
-rw-r--r--table-fan/fan.c3
-rw-r--r--table-fan/rotary.c82
-rw-r--r--table-fan/rotary.h31
-rw-r--r--table-fan/table-fan.c26
5 files changed, 134 insertions, 10 deletions
diff --git a/table-fan/Makefile b/table-fan/Makefile
index 36e971a..bded127 100644
--- a/table-fan/Makefile
+++ b/table-fan/Makefile
@@ -22,7 +22,7 @@
NAME := table-fan
BOARD_TYPE := sparkfunProMicro
-OBJ := $(NAME).o fan.o
+OBJ := $(NAME).o fan.o rotary.o
LIBS := util led lufa-descriptor-usbserial usbio
EXTERNAL_LIBS := lufa
SPREADAVR_PATH := ../contrib/avr-utils
diff --git a/table-fan/fan.c b/table-fan/fan.c
index a843924..a7de8f3 100644
--- a/table-fan/fan.c
+++ b/table-fan/fan.c
@@ -23,8 +23,7 @@
#include <avr/io.h>
#include <avr/interrupt.h>
-#include <stdio.h>
-
+#include "fan.h"
#define PWM_VAL_MAX 160
diff --git a/table-fan/rotary.c b/table-fan/rotary.c
new file mode 100644
index 0000000..beb95c1
--- /dev/null
+++ b/table-fan/rotary.c
@@ -0,0 +1,82 @@
+/*
+ * spreadspace avr utils
+ *
+ *
+ * Copyright (C) 2021 Christian Pointner <equinox@spreadspace.org>
+ *
+ * This file is part of spreadspace avr utils.
+ *
+ * spreadspace avr utils is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * spreadspace avr utils is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+#include "rotary.h"
+
+#define DEBOUNCE_MAX 100
+uint8_t last_stable = 0;
+
+static inline uint8_t get_current(void)
+{
+ return (PINB & ((1<<PB4) | (1<<PB5))) >> PB4;
+}
+
+void rotary_init(void)
+{
+ DDRB &= ~((1<<PB4) | (1<<PB5));
+ PORTB |= (1<<PB4) | (1<<PB5);
+ last_stable = get_current();
+}
+
+rotary_dir_t rotary_task(void)
+{
+ static uint8_t last = 0;
+ static uint8_t cnt = 0;
+
+ uint8_t current = get_current();
+ if(last == current)
+ cnt += (cnt < DEBOUNCE_MAX) ? 1 : 0;
+ else
+ cnt -= (cnt > 0) ? 1 : 0;
+ last = current;
+
+ if(cnt < DEBOUNCE_MAX)
+ return rotary_dir_none;
+ cnt = 0;
+
+ rotary_dir_t dir = rotary_dir_none;
+ if(last_stable != current) {
+ switch(last_stable) {
+ case 0:
+ if(current == 1) dir = rotary_dir_up;
+ if(current == 2) dir = rotary_dir_down;
+ break;
+ case 1:
+ if(current == 3) dir = rotary_dir_up;
+ if(current == 0) dir = rotary_dir_down;
+ break;
+ case 2:
+ if(current == 0) dir = rotary_dir_up;
+ if(current == 3) dir = rotary_dir_down;
+ break;
+ case 3:
+ if(current == 2) dir = rotary_dir_up;
+ if(current == 1) dir = rotary_dir_down;
+ break;
+ }
+ }
+ last_stable = current;
+ return dir;
+}
diff --git a/table-fan/rotary.h b/table-fan/rotary.h
new file mode 100644
index 0000000..1f69c17
--- /dev/null
+++ b/table-fan/rotary.h
@@ -0,0 +1,31 @@
+/*
+ * spreadspace avr utils
+ *
+ *
+ * Copyright (C) 2021 Christian Pointner <equinox@spreadspace.org>
+ *
+ * This file is part of spreadspace avr utils.
+ *
+ * spreadspace avr utils is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * spreadspace avr utils is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TABLEFAN_rotary_h_INCLUDED
+#define TABLEFAN_rotary_h_INCLUDED
+
+typedef enum { rotary_dir_none, rotary_dir_up, rotary_dir_down } rotary_dir_t;
+
+void rotary_init(void);
+rotary_dir_t rotary_task(void);
+
+#endif
diff --git a/table-fan/table-fan.c b/table-fan/table-fan.c
index 1f0cf4f..b59d7e9 100644
--- a/table-fan/table-fan.c
+++ b/table-fan/table-fan.c
@@ -31,12 +31,13 @@
#include "usbio.h"
#include "fan.h"
+#include "rotary.h"
void handle_cmd(uint8_t cmd)
{
switch(cmd) {
- case '+': fan_speed_inc();/* printf("pwm = %d\r\n", fan_speed_get()); */ break;
- case '-': fan_speed_dec();/* printf("pwm = %d\r\n", fan_speed_get()); */ break;
+ case '+': fan_speed_inc(); break;
+ case '-': fan_speed_dec(); break;
case 'r': reset2bootloader(); break;
}
printf("\rspeed: %5d rpm", fan_get_rpm());
@@ -53,13 +54,11 @@ int main(void)
led_init();
usbio_init();
fan_init();
+ rotary_init();
sei();
- // rotary encoder
- DDRB &= ~((1<<PB4) | (1<<PB5));
- PORTB |= (1<<PB4) | (1<<PB5);
-
- led_on();
+ led_off();
+ led2_off();
for(;;) {
int16_t BytesReceived = usbio_bytes_received();
while(BytesReceived > 0) {
@@ -70,6 +69,19 @@ int main(void)
BytesReceived--;
}
+ rotary_dir_t dir = rotary_task();
+ switch(dir) {
+ case rotary_dir_up: fan_speed_inc(); break;
+ case rotary_dir_down: fan_speed_dec(); break;
+ default: break;
+ }
+
+ if(fan_speed_get() > 0) {
+ led_on();
+ } else {
+ led_off();
+ }
+
usbio_task();
}
}