Skip to content

Commit 01920cf

Browse files
committed
I2S - ::read non-virtual method shadowing virtual
> In file included from cores/esp8266/HardwareSerial.h:32, > from cores/esp8266/Arduino.h:303, > from /tmp/tmp.rNXlUuH9fi/build/sketch/SimpleTone.ino.cpp:1: > cores/esp8266/Stream.h:120:21: warning: 'virtual int Stream::read(uint8_t*, size_t)' was hidden [-Woverloaded-virtual=] > 120 | virtual int read (uint8_t* buffer, size_t len); > | ^~~~ > In file included from libraries/I2S/examples/SimpleTone/SimpleTone.ino:11: > libraries/I2S/src/I2S.h:58:7: note: by 'I2SClass::read' > 58 | int read(void* buffer, size_t size); > | ^~~~ declarations clean-up plus c++-style casts for these specific call sites
1 parent 672acde commit 01920cf

2 files changed

Lines changed: 40 additions & 23 deletions

File tree

‎libraries/I2S/src/I2S.cpp‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ void I2SClass::end() {
5757
_running = false;
5858
}
5959

60-
void I2SClass::onTransmit(void(*fcn)(void)) {
60+
void I2SClass::onTransmit(i2s_callback_type fcn) {
6161
i2s_set_callback(fcn);
6262
_onTransmit = fcn;
6363
}
6464

65-
void I2SClass::onReceive(void(*fcn)(void)) {
65+
void I2SClass::onReceive(i2s_callback_type fcn) {
6666
i2s_rx_set_callback(fcn);
6767
_onReceive = fcn;
6868
}
@@ -122,6 +122,10 @@ int I2SClass::peek() {
122122
}
123123
}
124124

125+
int I2SClass::read(uint8_t* buffer, size_t size) {
126+
return read(reinterpret_cast<void *>(buffer), size);
127+
}
128+
125129
int I2SClass::read(void *buffer, size_t size) {
126130
if (!_running) return -1;
127131
int cnt = 0;
@@ -170,12 +174,7 @@ int I2SClass::read(void *buffer, size_t size) {
170174
}
171175

172176
size_t I2SClass::write(uint8_t s) {
173-
if (!_running) return 0;
174-
return write((int32_t)s);
175-
}
176-
177-
size_t I2SClass::write(const uint8_t *buffer, size_t size) {
178-
return write((const void *)buffer, size);
177+
return write(static_cast<int32_t>(s));
179178
}
180179

181180
size_t I2SClass::write(int32_t s) {
@@ -194,14 +193,18 @@ size_t I2SClass::write(int32_t s) {
194193
return 1;
195194
}
196195
} else {
197-
return i2s_write_sample((uint32_t)s) ? 1 : 0;
196+
return i2s_write_sample(static_cast<uint32_t>(s)) ? 1 : 0;
198197
}
199198
}
200199

200+
size_t I2SClass::write(const uint8_t *buffer, size_t size) {
201+
return write(reinterpret_cast<const void *>(buffer), size);
202+
}
203+
201204
// SAMD core has this as non-blocking
202205
size_t I2SClass::write(const void *buffer, size_t size) {
203206
if (!_running) return 0;
204-
return i2s_write_buffer_nb((int16_t *)buffer, size / 2);
207+
return i2s_write_buffer_nb(reinterpret_cast<const int16_t *>(buffer), size / 2);
205208
}
206209

207210

‎libraries/I2S/src/I2S.h‎

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
#ifndef _I2S_H_INCLUDED
2323
#define _I2S_H_INCLUDED
2424

25-
#include <Arduino.h>
25+
#include <Print.h>
26+
#include <Stream.h>
2627
#include <core_esp8266_i2s.h>
2728

2829
typedef enum {
@@ -42,34 +43,47 @@ class I2SClass : public Stream
4243
void end();
4344

4445
// from Stream
45-
virtual int available();
46-
virtual int read(); // Blocking, will wait for incoming data
47-
virtual int peek(); // Blocking, will wait for incoming data
48-
virtual void flush();
46+
using Stream::read;
47+
48+
int available() override;
49+
50+
int peek() override; // Blocking, will wait for incoming data
51+
int read() override; // Blocking, will wait for incoming data
52+
53+
int read(uint8_t* buffer, size_t size) override;
4954

5055
// from Print (see notes on write() methods below)
51-
virtual size_t write(uint8_t);
52-
virtual size_t write(const uint8_t *buffer, size_t size);
53-
virtual int availableForWrite();
56+
using Print::write;
57+
58+
size_t write(uint8_t s) override;
59+
size_t write(const uint8_t *buffer, size_t size) override;
60+
61+
int availableForWrite() override;
62+
void flush() override;
63+
64+
// Methods below are only available for I2SClass instance
65+
// Everything else goes through Stream / Print methods
5466

5567
// Read up to size samples from the I2S device. Non-blocking, will read
5668
// from 0...size samples and return the count read. Be sure your app handles
5769
// the partial read case (i.e. yield()ing and trying to read more).
5870
int read(void* buffer, size_t size);
5971

60-
// Write a single sample to the I2S device. Blocking until write succeeds
61-
size_t write(int32_t);
6272
// Write up to size samples to the I2S device. Non-blocking, will write
6373
// from 0...size samples and return that count. Be sure your app handles
6474
// partial writes (i.e. by yield()ing and then retrying to write the
6575
// remaining data.
66-
size_t write(const void *buffer, size_t size);
76+
size_t write(const void* buffer, size_t size);
77+
78+
// Write a single sample to the I2S device. Blocking until write succeeds
79+
size_t write(int32_t);
6780

6881
// Note that these callback are called from **INTERRUPT CONTEXT** and hence
6982
// must be both stored in IRAM and not perform anything that's not legal in
7083
// an interrupt
71-
void onTransmit(void(*)(void));
72-
void onReceive(void(*)(void));
84+
using i2s_callback_type = void (*)();
85+
void onTransmit(i2s_callback_type);
86+
void onReceive(i2s_callback_type);
7387

7488
private:
7589
int _bps;

0 commit comments

Comments
 (0)