From b40f4273fde9e8c8e6fb3d1ebcef1e8ced624415 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 13 Sep 2021 21:22:42 +0200 Subject: table-fan: basic functuinality is done --- table-fan/Makefile | 2 +- table-fan/fan.c | 3 +- table-fan/rotary.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ table-fan/rotary.h | 31 +++++++++++++++++++ table-fan/table-fan.c | 26 +++++++++++----- 5 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 table-fan/rotary.c create mode 100644 table-fan/rotary.h 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 #include -#include - +#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 + * + * 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 . + */ + +#include +#include + +#include "rotary.h" + +#define DEBOUNCE_MAX 100 +uint8_t last_stable = 0; + +static inline uint8_t get_current(void) +{ + return (PINB & ((1<> PB4; +} + +void rotary_init(void) +{ + DDRB &= ~((1< 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 + * + * 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 . + */ + +#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< 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(); } } -- cgit v1.2.3