PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Revision:
35:4f7edccf8ed6
Parent:
34:427237571c6a
--- a/POKITTO_HW/HWSound.cpp	Sat Mar 24 13:17:22 2018 +0000
+++ b/POKITTO_HW/HWSound.cpp	Mon Apr 02 22:37:04 2018 +0000
@@ -44,15 +44,16 @@
 #include "Synth.h"
 #include "timer_11u6x.h"
 #include "clock_11u6x.h"
+#include "HWLCD.h"
 //#include "beat_11025.h"
 
 
-
+Pokitto::Sound __shw;
 
 using namespace Pokitto;
 
 #ifndef POK_SIM
-#if POK_ENABLE_SOUND > 0
+#if POK_ENABLE_SOUND
 pwmout_t* obj = &audiopwm;
 #endif
 #endif
@@ -78,7 +79,11 @@
 /** stream output and status */
 uint8_t Pokitto::streambyte,Pokitto::streamon;
 
-uint8_t soundbuf[256], soundbufindex=0, Pokitto::HWvolume=0;
+uint8_t soundbuf[SBUFSIZE];
+uint8_t Pokitto::HWvolume=0;
+uint16_t soundbufindex;
+uint8_t* soundbufptr;
+
 bool volpotError=false; //test for broken MCP4018
 
 uint16_t soundbyte;
@@ -121,6 +126,7 @@
  */
 
 uint32_t p=0;
+uint8_t pixx=0, pixy=0;
 
 extern "C" void TIMER32_0_IRQHandler(void)
 {
@@ -132,7 +138,11 @@
     	Pokitto::audio_IRQ();
     	#else
     	/** NOT GAMEBUINO SOUND **/
-		pokSoundIRQ();
+            #if POK_SOUND_BUFFERED
+                pokSoundBufferedIRQ();
+            #else
+                pokSoundIRQ();
+            #endif
 		#endif
 	}
 }
@@ -194,7 +204,7 @@
     value >>= 1;
     if (value & 1) SET_DAC7 else CLR_DAC7;
     #else
-    //uint32_t val;
+
     //val = value<<28; //lower 4 bits go higher - because port mask is used, no AND is needed to clear bits
     //val += value<<(15-4); //higher 4 bits go lower. No need to shift by 15 because bits are in the higher nibble
     /* daniel has made a mistake with ports */
@@ -204,13 +214,17 @@
     //LPC_GPIO_PORT->MPIN[1] = val; // write bits to port
     //CLR_MASK_DAC;
     /* fixed here */
-    /*val=value;
+    #define MASKED_DAC 0
+    #if MASKED_DAC
+    uint32_t val;
+    val=value;
     SET_MASK_DAC_LO;
     LPC_GPIO_PORT->MPIN[1] = val<<28; // write lower 4 bits to port
     CLR_MASK_DAC_LO;
     SET_MASK_DAC_HI;
     LPC_GPIO_PORT->MPIN[2] = val<<(20-4); // write bits to port
-    CLR_MASK_DAC_HI; */
+    CLR_MASK_DAC_HI;
+    #else
     if (value & 1) SET_DAC0 else CLR_DAC0;
     value >>= 1;
     if (value & 1) SET_DAC1 else CLR_DAC1;
@@ -226,6 +240,7 @@
     if (value & 1) SET_DAC6 else CLR_DAC6;
     value >>= 1;
     if (value & 1) SET_DAC7 else CLR_DAC7;
+    #endif //MASKED_DAC
     //CLR_MASK_DAC;
     #endif // BOARDREV
     #endif
@@ -233,11 +248,14 @@
 
 /** SOUND INIT **/
 void Pokitto::soundInit() {
+    #if POK_ENABLE_SOUND > 0
     uint32_t timerFreq;
-    #if POK_ENABLE_SOUND
+    #if POK_USE_PWM
+
     pwmout_init(&audiopwm,POK_AUD_PIN);
     pwmout_period_us(&audiopwm,POK_AUD_PWM_US); //was 31us
     pwmout_write(&audiopwm,0.1f);
+    #endif
 
     //#if POK_GBSOUND > 0
     /** GAMEBUINO SOUND **/
@@ -287,7 +305,14 @@
     #if POK_BOARDREV == 2
         initHWvolumecontrol();
     #endif
-	#endif //POK_ENABLE_SOUND
+
+    #if POK_ENABLE_SYNTH
+        emptyOscillators();
+    #endif
+
+
+    #endif // POK_ENABLE_SOUND
+
 }
 
 
@@ -314,20 +339,33 @@
 
 
 
-void pokSoundBufferedIRQ() {
-           uint8_t output = soundbuf[soundbufindex++];
+inline void pokSoundBufferedIRQ() {
+           uint8_t output = soundbuf[soundbufindex+=Pokitto::streamon];
+           if (soundbufindex==SBUFSIZE) soundbufindex=0;
            //if (p==sizeof(beat_11025_raw)) p=0;
            //soundbuf[soundbufindex++] = output;
-           uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*output)>>8); //cut out float
-           obj->pwm->MATCHREL1 = t_on;
+           //uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*output)>>8); //cut out float
+           //obj->pwm->MATCHREL1 = t_on;
+           dac_write(output);
+
+           //setDRAMpoint(pixx, pixy);
 }
 
-void pokSoundIRQ() {
+inline void pokSoundIRQ() {
     #if POK_ENABLE_SOUND > 0
+    //#define TICKY 0xFFFF //160
+    //#define INCY 409
     uint8_t output=0;
     streamon=1;
+    //if (test==TICKY) test=0;
+    //if (test<(TICKY/2)) { tpin=1; pwmout_write(&audiopwm,(float)0/(float)255);}//dac_write(0);}
+    //else {tpin=0; pwmout_write(&audiopwm,(float)255/(float)255);}//dac_write(64);}
+    //test+=INCY;
+    //return;
     #ifndef POK_SIM
-    pwmout_t* obj = &audiopwm;
+        #if POK_USE_PWM
+        pwmout_t* obj = &audiopwm;
+        #endif
     #endif
     #if POK_STREAMING_MUSIC > 0
         #if POK_STREAMFREQ_HALVE
@@ -361,7 +399,7 @@
         /** if song is being played from sd **/
         if (playing) {
                 notetick++;
-                updatePlaybackSD(playerpos&7);
+                updatePlayback();
         }
         /** oscillators update **/
         osc1.count += osc1.cinc + (osc1.pitchbend >> 4); // counts to 65535 and overflows to zero WAS 8 !
@@ -387,35 +425,41 @@
                 #if POK_STREAM_TO_DAC > 0
                     /** stream goes to DAC */
                     #if POK_USE_DAC > 0
-                    if (streamstep) dac_write((uint8_t)streambyte); // duty cycle
+                    dac_write(streambyte>>__shw.headPhoneLevel); // duty cycle
                     #endif // POK_USE_DAC
                 #else
                     /** stream goes to PWM */
                     if (streamstep) {
                             //pwmout_write(&audiopwm,(float)streambyte/(float)255);
-                            uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*streambyte)>>8); //cut out float
+                            #if POK_USE_PWM
+                            uint32_t t_on = (uint32_t)((((obj->pwm->MATCHREL0)*streambyte)>>8)>>__shw.headPhoneLevel); //cut out float
                             obj->pwm->MATCHREL1 = t_on;
+                            #endif
                             //dac_write((uint8_t)streambyte); // duty cycle
                     }
                 #endif // POK_STREAM_TO_DAC
             #endif // POK_STREAMING_MUSIC
             #if POK_STREAM_TO_DAC > 0
-            /** synth goes to PWM */
-            //pwmout_write(&audiopwm,(float)output/(float)255);
-            uint32_t t_on = (uint32_t)(((obj->pwm->MATCHREL0)*output)>>8); //cut out float
-            obj->pwm->MATCHREL1 = t_on;
-            #else
-            dac_write((uint8_t)output);
-            #endif // decide where synth is output
+                /** synth goes to PWM */
+                //pwmout_write(&audiopwm,(float)output/(float)255);
+                #if POK_USE_PWM
+                    uint32_t t_on = (uint32_t)((((obj->pwm->MATCHREL0)*output)>>8)>>__shw.headPhoneLevel); //cut out float
+                    obj->pwm->MATCHREL1 = t_on;
+                #endif
+            #else // POK_STREAMING_MUSIC
+                dac_write((uint8_t)output>>__shw.headPhoneLevel); // SYNTH to DAC
+            #endif
             soundbyte = (output+streambyte)>>1;
             soundbuf[soundbufindex++]=soundbyte;
+            if (soundbufindex==256) soundbufindex=0;
         #endif //POK_ENABLE_SOUND
     #endif // HARDWARE
-    #endif //POK_ENABLE_SOUND
+#endif //POK_ENABLE_SOUND
 }
 
 
 void Pokitto::updateSDAudioStream() {
+    #ifndef NOPETITFATFS
     if (streamPaused()) return;
 
     #if POK_STREAMING_MUSIC > 0
@@ -441,6 +485,7 @@
         #endif
     }
     #endif
+    #endif // NOPETITFATFS
 }