Skip to content

Commit 7ef27d9

Browse files
authored
Merge pull request #13209 from AGlass0fMilk/virtual-gpio-methods
Implement polymorphism for DigitalIn/Out/InOut
2 parents 1fdb1c1 + 78cfad9 commit 7ef27d9

File tree

6 files changed

+227
-5
lines changed

6 files changed

+227
-5
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

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

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: 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+
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_ */
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
DigitalInOut &operator= (int value)
46+
{
47+
// Underlying implementation is responsible for thread-safety
48+
write(value);
49+
return *this;
50+
}
51+
52+
DigitalInOut &operator= (DigitalInOut &rhs)
53+
{
54+
// Underlying implementation is responsible for thread-safety
55+
write(rhs.read());
56+
return *this;
57+
}
58+
59+
operator int()
60+
{
61+
// Underlying implementation is responsible for thread-safety
62+
return read();
63+
}
64+
65+
};
66+
#else
67+
using DigitalInOut = ::mbed::DigitalInOut;
68+
#endif /* FEATURE_EXPERIMENTAL_API */
69+
70+
} // namespace interface
71+
} // namespace mbed
72+
73+
#endif /* MBED_INTERFACE_DIGITALINOUT_H_ */
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
DigitalOut &operator= (int value)
43+
{
44+
// Underlying implementation is responsible for thread-safety
45+
write(value);
46+
return *this;
47+
}
48+
49+
DigitalOut &operator= (DigitalOut &rhs)
50+
{
51+
// Underlying implementation is responsible for thread-safety
52+
write(rhs.read());
53+
return *this;
54+
}
55+
56+
operator int()
57+
{
58+
// Underlying implementation is responsible for thread-safety
59+
return read();
60+
}
61+
62+
};
63+
#else
64+
using DigitalOut = ::mbed::DigitalOut;
65+
#endif /* FEATURE_EXPERIMENTAL_API */
66+
67+
} // namespace interface
68+
} // namespace mbed
69+
70+
71+
#endif /* MBED_INTERFACE_DIGITALOUT_H_ */

0 commit comments

Comments
 (0)