giuliomoro
#include <I2c.h>
#include <stdexcept>
class TCA9548A : I2c
{
public:
TCA9548A(int bus, int address)
{
int ret = initI2C_RW(bus, address, -1);
if(ret || select(-1)) // disable all channels and at the same time verify address is valid
throw std::runtime_error("Unable to open TCA9548A. Ensure the multiplexer is connected and the bus and address are correct.");
}
int readI2C() override {return 0;};
int select(int channel)
{
i2c_char_t byte = channel < 0 || channel >= 8 ? 0 : 1 << channel;
return (sizeof(byte) != write(i2C_file, &byte, sizeof(byte)));
}
};
TCA9548A tca(1, 0x70);
#include <signal.h>
#include <libraries/OscReceiver/OscReceiver.h>
#include <unistd.h>
#include "u8g2/cppsrc/U8g2lib.h"
const unsigned int SSD1306_addr = 0x3c;
class U8G2_SSD1306_128X64_NONAME_F_HW_I2C_LINUX : public U8G2 {
public: U8G2_SSD1306_128X64_NONAME_F_HW_I2C_LINUX(const u8g2_cb_t *rotation) : U8G2() {
u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, rotation, u8x8_byte_linux_i2c, u8x8_linux_i2c_delay);
}
};
U8G2_SSD1306_128X64_NONAME_F_HW_I2C_LINUX u8g2(U8G2_R0);
OscReceiver oscReceiver;
int localPort = 7562; //port for incoming OSC messages
int gStop = 0;
// Handle Ctrl-C by requesting that the audio rendering stop
void interrupt_handler(int var)
{
gStop = true;
}
int parseMessage(oscpkt::Message msg, void* arg)
{
u8g2.clearBuffer();
int displayWidth = u8g2.getDisplayWidth();
int displayHeight = u8g2.getDisplayHeight();
std::string trString;
std::string trString1;
std::string trString2;
std::string trString3;
std::string trString4;
float param1Value;
float param2Value;
float param3Value;
float param4Value;
std::string text1;
std::string text2;
std::string text3;
if (msg.match("/osc-test").isOkNoMoreArgs())
{
printf("received /osc-test\n");
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.setFontRefHeightText();
u8g2.drawStr(0, displayHeight * 0.5, "OSC TEST SUCCESS!");
u8g2.sendBuffer();
} else if (msg.match("/number").popStr(trString1).popStr(trString2).popStr(trString3).popStr(trString4).isOkNoMoreArgs())
{
const char *ctrStr1 = trString1.c_str();
const char *ctrStr2 = trString2.c_str();
const char *ctrStr3 = trString3.c_str();
const char *ctrStr4 = trString4.c_str();
printf("received /number %s %s %s %s\n", ctrStr1,ctrStr2,ctrStr3,ctrStr4);
u8g2.drawUTF8(0, 0, ctrStr1);
u8g2.drawUTF8(2, 0, ctrStr2);
u8g2.drawUTF8(4, 0, ctrStr3);
u8g2.drawUTF8(6, 23, ctrStr4);
u8g2.sendBuffer();
} else if (msg.match("/ratio").popStr(trString).isOkNoMoreArgs())
{
const char *ctrStr = trString.c_str();
printf("received /ratio %s\n", ctrStr);
u8g2.drawUTF8(0, 0, ctrStr);
u8g2.sendBuffer();
} else if (msg.match("/display-text").popStr(text1).popStr(text2).popStr(text3).isOkNoMoreArgs())
{
const char *ctrStr1 = text1.c_str();
const char *ctrStr2 = text2.c_str();
const char *ctrStr3 = text3.c_str();
printf("received /display-text string %s %s %s\n", ctrStr1, ctrStr2, ctrStr3);
u8g2.setFont(u8g2_font_4x6_tf);
u8g2.setFontRefHeightText();
u8g2.drawUTF8(displayWidth * 0.5, displayHeight * 0.25, ctrStr1);
u8g2.drawUTF8(displayWidth * 0.5, displayHeight * 0.5, ctrStr2);
u8g2.drawUTF8(displayWidth * 0.5, displayHeight * 0.75, ctrStr3);
u8g2.sendBuffer();
} else if (msg.match("/parameters").popFloat(param1Value).popFloat(param2Value).popFloat(param3Value).popFloat(param4Value).isOkNoMoreArgs())
{
printf("received /parameters float %f float %f float %f float %f\n", param1Value, param2Value, param3Value, param4Value);
u8g2.setFont(u8g2_font_4x6_tf);
u8g2.setFontRefHeightText();
u8g2.drawStr(0, 0, "PARAMETER 1:");
u8g2.drawBox(0, 10, u8g2.getDisplayWidth() * param1Value, 10);
u8g2.drawStr(0, 22, "PARAMETER 2:");
u8g2.drawBox(0, 32, u8g2.getDisplayWidth() * param2Value, 10);
u8g2.drawStr(0, 44, "PARAMETER 3:");
u8g2.drawBox(0, 54, u8g2.getDisplayWidth() * param3Value, 10);
u8g2.drawStr(0, 66, "PARAMETER 4:");
u8g2.drawBox(0, 76, u8g2.getDisplayWidth() * param4Value, 10);
u8g2.sendBuffer();
} else if (msg.match("/lfos").popFloat(param1Value).popFloat(param2Value).popFloat(param3Value).isOkNoMoreArgs())
{
printf("received /lfos float %f float %f float %f\n", param1Value, param2Value, param3Value);
u8g2.drawEllipse(displayWidth * 0.2, displayHeight * 0.5, 10, displayHeight * 0.5 * param1Value);
u8g2.drawEllipse(displayWidth * 0.5, displayHeight * 0.5, 10, displayHeight * 0.5 * param2Value);
u8g2.drawEllipse(displayWidth * 0.8, displayHeight * 0.5, 10, displayHeight * 0.5 * param3Value);
u8g2.drawHLine(0, displayHeight * 0.5, displayWidth);
u8g2.sendBuffer();
} else if (msg.match("/waveform"))
{
oscpkt::Message::ArgReader args(msg);
if(args)
{
const unsigned int nValues = args.nbArgRemaining();
float values[nValues];
for(unsigned int n = 0; n < nValues; ++n)
{
if(args.isFloat())
{
args.popFloat(values[n]);
} else if(args.isInt32()) {
int i;
args.popInt32(i);
values[n] = i;
} else {
fprintf(stderr, "Wrong type at argument %d\n", n);
return -1;
}
}
// now tenValues contains the 10 values.
// prepare a bitmap:
printf("received /waveform floats:");
for(unsigned int n = 0; n < 10; ++n)
printf("%f ", values[n]);
printf("\n");
for(unsigned int x = 0; x < displayWidth; ++x)
{
// we interpret each value as the vertical displacement and
// we want to draw a series of horizontal lines at the specified points
unsigned int valIdx = x * float(nValues) / displayWidth;
unsigned int y = values[valIdx] * displayHeight;
u8g2.drawPixel(x, y);
}
}
} else
{
printf("unhandled message to: %s \n", msg.addressPattern().c_str());
}
u8g2.drawStr(0, 56," ");
u8g2.sendBuffer();
return 0;
}
int main(int main_argc, char *main_argv[])
{
// Set up interrupt handler to catch Control-C and SIGTERM
signal(SIGINT, interrupt_handler);
signal(SIGTERM, interrupt_handler);
u8g2.setI2CAddress(SSD1306_addr);
u8g2.initDisplay();
u8g2.setPowerSave(0);
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_4x6_tf);
u8g2.setFontRefHeightText();
u8g2.setFontPosTop();
u8g2.drawStr(0, 0, " ");
u8g2.drawStr(0, 7, " ");
u8g2.drawStr(0, 14," ");
u8g2.drawStr(0, 21," ");
u8g2.drawStr(0, 28," ");
u8g2.drawStr(0, 35," ");
u8g2.drawStr(0, 42," ");
u8g2.drawStr(0, 56,"");
u8g2.sendBuffer();
// OSC
oscReceiver.setup(localPort, parseMessage);
while(!gStop)
{
usleep(100000);
}
return false;
}