summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2022-01-05 03:14:50 +0100
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2022-01-05 03:14:50 +0100
commit4a4ba3538cf52cc1e0238c673d4a97dbbf68c56a (patch)
treec01e5d0ab22eb618eef08ea85b9e233a87577d4c
parentd9b9b3e71397a2da9e16cbee75c4954105237363 (diff)
downloadanalog_instruments-4a4ba3538cf52cc1e0238c673d4a97dbbf68c56a.tar.gz
fix commands, add fade
-rw-r--r--src/cmd.c13
-rw-r--r--src/cmd.h10
-rw-r--r--src/helpers.c4
-rw-r--r--src/pwm.c47
-rw-r--r--src/pwm.h7
5 files changed, 63 insertions, 18 deletions
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
@@ -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;
}
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 <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;
}
diff --git a/src/pwm.h b/src/pwm.h
index 822e76d..0c232a2 100644
--- a/src/pwm.h
+++ b/src/pwm.h
@@ -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_ */