aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2014-07-22 15:58:55 +0200
committerLars-Dominik Braun <lars@6xq.net>2014-07-22 15:59:09 +0200
commitbedbcdc4de680720deff4b6e113a3ec1a0690482 (patch)
tree448003d0a8d8a356c956baa427bc3c35f620d314
parent42e32ed74655e98e9f2d5a0152a047dcf9e72806 (diff)
downloadhourglass-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.c1
-rw-r--r--pwm.c15
-rw-r--r--speaker.c32
-rw-r--r--speaker.h6
-rw-r--r--timer.c3
-rw-r--r--uart.c3
-rw-r--r--ui.c51
7 files changed, 73 insertions, 38 deletions
diff --git a/main.c b/main.c
index 37592a7..c5a7b1d 100644
--- a/main.c
+++ b/main.c
@@ -47,7 +47,6 @@ int main () {
sei ();
gyroStart ();
accelStart ();
- pwmStart ();
uiLoop ();
diff --git a/pwm.c b/pwm.c
index c38a162..669d9c3 100644
--- a/pwm.c
+++ b/pwm.c
@@ -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) {
diff --git a/speaker.c b/speaker.c
index 2eace8d..92c1e78 100644
--- a/speaker.c
+++ b/speaker.c
@@ -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);
}
diff --git a/speaker.h b/speaker.h
index dec1d20..a035a6b 100644
--- a/speaker.h
+++ b/speaker.h
@@ -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 */
diff --git a/timer.c b/timer.c
index 3565764..d2634f2 100644
--- a/timer.c
+++ b/timer.c
@@ -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
diff --git a/uart.c b/uart.c
index 6906a8c..5edac91 100644
--- a/uart.c
+++ b/uart.c
@@ -1,8 +1,9 @@
+#include "common.h"
+
#include <stdio.h>
#include <avr/io.h>
#include "uart.h"
-#include "common.h"
/* blocking uart send
*/
diff --git a/ui.c b/ui.c
index 0bfe7ba..cadbcbf 100644
--- a/ui.c
+++ b/ui.c
@@ -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 ();