changeset 352:749aa016fa16 prerelease

pinMode can now be controlled from Pd
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 08 Jun 2016 04:31:21 +0100
parents 09397ded8966
children bec5d43e4e7a
files examples/basic_libpd/render.cpp
diffstat 1 files changed, 62 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/examples/basic_libpd/render.cpp	Wed Jun 08 02:00:17 2016 +0100
+++ b/examples/basic_libpd/render.cpp	Wed Jun 08 04:31:21 2016 +0100
@@ -51,11 +51,64 @@
 
 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){
 	libpd_float((char*)receiverName, (float)state);
 //	rt_printf("%s: %d\n", (char*)receiverName, state);
 }
 
+#define LIBPD_DIGITAL_OFFSET 11 // digitals are preceded by 2 audio and 8 analogs (even if using a different number of analogs)
+
+void Bela_messageHook(const char *source, const char *symbol, int argc, t_atom *argv){
+	if(strcmp(source, "bela_setDigital") == 0){
+		// 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 direction;
+		if(strcmp(symbol, "in") == 0){
+			direction = INPUT;
+		} else if(strcmp(symbol, "out") == 0){
+			direction = OUTPUT;
+		} else {
+			error = true;
+		}
+		if(argc == 0){
+			error = true;
+		} else if (libpd_is_float(&argv[0]) == false){
+			error = true;
+		}
+		if(error == true){
+			rt_printf("bela_setDigital requires at least [direction channel(\n");
+			return;
+		}
+		int channel = libpd_get_float(&argv[0]) - LIBPD_DIGITAL_OFFSET;
+		if(argc >= 2){
+			t_atom* a = &argv[1];
+			if(libpd_is_symbol(a)){
+				char *s = libpd_get_symbol(a);
+				if(strcmp(s, "~") == 0  || strncmp(s, "sig", 3) == 0){
+					signal = true;
+				}
+			}
+		}
+		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);
+	}
+}
+
 void Bela_floatHook(const char *source, float value){
 //	rt_printf("received: %s %f\n", source, value);
 	// let's make this as optimized as possible for built-in digital Out parsing
@@ -131,6 +184,7 @@
 	// set hooks before calling libpd_init
 	libpd_set_printhook(Bela_printHook);
 	libpd_set_floathook(Bela_floatHook);
+	libpd_set_messagehook(Bela_messageHook);
 	libpd_set_noteonhook(pdnoteon);
 	//TODO: add hooks for other midi events and generate MIDI output appropriately
 	libpd_init();
@@ -167,6 +221,7 @@
 	libpd_bind("bela_digitalOut24");
 	libpd_bind("bela_digitalOut25");
 	libpd_bind("bela_digitalOut26");
+	libpd_bind("bela_setDigital");
 
 	libpdReadFilesTask = Bela_createAuxiliaryTask(libpdReadFilesLoop, 60, "libpdReadFiles");
 	Bela_scheduleAuxiliaryTask(libpdReadFilesTask);
@@ -339,12 +394,14 @@
 		}
 
 		// digital out at message-rate
-		if(clearDataOut || setDataOut){
+		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) {
-					// TODO: only process the channels marked as such
-				uint32_t outWord = context->digital[frame];
-				outWord = outWord | (setDataOut << 16);
-				outWord = outWord &~ (clearDataOut << 16);
+				outWord = context->digital[frame];
+				outWord = outWord | orWord;
+				outWord = outWord &~ andTildeWord;
 				context->digital[frame] = outWord;
 			}
 		}