Skip to content

Commit 326f5bb

Browse files
AGlass0fMilkpan-
andcommitted
Implement polymorphism for DigitalInOut
Co-authored-by: Vincent Coubard (pan-) <[email protected]>
1 parent 2e6a552 commit 326f5bb

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

drivers/include/drivers/DigitalInOut.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "platform/platform.h"
2121

22+
#include "interfaces/InterfaceDigitalInOut.h"
2223
#include "hal/gpio_api.h"
2324

2425
namespace mbed {
@@ -32,7 +33,11 @@ namespace mbed {
3233
*
3334
* @note Synchronization level: Interrupt safe
3435
*/
35-
class DigitalInOut {
36+
class DigitalInOut
37+
#ifdef FEATURE_EXPERIMENTAL_API
38+
final : public interface::DigitalInOut
39+
#endif
40+
{
3641

3742
public:
3843
/** Create a DigitalInOut connected to the specified pin
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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_DIGITALINOUT_H_
21+
#define MBED_INTERFACE_DIGITALINOUT_H_
22+
23+
#include "PinNames.h"
24+
25+
namespace mbed {
26+
27+
class DigitalInOut;
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 DigitalInOut
36+
struct DigitalInOut {
37+
virtual ~DigitalInOut() = default;
38+
virtual void write(int value) = 0;
39+
virtual int read() = 0;
40+
virtual void output() = 0;
41+
virtual void input() = 0;
42+
virtual void mode(PinMode pull) = 0;
43+
virtual int is_connected() = 0;
44+
45+
virtual DigitalInOut &operator= (int value)
46+
{
47+
// Underlying implementation is responsible for thread-safety
48+
write(value);
49+
return *this;
50+
}
51+
52+
virtual DigitalInOut &operator= (DigitalInOut &rhs) {
53+
// Underlying implementation is responsible for thread-safety
54+
write(rhs.read());
55+
return *this;
56+
}
57+
58+
virtual operator int()
59+
{
60+
// Underlying implementation is responsible for thread-safety
61+
return read();
62+
}
63+
64+
};
65+
#else
66+
using DigitalInOut = ::mbed::DigitalInOut;
67+
#endif /* FEATURE_EXPERIMENTAL_API */
68+
69+
} // namespace interface
70+
} // namespace mbed
71+
72+
#endif /* MBED_INTERFACE_DIGITALINOUT_H_ */

0 commit comments

Comments
 (0)