arduino / arduino/ArduinoCore-renesas
UNOWIFOR4 - Sketch that uses HID - Some Serial.print(...) don't output.
- Dominant language
- C
- Stars
- 193
- Forks
- 112
- PR merge metrics
- No merged PRs in 30d
Description
The file remaps Serial to USBSerial.
This only works for those files that include HID.h
**Problem/Example:**
So for example if you have a sketch like:
```
extern void print_without_hid(int loop_count);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while(!Serial && millis() < 5000) {}
delay(1000);
}
int loop_count = 0;
void loop() {
Serial.print("Loop Count: ");
Serial.print(++loop_count, DEC);
Serial.print(" Baud: ");
//Serial.println(Serial.baud(), DEC);
Serial.println();
print_without_hid(loop_count);
delay(1000);
}
```
And a second tab, with name like: not_including_hid.cpp
```
#include
void print_without_hid(int loop_count) {
Serial.print("\tNO HID: ");
Serial.println(loop_count, DEC);
}
```
The prints in this second file will not show up in the Serial monitor. As they instead are output through _UART1_ to the ESP32-S3.
**Possible Solutions:**
1) Create a new implementation for Serial that detects or is told which of the two paths to take: Example from a test sketch I have
```
class _Serial : public _SerialUSB {
public:
_Serial() {}
void begin(unsigned long baud = 115200) {
if (_native_USB) SerialUSB.begin(baud);
else _UART1_.begin(baud);
}
void begin(unsigned long baud, uint16_t config) {
if (_native_USB) SerialUSB.begin(baud, config);
else _UART1_.begin(baud, config);
};
void end() {
if (_native_USB) SerialUSB.end();
else _UART1_.end();
}
void nativeUSB(bool native_usb) {
if (native_usb != _native_USB) {
_native_USB = native_usb;
if (native_usb) {
pinMode(21, OUTPUT);
digitalWrite(21, HIGH);
__USBStart();
SerialUSB.begin(115200);
} else {
pinMode(21, OUTPUT);
digitalWrite(21, LOW);
// need some way to tell SerialUSB to end and other to start
SerialUSB.end();
}
}
}
virtual int peek() {
if (_native_USB) return SerialUSB.peek();
else return _UART1_.peek();
}
virtual int read() {
if (_native_USB) return SerialUSB.read();
else return _UART1_.read();
}
virtual int available() {
if (_native_USB) return SerialUSB.available();
else return _UART1_.available();
}
virtual int availableForWrite() {
if (_native_USB) return SerialUSB.availableForWrite();
else return _UART1_.availableForWrite();
}
virtual void flush() {
if (_native_USB) SerialUSB.flush();
else _UART1_.flush();
}
virtual size_t write(uint8_t c) {
if (_native_USB) return SerialUSB.write(c);
else return _UART1_.write(c);
}
virtual size_t write(const uint8_t *p, size_t len) {
if (_native_USB) return SerialUSB.write(p, len);
else return _UART1_.write(p, len);
}
using Print::write;
operator bool() {
if (_native_USB) return SerialUSB;
else return _UART1_;
}
private:
bool _native_USB = false;
};
```
2) Remap in Arduino.h
Like:
```
#ifndef NO_USB
#define Serial SerialUSB
#define Serial1 _UART1_
#define Serial2 _UART2_
#define Serial3 _UART3_
#define Serial4 _UART4_
#define Serial5 _UART5_
#else
#if __has_include()
#define Serial SerialUSB
#else
#define Serial _UART1_
#endif
#define Serial1 _UART2_
#define Serial2 _UART3_
#define Serial3 _UART4_
#define Serial4 _UART5_
#endif
```
Which worked with my example.
2a) same as 2, but maybe has:
```
#if __has_include()
#define USE_USBSERIAL_FOR_SERIAL
#endif
...
#else
#ifdef USE_USBSERIAL_FOR_SERIAL
#define Serial SerialUSB
#else
#define Serial _UART1_
#endif
```
Which also allows you to define it some other ways, like in boards.txt...
Thoughts? Preferences?
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reading HID.h and Arduino.h, then reproduce the provided sketch with the second tab that includes only Arduino.h. Compare the Serial routing in both files; done means Serial.print calls from the secondary file appear in the same Serial monitor without requiring a project-specific workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- embedded-iot
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100