]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - quantum/audio.c
audio enable stored in eeprom
[qmk_firmware.git] / quantum / audio.c
index f29d941d7cce0be321a060b38ae2278806a9223c..73985479cceb763bcdf5d720e941d611d6554358 100644 (file)
@@ -8,6 +8,8 @@
 #include "audio.h"
 #include "keymap_common.h"
 
+#include "eeconfig.h"
+
 #define PI 3.14159265
 
 // #define PWM_AUDIO
@@ -57,6 +59,25 @@ uint8_t notes_length;
 bool notes_repeat;
 uint8_t current_note = 0;
 
+audio_config_t audio_config;
+
+
+void audio_toggle(void) {
+    audio_config.enable ^= 1;
+    eeconfig_write_audio(audio_config.raw);
+}
+
+void audio_on(void) {
+    audio_config.enable = 1;
+    eeconfig_write_audio(audio_config.raw);
+}
+
+void audio_off(void) {
+    audio_config.enable = 0;
+    eeconfig_write_audio(audio_config.raw);
+}
+
+
 void stop_all_notes() {
     voices = 0;
     #ifdef PWM_AUDIO
@@ -129,6 +150,12 @@ void stop_note(double freq) {
 
 void init_notes() {
 
+    /* check signature */
+    if (!eeconfig_is_enabled()) {
+        eeconfig_init();
+    }
+    audio_config.raw = eeconfig_read_audio();
+
     #ifdef PWM_AUDIO
         PLLFRQ = _BV(PDIV2);
         PLLCSR = _BV(PLLE);
@@ -160,7 +187,6 @@ void init_notes() {
 
 
 ISR(TIMER3_COMPA_vect) {
-
     if (note) {
         #ifdef PWM_AUDIO
             if (voices == 1) {
@@ -255,7 +281,12 @@ ISR(TIMER3_COMPA_vect) {
 
 
         note_position++;
-        if (note_position >= note_length) {
+        bool end_of_note = false;
+        if (ICR3 > 0) 
+            end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF));
+        else 
+            end_of_note = (note_position >= (note_length * 0x7FF));
+        if (end_of_note) {
             current_note++;
             if (current_note >= notes_length) {
                 if (notes_repeat) {
@@ -283,9 +314,16 @@ ISR(TIMER3_COMPA_vect) {
 
     }
 
+    if (!audio_config.enable) {
+        notes = false;
+        note = false;
+    }
 }
 
 void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat) {
+
+if (audio_config.enable) {
+
     if (note)
         stop_all_notes();
     notes = true;
@@ -314,7 +352,12 @@ void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat) {
     #endif
 }
 
+}
+
 void play_sample(uint8_t * s, uint16_t l, bool r) {
+
+if (audio_config.enable) {
+
     stop_all_notes();
     place_int = 0;
     sample = s;
@@ -325,9 +368,15 @@ void play_sample(uint8_t * s, uint16_t l, bool r) {
         TIMSK3 |= _BV(OCIE3A);
     #else
     #endif
+
+}
+
 }
 
 void play_note(double freq, int vol) {
+
+if (audio_config.enable) {
+
     if (notes)
         stop_all_notes();
     note = true;
@@ -362,4 +411,6 @@ void play_note(double freq, int vol) {
         TCCR3A |= _BV(COM3A1);
     #endif
 
+}
+
 }
\ No newline at end of file