comparison examples/basic_libpd/render.cpp @ 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 ebaeffa5d493
children bec5d43e4e7a
comparison
equal deleted inserted replaced
351:09397ded8966 352:749aa016fa16
49 Midi midi; 49 Midi midi;
50 //UdpServer udpServer; 50 //UdpServer udpServer;
51 51
52 static uint32_t clearDataOut; 52 static uint32_t clearDataOut;
53 static uint32_t setDataOut; 53 static uint32_t setDataOut;
54 static uint16_t digitalModeClear;
55 static uint16_t digitalModeSet;
56 static uint16_t digitalRates;
57
58
54 void sendDigitalMessage(bool state, unsigned int delay, void* receiverName){ 59 void sendDigitalMessage(bool state, unsigned int delay, void* receiverName){
55 libpd_float((char*)receiverName, (float)state); 60 libpd_float((char*)receiverName, (float)state);
56 // rt_printf("%s: %d\n", (char*)receiverName, state); 61 // rt_printf("%s: %d\n", (char*)receiverName, state);
62 }
63
64 #define LIBPD_DIGITAL_OFFSET 11 // digitals are preceded by 2 audio and 8 analogs (even if using a different number of analogs)
65
66 void Bela_messageHook(const char *source, const char *symbol, int argc, t_atom *argv){
67 if(strcmp(source, "bela_setDigital") == 0){
68 // symbol is the direction, argv[0] is the channel, argv[1] (optional)
69 // is signal(\"sig\" or \"~\") or message(\"mess\", default) rate
70 bool error = false;
71 bool signal = false; // defaults to message rate
72 bool direction;
73 if(strcmp(symbol, "in") == 0){
74 direction = INPUT;
75 } else if(strcmp(symbol, "out") == 0){
76 direction = OUTPUT;
77 } else {
78 error = true;
79 }
80 if(argc == 0){
81 error = true;
82 } else if (libpd_is_float(&argv[0]) == false){
83 error = true;
84 }
85 if(error == true){
86 rt_printf("bela_setDigital requires at least [direction channel(\n");
87 return;
88 }
89 int channel = libpd_get_float(&argv[0]) - LIBPD_DIGITAL_OFFSET;
90 if(argc >= 2){
91 t_atom* a = &argv[1];
92 if(libpd_is_symbol(a)){
93 char *s = libpd_get_symbol(a);
94 if(strcmp(s, "~") == 0 || strncmp(s, "sig", 3) == 0){
95 signal = true;
96 }
97 }
98 }
99 digitalRates = changeBit(digitalRates, channel, signal);
100 if(direction == 1){
101 digitalModeClear = setBit(digitalModeClear, channel);
102 digitalModeSet = clearBit(digitalModeSet, channel);
103 } else {
104 digitalModeSet = setBit(digitalModeSet, channel);
105 digitalModeClear = clearBit(digitalModeClear, channel);
106 }
107 // rt_printf("Channel: %d, input?: %d, ~?: %d\n", channel, direction==INPUT, signal);
108 // rt_printf("modeSet 0x%x modeClear 0x%x\n", digitalModeSet, digitalModeClear);
109 }
57 } 110 }
58 111
59 void Bela_floatHook(const char *source, float value){ 112 void Bela_floatHook(const char *source, float value){
60 // rt_printf("received: %s %f\n", source, value); 113 // rt_printf("received: %s %f\n", source, value);
61 // let's make this as optimized as possible for built-in digital Out parsing 114 // let's make this as optimized as possible for built-in digital Out parsing
129 return false; 182 return false;
130 } 183 }
131 // set hooks before calling libpd_init 184 // set hooks before calling libpd_init
132 libpd_set_printhook(Bela_printHook); 185 libpd_set_printhook(Bela_printHook);
133 libpd_set_floathook(Bela_floatHook); 186 libpd_set_floathook(Bela_floatHook);
187 libpd_set_messagehook(Bela_messageHook);
134 libpd_set_noteonhook(pdnoteon); 188 libpd_set_noteonhook(pdnoteon);
135 //TODO: add hooks for other midi events and generate MIDI output appropriately 189 //TODO: add hooks for other midi events and generate MIDI output appropriately
136 libpd_init(); 190 libpd_init();
137 //TODO: ideally, we would analyse the ASCII of the patch file and find the in/outs to use 191 //TODO: ideally, we would analyse the ASCII of the patch file and find the in/outs to use
138 libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate); 192 libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate);
165 libpd_bind("bela_digitalOut22"); 219 libpd_bind("bela_digitalOut22");
166 libpd_bind("bela_digitalOut23"); 220 libpd_bind("bela_digitalOut23");
167 libpd_bind("bela_digitalOut24"); 221 libpd_bind("bela_digitalOut24");
168 libpd_bind("bela_digitalOut25"); 222 libpd_bind("bela_digitalOut25");
169 libpd_bind("bela_digitalOut26"); 223 libpd_bind("bela_digitalOut26");
224 libpd_bind("bela_setDigital");
170 225
171 libpdReadFilesTask = Bela_createAuxiliaryTask(libpdReadFilesLoop, 60, "libpdReadFiles"); 226 libpdReadFilesTask = Bela_createAuxiliaryTask(libpdReadFilesLoop, 60, "libpdReadFiles");
172 Bela_scheduleAuxiliaryTask(libpdReadFilesTask); 227 Bela_scheduleAuxiliaryTask(libpdReadFilesTask);
173 228
174 229
337 digitalWriteOnce(context, digitalFrame, k, *p1 > 0.5); 392 digitalWriteOnce(context, digitalFrame, k, *p1 > 0.5);
338 } 393 }
339 } 394 }
340 395
341 // digital out at message-rate 396 // digital out at message-rate
342 if(clearDataOut || setDataOut){ 397 if(clearDataOut || setDataOut || digitalModeSet || digitalModeClear){
398 uint32_t orWord = ((setDataOut << 16) | digitalModeSet);
399 uint32_t andTildeWord = ((clearDataOut << 16) | digitalModeClear);
400 uint32_t outWord;
343 for (unsigned int frame = audioFrameBase; frame < audioFrameBase + gLibpdBlockSize; ++frame) { 401 for (unsigned int frame = audioFrameBase; frame < audioFrameBase + gLibpdBlockSize; ++frame) {
344 // TODO: only process the channels marked as such 402 outWord = context->digital[frame];
345 uint32_t outWord = context->digital[frame]; 403 outWord = outWord | orWord;
346 outWord = outWord | (setDataOut << 16); 404 outWord = outWord &~ andTildeWord;
347 outWord = outWord &~ (clearDataOut << 16);
348 context->digital[frame] = outWord; 405 context->digital[frame] = outWord;
349 } 406 }
350 } 407 }
351 408
352 //audio 409 //audio