Skip to content

Instantly share code, notes, and snippets.

@adielfernandez
Created March 3, 2022 23:36
Show Gist options
  • Save adielfernandez/9f1e118348932c5d87e157afd6e3d1e7 to your computer and use it in GitHub Desktop.
Save adielfernandez/9f1e118348932c5d87e157afd6e3d1e7 to your computer and use it in GitHub Desktop.
#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
#include "cinder/Log.h"
#include "cinder/Serial.h"
#include "Cinder-Serial.h"
using namespace ci;
using namespace ci::app;
using namespace std;
using namespace Cinder::Serial;
class CinderProjectApp : public App {
public:
void setup() override;
void mouseDown(MouseEvent event) override;
void update() override;
void draw() override;
SerialDeviceRef mDevice;
SerialRef mSerial;
bool useCinderSerial = true;
};
void CinderProjectApp::setup() {
if (useCinderSerial) {
// print the devices
for (const auto& dev : Serial::getDevices())
console() << "Device: " << dev.getName() << endl;
try {
Serial::Device dev = Serial::findDeviceByNameContains("COM3");
mSerial = Serial::create(dev, 9600);
} catch (SerialExc& exc) {
CI_LOG_EXCEPTION("coult not initialize the serial device", exc);
exit(-1);
}
} else {
auto ports = SerialPort::getPorts();
for (auto port : ports) {
console() << "SERIAL DEVICE" << endl;
console() << "\tNAME: " << port->getName() << endl;
console() << "\tDESCRIPTION: " << port->getDescription() << endl;
console() << "\tHARDWARE IDENTIFIER: " << port->getHardwareIdentifier() << endl;
}
// grab a port and create a device
if (!ports.empty()) {
SerialPortRef port = SerialPort::findPortByNameMatching(std::regex("\\/dev\\/cu\\.usbmodem.*"));
if (!port) {
port = ports.back();
}
try {
mDevice = SerialDevice::create(port, 115200);
} catch (serial::IOException& e) {
CI_LOG_EXCEPTION("failed to create serial device", e);
quit();
}
//NB: device is opened on creation
}
}
}
void CinderProjectApp::mouseDown(MouseEvent event) {
}
void CinderProjectApp::update() {
}
void CinderProjectApp::draw() {
gl::clear(Color(0, 0, 0));
}
CINDER_APP(CinderProjectApp, RendererGl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment