-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved example having serial connectivity
- Loading branch information
Showing
6 changed files
with
135 additions
and
13 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
examples/arduino32/stm32DuinoOneButton/generated/SerialTransport.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry). | ||
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory. | ||
*/ | ||
|
||
/** | ||
* Serial remote capability plugin. This file is a plugin file and should not be directly edited, | ||
* it will be replaced each time the project is built. If you want to edit this file in place, | ||
* make sure to rename it first. | ||
*/ | ||
|
||
// we'll wait 100 times this amount in a loop when serial is not available | ||
#ifndef MICROS_TO_WAIT_FOR_SERIAL | ||
#define MICROS_TO_WAIT_FOR_SERIAL 10000 | ||
#endif //MICROS_TO_WAIT_FOR_SERIAL | ||
|
||
#include "SerialTransport.h" | ||
#include <tcMenu.h> | ||
|
||
SerialTagValueTransport::SerialTagValueTransport(Stream* thePort) : TagValueTransport(TVAL_UNBUFFERED) { | ||
this->serialPort = thePort; | ||
} | ||
|
||
void SerialTagValueTransport::close() { | ||
currentField.msgType = UNKNOWN_MSG_TYPE; | ||
currentField.fieldType = FVAL_PROCESSING_AWAITINGMSG; | ||
} | ||
|
||
// DO NOT replace this with the standard char* write method on serial. | ||
// It cannot handle large volumes of data going through at once and often | ||
// overflows the buffer causing data errors. | ||
int SerialTagValueTransport::writeChar(char ch) { | ||
if(available()) { | ||
serialPort->write(ch); | ||
} | ||
else { | ||
int tries = 100; | ||
while(tries && !available()) { | ||
--tries; | ||
serialPort->flush(); | ||
taskManager.yieldForMicros(MICROS_TO_WAIT_FOR_SERIAL); | ||
} | ||
|
||
// if it's not available now, it probably will timeout anyway. | ||
if(!available()) { | ||
return 0; | ||
} | ||
|
||
serialPort->write(ch); | ||
} | ||
return 1; | ||
} | ||
|
||
int SerialTagValueTransport::writeStr(const char* str) { | ||
int i=0; | ||
bool lastWriteOk = true; | ||
while(str[i] && lastWriteOk) { | ||
lastWriteOk = writeChar(str[i]); | ||
i++; | ||
} | ||
return i; | ||
} | ||
|
53 changes: 53 additions & 0 deletions
53
examples/arduino32/stm32DuinoOneButton/generated/SerialTransport.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry). | ||
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory. | ||
*/ | ||
|
||
/** | ||
* @file SerialTransport.h | ||
* | ||
* Serial remote capability plugin. This file is a plugin file and should not be directly edited, | ||
* it will be replaced each time the project is built. If you want to edit this file in place, | ||
* make sure to rename it first. | ||
*/ | ||
|
||
#ifndef TCMENU_SERIALTRANSPORT_H_ | ||
#define TCMENU_SERIALTRANSPORT_H_ | ||
|
||
#include <Arduino.h> | ||
#include <RemoteConnector.h> | ||
#include <MessageProcessors.h> | ||
#include <tcUtil.h> | ||
#include <RemoteAuthentication.h> | ||
#include <remote/BaseRemoteComponents.h> | ||
|
||
namespace tcremote { | ||
|
||
/** | ||
* Serial transport is an implementation of TagValueTransport that works over a serial port | ||
*/ | ||
class SerialTagValueTransport : public TagValueTransport { | ||
private: | ||
Stream* serialPort; | ||
public: | ||
explicit SerialTagValueTransport(Stream* thePort); | ||
~SerialTagValueTransport() override = default; | ||
|
||
void flush() override {serialPort->flush();} | ||
int writeChar(char data) override; | ||
int writeStr(const char* data) override; | ||
|
||
uint8_t readByte() override { return serialPort->read(); } | ||
bool readAvailable() override { return serialPort->available(); } | ||
bool available() override { return serialPort->availableForWrite() != 0;} | ||
bool connected() override { return true;} | ||
|
||
void close() override; | ||
}; | ||
} | ||
|
||
#ifndef TC_MANUAL_NAMESPACING | ||
using namespace tcremote; | ||
#endif // TC_MANUAL_NAMESPACING | ||
|
||
#endif /* TCMENU_SERIALTRANSPORT_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters