Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writeOrFlush on Serial2 data handling in loop #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
234 changes: 117 additions & 117 deletions konekt_user_module_serial_gateway/konekt_user_module_serial_gateway.ino
Original file line number Diff line number Diff line change
@@ -1,118 +1,118 @@
/* Hologram Dash User Module Cloud Serial Gateway Program
*
* Author: Hologram <[email protected]>
*
* Purpose: This firmware sends all data that comes in on a serial port to the
* Hologram Cloud. Data is sent once a newline is detected.
*
* License: Licensed under the MIT License.
*
* The MIT License (MIT)
*
* Copyright 2017 Hologram (Konekt, Inc.)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#define LED_DATA_INDICATOR_DURATION 200

/* globals */
unsigned ledStopMillis;
bool isHandlingData;

void setup() {
Serial2.begin(9600);
SerialUSB.begin(9600);
ledStopMillis = 0; /* LED off by default */
isHandlingData = false;
printToBoth("+++");
}

void printToBoth(String msg) {
// Write to USB and UART
Serial2.println(msg);
SerialUSB.println(msg);
}

void writeOrFlush(char cur) {
// Write each character to HologramCloud buffer and then
// send to cloud on a newline character
isHandlingData = true;
if(cur == '\n') {
printToBoth("Sending message to cloud...");
int retriesLeft = 2;
while(retriesLeft > 0) {
bool res = HologramCloud.sendMessage();
if(!res) {
String msg = "Failed. Trying ";
msg += retriesLeft + " more times";
printToBoth(msg);
} else {
printToBoth("Data sent to cloud successfully");
break;
}
retriesLeft--;
}
} else {
HologramCloud.write(cur);
setLedState();
}
}


void loop() {
char currChar;

isHandlingData = false;

while(Serial2.available()) {
writeOrFlush(Serial2.available());
}

while(SerialUSB.available()) {
writeOrFlush(SerialUSB.read());
}

while(SerialCloud.available()) {
currChar = (char)SerialCloud.read();
SerialUSB.write(currChar);
Serial2.write(currChar);
isHandlingData = true;
}

setLedState();

delay(1);
}

void setLedState() {
/* Blink LED while we're doing data operations and 200ms afterward */

if(ledStopMillis > 0 && ledStopMillis < millis()) {
Dash.offLED();
ledStopMillis = 0;
}

if(isHandlingData && ledStopMillis == 0) {
Dash.pulseLED(25, 50);
ledStopMillis = millis() + LED_DATA_INDICATOR_DURATION;
}
}
/* Hologram Dash User Module Cloud Serial Gateway Program
*
* Author: Hologram <[email protected]>
*
* Purpose: This firmware sends all data that comes in on a serial port to the
* Hologram Cloud. Data is sent once a newline is detected.
*
* License: Licensed under the MIT License.
*
* The MIT License (MIT)
*
* Copyright 2017 Hologram (Konekt, Inc.)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#define LED_DATA_INDICATOR_DURATION 200
/* globals */
unsigned ledStopMillis;
bool isHandlingData;
void setup() {
Serial2.begin(9600);
SerialUSB.begin(9600);
ledStopMillis = 0; /* LED off by default */
isHandlingData = false;
printToBoth("+++");
}
void printToBoth(String msg) {
// Write to USB and UART
Serial2.println(msg);
SerialUSB.println(msg);
}
void writeOrFlush(char cur) {
// Write each character to HologramCloud buffer and then
// send to cloud on a newline character
isHandlingData = true;
if(cur == '\n') {
printToBoth("Sending message to cloud...");
int retriesLeft = 2;
while(retriesLeft > 0) {
bool res = HologramCloud.sendMessage();
if(!res) {
String msg = "Failed. Trying ";
msg += retriesLeft + " more times";
printToBoth(msg);
} else {
printToBoth("Data sent to cloud successfully");
break;
}
retriesLeft--;
}
} else {
HologramCloud.write(cur);
setLedState();
}
}
void loop() {
char currChar;
isHandlingData = false;
while(Serial2.available()) {
writeOrFlush(Serial2.read());
}
while(SerialUSB.available()) {
writeOrFlush(SerialUSB.read());
}
while(SerialCloud.available()) {
currChar = (char)SerialCloud.read();
SerialUSB.write(currChar);
Serial2.write(currChar);
isHandlingData = true;
}
setLedState();
delay(1);
}
void setLedState() {
/* Blink LED while we're doing data operations and 200ms afterward */
if(ledStopMillis > 0 && ledStopMillis < millis()) {
Dash.offLED();
ledStopMillis = 0;
}
if(isHandlingData && ledStopMillis == 0) {
Dash.pulseLED(25, 50);
ledStopMillis = millis() + LED_DATA_INDICATOR_DURATION;
}
}