changeset 353:bec5d43e4e7a prerelease

Digital channels are fully managed by Pd
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 08 Jun 2016 19:44:21 +0100
parents 749aa016fa16
children 1b23300f3e78
files examples/basic_libpd/render.cpp
diffstat 1 files changed, 26 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/examples/basic_libpd/render.cpp	Wed Jun 08 04:31:21 2016 +0100
+++ b/examples/basic_libpd/render.cpp	Wed Jun 08 19:44:21 2016 +0100
@@ -6,7 +6,7 @@
  */
 
 #include <Bela.h>
-#include <DigitalToMessage.h>
+#include <DigitalChannelManager.h>
 #include <cmath>
 #include <Utilities.h>
 #include <I2c_Codec.h>
@@ -45,15 +45,11 @@
 }
 
 #define PARSE_MIDI
-AuxiliaryTask libpdReadFilesTask;
-Midi midi;
+static AuxiliaryTask libpdReadFilesTask;
+static Midi midi;
+static DigitalChannelManager dcm;
 //UdpServer udpServer;
 
-static uint32_t clearDataOut;
-static uint32_t setDataOut;
-static uint16_t digitalModeClear;
-static uint16_t digitalModeSet;
-static uint16_t digitalRates;
 
 
 void sendDigitalMessage(bool state, unsigned int delay, void* receiverName){
@@ -68,7 +64,7 @@
 		// symbol is the direction, argv[0] is the channel, argv[1] (optional)
 		// is signal(\"sig\" or \"~\") or message(\"mess\", default) rate
 		bool error = false;
-		bool signal = false; // defaults to message rate
+		bool isMessageRate = true; // defaults to message rate
 		bool direction;
 		if(strcmp(symbol, "in") == 0){
 			direction = INPUT;
@@ -92,20 +88,11 @@
 			if(libpd_is_symbol(a)){
 				char *s = libpd_get_symbol(a);
 				if(strcmp(s, "~") == 0  || strncmp(s, "sig", 3) == 0){
-					signal = true;
+					isMessageRate = false;
 				}
 			}
 		}
-		digitalRates = changeBit(digitalRates, channel, signal);
-		if(direction == 1){
-			digitalModeClear = setBit(digitalModeClear, channel);
-			digitalModeSet = clearBit(digitalModeSet, channel);
-		} else {
-			digitalModeSet = setBit(digitalModeSet, channel);
-			digitalModeClear = clearBit(digitalModeClear, channel);
-		}
-//		rt_printf("Channel: %d, input?: %d, ~?: %d\n", channel, direction==INPUT, signal);
-//		rt_printf("modeSet 0x%x modeClear 0x%x\n", digitalModeSet, digitalModeClear);
+		dcm.setPinMode(channel, direction, isMessageRate);
 	}
 }
 
@@ -124,13 +111,7 @@
 				unsigned int channel = receiver - 11; // go back to the actual Bela digital channel number
 				if(channel >= 0 && channel < 16){ //16 is the hardcoded value for the number of digital channels
 					//TODO: here we should check if this channel has been registered as message-rate output
-					if(value == 0){
-						clearDataOut = setBit(clearDataOut, channel);
-						setDataOut = clearBit(setDataOut, channel);
-					} else {
-						setDataOut = setBit(setDataOut, channel);
-						clearDataOut = clearBit(clearDataOut, channel);
-					}
+					dcm.setOutput(channel, value);
 //					rt_printf("clear 0x%x set 0x%x\n", clearDataOut, setDataOut);
 				}
 			}
@@ -145,23 +126,17 @@
 	{"bela_digitalIn26"}
 };
 
-static DigitalToMessage** dtm;
 static unsigned int analogChannelsInUse;
 static unsigned int gLibpdBlockSize;
 static unsigned int gChannelsInUse = 26;
 
 bool setup(BelaContext *context, void *userData)
 {
+	dcm.setCallback(sendDigitalMessage);
 	analogChannelsInUse = min(context->analogChannels, gChannelsInUse - context->audioChannels - context->digitalChannels);
-	dtm = new DigitalToMessage* [context->digitalChannels];
 	if(context->digitalChannels > 0){
 		for(unsigned int ch = 0; ch < context->digitalChannels; ++ch){
-			dtm[ch] = new DigitalToMessage;
-			dtm[ch]->setCallback(sendDigitalMessage, receiverNames[ch]);
-			if((ch%2) == 0 )
-				pinMode(context, 0, ch, INPUT);
-			else
-				pinMode(context, 0, ch, OUTPUT);
+			dcm.setCallbackArgument(ch, receiverNames[ch]);
 		}
 	}
 	midi.readFrom(0);
@@ -199,10 +174,6 @@
 
 	gBufLength = max(gLibpdBlockSize, context->audioFrames);
 
-	char file[] = "_main.pd";
-	char folder[] = "./";
-	// open patch       [; pd open file folder(
-	libpd_openfile(file, folder);
 
 	// bind your receivers here
 	libpd_bind("bela_digitalOut11");
@@ -223,6 +194,10 @@
 	libpd_bind("bela_digitalOut26");
 	libpd_bind("bela_setDigital");
 
+	char file[] = "_main.pd";
+	char folder[] = "./";
+	// open patch       [; pd open file folder(
+	libpd_openfile(file, folder);
 	libpdReadFilesTask = Bela_createAuxiliaryTask(libpdReadFilesLoop, 60, "libpdReadFiles");
 	Bela_scheduleAuxiliaryTask(libpdReadFilesTask);
 
@@ -359,52 +334,37 @@
 		}
 
 		//then digital
-		 //TODO: in multiple places below we assume that the number of digitals is same as number of audio
+		// note: in multiple places below we assume that the number of digitals is same as number of audio
 		// digital in at message-rate
-		for(unsigned int n = 0; n < context->digitalChannels; ++n){
-			// note that we consider only the first sample of the block
-			// considering all of them is notably more expensive
-			// TODO: only process the channels marked as such
-			dtm[n]->process(n + 16, &context->digital[audioFrameBase], 1);
-		}
+		dcm.processInput(&context->digital[audioFrameBase], gLibpdBlockSize);
+
 		// digital in at signal-rate
 		for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
+			unsigned int digitalFrame = audioFrameBase + j;
 			for (k = 0, p1 = p0 + gLibpdBlockSize * (context->audioChannels + 8);
 					k < 16; ++k, p1 += gLibpdBlockSize) {
-				// note that we only write the last sample of the block
-				// writing all of them is notably more expensive.
-
-				// TODO: only process the channels marked as such
-				unsigned int digitalFrame = audioFrameBase + j;
-				*p1 = digitalRead(context, digitalFrame, k);
+				if(dcm.isSignalRate(k) && dcm.isInput(k)){ // only process input channels that are handled at signal rate
+					*p1 = digitalRead(context, digitalFrame, k);
+				}
 			}
 		}
 
 		libpd_process_sys(); // process the block
 
 		//digital out
-		// digital out at  signal-rate
+		// digital out at signal-rate
 		for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
 			unsigned int digitalFrame = (audioFrameBase + j);
 			for (k = 0, p1 = p0  + gLibpdBlockSize * (context->audioChannels + 8);
 					k < context->digitalChannels; k++, p1 += gLibpdBlockSize) {
-				// TODO: only process the channels marked as such
-				digitalWriteOnce(context, digitalFrame, k, *p1 > 0.5);
+				if(dcm.isSignalRate(k) && dcm.isOutput(k)){ // only process output channels that are handled at signal rate
+					digitalWriteOnce(context, digitalFrame, k, *p1 > 0.5);
+				}
 			}
 		}
 
 		// digital out at message-rate
-		if(clearDataOut || setDataOut || digitalModeSet || digitalModeClear){
-			uint32_t orWord = ((setDataOut << 16) | digitalModeSet);
-			uint32_t andTildeWord = ((clearDataOut << 16) | digitalModeClear);
-			uint32_t outWord;
-			for (unsigned int frame = audioFrameBase; frame < audioFrameBase + gLibpdBlockSize; ++frame) {
-				outWord = context->digital[frame];
-				outWord = outWord | orWord;
-				outWord = outWord &~ andTildeWord;
-				context->digital[frame] = outWord;
-			}
-		}
+		dcm.processOutput(&context->digital[audioFrameBase], gLibpdBlockSize);
 
 		//audio
 		for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) {
@@ -444,5 +404,4 @@
 
 void cleanup(BelaContext *context, void *userData)
 {
-	delete[] dtm;
 }