From 4a4ba3538cf52cc1e0238c673d4a97dbbf68c56a Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Wed, 5 Jan 2022 03:14:50 +0100 Subject: fix commands, add fade --- src/cmd.c | 13 ++++++++++++- src/cmd.h | 10 +++++++++- src/helpers.c | 4 ++-- src/pwm.c | 47 +++++++++++++++++++++++++++++++++++------------ src/pwm.h | 7 +++++-- 5 files changed, 63 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/cmd.c b/src/cmd.c index 6e03c08..5d4fba6 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -20,7 +20,8 @@ const struct { uint8_t argc; } cmd[_CMD_CNT] = { {0,cmd_err,0}, - {'p',cmd_set_pwm,3} + {'p',cmd_set_pwm,3}, + {'f',cmd_fade_pwm,3} }; void cmd_init() { @@ -67,6 +68,9 @@ void cmd_tick(char _c) { if (cmd_state.state >= _CMD_MAX_ARGC) { cmd_state.cmd = 0; cmd_state.argv[0] = _ERR_ARGC; + + /* TODO this fixes reset on keysmashing. why? */ + return; } cmd_state.argv[ (cmd_state.state++) - 1 ] = _c; @@ -79,6 +83,13 @@ void cmd_set_pwm(uint8_t _argv[]) { pwm_set_pin(pin, duty); } +void cmd_fade_pwm(uint8_t _argv[]) { + uint8_t duty = hex_to_byte((char*)&(_argv[1])); + uint8_t pin = _argv[0] - 48; + + pwm_fade_pin(pin, duty); +} + void cmd_err( uint8_t _argv[] ) { switch(_argv[0]) { case _ERR_ARGC: diff --git a/src/cmd.h b/src/cmd.h index e85093f..e246933 100644 --- a/src/cmd.h +++ b/src/cmd.h @@ -13,7 +13,7 @@ #include "pwm.h" #include "helpers.h" -#define _CMD_CNT 2 +#define _CMD_CNT 3 #define _CMD_MAX_ARGC 4 #define _ERR_CMD 1 @@ -31,6 +31,14 @@ void cmd_tick(char _c); */ void cmd_set_pwm(uint8_t _argv[]); +/** + * argc=3 + * + * 0: Pin Num + * 1-2: Dutycycle in HEX (00-FF) + */ +void cmd_fade_pwm(uint8_t _argv[]); + /** * argc=1 * diff --git a/src/helpers.c b/src/helpers.c index b8c3373..61c4bc5 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -7,8 +7,8 @@ #include "helpers.h" uint8_t hex_to_byte(char c[]) { - uint8_t ret = hex_to_halfbyte(c[0]); - ret += 16 * hex_to_halfbyte(c[1]); + uint8_t ret = hex_to_halfbyte(c[1]); + ret += 16 * hex_to_halfbyte(c[0]); return ret; } diff --git a/src/pwm.c b/src/pwm.c index 787fcae..5e8fa3f 100644 --- a/src/pwm.c +++ b/src/pwm.c @@ -5,47 +5,70 @@ */ #include "pwm.h" +#include uint8_t t0_ovf_cnt = 0; -uint8_t pb0_thresh = 128; +uint8_t pwm_duty[PWM_PINS]; +uint8_t pwm_target[PWM_PINS]; ISR(TIMER0_OVF_vect) { + uint8_t i; cli(); /*TCNT0 = (1<<7);*/ /* Hack-increase Interrupt trigger freq */ t0_ovf_cnt ++; - if ( t0_ovf_cnt >= pb0_thresh ) - PORTB &= ~(1< pwm_target[i]) + pwm_duty[i]--; + } + sei(); } void pwm_init() { - /* Pins */ - DDRB |= (1<> (8-PWM_PINS)); + PWM_PORT &= ~(0xff >> (8-PWM_PINS)); + + for ( i=0; i #include -extern uint8_t t0_ovf_cnt; -extern uint8_t pb0_thresh; +#define PWM_PINS 3 +#define PWM_DDR DDRB +#define PWM_PORT PORTB void pwm_init(); void pwm_set_pin(uint8_t _pin, uint8_t _duty); +void pwm_fade_pin(uint8_t _pin, uint8_t _duty); + #endif /* _PWM_H_ */ -- cgit v1.2.3