Skip to content

Proximity Limit Switch Library #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libs/sensors/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ target_link_libraries(CurrentSensor
Sensor
mbed-os
)

add_library(ProximitySwitch STATIC)
target_sources(ProximitySwitch PRIVATE src/ProximitySwitch.cpp)
target_include_directories(ProximitySwitch PUBLIC include)
target_link_libraries(ProximitySwitch
PRIVATE
Sensor
mbed-os
Logger
)
21 changes: 21 additions & 0 deletions libs/sensors/include/ProximitySwitch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "Sensor.h"
#include "mbed.h"

namespace Sensor {
class ProximitySwitch final : Sensor {
public:
ProximitySwitch(PinName proximity_in);

// read() returns 1 for HIGH, 0 for LOW, and 2 for ERROR
float read() override;
float alternateRead() override {}
bool getStatus() const override {}
[[nodiscard]] bool reset() override {}
[[nodiscard]] bool update() override {}

private:
DigitalIn m_proximity_in_adc;
};
} // namespace Sensor
17 changes: 17 additions & 0 deletions libs/sensors/src/ProximitySwitch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "ProximitySwitch.h"

#include "Logger.h"

using namespace Sensor;

ProximitySwitch::ProximitySwitch(PinName proximity_in) : m_proximity_in_adc(proximity_in) {}

/*https://www.dfrobot.com/product-2025.html*/
float ProximitySwitch::read() {
if (m_proximity_in_adc.is_connected()) {
return m_proximity_in_adc.read();
} else {
Utility::logger << "ERROR: Sensor pin not connected";
return 2;
}
}