Skip to content

Commit ee0f148

Browse files
committed
improved example having serial connectivity
1 parent 4602069 commit ee0f148

File tree

6 files changed

+135
-13
lines changed

6 files changed

+135
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
3+
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4+
*/
5+
6+
/**
7+
* Serial remote capability plugin. This file is a plugin file and should not be directly edited,
8+
* it will be replaced each time the project is built. If you want to edit this file in place,
9+
* make sure to rename it first.
10+
*/
11+
12+
// we'll wait 100 times this amount in a loop when serial is not available
13+
#ifndef MICROS_TO_WAIT_FOR_SERIAL
14+
#define MICROS_TO_WAIT_FOR_SERIAL 10000
15+
#endif //MICROS_TO_WAIT_FOR_SERIAL
16+
17+
#include "SerialTransport.h"
18+
#include <tcMenu.h>
19+
20+
SerialTagValueTransport::SerialTagValueTransport(Stream* thePort) : TagValueTransport(TVAL_UNBUFFERED) {
21+
this->serialPort = thePort;
22+
}
23+
24+
void SerialTagValueTransport::close() {
25+
currentField.msgType = UNKNOWN_MSG_TYPE;
26+
currentField.fieldType = FVAL_PROCESSING_AWAITINGMSG;
27+
}
28+
29+
// DO NOT replace this with the standard char* write method on serial.
30+
// It cannot handle large volumes of data going through at once and often
31+
// overflows the buffer causing data errors.
32+
int SerialTagValueTransport::writeChar(char ch) {
33+
if(available()) {
34+
serialPort->write(ch);
35+
}
36+
else {
37+
int tries = 100;
38+
while(tries && !available()) {
39+
--tries;
40+
serialPort->flush();
41+
taskManager.yieldForMicros(MICROS_TO_WAIT_FOR_SERIAL);
42+
}
43+
44+
// if it's not available now, it probably will timeout anyway.
45+
if(!available()) {
46+
return 0;
47+
}
48+
49+
serialPort->write(ch);
50+
}
51+
return 1;
52+
}
53+
54+
int SerialTagValueTransport::writeStr(const char* str) {
55+
int i=0;
56+
bool lastWriteOk = true;
57+
while(str[i] && lastWriteOk) {
58+
lastWriteOk = writeChar(str[i]);
59+
i++;
60+
}
61+
return i;
62+
}
63+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
3+
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4+
*/
5+
6+
/**
7+
* @file SerialTransport.h
8+
*
9+
* Serial remote capability plugin. This file is a plugin file and should not be directly edited,
10+
* it will be replaced each time the project is built. If you want to edit this file in place,
11+
* make sure to rename it first.
12+
*/
13+
14+
#ifndef TCMENU_SERIALTRANSPORT_H_
15+
#define TCMENU_SERIALTRANSPORT_H_
16+
17+
#include <Arduino.h>
18+
#include <RemoteConnector.h>
19+
#include <MessageProcessors.h>
20+
#include <tcUtil.h>
21+
#include <RemoteAuthentication.h>
22+
#include <remote/BaseRemoteComponents.h>
23+
24+
namespace tcremote {
25+
26+
/**
27+
* Serial transport is an implementation of TagValueTransport that works over a serial port
28+
*/
29+
class SerialTagValueTransport : public TagValueTransport {
30+
private:
31+
Stream* serialPort;
32+
public:
33+
explicit SerialTagValueTransport(Stream* thePort);
34+
~SerialTagValueTransport() override = default;
35+
36+
void flush() override {serialPort->flush();}
37+
int writeChar(char data) override;
38+
int writeStr(const char* data) override;
39+
40+
uint8_t readByte() override { return serialPort->read(); }
41+
bool readAvailable() override { return serialPort->available(); }
42+
bool available() override { return serialPort->availableForWrite() != 0;}
43+
bool connected() override { return true;}
44+
45+
void close() override;
46+
};
47+
}
48+
49+
#ifndef TC_MANUAL_NAMESPACING
50+
using namespace tcremote;
51+
#endif // TC_MANUAL_NAMESPACING
52+
53+
#endif /* TCMENU_SERIALTRANSPORT_H_ */

examples/arduino32/stm32DuinoOneButton/generated/stm32DuinoOneButton_menu.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use elsewhere.
99
*/
1010

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

1313
#include <tcMenu.h>
1414
#include "stm32DuinoOneButton_menu.h"
@@ -17,12 +17,15 @@
1717

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

2730
// Global Menu Item declarations
2831
const char enumStrSettingsEnumProp_0[] = "Item1";
@@ -48,14 +51,13 @@ void setupMenu() {
4851
setSizeBasedEEPROMStorageEnabled(false);
4952
glBspRom.initialise(0);
5053
menuMgr.setEepromRef(&glBspRom);
51-
authManager.initialise(menuMgr.getEepromAbstraction(), 150);
52-
menuMgr.setAuthenticator(&authManager);
5354
// Code generated by plugins and new operators.
5455
gfx.begin();
5556
renderer.setUpdatesPerSecond(5);
5657
switches.init(internalDigitalIo(), SWITCHES_POLL_EVERYTHING, false);
5758
menuMgr.initWithoutInput(&renderer, &menuPressMe);
5859
oneButtonHandler.start();
60+
remoteServer.addConnection(&serialConnection);
5961
installMonoInverseTitleTheme(renderer, MenuFontDef(&OpenSansRegular8pt, 0), MenuFontDef(&OpenSansRegular8pt, 0), true, BaseGraphicalRenderer::NO_TITLE, true);
6062
}
6163

examples/arduino32/stm32DuinoOneButton/generated/stm32DuinoOneButton_menu.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
#include <tcUnicodeHelper.h>
1717
#include "tcMenuU8g2.h"
1818
#include <extras/TcOneButtonHandler.h>
19+
#include <RemoteConnector.h>
20+
#include "SerialTransport.h"
1921
#include <IoAbstraction.h>
2022
#include <EepromItemStorage.h>
2123
#include <mbed/HalStm32EepromAbstraction.h>
22-
#include <RemoteAuthentication.h>
2324

2425
// variables we declare that you may need to access
2526
extern const PROGMEM ConnectorLocalInfo applicationInfo;
27+
extern TcMenuRemoteServer remoteServer;
2628
extern U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI gfx;
2729
extern U8g2Drawable gfxDrawable;
2830
extern GraphicsDeviceRenderer renderer;

examples/arduino32/stm32DuinoOneButton/stm32DuinoOneButton.emf

+8-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
"lastDisplayUuid": "fd998437-c4b2-4386-ba88-d0ae7c20620b",
116116
"lastInputUuid": "4F92CBC0-D13A-4090-9B86-C1952FE42D9B",
117117
"lastRemoteUuids": [
118-
"2c101fec-1f7d-4ff3-8d2b-992ad41e7fcb"
118+
"1e38dc42-672d-4c1c-a393-2c7632bf6c5c"
119119
],
120120
"lastThemeUuid": "396ED4DF-AD7B-4951-A848-A9E5838A549B",
121121
"applicationUUID": "4fe6e85d-2bbd-4d19-84e5-5d6746883028",
@@ -201,6 +201,11 @@
201201
"latestValue": "250",
202202
"subsystem": "INPUT"
203203
},
204+
{
205+
"name": "SERIAL_PORT",
206+
"latestValue": "Serial",
207+
"subsystem": "REMOTE"
208+
},
204209
{
205210
"name": "ITEM_FONT",
206211
"latestValue": "ada:OpenSansRegular8pt,0",
@@ -237,16 +242,15 @@
237242
"saveLocation": "PROJECT_TO_CURRENT_WITH_GENERATED",
238243
"usingSizedEEPROMStorage": false,
239244
"eepromDefinition": "bsp:0",
240-
"authenticatorDefinition": "rom:150:4",
245+
"authenticatorDefinition": "",
241246
"projectIoExpanders": [
242247
"deviceIO:"
243248
],
244249
"menuInMenuCollection": {
245250
"menuDefinitions": []
246251
},
247252
"packageNamespace": "",
248-
"appIsModular": false,
249-
"listOfEmbeddedForms": []
253+
"appIsModular": false
250254
},
251255
"stringLists": []
252256
}

examples/arduino32/stm32DuinoOneButton/stm32DuinoOneButton.ino

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
#include <SPI.h>
1212

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

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

0 commit comments

Comments
 (0)