pomodoro-avr

Pomodoro timer на базе Attiny45
git clone git://git.konyahin.xyz/pomodoro-avr
Log | Files | Refs | LICENSE

commit e7d4be86c104bdd2c83a9e95102987b52259fd31
parent 698fb60c28720a888c261458f154fb5e3aafeb01
Author: Anton Konyahin <me@konyahin.xyz>
Date:   Wed, 13 Apr 2022 22:31:51 +0300

add timer

Diffstat:
MMakefile | 4++--
Minterrupt.c | 5++++-
Minterrupt.h | 5+++++
Mmain.c | 142++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
Mmusic.h | 6++++++
Atimer.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
Atimer.h | 10++++++++++
7 files changed, 156 insertions(+), 63 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,11 +1,11 @@ PROG=USBasp MCU=attiny45 -F_CPU=1200000 +F_CPU=1000000 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 music.c +SRCS=main.c interrupt.c music.c timer.c build: $(SRCS) $(CC) $(CFLAGS) -o $(TARGET).bin $(SRCS) diff --git a/interrupt.c b/interrupt.c @@ -4,14 +4,17 @@ #include "interrupt.h" -static void (*callback)(void); +static void (*callback) (void); void init_interrupt(void (*on_press) (void)) { callback = on_press; + // as input DDRB &= ~(1 << BUTTON); + // activate inner pull-up resitor + PORTB |= (1 << BUTTON); cli(); // enable pin-change interrupt diff --git a/interrupt.h b/interrupt.h @@ -1,5 +1,10 @@ +#ifndef INTERRUPTH +#define INTERRUPTH + #define BUTTON PB1 #define INTERRUPT PCINT1 void init_interrupt(void (*on_press) (void)); + +#endif diff --git a/main.c b/main.c @@ -1,13 +1,93 @@ #include <avr/io.h> +#include <avr/sleep.h> #include <util/delay.h> #include <stdbool.h> +#include <stddef.h> #include "interrupt.h" #include "music.h" +#include "timer.h" #define LED_GREEN PB4 #define LED_RED PB3 +#define WORK_MIN 25 +#define REST_MIN 5 + +melody_t start_melody; +melody_t work_melody; +melody_t rest_melody; +melody_t end_melody; + +enum status_e +{ + ON, + WORK, + REST +}; + +int8_t status = ON; + +void status_to_on() +{ + status = ON; + PORTB &= ~(1 << LED_RED); + PORTB &= ~(1 << LED_GREEN); + play_melody(&end_melody); + set_timer_callback(0, NULL); + + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + sleep_enable(); + sleep_cpu(); +} + +void status_to_rest() +{ + status = REST; + PORTB &= ~(1 << LED_RED); + PORTB |= (1 << LED_GREEN); + play_melody(&rest_melody); + set_timer_callback(REST_MIN, &status_to_on); +} + +void status_to_work() +{ + status = WORK; + PORTB |= (1 << LED_RED); + PORTB &= ~(1 << LED_GREEN); + play_melody(&work_melody); + set_timer_callback(WORK_MIN, &status_to_rest); +} + +void switch_status() +{ + if (status == ON) + status_to_work(); + else + status_to_on(); +} + +int main(void) +{ + DDRB |= (1 << LED_GREEN); + DDRB |= (1 << LED_RED); + + init_interrupt(switch_status); + init_music(); + init_timer(); + + PORTB |= (1 << LED_GREEN); + PORTB |= (1 << LED_RED); + play_melody(&start_melody); + PORTB &= ~(1 << LED_GREEN); + PORTB &= ~(1 << LED_RED); + + while (true) { + } + + return 0; +} + melody_t start_melody = { .length = 5, .tones = { @@ -19,7 +99,7 @@ melody_t start_melody = { } }; -melody_t start_work_melody = { +melody_t work_melody = { .length = 4, .tones = { TONE_A, @@ -29,7 +109,7 @@ melody_t start_work_melody = { } }; -melody_t start_rest_melody = { +melody_t rest_melody = { .length = 4, .tones = { TONE_A, @@ -50,61 +130,3 @@ melody_t end_melody = { TONE_D } }; - -enum status_e -{ - ON, - WORK, - REST -}; - -int8_t status = ON; - -void set_status() -{ - switch (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; - } -} - -void switch_status() -{ - if (++status > REST) - status = ON; - set_status(); -} - -int main(void) -{ - DDRB |= (1 << LED_GREEN); - DDRB |= (1 << LED_RED); - - 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); - - while (true) { - } - - return 0; -} diff --git a/music.h b/music.h @@ -1,3 +1,6 @@ +#ifndef MUSICH +#define MUSICH + #define SOUND PB0 #define TONE_A 42 @@ -25,3 +28,6 @@ init_music(void); void play_melody(const melody_t *melody); + + +#endif diff --git a/timer.c b/timer.c @@ -0,0 +1,47 @@ +#include <avr/io.h> +#include <avr/interrupt.h> +#include <stddef.h> + +#include "timer.h" + +static uint8_t minutes = 0; +static uint8_t seconds = 0; +static void (*callback) (void) = NULL; + +ISR (TIMER1_COMPA_vect) +{ + if (seconds > 0) + { + --seconds; + return; + } + else + { + seconds = 60; + --minutes; + } + if (minutes == 0 && callback != NULL) + callback(); +} + +void +init_timer(void) +{ + cli(); + // value for compare + OCR1A = 244; + // enable compare unterrupt + TIMSK |= (1 << OCIE1A); + // CTC1 start comparing + // CS13 CS12 CS10 - CK/4096 prescaling + TCCR1 |= (1 << CTC1) | (1 << CS13) | (1 << CS12) | (1 << CS10); + sei(); +} + +void +set_timer_callback(uint8_t min, void (*call) (void)) +{ + minutes = min; + callback = call; + seconds = 60; +} diff --git a/timer.h b/timer.h @@ -0,0 +1,10 @@ +#ifndef TIMERH +#define TIMERH + +void +init_timer(void); + +void +set_timer_callback(uint8_t min, void (*call) (void)); + +#endif