Skip to content

Commit 2e6a552

Browse files
committed
Implement polymorphism for DigitalOut
1 parent c640915 commit 2e6a552

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

drivers/include/drivers/DigitalOut.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#define MBED_DIGITALOUT_H
1919

2020
#include "platform/platform.h"
21+
22+
#include "interfaces/InterfaceDigitalOut.h"
2123
#include "hal/gpio_api.h"
2224

2325
namespace mbed {
@@ -46,7 +48,11 @@ namespace mbed {
4648
* }
4749
* @endcode
4850
*/
49-
class DigitalOut {
51+
class DigitalOut
52+
#ifdef FEATURE_EXPERIMENTAL_API
53+
final : public interface::DigitalOut
54+
#endif
55+
{
5056

5157
public:
5258
/** Create a DigitalOut connected to the specified pin
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Mbed-OS Microcontroller Library
3+
* Copyright (c) 2021 Embedded Planet
4+
* Copyright (c) 2021 ARM Limited
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License
18+
*/
19+
20+
#ifndef MBED_INTERFACE_DIGITALOUT_H_
21+
#define MBED_INTERFACE_DIGITALOUT_H_
22+
23+
#include "PinNames.h"
24+
25+
namespace mbed {
26+
27+
class DigitalOut;
28+
29+
namespace interface {
30+
31+
#ifdef FEATURE_EXPERIMENTAL_API
32+
33+
// TODO - move method doxygen comments to interface once this polymorphism is mainstream
34+
35+
// Pure interface definition for DigitalOut
36+
struct DigitalOut {
37+
virtual ~DigitalOut() = default;
38+
virtual void write(int value) = 0;
39+
virtual int read() = 0;
40+
virtual int is_connected() = 0;
41+
42+
virtual DigitalOut &operator= (int value)
43+
{
44+
// Underlying implementation is responsible for thread-safety
45+
write(value);
46+
return *this;
47+
}
48+
49+
virtual DigitalOut &operator= (DigitalOut &rhs) {
50+
// Underlying implementation is responsible for thread-safety
51+
write(rhs.read());
52+
return *this;
53+
}
54+
55+
virtual operator int()
56+
{
57+
// Underlying implementation is responsible for thread-safety
58+
return read();
59+
}
60+
61+
};
62+
#else
63+
using DigitalOut = ::mbed::DigitalOut;
64+
#endif /* FEATURE_EXPERIMENTAL_API */
65+
66+
} // namespace interface
67+
} // namespace mbed
68+
69+
70+
#endif /* MBED_INTERFACE_DIGITALOUT_H_ */

0 commit comments

Comments
 (0)