diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2021-05-04 02:22:31 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2021-05-04 02:22:31 +0200 |
commit | f29443cd40f9050beedab3a822b6e0f24fb789cd (patch) | |
tree | 5f53eb3d8c904af00f13d58976c8d6f04ba6339e /song.c | |
parent | af302afcb0aaa08bd9c093b2c8ff20647a001919 (diff) | |
download | tmk7637-f29443cd40f9050beedab3a822b6e0f24fb789cd.tar.gz |
optimized the LED and buzzer wiring
* since we use timer 3 exclusively for the buzzer but trigger the pin
using an IRQ handler, there is no longer any need to occupy a high-resolution
pin for the buzzer.
* PD5 became a new high resolution LED
* PD0 became the new buzzer pin
* rearranged the pins used for the different LEDs so that the distribution
of high-resolution LEDs is symmetric
* Timer 0 is no longer used/modified by setting LEDs.
This avoids some workarounds as timer 0 is also used by tmk's timer module.
* The song routines could be slightly improved using the timer module.
* documented the LED and buzzer pinout in README
Diffstat (limited to 'song.c')
-rw-r--r-- | song.c | 51 |
1 files changed, 19 insertions, 32 deletions
@@ -193,11 +193,13 @@ void song_play_ruinen(void) { /* could be activated due to keyclick mode */ keyclick_solenoid_set(false); - pwm_pb5_set_tone(0); + pwm_pd0_set_tone(0); uint8_t i = 0; for (long unsigned int cur_note = 0; cur_note < sizeof(song_ruinen)/sizeof(song_ruinen[0]); cur_note++) { - pwm_pb5_set_tone(pgm_read_word(&song_ruinen[cur_note].freq)); + uint16_t cur_note_time = timer_read(); + + pwm_pd0_set_tone(pgm_read_word(&song_ruinen[cur_note].freq)); uint8_t max_brightness = ((uint32_t)pgm_read_word(&song_ruinen[cur_note].freq)*255)/600; uint16_t fade_dur = pgm_read_word(&song_ruinen[cur_note].dur)/2/(max_brightness+1); @@ -206,14 +208,6 @@ void song_play_ruinen(void) delay_long(fade_dur); } - /* - * The fade duration is probably not very precise, so compensate for it. - * - * FIXME: Once timer 0 is freed, we should use timer_read() and timer_elapsed() - * which will be more precise. - */ - delay_long(pgm_read_word(&song_ruinen[cur_note].dur) - fade_dur*(max_brightness+1)*2); - for (int16_t brightness = max_brightness; brightness >= 0; brightness--) { pwm_set_led(i % 5, brightness); delay_long(fade_dur); @@ -221,12 +215,14 @@ void song_play_ruinen(void) if (pgm_read_word(&song_ruinen[cur_note].freq)) i++; - } - pwm_pb5_set_tone(0); + /* The fade duration is probably not very precise, so compensate for it. */ + uint16_t elapsed = timer_elapsed(cur_note_time); + if (elapsed < pgm_read_word(&song_ruinen[cur_note].dur)) + delay_long(pgm_read_word(&song_ruinen[cur_note].dur) - elapsed); + } - /* we screwed up timer 0 settings and they are also used by the timer module */ - timer_init(); + pwm_pd0_set_tone(0); /* restore the previous lock lights */ led_set(host_keyboard_leds()); @@ -414,7 +410,7 @@ void song_play_kitt(void) { /* could be activated due to keyclick mode */ keyclick_solenoid_set(false); - pwm_pb5_set_tone(0); + pwm_pd0_set_tone(0); int16_t i; @@ -423,38 +419,29 @@ void song_play_kitt(void) song_larsen_light(i); long unsigned int cur_note = 0; - uint16_t cur_note_dur = 0; + uint16_t cur_note_time = timer_read(); int8_t dir = 1; for (i = 0; cur_note < sizeof(song_knight_rider)/sizeof(song_knight_rider[0]); i += dir) { - /* - * FIXME: Once we freed up timer 0 by rearranging the LED pins, - * it might be more precise to use timer_read()/timer_elapsed() to wait for the next note. - */ - if (cur_note_dur < LARSEN_CURVE_STEP) { - cur_note_dur = pgm_read_word(&song_knight_rider[cur_note].dur); - pwm_pb5_set_tone(pgm_read_word(&song_knight_rider[cur_note].freq)); - } + pwm_pd0_set_tone(pgm_read_word(&song_knight_rider[cur_note].freq)); song_larsen_light(i); - cur_note_dur -= LARSEN_CURVE_STEP; - if (cur_note_dur < LARSEN_CURVE_STEP) - cur_note++; - if (i == sizeof(song_larsen_curve)-1 || (dir < 0 && i == 0)) dir *= -1; + + if (timer_elapsed(cur_note_time) >= pgm_read_word(&song_knight_rider[cur_note].dur)) { + cur_note_time = timer_read(); + cur_note++; + } } - pwm_pb5_set_tone(0); + pwm_pd0_set_tone(0); /* fade out larsen light */ while (i < sizeof(song_larsen_curve)*3/2) song_larsen_light(i++); - /* we screwed up timer 0 settings and they are also used by the timer module */ - timer_init(); - /* restore the previous lock lights */ led_set(host_keyboard_leds()); } |