123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- #include "util.h"
- #include <util/delay.h>
- #include <avr/sleep.h>
- #include <avr/interrupt.h>
- #include <avr/eeprom.h>
- #include "../encoder/morse_encoder.h"
- #define MORSE_BEEPER_PORT PORTB
- #define MORSE_BEEPER_DDR DDRB
- #define MORSE_BEEPER_BIT 0
- static const morse_sym_t EEMEM morse_symbols[32];
- enum {
- BEEPER_STOPPED,
- BEEPER_NEXTSYM,
- BEEPER_INSYM,
- };
- static uint8_t beeper_state;
- static uint16_t timer;
- static uint16_t counter;
- static uint8_t morse_sym_ptr;
- static uint8_t morse_sym_size;
- static uint16_t morse_sym_marks;
- #define TIMER_INBEEP 1
- #define TIMER_DIT (TIMER_INBEEP * 240)
- #define TIMER_DAH (TIMER_DIT * 3)
- #define TIMER_INTERMARK (TIMER_DIT * 1)
- #define TIMER_INTERCHAR (TIMER_DIT * 3)
- #define TIMER_INTERWORD (TIMER_DIT * 7)
- ISR(TIM0_COMPA_vect)
- {
- if (timer)
- timer--;
- }
- static inline void timer_set(uint16_t value)
- {
- timer = value;
- }
- static inline uint8_t timer_running(void)
- {
- return (timer != 0);
- }
- static void timer_init(void)
- {
- TCCR0B = 0;
- TIFR0 |= (1 << OCF0A);
- TIMSK0 |= (1 << OCIE0A);
- TCNT0 = 0;
- OCR0A = 38;
- TCCR0A = (1 << WGM01);
- TCCR0B = (0 << CS02) | (1 << CS01) | (1 << CS00);
- }
- static void beeper_startup(void)
- {
- beeper_state = BEEPER_NEXTSYM;
- morse_sym_ptr = 0;
- }
- static void beeper_stop(void)
- {
- beeper_state = BEEPER_STOPPED;
- MORSE_BEEPER_PORT &= ~(1 << MORSE_BEEPER_BIT);
- }
- static inline uint8_t int0_triggered(void)
- {
- return !(PINB & (1 << 1));
- }
- ISR(INT0_vect)
- {
- if (beeper_state == BEEPER_STOPPED)
- beeper_startup();
- else
- beeper_stop();
- while (1) {
- while (int0_triggered())
- ;
- _delay_ms(50);
- if (!int0_triggered())
- break;
- }
- }
- static void run_beeper(void)
- {
- morse_sym_t sym;
- if (timer_running())
- return;
- switch (beeper_state) {
- case BEEPER_NEXTSYM:
- if (morse_sym_ptr >= ARRAY_SIZE(morse_symbols)) {
- beeper_stop();
- return;
- }
- sym = eeprom_read_word(&morse_symbols[morse_sym_ptr]);
- if (sym == 0xFFFF) {
- beeper_stop();
- return;
- }
- morse_sym_ptr++;
- if (MORSE_SYM_IS_SPACE(sym)) {
- timer_set(TIMER_INTERWORD);
- return;
- }
- morse_sym_marks = MORSE_SYM_MARKS(sym);
- morse_sym_size = MORSE_SYM_SIZE(sym);
- beeper_state = BEEPER_INSYM;
- timer_set(TIMER_INBEEP);
- counter = 0;
-
- case BEEPER_INSYM:
- if (counter == 0) {
- if (!morse_sym_size) {
- beeper_state = BEEPER_NEXTSYM;
- timer_set(TIMER_INTERCHAR);
- return;
- }
- if ((morse_sym_marks & 1) == MORSE_DIT)
- counter = TIMER_DIT;
- else
- counter = TIMER_DAH;
- morse_sym_size--;
- morse_sym_marks >>= 1;
- } else {
- counter--;
- if (counter == 0) {
- timer_set(TIMER_INTERMARK);
- MORSE_BEEPER_PORT &= ~(1 << MORSE_BEEPER_BIT);
- return;
- }
- }
- MORSE_BEEPER_PORT ^= (1 << MORSE_BEEPER_BIT);
- timer_set(TIMER_INBEEP);
- break;
- }
- }
- int main(void)
- {
- irq_disable();
- PORTB = 0xFF;
- DDRB = 0x00;
- MORSE_BEEPER_DDR |= (1 << MORSE_BEEPER_BIT);
- MORSE_BEEPER_PORT &= ~(1 << MORSE_BEEPER_BIT);
-
- MCUCR |= (0 << ISC01) | (0 << ISC00);
- PORTB |= (1 << 1);
- DDRB &= ~(1 << 1);
- GIMSK |= (1 << INT0);
- timer_init();
- set_sleep_mode(SLEEP_MODE_IDLE);
- sleep_enable();
- while (1) {
- mb();
- if (beeper_state == BEEPER_STOPPED)
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- else
- set_sleep_mode(SLEEP_MODE_IDLE);
- irq_enable();
- sleep_cpu();
- irq_disable();
- if (beeper_state != BEEPER_STOPPED)
- run_beeper();
- }
- }
|