Skip to content

Commit

Permalink
improved example having serial connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
davetcc committed Oct 19, 2024
1 parent 4602069 commit ee0f148
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 13 deletions.
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 examples/arduino32/stm32DuinoOneButton/generated/SerialTransport.h
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_ */
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use elsewhere.
*/

// Generated for STM32Duino by TcMenu 4.3.1 on 2024-09-22T11:33:46.688735900Z.
// Generated for STM32Duino by TcMenu 4.4.0-SNAPSHOT on 2024-10-19T08:59:45.395538500Z.

#include <tcMenu.h>
#include "stm32DuinoOneButton_menu.h"
Expand All @@ -17,12 +17,15 @@

// Global variable declarations
const ConnectorLocalInfo applicationInfo = { "One Button", "4fe6e85d-2bbd-4d19-84e5-5d6746883028" };
TcMenuRemoteServer remoteServer(applicationInfo);
HalStm32EepromAbstraction glBspRom;
EepromAuthenticatorManager authManager(4);
U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI gfx(U8G2_R0, PF13, PD15, PF12);
U8g2Drawable gfxDrawable(&gfx);
GraphicsDeviceRenderer renderer(30, applicationInfo.name, &gfxDrawable);
TcOneButtonHandler oneButtonHandler(USER_BTN, 250);
NoInitialisationNeeded serialInitializer;
SerialTagValueTransport serialTransport(&Serial);
TagValueRemoteServerConnection serialConnection(serialTransport, serialInitializer);

// Global Menu Item declarations
const char enumStrSettingsEnumProp_0[] = "Item1";
Expand All @@ -48,14 +51,13 @@ void setupMenu() {
setSizeBasedEEPROMStorageEnabled(false);
glBspRom.initialise(0);
menuMgr.setEepromRef(&glBspRom);
authManager.initialise(menuMgr.getEepromAbstraction(), 150);
menuMgr.setAuthenticator(&authManager);
// Code generated by plugins and new operators.
gfx.begin();
renderer.setUpdatesPerSecond(5);
switches.init(internalDigitalIo(), SWITCHES_POLL_EVERYTHING, false);
menuMgr.initWithoutInput(&renderer, &menuPressMe);
oneButtonHandler.start();
remoteServer.addConnection(&serialConnection);
installMonoInverseTitleTheme(renderer, MenuFontDef(&OpenSansRegular8pt, 0), MenuFontDef(&OpenSansRegular8pt, 0), true, BaseGraphicalRenderer::NO_TITLE, true);
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
#include <tcUnicodeHelper.h>
#include "tcMenuU8g2.h"
#include <extras/TcOneButtonHandler.h>
#include <RemoteConnector.h>
#include "SerialTransport.h"
#include <IoAbstraction.h>
#include <EepromItemStorage.h>
#include <mbed/HalStm32EepromAbstraction.h>
#include <RemoteAuthentication.h>

// variables we declare that you may need to access
extern const PROGMEM ConnectorLocalInfo applicationInfo;
extern TcMenuRemoteServer remoteServer;
extern U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI gfx;
extern U8g2Drawable gfxDrawable;
extern GraphicsDeviceRenderer renderer;
Expand Down
12 changes: 8 additions & 4 deletions examples/arduino32/stm32DuinoOneButton/stm32DuinoOneButton.emf
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"lastDisplayUuid": "fd998437-c4b2-4386-ba88-d0ae7c20620b",
"lastInputUuid": "4F92CBC0-D13A-4090-9B86-C1952FE42D9B",
"lastRemoteUuids": [
"2c101fec-1f7d-4ff3-8d2b-992ad41e7fcb"
"1e38dc42-672d-4c1c-a393-2c7632bf6c5c"
],
"lastThemeUuid": "396ED4DF-AD7B-4951-A848-A9E5838A549B",
"applicationUUID": "4fe6e85d-2bbd-4d19-84e5-5d6746883028",
Expand Down Expand Up @@ -201,6 +201,11 @@
"latestValue": "250",
"subsystem": "INPUT"
},
{
"name": "SERIAL_PORT",
"latestValue": "Serial",
"subsystem": "REMOTE"
},
{
"name": "ITEM_FONT",
"latestValue": "ada:OpenSansRegular8pt,0",
Expand Down Expand Up @@ -237,16 +242,15 @@
"saveLocation": "PROJECT_TO_CURRENT_WITH_GENERATED",
"usingSizedEEPROMStorage": false,
"eepromDefinition": "bsp:0",
"authenticatorDefinition": "rom:150:4",
"authenticatorDefinition": "",
"projectIoExpanders": [
"deviceIO:"
],
"menuInMenuCollection": {
"menuDefinitions": []
},
"packageNamespace": "",
"appIsModular": false,
"listOfEmbeddedForms": []
"appIsModular": false
},
"stringLists": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
#include <SPI.h>

void setup() {
// This example logs using IoLogging, see the following guide to enable
// https://tcmenu.github.io/documentation/arduino-libraries/io-abstraction/arduino-logging-with-io-logging/
IOLOG_START_SERIAL
serEnableLevel(SER_NETWORK_DEBUG, true);
// Start the serial port so that we can use the remote connectivity
Serial.begin(115200);

// Start up serial and prepare the correct SPI, your pins may differ
SPI.setMISO(PB4);
Expand Down

0 comments on commit ee0f148

Please sign in to comment.