diff options
author | Lars-Dominik Braun <lars@6xq.net> | 2014-07-22 15:58:55 +0200 |
---|---|---|
committer | Lars-Dominik Braun <lars@6xq.net> | 2014-07-22 15:59:09 +0200 |
commit | bedbcdc4de680720deff4b6e113a3ec1a0690482 (patch) | |
tree | 448003d0a8d8a356c956baa427bc3c35f620d314 | |
parent | 42e32ed74655e98e9f2d5a0152a047dcf9e72806 (diff) | |
download | hourglass-bedbcdc4de680720deff4b6e113a3ec1a0690482.tar.gz hourglass-bedbcdc4de680720deff4b6e113a3ec1a0690482.tar.bz2 hourglass-bedbcdc4de680720deff4b6e113a3ec1a0690482.zip |
Speaker and LED pwm
Something is not correct here, stack overflow?
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | pwm.c | 15 | ||||
-rw-r--r-- | speaker.c | 32 | ||||
-rw-r--r-- | speaker.h | 6 | ||||
-rw-r--r-- | timer.c | 3 | ||||
-rw-r--r-- | uart.c | 3 | ||||
-rw-r--r-- | ui.c | 51 |
7 files changed, 73 insertions, 38 deletions
@@ -47,7 +47,6 @@ int main () { sei (); gyroStart (); accelStart (); - pwmStart (); uiLoop (); @@ -1,6 +1,8 @@ /* LED pwm, uses timer0 */ +#include "common.h" + #include <avr/io.h> #include <avr/interrupt.h> #include <stdbool.h> @@ -18,6 +20,11 @@ static uint8_t state = 0; static uint8_t val[2]; static uint8_t init[2]; +static void ledOff () { + PORTB = PORTB & ~((1 << PB6) | (1 << PB7)); + PORTD = PORTD & ~((1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5)); +} + static uint8_t ledToArray (const uint8_t i) { assert (i < PWM_LED_COUNT); if (i >= 2) { @@ -59,10 +66,7 @@ ISR(TIMER0_COMPA_vect) { } if (comphit % 2 == 0) { - /* switch off: we can’t just set to 0 here, since PB6 is used by - * speaker */ - PORTB = PORTB & ~((1 << PB6) | (1 << PB7)); - PORTD = PORTD & ~((1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5)); + ledOff (); } else { PORTB |= val[0]; PORTD |= val[1]; @@ -77,6 +81,8 @@ void pwmInit () { } void pwmStart () { + comphit = 0; + state = 0; /* reset timer value */ TCNT0 = 0; /* set ctc timer0 (part 1) */ @@ -92,6 +98,7 @@ void pwmStart () { void pwmStop () { /* zero clock source */ TCCR0B = 0; + ledOff (); } void pwmSetBlink (const uint8_t i, const uint8_t value) { @@ -1,49 +1,45 @@ /* speaker control, uses timer2 */ +#include "common.h" + #include <avr/io.h> #include <avr/interrupt.h> +#include <util/delay.h> #include <stdbool.h> +#include <assert.h> +#include <stdlib.h> #include "speaker.h" -static bool value = false; - -static void speakerSet () { - if (value) { - PORTD = PORTD | (1 << PD6); - } else { - PORTD = PORTD & ~(1 << PD6); - } -} - ISR(TIMER2_OVF_vect) { - value = !value; - speakerSet (); + PORTD ^= (1 << PD6); } void speakerInit () { /* set PD6 to output */ DDRD |= (1 << PD6); - /* value is false */ - speakerSet (); + /* turn off */ + PORTD = PORTD & ~(1 << PD6); } -void speakerStart () { +void speakerStart (const speakerMode mode) { /* reset timer value */ TCNT2 = 0; - /* set normal mode timer0 */ + /* set normal mode */ TCCR2A = 0; /* enable overflow interrupt */ TIMSK2 = (1 << TOIE2); /* io clock with 1024 prescaler */ TCCR2B = ((TCCR2B & ~(1 << CS21)) | (1 << CS22) | (1 << CS20)); + _delay_ms (50); + speakerStop (); } void speakerStop () { /* zero clock source */ TCCR2B = 0; - value = false; - speakerSet (); + /* turn off */ + PORTD = PORTD & ~(1 << PD6); } @@ -1,8 +1,12 @@ #ifndef SPEAKER_H #define SPEAKER_H +typedef enum { + SPEAKER_BEEP, +} speakerMode; + void speakerInit (); -void speakerStart (); +void speakerStart (const speakerMode); void speakerStop (); #endif /* SPEAKER_H */ @@ -1,7 +1,8 @@ +#include "common.h" + #include <avr/io.h> #include <avr/interrupt.h> -#include "common.h" #include "timer.h" /* counts one second, off by +1s per 2m @@ -1,8 +1,9 @@ +#include "common.h" + #include <stdio.h> #include <avr/io.h> #include "uart.h" -#include "common.h" /* blocking uart send */ @@ -86,6 +86,7 @@ static void doSelectCoarse () { accelResetShakeCount (); mode = UIMODE_SELECT_FINE; printf ("selectcoarse->selectfine(%i)\n", coarseSeconds); + speakerStart (SPEAKER_BEEP); return; } @@ -96,6 +97,7 @@ static void doSelectCoarse () { coarseSeconds = limits(coarseSeconds + zticks*60*5, 0, 60*60); printf ("c:%it:%i\n", coarseSeconds, zticks); + pwmStop (); const uint8_t tenminutes = coarseSeconds/60/10; for (uint8_t i = 0; i < tenminutes; i++) { pwmSetBlink (horizonLed (i), PWM_BLINK_ON); @@ -103,6 +105,7 @@ static void doSelectCoarse () { for (uint8_t i = tenminutes; i < PWM_LED_COUNT; i++) { pwmSetBlink (horizonLed (i), PWM_BLINK_OFF); } + pwmStart (); } } @@ -115,6 +118,7 @@ static void doSelectFine () { secPerSubstep = (coarseSeconds + fineSeconds)/(6*3); mode = UIMODE_IDLE; printf ("selectfine->idle(%u,%u)\n", coarseSeconds + fineSeconds, secPerSubstep); + speakerStart (SPEAKER_BEEP); return; } @@ -125,6 +129,7 @@ static void doSelectFine () { fineSeconds = limits(fineSeconds + zticks*30, -5*60, 5*60); printf ("f:%it:%i\n", fineSeconds, zticks); + pwmStop (); const uint8_t minutes = abs (fineSeconds)/60; for (uint8_t i = 0; i < minutes; i++) { pwmSetBlink (horizonLed (i), PWM_BLINK_ON); @@ -137,6 +142,7 @@ static void doSelectFine () { } else { pwmSetBlink (horizonLed (PWM_LED_COUNT-1), PWM_BLINK_OFF); } + pwmStart (); } } @@ -148,17 +154,13 @@ static void doIdle () { mode = UIMODE_RUN; timerStart (); printf ("idle->run\n"); - speakerStart (); - _delay_ms (50); - speakerStop (); + speakerStart (SPEAKER_BEEP); } else if (accelGetShakeCount () >= 2) { /* set timer */ accelResetShakeCount (); mode = UIMODE_SELECT_COARSE; printf ("idle->select\n"); - speakerStart (); - _delay_ms (50); - speakerStop (); + speakerStart (SPEAKER_BEEP); return; } } @@ -180,13 +182,17 @@ static void doRun () { printf("s:%uss:%u\n", step, substep); if (step == 0) { /* blink all leds */ + pwmStop (); for (uint8_t i = 0; i < PWM_LED_COUNT; i++) { pwmSetBlink (i, 1); } + pwmStart (); step = ALARM_TIME; mode = UIMODE_ALARM; printf ("run->alarm\n"); + speakerStart (SPEAKER_BEEP); } else { + pwmStop (); for (uint8_t i = 0; i < step-1; i++) { pwmSetBlink (horizonLed (i), PWM_BLINK_ON); } @@ -194,11 +200,13 @@ static void doRun () { for (uint8_t i = step; i < PWM_LED_COUNT; i++) { pwmSetBlink (horizonLed (i), PWM_BLINK_OFF); } + pwmStart (); } } else if (horizonChanged) { /* stop timer */ mode = UIMODE_IDLE; printf ("run->idle (stopped)\n"); + speakerStart (SPEAKER_BEEP); } } @@ -212,9 +220,7 @@ static void doAlarm () { timerStop (); step = 0; /* stop blinking */ - for (uint8_t i = 0; i < PWM_LED_COUNT; i++) { - pwmSetBlink (i, PWM_BLINK_OFF); - } + pwmStop (); mode = UIMODE_IDLE; } } @@ -227,9 +233,7 @@ static void doInit () { if (h != HORIZON_NONE) { mode = UIMODE_IDLE; printf ("init->idle\n"); - for (uint8_t i = 0; i < PWM_LED_COUNT; i++) { - pwmSetBlink (horizonLed (i), PWM_BLINK_OFF); - } + pwmStop (); } } @@ -244,6 +248,29 @@ static void cpuSleep () { /* Main loop */ void uiLoop () { +#if 0 + /* LED test mode */ + uint8_t i = 0; + uint8_t blink = 0; + while (1) { + pwmStop (); + for (uint8_t j = 0; j < PWM_LED_COUNT; j++) { + pwmSetBlink (horizonLed (j), PWM_BLINK_OFF); + } + pwmSetBlink (horizonLed (i), blink == 0 ? PWM_BLINK_ON : blink); + ++i; + if (i >= PWM_LED_COUNT) { + i = 0; + ++blink; + if (blink >= 3) { + blink = 0; + } + } + pwmStart (); + _delay_ms (1000); + } +#endif + while (1) { processSensors (); |