diff options
-rw-r--r-- | src/cmd.c | 13 | ||||
-rw-r--r-- | src/cmd.h | 10 | ||||
-rw-r--r-- | src/helpers.c | 4 | ||||
-rw-r--r-- | src/pwm.c | 47 | ||||
-rw-r--r-- | src/pwm.h | 7 |
5 files changed, 63 insertions, 18 deletions
@@ -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: @@ -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 @@ -32,6 +32,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 * * 0: Error type 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; } @@ -5,47 +5,70 @@ */ #include "pwm.h" +#include <stdint.h> 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<<PINB0); - else - PORTB |= (1<<PINB0); + for( i=0; i<PWM_PINS; i++) { + if(t0_ovf_cnt <= pwm_duty[i]) + PWM_PORT |= (1<<i); + else + PWM_PORT &= ~(1<<i); + } sei(); } ISR(TIMER2_OVF_vect) { + uint8_t i; cli(); - pb0_thresh ++; + + for( i=0; i<PWM_PINS; i++) { + if (pwm_duty[i] < pwm_target[i]) + pwm_duty[i]++; + else if (pwm_duty[i] > pwm_target[i]) + pwm_duty[i]--; + } + sei(); } void pwm_init() { - /* Pins */ - DDRB |= (1<<PINB0); + uint8_t i; + + PWM_DDR |= (0xff >> (8-PWM_PINS)); + PWM_PORT &= ~(0xff >> (8-PWM_PINS)); + + for ( i=0; i<PWM_PINS; i++ ) + pwm_duty[i] = pwm_target[i] = 0; /* Enable TIMER0 with interrupt, no prescaler */ TCCR0 |= (1<<CS00); TIMSK |= (1<<TOIE0); /* Enable TIMER2 with interrupt, Clk divide by 1024 */ - /* TCCR2 |= (7<<CS20); TIMSK |= (1<<TOIE2); - */ } void pwm_set_pin(uint8_t _pin, uint8_t _duty) { - if(_pin == 0) - pb0_thresh = _duty; + if(_pin < PWM_PINS) { + pwm_duty[_pin] = _duty; + pwm_target[_pin] = _duty; + } +} + +void pwm_fade_pin(uint8_t _pin, uint8_t _duty) { + if(_pin < PWM_PINS) + pwm_target[_pin] = _duty; } @@ -11,11 +11,14 @@ #include <avr/interrupt.h> #include <stdint.h> -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_ */ |