Skip to content

Commit c640915

Browse files
committed
Implement polymorphism for DigitalIn
1 parent 0e89f9d commit c640915

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

drivers/include/drivers/DigitalIn.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2006-2019 ARM Limited
2+
* Copyright (c) 2006-2020 ARM Limited
33
* SPDX-License-Identifier: Apache-2.0
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,9 +19,11 @@
1919

2020
#include "platform/platform.h"
2121

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

2425
namespace mbed {
26+
2527
/**
2628
* \defgroup drivers_DigitalIn DigitalIn class
2729
* \ingroup drivers-public-api-gpio
@@ -51,7 +53,11 @@ namespace mbed {
5153
* }
5254
* @endcode
5355
*/
54-
class DigitalIn {
56+
class DigitalIn
57+
#ifdef FEATURE_EXPERIMENTAL_API
58+
final : public interface::DigitalIn
59+
#endif
60+
{
5561

5662
public:
5763
/** Create a DigitalIn connected to the specified pin
@@ -75,6 +81,13 @@ class DigitalIn {
7581
gpio_init_in_ex(&gpio, pin, mode);
7682
}
7783

84+
/** Class destructor, deinitialize the pin
85+
*/
86+
~DigitalIn()
87+
{
88+
gpio_free(&gpio);
89+
}
90+
7891
/** Read the input, represented as 0 or 1 (int)
7992
*
8093
* @returns
@@ -92,7 +105,6 @@ class DigitalIn {
92105
* @param pull PullUp, PullDown, PullNone, OpenDrain
93106
*/
94107
void mode(PinMode pull);
95-
96108
/** Return the output setting, represented as 0 or 1 (int)
97109
*
98110
* @returns
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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_DIGITALIN_H_
21+
#define MBED_INTERFACE_DIGITALIN_H_
22+
23+
24+
namespace mbed {
25+
26+
class DigitalIn;
27+
28+
namespace interface {
29+
30+
#ifdef FEATURE_EXPERIMENTAL_API
31+
32+
// TODO - move method doxygen comments to interface once this polymorphism is mainstream
33+
34+
// Pure interface definition for DigitalIn
35+
struct DigitalIn {
36+
virtual ~DigitalIn() = default;
37+
virtual int read() = 0;
38+
virtual void mode(PinMode pull) = 0;
39+
virtual int is_connected() = 0;
40+
41+
virtual operator int()
42+
{
43+
// Underlying implementation is responsible for thread-safety
44+
return read();
45+
}
46+
47+
};
48+
#else
49+
using DigitalIn = ::mbed::DigitalIn;
50+
#endif /* FEATURE_EXPERIMENTAL_API */
51+
52+
} // namespace interface
53+
} // namespace mbed
54+
55+
#endif /* MBED_INTERFACE_DIGITALIN_H_ */

0 commit comments

Comments
 (0)