commit 698fb60c28720a888c261458f154fb5e3aafeb01
parent d801e67aed47f7f4f3b89d619eaf286a15e12eb8
Author: Anton Konyahin <me@konyahin.xyz>
Date: Tue, 12 Apr 2022 21:23:24 +0300
add music
Diffstat:
6 files changed, 122 insertions(+), 12 deletions(-)
diff --git a/Makefile b/Makefile
@@ -5,7 +5,7 @@ CC=avr-gcc
OBJCOPY=avr-objcopy
CFLAGS=-std=c99 -Wall -g -Os -mmcu=$(MCU) -DF_CPU=$(F_CPU) -I.
TARGET=pomodoro
-SRCS=main.c interrupt.c
+SRCS=main.c interrupt.c music.c
build: $(SRCS)
$(CC) $(CFLAGS) -o $(TARGET).bin $(SRCS)
diff --git a/interrupt.c b/interrupt.c
@@ -24,7 +24,8 @@ init_interrupt(void (*on_press) (void))
ISR (PCINT0_vect)
{
int8_t pressed = !(PINB & (1 << BUTTON));
- if (pressed) {
+ if (pressed)
+ {
_delay_ms(30);
pressed = !(PINB & (1 << BUTTON));
if (pressed)
diff --git a/interrupt.h b/interrupt.h
@@ -1,5 +1,5 @@
-#define BUTTON PB3
-#define INTERRUPT PCINT3
+#define BUTTON PB1
+#define INTERRUPT PCINT1
void
init_interrupt(void (*on_press) (void));
diff --git a/main.c b/main.c
@@ -1,15 +1,58 @@
#include <avr/io.h>
-#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdbool.h>
#include "interrupt.h"
+#include "music.h"
#define LED_GREEN PB4
-#define LED_RED PB1
-#define SOUND PB0
+#define LED_RED PB3
-enum status_e {
+melody_t start_melody = {
+ .length = 5,
+ .tones = {
+ TONE_A,
+ TONE_D,
+ TONE_G,
+ TONE_E,
+ TONE_B
+ }
+};
+
+melody_t start_work_melody = {
+ .length = 4,
+ .tones = {
+ TONE_A,
+ TONE_E,
+ TONE_F,
+ TONE_D
+ }
+};
+
+melody_t start_rest_melody = {
+ .length = 4,
+ .tones = {
+ TONE_A,
+ TONE_D,
+ TONE_E,
+ TONE_A
+ }
+};
+
+melody_t end_melody = {
+ .length = 6,
+ .tones = {
+ TONE_A,
+ TONE_B,
+ TONE_A,
+ TONE_C,
+ TONE_A,
+ TONE_D
+ }
+};
+
+enum status_e
+{
ON,
WORK,
REST
@@ -24,14 +67,17 @@ void set_status()
case ON:
PORTB &= ~(1 << LED_RED);
PORTB &= ~(1 << LED_GREEN);
+ play_melody(&end_melody);
break;
case WORK:
PORTB |= (1 << LED_RED);
PORTB &= ~(1 << LED_GREEN);
+ play_melody(&start_work_melody);
break;
case REST:
PORTB &= ~(1 << LED_RED);
PORTB |= (1 << LED_GREEN);
+ play_melody(&start_rest_melody);
break;
}
}
@@ -47,13 +93,16 @@ int main(void)
{
DDRB |= (1 << LED_GREEN);
DDRB |= (1 << LED_RED);
- DDRB |= (1 << SOUND);
init_interrupt(switch_status);
+ init_music();
+
+ PORTB |= (1 << LED_GREEN);
+ PORTB |= (1 << LED_RED);
+ play_melody(&start_melody);
+ PORTB &= ~(1 << LED_GREEN);
+ PORTB &= ~(1 << LED_RED);
- cli();
- sei();
-
while (true) {
}
diff --git a/music.c b/music.c
@@ -0,0 +1,33 @@
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <util/delay.h>
+#include <stdarg.h>
+
+#include "music.h"
+
+void
+play_melody(const melody_t *melody)
+{
+ // set clk/8 prescaling
+ TCCR0B |= (1 << CS01);
+
+ for (int8_t i = 0; i < melody->length; i++)
+ {
+ OCR0A = melody->tones[i];
+ _delay_ms(DELAY);
+ }
+
+ // disable timer
+ TCCR0B &= ~(1 << CS01);
+}
+
+void
+init_music(void)
+{
+ DDRB |= (1 << SOUND);
+ PORTB &= ~(1 << SOUND);
+
+ // set timer setting
+ TCCR0A |= (1 << WGM01);
+ TCCR0A |= (1 << COM0A0);
+}
diff --git a/music.h b/music.h
@@ -0,0 +1,27 @@
+#define SOUND PB0
+
+#define TONE_A 42
+#define TONE_AS 39
+#define TONE_B 37
+#define TONE_C 71
+#define TONE_CS 67
+#define TONE_D 63
+#define TONE_DS 59
+#define TONE_E 56
+#define TONE_F 53
+#define TONE_FS 50
+#define TONE_G 47
+#define TONE_GS 44
+
+#define DELAY 250
+
+typedef struct melody_s {
+ uint8_t length;
+ uint8_t tones[];
+} melody_t;
+
+void
+init_music(void);
+
+void
+play_melody(const melody_t *melody);