pomodoro-avr

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

main.c (2928B)


      1 /*
      2  * avr pomodoro timer
      3  * © 2022 Anton Konyahin <me@konyahin.xyz>
      4  *
      5  * This program is free software: you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation, either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This program is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17 */
     18 
     19 #include <avr/io.h>
     20 #include <avr/sleep.h>
     21 #include <avr/interrupt.h>
     22 #include <util/delay.h>
     23 #include <stdbool.h>
     24 #include <stddef.h>
     25 
     26 #include "interrupt.h"
     27 #include "music.h"
     28 #include "timer.h"
     29 
     30 #define LED_GREEN PB4
     31 #define LED_RED PB3
     32 
     33 #define WORK_MIN 25
     34 #define REST_MIN 5
     35 
     36 melody_t start_melody;
     37 melody_t work_melody;
     38 melody_t rest_melody;
     39 melody_t end_melody;
     40 
     41 enum status_e
     42 {
     43     ON,
     44     WORK,
     45     REST
     46 };
     47 
     48 int8_t status = ON;
     49 
     50 int8_t sleep = false;
     51 void status_to_on()
     52 {
     53     status = ON;
     54     PORTB &= ~(1 << LED_RED);
     55     PORTB &= ~(1 << LED_GREEN);
     56     play_melody(&end_melody);
     57     set_timer_callback(0, NULL);
     58     sleep = true;
     59 }
     60 
     61 void status_to_rest()
     62 {
     63     status = REST;
     64     PORTB &= ~(1 << LED_RED);
     65     PORTB |= (1 << LED_GREEN);
     66     play_melody(&rest_melody);
     67     set_timer_callback(REST_MIN, &status_to_on);
     68 }
     69 
     70 void status_to_work()
     71 {
     72     status = WORK;
     73     PORTB |= (1 << LED_RED);
     74     PORTB &= ~(1 << LED_GREEN);
     75     play_melody(&work_melody);
     76     set_timer_callback(WORK_MIN, &status_to_rest);
     77 }
     78 
     79 void switch_status()
     80 {
     81     if (status == ON)
     82         status_to_work();
     83     else
     84         status_to_on();
     85 }
     86 
     87 int main(void)
     88 {
     89     DDRB |= (1 << LED_GREEN);
     90     DDRB |= (1 << LED_RED);
     91 
     92     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
     93     sleep_enable();
     94     sei();
     95 
     96     init_interrupt(switch_status);
     97     init_music();
     98     init_timer();
     99 
    100     PORTB |= (1 << LED_GREEN);
    101     PORTB |= (1 << LED_RED);
    102     play_melody(&start_melody);
    103     PORTB &= ~(1 << LED_GREEN);
    104     PORTB &= ~(1 << LED_RED);
    105 
    106     while (true) {
    107         if (sleep)
    108         {
    109             sleep = false;
    110             sleep_cpu();
    111         }
    112     }
    113 
    114     return 0;
    115 }
    116 
    117 melody_t start_melody = {
    118     .length = 5,
    119     .tones = {
    120         TONE_A,
    121         TONE_D,
    122         TONE_G,
    123         TONE_E,
    124         TONE_B
    125     }
    126 };
    127 
    128 melody_t work_melody = {
    129     .length = 4,
    130     .tones = {
    131         TONE_A,
    132         TONE_E,
    133         TONE_F,
    134         TONE_D
    135     }
    136 };
    137 
    138 melody_t rest_melody = {
    139     .length = 4,
    140     .tones = {
    141         TONE_A,
    142         TONE_D,
    143         TONE_E,
    144         TONE_A
    145     }
    146 };
    147 
    148 melody_t end_melody = {
    149     .length = 6,
    150     .tones = {
    151         TONE_A,
    152         TONE_B,
    153         TONE_A,
    154         TONE_C,
    155         TONE_A,
    156         TONE_D
    157     }
    158 };