Skip to content

Commit f3b07c7

Browse files
Creating Limit Switch Class (#330)
* Added LimitSwitch header and cpp files * Modified LimitSwitch header and CMakeLists * Modified Class member variables and functions * Removed some of the comments * Added check for NC pin * Created Limit Switch test app * Created CMakeLists file for test app * Modified limit switch class and test app, changed CMakeLists name * Changed class and test app * Minor changes to class and test app * A little modification to test app * Adjustment made to clang-format settings * Adjustment made to LimitSwitch.h formatting * Made changes based on more feedback * Clang check fix * Fix submodules * More changes * More fixes
1 parent 49f90e2 commit f3b07c7

File tree

7 files changed

+86
-2
lines changed

7 files changed

+86
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ add_app_subdirectory(${TEST_APPS_DIR}/test-blockingneo)
105105
add_app_subdirectory(${TEST_APPS_DIR}/test-can)
106106
add_app_subdirectory(${TEST_APPS_DIR}/test-mae3)
107107
add_app_subdirectory(${TEST_APPS_DIR}/test-led-matrix)
108+
add_app_subdirectory(${TEST_APPS_DIR}/test-limit-switch)
108109
add_app_subdirectory(${TEST_APPS_DIR}/test-logger)
109110
add_app_subdirectory(${TEST_APPS_DIR}/test-lookup-table)
110111
add_app_subdirectory(${TEST_APPS_DIR}/test-pid)

libs/gpio/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ target_link_libraries(QEI
1616
PRIVATE
1717
mbed-os
1818
)
19+
20+
21+
add_library(LimitSwitch STATIC)
22+
target_sources(LimitSwitch PRIVATE src/LimitSwitch.cpp)
23+
target_include_directories(LimitSwitch PUBLIC include)
24+
target_link_libraries(LimitSwitch
25+
PRIVATE
26+
mbed-os
27+
)

libs/gpio/include/LimitSwitch.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
#include "mbed.h"
3+
namespace GPIO {
4+
5+
class LimitSwitch {
6+
public:
7+
LimitSwitch(DigitalIn limitPin, bool ActiveHigh = 1);
8+
// Tells you whether the Limit Switch has been pressed or not
9+
bool isPressed();
10+
11+
// Overloaded bool operator
12+
operator bool();
13+
14+
bool isConnected();
15+
16+
private:
17+
// Active High is 1 and Active Low is 0
18+
bool m_ActiveHigh;
19+
DigitalIn m_limitPin;
20+
};
21+
22+
} // namespace GPIO

libs/gpio/src/LimitSwitch.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "LimitSwitch.h"
2+
3+
using namespace GPIO;
4+
5+
LimitSwitch::LimitSwitch(DigitalIn limitPin, bool ActiveHigh) : m_limitPin(limitPin), m_ActiveHigh(ActiveHigh) {}
6+
7+
bool LimitSwitch::isPressed() {
8+
if (m_limitPin.is_connected()) {
9+
return !m_ActiveHigh ? !m_limitPin.read() : m_limitPin.read();
10+
}
11+
return false;
12+
}
13+
14+
LimitSwitch::operator bool() {
15+
return isPressed();
16+
}
17+
18+
bool LimitSwitch::isConnected() {
19+
return m_limitPin.is_connected();
20+
}

supported_build_configurations.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ test-mae3:
5454

5555
test-led-matrix:
5656
- UWRT_NUCLEO
57-
57+
58+
test-limit-switch:
59+
- UWRT_NUCLEO
60+
5861
test-logger:
5962
- GIMBAL_REV2
6063
- PDB_REV2
@@ -103,4 +106,4 @@ test-stress-can:
103106
- GIMBAL_REV2
104107
- PDB_REV2
105108
- SCIENCE_REV2
106-
- UWRT_NUCLEO
109+
- UWRT_NUCLEO
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_executable(test-limit-switch)
2+
target_sources(test-limit-switch PRIVATE src/main.cpp)
3+
target_link_libraries(test-limit-switch PRIVATE LimitSwitch Logger)
4+
mbed_set_post_build(test-limit-switch)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "LimitSwitch.h"
2+
#include "PinNames.h"
3+
#include "mbed.h"
4+
5+
GPIO::LimitSwitch hSwitch(PA_0);
6+
GPIO::LimitSwitch lSwitch(PA_0, 0);
7+
GPIO::LimitSwitch ncSwitch(NC);
8+
9+
int main() {
10+
// Check to see if limit switch is pressed
11+
while (true) {
12+
// check for active high and active low case
13+
if (hSwitch.isPressed()) {
14+
printf("Active high limit switch is pressed \n");
15+
}
16+
17+
if (lSwitch.isPressed()) {
18+
printf("Active low limit switch is pressed \n");
19+
}
20+
// test for NC limit switch
21+
if (!ncSwitch.isConnected()) {
22+
printf("Pin is not connected \n");
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)