|
|
@@ -9,7 +9,8 @@ using namespace rack; |
|
|
|
// there is no multi-instance support for receiving messages from libpd |
|
|
|
// for now, received values for the prototype gui will be stored in global variables |
|
|
|
|
|
|
|
float g_leds[NUM_ROWS][3]; |
|
|
|
float g_lights[NUM_ROWS][3] = {}; |
|
|
|
float g_switchLights[NUM_ROWS][3] = {}; |
|
|
|
|
|
|
|
std::vector<std::string> split (const std::string &s, char delim) { |
|
|
|
std::vector<std::string> result; |
|
|
@@ -25,7 +26,6 @@ std::vector<std::string> split (const std::string &s, char delim) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct LibPDEngine : ScriptEngine { |
|
|
|
|
|
|
|
~LibPDEngine() { |
|
|
@@ -33,23 +33,25 @@ struct LibPDEngine : ScriptEngine { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
static void receiveLEDs(const char *s); |
|
|
|
|
|
|
|
void sendInitialStates(const ProcessBlock* block); |
|
|
|
static void receiveLights(const char *s); |
|
|
|
bool knobChanged(const float* knobs, int idx); |
|
|
|
|
|
|
|
bool switchChanged(const float* knobs, int idx); |
|
|
|
bool switchChanged(const bool* knobs, int idx); |
|
|
|
void sendKnob(const int idx, const float value); |
|
|
|
void sendSwitch(const int idx, const bool value); |
|
|
|
|
|
|
|
t_pdinstance *_lpd; |
|
|
|
int _pd_block_size = 64; |
|
|
|
int _sampleRate = 0; |
|
|
|
int _ticks = 0; |
|
|
|
bool _init = true; |
|
|
|
|
|
|
|
float _old_knobs[NUM_ROWS]; |
|
|
|
bool _old_switches[NUM_ROWS]; |
|
|
|
float _output[BUFFERSIZE]; |
|
|
|
float _input[BUFFERSIZE];// = (float*)malloc(1024*2*sizeof(float)); |
|
|
|
const static std::map<std::string, int> _led_map; |
|
|
|
|
|
|
|
float _old_knobs[NUM_ROWS] = {}; |
|
|
|
bool _old_switches[NUM_ROWS] = {}; |
|
|
|
float _output[BUFFERSIZE] = {}; |
|
|
|
float _input[BUFFERSIZE] = {};// = (float*)malloc(1024*2*sizeof(float)); |
|
|
|
const static std::map<std::string, int> _light_map; |
|
|
|
const static std::map<std::string, int> _switchLight_map; |
|
|
|
|
|
|
|
std::string getEngineName() override { |
|
|
|
return "Pure Data"; |
|
|
@@ -65,12 +67,8 @@ struct LibPDEngine : ScriptEngine { |
|
|
|
_lpd = libpd_new_instance(); |
|
|
|
|
|
|
|
libpd_set_printhook((t_libpd_printhook)libpd_print_concatenator); |
|
|
|
libpd_set_concatenated_printhook( receiveLEDs ); |
|
|
|
//libpd_set_printhook(receiveLEDs); |
|
|
|
//libpd_bind("L2"); |
|
|
|
//libpd_bind("foo"); |
|
|
|
//libpd_set_listhook(receiveList); |
|
|
|
//libpd_set_messagehook(receiveMessage); |
|
|
|
libpd_set_concatenated_printhook( receiveLights ); |
|
|
|
|
|
|
|
|
|
|
|
if(libpd_num_instances()>2) |
|
|
|
{ |
|
|
@@ -89,6 +87,8 @@ struct LibPDEngine : ScriptEngine { |
|
|
|
std::string name = string::filename(path); |
|
|
|
std::string dir = string::directory(path); |
|
|
|
libpd_openfile(name.c_str(), dir.c_str()); |
|
|
|
|
|
|
|
sendInitialStates(block); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
@@ -110,22 +110,25 @@ struct LibPDEngine : ScriptEngine { |
|
|
|
// knobs |
|
|
|
for (int i=0; i<NUM_ROWS; i++){ |
|
|
|
if( knobChanged(block->knobs, i) ){ |
|
|
|
std::string knob = "K"+std::to_string(i+1); |
|
|
|
libpd_float(knob.c_str(), block->knobs[i]); |
|
|
|
sendKnob(i, block->knobs[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
// leds |
|
|
|
//display(std::to_string(g_leds[0])); |
|
|
|
// lights |
|
|
|
for(int i=0; i<NUM_ROWS; i++){ |
|
|
|
block->lights[i][0] = g_lights[i][0]; |
|
|
|
block->lights[i][1] = g_lights[i][1]; |
|
|
|
block->lights[i][2] = g_lights[i][2]; |
|
|
|
} |
|
|
|
// switch lights |
|
|
|
for(int i=0; i<NUM_ROWS; i++){ |
|
|
|
block->lights[i][0] = g_leds[i][0]; |
|
|
|
block->lights[i][1] = g_leds[i][1]; |
|
|
|
block->lights[i][2] = g_leds[i][2]; |
|
|
|
block->switchLights[i][0] = g_switchLights[i][0]; |
|
|
|
block->switchLights[i][1] = g_switchLights[i][1]; |
|
|
|
block->switchLights[i][2] = g_switchLights[i][2]; |
|
|
|
} |
|
|
|
// switches |
|
|
|
for(int i=0; i<NUM_ROWS; i++){ |
|
|
|
if( switchChanged(block->switches, i) ){ |
|
|
|
std::string sw = "S"+std::to_string(i+1); |
|
|
|
libpd_float(sw.c_str(), block->switches[i]); |
|
|
|
sendSwitch(i, block->switches[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
// process samples in libpd |
|
|
@@ -150,29 +153,48 @@ static void constructor() { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void LibPDEngine::receiveLEDs(const char *s) { |
|
|
|
void LibPDEngine::receiveLights(const char *s) { |
|
|
|
std::string str = std::string(s); |
|
|
|
std::vector<std::string> atoms = split (str, ' '); |
|
|
|
|
|
|
|
if(atoms[0]=="toVCV:"){ |
|
|
|
// led list |
|
|
|
bool led_is_valid = true; |
|
|
|
int led_idx = -1; |
|
|
|
// light list |
|
|
|
bool light_is_valid = true; |
|
|
|
int light_idx = -1; |
|
|
|
try { |
|
|
|
led_idx = _led_map.at(atoms[1]); // map::at throws an out-of-range |
|
|
|
light_idx = _light_map.at(atoms[1]); // map::at throws an out-of-range |
|
|
|
} |
|
|
|
catch (const std::out_of_range& oor) { |
|
|
|
led_is_valid = false; |
|
|
|
light_is_valid = false; |
|
|
|
//display("Warning:"+atoms[1]+" not found!"); |
|
|
|
} |
|
|
|
//std::cout << v[1] << ", " << g_led_map[v[1]] << std::endl; |
|
|
|
if(led_is_valid && atoms.size()==5){ |
|
|
|
g_leds[led_idx][0] = stof(atoms[2]); // red |
|
|
|
g_leds[led_idx][1] = stof(atoms[3]); // green |
|
|
|
g_leds[led_idx][2] = stof(atoms[4]); // blue |
|
|
|
if(light_is_valid && atoms.size()==5){ |
|
|
|
g_lights[light_idx][0] = stof(atoms[2]); // red |
|
|
|
g_lights[light_idx][1] = stof(atoms[3]); // green |
|
|
|
g_lights[light_idx][2] = stof(atoms[4]); // blue |
|
|
|
} |
|
|
|
else { |
|
|
|
|
|
|
|
// error |
|
|
|
} |
|
|
|
|
|
|
|
bool switchLight_is_valid = true; |
|
|
|
int switchLight_idx = -1; |
|
|
|
try { |
|
|
|
switchLight_idx = _switchLight_map.at(atoms[1]); // map::at throws an out-of-range |
|
|
|
} |
|
|
|
catch (const std::out_of_range& oor) { |
|
|
|
switchLight_is_valid = false; |
|
|
|
//display("Warning:"+atoms[1]+" not found!"); |
|
|
|
} |
|
|
|
//std::cout << v[1] << ", " << g_led_map[v[1]] << std::endl; |
|
|
|
if(switchLight_is_valid && atoms.size()==5){ |
|
|
|
g_switchLights[switchLight_idx][0] = stof(atoms[2]); // red |
|
|
|
g_switchLights[switchLight_idx][1] = stof(atoms[3]); // green |
|
|
|
g_switchLights[switchLight_idx][2] = stof(atoms[4]); // blue |
|
|
|
} |
|
|
|
else { |
|
|
|
// error |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
@@ -189,7 +211,7 @@ bool LibPDEngine::knobChanged(const float* knobs, int i){ |
|
|
|
return knob_changed; |
|
|
|
} |
|
|
|
|
|
|
|
bool LibPDEngine::switchChanged(const float* switches, int i){ |
|
|
|
bool LibPDEngine::switchChanged(const bool* switches, int i){ |
|
|
|
bool switch_changed = false; |
|
|
|
if (_old_switches[i] != switches[i]){ |
|
|
|
switch_changed = true; |
|
|
@@ -198,11 +220,38 @@ bool LibPDEngine::switchChanged(const float* switches, int i){ |
|
|
|
return switch_changed; |
|
|
|
} |
|
|
|
|
|
|
|
const std::map<std::string, int> LibPDEngine::_led_map{ |
|
|
|
const std::map<std::string, int> LibPDEngine::_light_map{ |
|
|
|
{ "L1", 0 }, |
|
|
|
{ "L2", 1 }, |
|
|
|
{ "L3", 2 }, |
|
|
|
{ "L4", 3 }, |
|
|
|
{ "L5", 4 }, |
|
|
|
{ "L6", 5 } |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
const std::map<std::string, int> LibPDEngine::_switchLight_map{ |
|
|
|
{ "S1", 0 }, |
|
|
|
{ "S2", 1 }, |
|
|
|
{ "S3", 2 }, |
|
|
|
{ "S4", 3 }, |
|
|
|
{ "S5", 4 }, |
|
|
|
{ "S6", 5 } |
|
|
|
}; |
|
|
|
|
|
|
|
void LibPDEngine::sendKnob(const int idx, const float value){ |
|
|
|
std::string knob = "K"+std::to_string(idx+1); |
|
|
|
libpd_float(knob.c_str(), value); |
|
|
|
} |
|
|
|
|
|
|
|
void LibPDEngine::sendSwitch(const int idx, const bool value){ |
|
|
|
std::string sw = "S"+std::to_string(idx+1); |
|
|
|
libpd_float(sw.c_str(), value); |
|
|
|
} |
|
|
|
|
|
|
|
void LibPDEngine::sendInitialStates(const ProcessBlock* block){ |
|
|
|
// knobs |
|
|
|
for (int i=0; i<NUM_ROWS; i++){ |
|
|
|
sendKnob(i, block->knobs[i]); |
|
|
|
sendSwitch(i, block->knobs[i]); |
|
|
|
} |
|
|
|
} |