void SerialCommunicationApp::update() { if (serialInitiallized()){ if(serial.getNumBytesAvailable() > 0){ console() << "Bytes available: " << serial.getNumBytesAvailable() << std::endl; try{ lastString = serial.readStringUntil('\n'); } catch(SerialTimeoutExc e) { console() << "timeout" << endl; } console() << lastString << endl; int16_t temp = lastString[0]; //OSC MESSAGE osc::Message message; message.addIntArg(temp); message.setAddress("coinTrigger"); message.setRemoteEndpoint(host, port); sender.sendMessage(message); TextLayout simple; simple.setFont( Font( "Arial Black", 54 ) ); simple.setColor( Color( .7, .7, .2 ) ); simple.addLine( lastString ); simple.setLeadingOffset( 0 ); mTexture = gl::Texture( simple.render( true, false ) ); bTextureComplete = true; serial.flush(); } } serial.flush(); }
void SerialCommunicationApp::setup() { lastString = ""; bTextureComplete = false; //SERIAL SETUP const vector<Serial::Device> &devices( Serial::getDevices() ); for( vector<Serial::Device>::const_iterator deviceIt = devices.begin(); deviceIt != devices.end(); ++deviceIt ) { console() << "Device: " << deviceIt->getName() << endl; } try { Serial::Device dev = Serial::findDeviceByNameContains("tty.usbmodem"); serial = Serial( dev, 9600); } catch( ... ) { console() << "There was an error initializing the serial device!" << std::endl; exit( -1 ); } contact = 0; serial.flush(); //OSC SETUP port = 12346; host = "127.0.0.1"; sender.setup( host, port, true ); }
bool waa::PC2Arduino(char *command){ int i = 0; char buffer = '\0'; bool status = false; //tstring commPortName(Comport); //Serial serial(commPortName); serial.flush(); do { i++; } while (strcmp(command, PCCommand[i]) != 0 || i >= DEF_ARDUINO_COMMAND_NUMBER); if (i<DEF_ARDUINO_COMMAND_NUMBER && i >= 0) { //serial.write(ArduinoCommand+i); serial.write(ArduinoCommand[i]); Sleep(5); serial.read(&buffer, 1); Sleep(5); if (buffer == '0') status = true; } else { printf("command error,please insert again\n"); } return status; }
void SerialCommunicationApp::update() { // console() << "Bytes available: " << serial.getNumBytesAvailable() << std::endl; double now = getElapsedSeconds(); double deltaTime = now - lastUpdate; lastUpdate = now; sinceLastRead += deltaTime; if(sinceLastRead > READ_INTERVAL) { bSendSerialMessage = true; sinceLastRead = 0.0; } if (bSendSerialMessage) { // request next chunk serial.writeByte(ctr); try{ // read until newline, to a maximum of BUFSIZE bytes lastString = serial.readStringUntil('\n', BUFSIZE ); } catch(SerialTimeoutExc e) { console() << "timeout" << endl; } bSendSerialMessage = false; ctr+=8; console() << lastString << endl; TextLayout simple; simple.setFont( Font( "Arial Black", 24 ) ); simple.setColor( Color( .7, .7, .2 ) ); simple.addLine( lastString ); simple.setLeadingOffset( 0 ); mTexture = gl::Texture( simple.render( true, false ) ); bTextureComplete = true; serial.flush(); } }
void SerialCommunicationApp::setup() { ctr = 0; lastString = ""; sinceLastRead = 0.0; lastUpdate = 0.0; bSendSerialMessage = false; bTextureComplete = false; // print the devices const vector<Serial::Device> &devices( Serial::getDevices() ); for( vector<Serial::Device>::const_iterator deviceIt = devices.begin(); deviceIt != devices.end(); ++deviceIt ) { console() << "Device: " << deviceIt->getName() << endl; } try { Serial::Device dev = Serial::findDeviceByNameContains("tty.usbserial"); serial = Serial( dev, 9600); } catch( ... ) { console() << "There was an error initializing the serial device!" << std::endl; exit( -1 ); } // wait for * as a sign for first contact char contact = 0; while(contact != '*') { contact = (char) serial.readByte(); } // request actual data serial.writeByte(ctr); // clear accumulated contact messages in buffer char b = '*'; while(serial.getNumBytesAvailable() > -1) { b = serial.readByte(); console() << b << "_"; } serial.flush(); }
void SerialTestApp::setup() { // print the devices const vector<Serial::Device> &devices( Serial::getDevices() ); for( vector<Serial::Device>::const_iterator deviceIt = devices.begin(); deviceIt != devices.end(); ++deviceIt ) { console() << "Device: " << deviceIt->getName() << endl; } try { //Serial::Device dev = Serial::findDeviceByNameContains("tty.usbmodemfa141"); Serial::Device dev = Serial::findDeviceByNameContains("tty.usbmodem1411");// hard code this for Harry's port serial = Serial( dev, 9600); serial.flush(); } catch( ... ) { console() << "There was an error initializing the serial device!" << std::endl; exit( -1 ); } }
void connectivityTask(void *params) { char * buffer = (char *)portMalloc(BUFFER_SIZE); size_t rxCount = 0; ConnParams *connParams = (ConnParams*)params; LoggerMessage msg; Serial *serial = get_serial(connParams->serial); xQueueHandle sampleQueue = connParams->sampleQueue; uint32_t connection_timeout = connParams->connection_timeout; DeviceConfig deviceConfig; deviceConfig.serial = serial; deviceConfig.buffer = buffer; deviceConfig.length = BUFFER_SIZE; const LoggerConfig *logger_config = getWorkingLoggerConfig(); bool logging_enabled = false; while (1) { bool should_stream = logging_enabled || logger_config->ConnectivityConfigs.telemetryConfig.backgroundStreaming || connParams->always_streaming; while (should_stream && connParams->init_connection(&deviceConfig) != DEVICE_INIT_SUCCESS) { pr_info("conn: not connected. retrying\r\n"); vTaskDelay(INIT_DELAY); } serial->flush(); rxCount = 0; size_t badMsgCount = 0; size_t tick = 0; size_t last_message_time = getUptimeAsInt(); bool should_reconnect = false; while (1) { if ( should_reconnect ) break; /*break out and trigger the re-connection if needed */ should_stream = logging_enabled || connParams->always_streaming || logger_config->ConnectivityConfigs.telemetryConfig.backgroundStreaming; const char res = receive_logger_message(sampleQueue, &msg, IDLE_TIMEOUT); /*/////////////////////////////////////////////////////////// // Process a pending message from logger task, if exists ////////////////////////////////////////////////////////////*/ if (pdFALSE != res) { switch(msg.type) { case LoggerMessageType_Start: { api_sendLogStart(serial); put_crlf(serial); tick = 0; logging_enabled = true; /* If we're not already streaming trigger a re-connect */ if (!should_stream) should_reconnect = true; break; } case LoggerMessageType_Stop: { api_sendLogEnd(serial); put_crlf(serial); if (! (logger_config->ConnectivityConfigs.telemetryConfig.backgroundStreaming || connParams->always_streaming)) should_reconnect = true; logging_enabled = false; break; } case LoggerMessageType_Sample: { if (!should_stream) break; const int send_meta = tick == 0 || (connParams->periodicMeta && (tick % METADATA_SAMPLE_INTERVAL == 0)); api_send_sample_record(serial, msg.sample, tick, send_meta); if (connParams->isPrimary) toggle_connectivity_indicator(); put_crlf(serial); tick++; break; } default: break; } } /*////////////////////////////////////////////////////////// // Process incoming message, if available //////////////////////////////////////////////////////////// //read in available characters, process message as necessary*/ int msgReceived = processRxBuffer(serial, buffer, &rxCount); /*check the latest contents of the buffer for something that might indicate an error condition*/ if (connParams->check_connection_status(&deviceConfig) != DEVICE_STATUS_NO_ERROR) { pr_info("conn: disconnected\r\n"); break; } /*now process a complete message if available*/ if (msgReceived) { last_message_time = getUptimeAsInt(); pr_debug(connParams->connectionName); pr_debug_str_msg(": rx: ", buffer); int msgRes = process_api(serial, buffer, BUFFER_SIZE); int msgError = (msgRes == API_ERROR_MALFORMED); if (msgError) { pr_debug("(failed)\r\n"); } if (msgError) { badMsgCount++; } else { badMsgCount = 0; } if (badMsgCount >= BAD_MESSAGE_THRESHOLD) { pr_warning_int_msg("re-connecting- empty/bad msgs :", badMsgCount ); break; } rxCount = 0; } /*disconnect if a timeout is configured and // we haven't heard from the other side for a while */ const size_t timeout = getUptimeAsInt() - last_message_time; if (connection_timeout && timeout > connection_timeout ) { pr_info_str_msg(connParams->connectionName, ": timeout"); should_reconnect = true; } } clear_connectivity_indicator(); connParams->disconnect(&deviceConfig); } }
void gravApp::update() { float now = getElapsedSeconds(); float dt = now - last; last = now; turncounter += dt; if(turncounter >= turntime) { turncounter = .0f; turn = (turn == 1 ? 2 : 1); if(turn == 1) curMoon = moon1; else curMoon = moon2; } params.turn = turn; if(collInert > .0f) collInert -= dt; if(collInert < .0f) collInert = .0f; vector<Planet*>::iterator it; for(it = planets.begin(); it < planets.end(); it++) { (*it)->update(dt); } moon1->update(dt, planets, params); moon2->update(dt, planets, params); egen->update(dt); int num1 = egen->collide(moon1->pos, MOONSIZE, true); int num2 = egen->collide(moon2->pos, MOONSIZE, true); moon1->score += num1; moon2->score += num2; if(num1 > 0 || num2 > 0) audio::Output::play( s_up ); if(moon1->pos.distance(moon2->pos) < 3*MOONSIZE) { Moon* loser = moon1->vel.length() < moon2->vel.length() ? moon1 : moon2; Moon* winner = (loser == moon1 ? moon2 : moon1); if(loser->losing.size() == 0 && loser->score >= 3 && !collInert) { audio::Output::play( s_hit ); loser->score -= 3; loser->loseScore(winner->vel, 3); collInert = 5.0f; } } if(num1 > 0) cout << "SCORE P1"; if(num2 > 0) cout << "SCORE P2"; mFreqTarget = math<float>::clamp(moon1->nearest->pos.distance(moon1->pos) + 80.0f, 100.0f, 1000.0f); mFreqTargetQ = math<float>::clamp(moon2->nearest->pos.distance(moon2->pos) + 80.0f, 100.0f, 1000.0f); if(moon1->score >= 12) { end = true; winner = moon1; } else if(moon2->score >= 12) { end = true; winner = moon2; } /// SERIAL lastUpdate = now; sinceLastRead += dt; if(sinceLastRead > .05) { bSendSerialMessage = true; sinceLastRead = 0.0; } if (bSendSerialMessage) { // request next chunk serial.writeByte(ctr); int on; try{ // read until newline, to a maximum of BUFSIZE bytes on = serial.readByte(); } catch(SerialTimeoutExc e) { console() << "timeout" << endl; } bSendSerialMessage = false; ctr+=8; console() << on << endl; serial.flush(); if(on == 1 && lastOn == 0) { if(turn == 1 && !moon1->charging) moon1->startCharge(); if(turn == 2 && !moon2->charging) moon2->startCharge(); } if(on == 0 && lastOn == 1) { if(turn == 1) moon1->jump(); if(turn == 2) moon2->jump(); } if(on == 0 && lastOn == 0) { moon1->charging = false; moon2->charging = false; moon1->charge = .0f; moon2->charge = .0f; } lastOn = on; } }
void gravApp::setup() { egen = new EnemyGenerator(1.0f, 10.0f); planets = vector<Planet*>(); planets.push_back(new Planet(Vec2f(750.0f, 600.0f), 40.0f)); planets.push_back(new Planet(Vec2f(430.0f, 270.0f), 50.0f)); planets.push_back(new Planet(Vec2f(650.0f, 400.0f), 30.0f)); planets.push_back(new Planet(Vec2f(450.0f, 320.0f), 70.0f)); planets.push_back(new Planet(Vec2f(520.0f, 560.0f), 40.0f)); // planets.push_back(new Planet(Vec2f(930.0f, 260.0f), 40.0f)); // planets.push_back(new Planet(Vec2f(1120.0f, 760.0f), 50.0f)); // planets.push_back(new Planet(Vec2f(1200.0f, 800.0f), 30.0f)); moon1 = new Moon(Vec2f(50.0f, 50.0f), Vec2f(50.0f, .0f), Vec2f(.0f, .0f), 1); moon2 = new Moon(Vec2f(250.0f, 750.0f), Vec2f(-50.0f, .0f), Vec2f(.0f, .0f), 2); moon1->color = Color(1.0f, 133.0f/255.0f, 163.0f/255.0f); moon2->color = Color(224.0f/255.0f, 1.0f, 133.0f/255.0f); last = getElapsedSeconds(); params.speed = .7f; params.repulse = 5000.0f; params.distFactor = 1.0f; // Setup the parameters ifParams = params::InterfaceGl( "Parameters", Vec2i( 200, 400 ) ); ifParams.addParam( "Speed", &(params.speed), "min=0.1 max=1.0 step=0.01 keyIncr=Z keyDecr=z" ); ifParams.addParam( "x", &(params.repulse), "min=0.1 max=1.0 step=0.01 keyIncr=X keyDecr=x" ); ifParams.addParam( "y", &(params.distFactor), "min=0.1 max=1.0 step=0.01 keyIncr=C keyDecr=c" ); turntime = 8.0f; turncounter = .0f; turn = 1; collInert = .0f; gl::enableAlphaBlending( false ); curMoon = moon1; // audio mMaxFreq = 2000.0f; mFreqTarget = 0.0f; mPhase = 0.0f; mPhaseAdjust = 0.0f; mMaxFreqQ = 2000.0f; mFreqTargetQ = 0.0f; mPhaseQ = 0.0f; mPhaseAdjustQ = 0.0f; s_hit = audio::load( loadResource( RES_HIT ) ); s_up = audio::load( loadResource( RES_UP ) ); end = false; winner = 0; /// SERIAL ctr = 0; lastString = ""; sinceLastRead = 0.0; lastUpdate = 0.0; bSendSerialMessage = false; const vector<Serial::Device> &devices( Serial::getDevices() ); for( vector<Serial::Device>::const_iterator deviceIt = devices.begin(); deviceIt != devices.end(); ++deviceIt ) { console() << "Device: " << deviceIt->getName() << endl; } try { Serial::Device dev = Serial::findDeviceByNameContains("tty.usbserial"); serial = Serial( dev, 9600); } catch( ... ) { console() << "There was an error initializing the serial device!" << std::endl; exit( -1 ); } // wait for * as a sign for first contact char contact = 0; while(contact != '*') { contact = (char) serial.readByte(); } // request actual data serial.writeByte(ctr); // clear accumulated contact messages in buffer char b = '*'; while(serial.getNumBytesAvailable() > -1) { b = serial.readByte(); console() << b << "_"; } serial.flush(); lastOn = 0; }
void sendArduinoMsg(int cmd) { if (!isConnected) return; serial.writeByte(cmd); serial.flush(false, true); }